void TFIn::FillBf(){ EAssertR( (BfC==BfL)&&((BfL==-1)||(BfL==MxBfL)), "Error reading file '"+GetSNm()+"'."); BfL=int(fread(Bf, 1, MxBfL, FileId)); EAssertR((BfC!=0)||(BfL!=0), "Error reading file '"+GetSNm()+"'."); BfC=0; }
void TZipOut::Flush(){ FlushBf(); #ifdef GLib_WIN EAssertR(FlushFileBuffers(ZipStdinWr)!=0, "Can not flush file '"+GetSNm()+"'."); #else EAssertR(fflush(ZipStdinWr)==0, "Can not flush file '"+GetSNm()+"'."); #endif }
void TZipIn::FillBf(){ EAssertR(CurFPos < FLen, "End of file "+GetSNm()+" reached."); EAssertR((BfC==BfL)/*&&((BfL==-1)||(BfL==MxBfL))*/, "Error reading file '"+GetSNm()+"'."); #ifdef GLib_WIN // Read output from the child process DWORD BytesRead; EAssert(ReadFile(ZipStdoutRd, Bf, MxBfL, &BytesRead, NULL) != 0); #else size_t BytesRead = fread(Bf, 1, MxBfL, ZipStdoutRd); EAssert(BytesRead != 0); #endif BfL = (int) BytesRead; CurFPos += BytesRead; EAssertR((BfC!=0)||(BfL!=0), "Error reading file '"+GetSNm()+"'."); BfC = 0; }
void TSIn::Load(char*& CStr){ char Ch; Load(Ch); int CStrLen=int(Ch); EAssertR(CStrLen>=0, "Error reading stream '"+GetSNm()+"'."); CStr=new char[CStrLen+1]; if (CStrLen>0){Cs+=GetBf(CStr, CStrLen);} CStr[CStrLen]=TCh::NullCh; }
int TFIn::GetFLen() const { const int FPos=GetFPos(); EAssertR( fseek(FileId, 0, SEEK_END)==0, "Error seeking into file '"+GetSNm()+"'."); const int FLen=GetFPos(); SetFPos(FPos); return FLen; }
// reads LBfL bytes into LBf int TFIn::GetBf(const void* LBf, const TSize& LBfL){ int LBfS=0; if (TSize(BfC+LBfL)>TSize(BfL)){ for (TSize LBfC=0; LBfC<LBfL; LBfC++){ if (BfC==BfL){ FillBf(); // we tried to fill a buffer (that is used in the next statement). // the available buffer BfL therefore has to be non-empty EAssertR(BfL > 0, "Unable to fill a buffer from " + GetSNm() + "'."); } LBfS+=((char*)LBf)[LBfC]=Bf[BfC++];} } else { for (TSize LBfC=0; LBfC<LBfL; LBfC++){ LBfS+=(((char*)LBf)[LBfC]=Bf[BfC++]);} } return LBfS; }
TStr TFInOut::GetFNm() const { return GetSNm(); }
void TFOut::Flush(){ FlushBf(); EAssertR(fflush(FileId)==0, "Can not flush file '"+GetSNm()+"'."); }
TFOut::~TFOut(){ if (FileId!=NULL){FlushBf();} if (Bf!=NULL){delete[] Bf;} if (FileId!=NULL){ EAssertR(fclose(FileId)==0, "Can not close file '"+GetSNm()+"'.");} }
void TFOut::FlushBf(){ EAssertR( fwrite(Bf, 1, BfL, FileId)==BfL, "Error writting to the file '"+GetSNm()+"'."); BfL=0; }
void TSIn::LoadCs(){ TCs CurCs=Cs; TCs TestCs; Cs+=GetBf(&TestCs, sizeof(TestCs)); EAssertR(CurCs==TestCs, "Invalid checksum reading '"+GetSNm()+"'."); }
int TFIn::GetFPos() const { const int FPos=(int)ftell(FileId); EAssertR(FPos!=-1, "Error seeking into file '"+GetSNm()+"'."); return FPos; }
void TFIn::SetFPos(const int& FPos) const { EAssertR( fseek(FileId, FPos, SEEK_SET)==0, "Error seeking into file '"+GetSNm()+"'."); }
void TSOut::Save(const char* CStr){ int CStrLen=int(strlen(CStr)); EAssertR(CStrLen<=127, "Error writting stream '"+GetSNm()+"'."); Save(char(CStrLen)); if (CStrLen>0){Cs+=PutBf(CStr, CStrLen);} }
void TZipOut::FlushBf() { #ifdef GLib_WIN DWORD BytesOut; EAssertR(WriteFile(ZipStdinWr, Bf, DWORD(BfL), &BytesOut, NULL)!=0, "Error writting to the file '"+GetSNm()+"'."); #else size_t BytesOut = fwrite(Bf, 1, BfL, ZipStdinWr); #endif EAssert(BytesOut == BfL); BfL = 0; }