///////////////////////////////////////////////// // Check-Sum const int TCs::MxMask=0x0FFFFFFF; TCs TCs::GetCsFromBf(char* Bf, const int& BfL){ TCs Cs; for (int BfC=0; BfC=0, "Error reading stream '"+GetSNm()+"'."); CStr=new char[CStrLen+1]; if (CStrLen>0){Cs+=GetBf(CStr, CStrLen);} CStr[CStrLen]=TCh::NullCh; } bool TSIn::GetNextLn(TStr& LnStr){ TChA LnChA; const bool IsNext=GetNextLn(LnChA); LnStr=LnChA; return IsNext; } bool TSIn::GetNextLn(TChA& LnChA){ LnChA.Clr(); while (!Eof()){ const char Ch=GetCh(); if (Ch=='\n'){return true;} if (Ch=='\r' && PeekCh()=='\n'){GetCh(); return true;} LnChA.AddCh(Ch); } return !LnChA.Empty(); } const PSIn TSIn::StdIn=PSIn(new TStdIn()); TStdIn::TStdIn(): TSBase("Standard input"), TSIn("Standard input") {} ///////////////////////////////////////////////// // Output-Stream TSOut::TSOut(const TStr& Str): TSBase(Str.CStr()), MxLnLen(-1), LnLen(0){} int TSOut::UpdateLnLen(const int& StrLen, const bool& ForceInLn){ int Cs=0; if (MxLnLen!=-1){ if ((!ForceInLn)&&(LnLen+StrLen>MxLnLen)){Cs+=PutLn();} LnLen+=StrLen; } return Cs; } int TSOut::PutMem(const TMem& Mem){ return PutBf(Mem(), Mem.Len()); } int TSOut::PutCh(const char& Ch, const int& Chs){ int Cs=0; for (int ChN=0; ChN0){ if (LnLen+1+NextStrLen>MxLnLen){Cs+=PutLn();} else {Cs+=PutCh(' ');} } } return Cs; } int TSOut::PutSepLn(const int& Lns){ int Cs=0; if (LnLen>0){Cs+=PutLn();} Cs+=PutLn(Lns); return Cs; } 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 TSOut::Save(TSIn& SIn, const TSize& BfL){ Fail; if (BfL==0){ //J: used to be ==-1 while (!SIn.Eof()){Save(SIn.GetCh());} } else { for (TSize BfC=0; BfCTSize(BfL)){ for (TSize LBfC=0; LBfCMxBfL){ for (TSize LBfC=0; LBfC0?_MxBfL:1024; Bf=new char[MxBfL]; } TMOut::TMOut(char* _Bf, const int& _MxBfL): TSBase("Output-Memory"), TSOut("Output-Memory"), Bf(_Bf), BfL(0), MxBfL(_MxBfL), OwnBf(false){} int TMOut::PutBf(const void* LBf, const TSize& LBfL){ int LBfS=0; if (TSize(BfL+LBfL)>TSize(MxBfL)){ for (TSize LBfC=0; LBfCEof()) { return false; } TChA LnChA; char Ch = TCh::EofCh; while (!SIn->Eof() && ((Ch=SIn->GetCh())!='\n')) { if (Ch != '\r') { LnChA += Ch; } } LnStr = LnChA; return true; } ///////////////////////////////////////////////// // fseek-Constants-Definitions // because of strange Borland CBuilder behaviour in sysdefs.h #ifndef SEEK_SET #define SEEK_CUR 1 #define SEEK_END 2 #define SEEK_SET 0 #endif ///////////////////////////////////////////////// // Random-File void TFRnd::RefreshFPos(){ EAssertR( fseek(FileId, 0, SEEK_CUR)==0, "Error seeking into file '"+TStr(FNm)+"'."); } TFRnd::TFRnd(const TStr& _FNm, const TFAccess& FAccess, const bool& CreateIfNo, const int& _HdLen, const int& _RecLen): FileId(NULL), FNm(_FNm.CStr()), RecAct(false), HdLen(_HdLen), RecLen(_RecLen){ RecAct=(HdLen>=0)&&(RecLen>0); switch (FAccess){ case faCreate: FileId=fopen(FNm.CStr(), "w+b"); break; case faUpdate: FileId=fopen(FNm.CStr(), "r+b"); break; case faAppend: FileId=fopen(FNm.CStr(), "r+b"); if (FileId!=NULL){fseek(FileId, SEEK_END, 0);} break; case faRdOnly: FileId=fopen(FNm.CStr(), "rb"); break; default: Fail; } if ((FileId==NULL)&&(CreateIfNo)){ FileId=fopen(FNm.CStr(), "w+b");} EAssertR(FileId!=NULL, "Can not open file '"+_FNm+"'."); } TFRnd::~TFRnd(){ EAssertR(fclose(FileId)==0, "Can not close file '"+TStr(FNm)+"'."); } TStr TFRnd::GetFNm() const { return FNm.CStr(); } void TFRnd::SetFPos(const int& FPos){ EAssertR( fseek(FileId, FPos, SEEK_SET)==0, "Error seeking into file '"+TStr(FNm)+"'."); } void TFRnd::MoveFPos(const int& DFPos){ EAssertR( fseek(FileId, DFPos, SEEK_CUR)==0, "Error seeking into file '"+TStr(FNm)+"'."); } int TFRnd::GetFPos(){ int FPos=ftell(FileId); EAssertR(FPos!=-1, "Error seeking into file '"+TStr(FNm)+"'."); return FPos; } int TFRnd::GetFLen(){ int FPos=GetFPos(); EAssertR( fseek(FileId, 0, SEEK_END)==0, "Error seeking into file '"+TStr(FNm)+"'."); int FLen=GetFPos(); SetFPos(FPos); return FLen; } void TFRnd::SetRecN(const int& RecN){ IAssert(RecAct); SetFPos(HdLen+RecN*RecLen); } int TFRnd::GetRecN(){ IAssert(RecAct); int FPos=GetFPos()-HdLen; EAssertR(FPos%RecLen==0, "Invalid position in file'"+TStr(FNm)+"'."); return FPos/RecLen; } int TFRnd::GetRecs(){ IAssert(RecAct); int FLen=GetFLen()-HdLen; EAssertR(FLen%RecLen==0, "Invalid length of file'"+TStr(FNm)+"'."); return FLen/RecLen; } void TFRnd::GetBf(void* Bf, const TSize& BfL){ RefreshFPos(); EAssertR( fread(Bf, 1, BfL, FileId)==BfL, "Error reading file '"+TStr(FNm)+"'."); } void TFRnd::PutBf(const void* Bf, const TSize& BfL){ RefreshFPos(); EAssertR( fwrite(Bf, 1, BfL, FileId)==BfL, "Error writting to the file '"+TStr(FNm)+"'."); } void TFRnd::Flush(){ EAssertR(fflush(FileId)==0, "Can not flush file '"+TStr(FNm)+"'."); } void TFRnd::PutCh(const char& Ch, const int& Chs){ if (Chs>0){ char* CStr=new char[Chs]; for (int ChN=0; ChNLen(); char* Bf=new char[BfL]; SIn->GetBf(Bf, BfL); Cs=TCs::GetCsFromBf(Bf, BfL); PutBf(Bf, BfL); delete[] Bf; } PSIn TFRnd::GetSIn(const int& BfL, TCs& Cs){ char* Bf=new char[BfL]; GetBf(Bf, BfL); Cs=TCs::GetCsFromBf(Bf, BfL); PSIn SIn=PSIn(new TMIn(Bf, BfL, true)); return SIn; } TStr TFRnd::GetStrFromFAccess(const TFAccess& FAccess){ switch (FAccess){ case faCreate: return "Create"; case faUpdate: return "Update"; case faAppend: return "Append"; case faRdOnly: return "ReadOnly"; case faRestore: return "Restore"; default: Fail; return TStr(); } } TFAccess TFRnd::GetFAccessFromStr(const TStr& Str){ TStr UcStr=Str.GetUc(); if (UcStr=="CREATE"){return faCreate;} if (UcStr=="UPDATE"){return faUpdate;} if (UcStr=="APPEND"){return faAppend;} if (UcStr=="READONLY"){return faRdOnly;} if (UcStr=="RESTORE"){return faRestore;} if (UcStr=="NEW"){return faCreate;} if (UcStr=="CONT"){return faUpdate;} if (UcStr=="CONTINUE"){return faUpdate;} if (UcStr=="REST"){return faRestore;} if (UcStr=="RESTORE"){return faRestore;} return faUndef; } ///////////////////////////////////////////////// // Files const TStr TFile::TxtFExt=".Txt"; const TStr TFile::HtmlFExt=".Html"; const TStr TFile::HtmFExt=".Htm"; const TStr TFile::GifFExt=".Gif"; const TStr TFile::JarFExt=".Jar"; bool TFile::Exists(const TStr& FNm){ bool DoExists; TFIn FIn(FNm, DoExists); return DoExists; } void TFile::Del(const TStr& FNm, const bool& ThrowExceptP){ if (ThrowExceptP){ EAssertR( remove(FNm.CStr())==0, "Error removing file '"+FNm+"'."); } else { remove(FNm.CStr()); } } void TFile::DelWc(const TStr& WcStr, const bool& RecurseDirP){ // collect file-names TStrV FNmV; TFFile FFile(WcStr, RecurseDirP); TStr FNm; while (FFile.Next(FNm)){ FNmV.Add(FNm);} // delete files for (int FNmN=0; FNmN.#.txt --> ..txt int Cnt=1; int ch; TStr NewFNm; TStr TmpFNm=FNm; if (FNm.SearchCh('#') == -1) { for (ch = FNm.Len()-1; ch >= 0; ch--) if (FNm[ch] == '.') break; if (ch != -1) TmpFNm.InsStr(ch, ".#"); else TmpFNm += ".#"; } forever{ NewFNm=TmpFNm; NewFNm.ChangeStr("#", TStr::Fmt("%03d", Cnt)); Cnt++; if (!TFile::Exists(NewFNm)){break;} } return NewFNm; }