SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fl.cpp
Go to the documentation of this file.
1 #ifdef GLib_LINUX
2 extern "C" {
3  #include <sys/mman.h>
4 }
5 
6 #endif
7 
9 // Check-Sum
10 const int TCs::MxMask=0x0FFFFFFF;
11 
12 TCs TCs::GetCsFromBf(char* Bf, const int& BfL){
13  TCs Cs;
14  for (int BfC=0; BfC<BfL; BfC++){Cs+=Bf[BfC];}
15  return Cs;
16 }
17 
19 // Stream-Base
21  return TStr(SNm.CStr());
22 }
23 
25 // Input-Stream
26 TSIn::TSIn(const TStr& Str) : TSBase(Str.CStr()), FastMode(false){}
27 
28 void TSIn::LoadCs(){
29  TCs CurCs=Cs; TCs TestCs;
30  Cs+=GetBf(&TestCs, sizeof(TestCs));
31  EAssertR(CurCs==TestCs, "Invalid checksum reading '"+GetSNm()+"'.");
32 }
33 
34 void TSIn::Load(char*& CStr){
35  char Ch; Load(Ch);
36  int CStrLen=int(Ch);
37  EAssertR(CStrLen>=0, "Error reading stream '"+GetSNm()+"'.");
38  CStr=new char[CStrLen+1];
39  if (CStrLen>0){Cs+=GetBf(CStr, CStrLen);}
40  CStr[CStrLen]=TCh::NullCh;
41 }
42 
43 bool TSIn::GetNextLn(TStr& LnStr){
44  TChA LnChA;
45  const bool IsNext=GetNextLn(LnChA);
46  LnStr=LnChA;
47  return IsNext;
48 }
49 
50 bool TSIn::GetNextLn(TChA& LnChA){
51  LnChA.Clr();
52  while (!Eof()){
53  const char Ch=GetCh();
54  if (Ch=='\n'){return true;}
55  if (Ch=='\r' && PeekCh()=='\n'){GetCh(); return true;}
56  LnChA.AddCh(Ch);
57  }
58  return !LnChA.Empty();
59 }
60 
61 const PSIn TSIn::StdIn=PSIn(new TStdIn());
62 
63 TStdIn::TStdIn(): TSBase("Standard input"), TSIn("Standard input") {}
64 
66 // Output-Stream
67 TSOut::TSOut(const TStr& Str):
68  TSBase(Str.CStr()), MxLnLen(-1), LnLen(0){}
69 
70 int TSOut::UpdateLnLen(const int& StrLen, const bool& ForceInLn){
71  int Cs=0;
72  if (MxLnLen!=-1){
73  if ((!ForceInLn)&&(LnLen+StrLen>MxLnLen)){Cs+=PutLn();}
74  LnLen+=StrLen;
75  }
76  return Cs;
77 }
78 
79 int TSOut::PutMem(const TMem& Mem){
80  return PutBf(Mem(), Mem.Len());
81 }
82 
83 int TSOut::PutCh(const char& Ch, const int& Chs){
84  int Cs=0;
85  for (int ChN=0; ChN<Chs; ChN++){Cs+=PutCh(Ch);}
86  return Cs;
87 }
88 
89 int TSOut::PutBool(const bool& Bool){
90  return PutStr(TBool::GetStr(Bool));
91 }
92 
93 int TSOut::PutInt(const int& Int){
94  return PutStr(TInt::GetStr(Int));
95 }
96 
97 int TSOut::PutInt(const int& Int, const char* FmtStr){
98  return PutStr(TInt::GetStr(Int, FmtStr));
99 }
100 
101 int TSOut::PutUInt(const uint& UInt){
102  return PutStr(TUInt::GetStr(UInt));
103 }
104 
105 int TSOut::PutUInt(const uint& UInt, const char* FmtStr){
106  return PutStr(TUInt::GetStr(UInt, FmtStr));
107 }
108 
109 int TSOut::PutFlt(const double& Flt){
110  return PutStr(TFlt::GetStr(Flt));
111 }
112 
113 int TSOut::PutFlt(const double& Flt, const char* FmtStr){
114  return PutStr(TFlt::GetStr(Flt, FmtStr));
115 }
116 
117 int TSOut::PutStr(const char* CStr){
118  int Cs=UpdateLnLen(int(strlen(CStr)));
119  return Cs+PutBf(CStr, int(strlen(CStr)));
120 }
121 
122 int TSOut::PutStr(const TChA& ChA){
123  int Cs=UpdateLnLen(ChA.Len());
124  return Cs+PutBf(ChA.CStr(), ChA.Len());
125 }
126 
127 int TSOut::PutStr(const TStr& Str, const char* FmtStr){
128  return PutStr(TStr::GetStr(Str, FmtStr));
129 }
130 
131 int TSOut::PutStr(const TStr& Str, const bool& ForceInLn){
132  int Cs=UpdateLnLen(Str.Len(), ForceInLn);
133  return Cs+PutBf(Str.CStr(), Str.Len());
134 }
135 
136 int TSOut::PutStrFmt(const char *FmtStr, ...){
137  char Bf[10*1024];
138  va_list valist;
139  va_start(valist, FmtStr);
140  const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
141  va_end(valist);
142  return RetVal!=-1 ? PutStr(TStr(Bf)) : 0;
143 }
144 
145 int TSOut::PutStrFmtLn(const char *FmtStr, ...){
146  char Bf[10*1024];
147  va_list valist;
148  va_start(valist, FmtStr);
149  const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
150  va_end(valist);
151  return RetVal!=-1 ? PutStrLn(TStr(Bf)) : PutLn();
152 }
153 
154 int TSOut::PutIndent(const int& IndentLev){
155  return PutCh(' ', IndentLev*2);
156 }
157 
158 int TSOut::PutLn(const int& Lns){
159  LnLen=0; int Cs=0;
160  for (int LnN=0; LnN<Lns; LnN++){Cs+=PutCh('\n');}
161  return Cs;
162 }
163 
164 int TSOut::PutDosLn(const int& Lns){
165  LnLen=0; int Cs=0;
166  for (int LnN=0; LnN<Lns; LnN++){Cs+=PutCh(TCh::CrCh)+PutCh(TCh::LfCh);}
167  return Cs;
168 }
169 
170 int TSOut::PutSep(const int& NextStrLen){
171  int Cs=0;
172  if (MxLnLen==-1){
173  Cs+=PutCh(' ');
174  } else {
175  if (LnLen>0){
176  if (LnLen+1+NextStrLen>MxLnLen){Cs+=PutLn();} else {Cs+=PutCh(' ');}
177  }
178  }
179  return Cs;
180 }
181 
182 int TSOut::PutSepLn(const int& Lns){
183  int Cs=0;
184  if (LnLen>0){Cs+=PutLn();}
185  Cs+=PutLn(Lns);
186  return Cs;
187 }
188 
189 void TSOut::Save(const char* CStr){
190  int CStrLen=int(strlen(CStr));
191  EAssertR(CStrLen<=127, "Error writting stream '"+GetSNm()+"'.");
192  Save(char(CStrLen));
193  if (CStrLen>0){Cs+=PutBf(CStr, CStrLen);}
194 }
195 
196 void TSOut::Save(TSIn& SIn, const TSize& BfL){
197  Fail;
198  if (BfL==0){ //J: used to be ==-1
199  while (!SIn.Eof()){Save(SIn.GetCh());}
200  } else {
201  for (TSize BfC=0; BfC<BfL; BfC++){Save(SIn.GetCh());}
202  }
203 }
204 
206  while (!SIn.Eof())
207  operator<<((char)SIn.GetCh());
208  return *this;
209 }
210 
211 const PSOut TSOut::StdOut=PSOut(new TStdOut());
212 
213 TStdOut::TStdOut(): TSBase(TSStr("Standard output")), TSOut("Standard output"){}
214 
216 // Standard-Input
217 int TStdIn::GetBf(const void* LBf, const TSize& LBfL){
218  int LBfS=0;
219  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
220  LBfS+=(((char*)LBf)[LBfC]=GetCh());}
221  return LBfS;
222 }
223 
225  // not implemented
226  FailR(TStr::Fmt("TStdIn::GetNextLnBf: not implemented").CStr());
227  return false;
228 }
229 
231 // Standard-Output
232 int TStdOut::PutBf(const void* LBf, const TSize& LBfL){
233  int LBfS=0;
234  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
235  LBfS+=PutCh(((char*)LBf)[LBfC]);}
236  return LBfS;
237 }
238 
240 // Input-File
241 const int TFIn::MxBfL=16*1024;
242 
243 void TFIn::SetFPos(const int& FPos) const {
244  EAssertR(
245  fseek(FileId, FPos, SEEK_SET)==0,
246  "Error seeking into file '"+GetSNm()+"'.");
247 }
248 
249 int TFIn::GetFPos() const {
250  const int FPos=(int)ftell(FileId);
251  EAssertR(FPos!=-1, "Error seeking into file '"+GetSNm()+"'.");
252  return FPos;
253 }
254 
255 int TFIn::GetFLen() const {
256  const int FPos=GetFPos();
257  EAssertR(
258  fseek(FileId, 0, SEEK_END)==0,
259  "Error seeking into file '"+GetSNm()+"'.");
260  const int FLen=GetFPos(); SetFPos(FPos);
261  return FLen;
262 }
263 
265  EAssertR(
266  (BfC==BfL)&&((BfL==-1)||(BfL==MxBfL)),
267  "Error reading file '"+GetSNm()+"'.");
268  BfL=int(fread(Bf, 1, MxBfL, FileId));
269  EAssertR((BfC!=0)||(BfL!=0), "Error reading file '"+GetSNm()+"'.");
270  BfC=0;
271 }
272 
273 TFIn::TFIn(const TStr& FNm):
274  TSBase(FNm.CStr()), TSIn(FNm), FileId(NULL), Bf(NULL), BfC(0), BfL(0){
275  EAssertR(!FNm.Empty(), "Empty file-name.");
276  FileId=fopen(FNm.CStr(), "rb");
277  EAssertR(FileId!=NULL, "Can not open file '"+FNm+"'.");
278  Bf=new char[MxBfL]; BfC=BfL=-1; FillBf();
279 }
280 
281 TFIn::TFIn(const TStr& FNm, bool& OpenedP):
282  TSBase(FNm.CStr()), TSIn(FNm), FileId(NULL), Bf(NULL), BfC(0), BfL(0){
283  EAssertR(!FNm.Empty(), "Empty file-name.");
284  FileId=fopen(FNm.CStr(), "rb");
285  OpenedP=(FileId!=NULL);
286  if (OpenedP){
287  Bf=new char[MxBfL]; BfC=BfL=-1; FillBf();}
288 }
289 
290 PSIn TFIn::New(const TStr& FNm){
291  try {
292  return PSIn(new TFIn(FNm));
293  } catch (PExcept& Except) {
294  printf("*** Exception: %s\n", Except->GetMsgStr().CStr());
295  EFailR(Except->GetMsgStr());
296  }
297 
298  return PSIn(new TFIn(FNm));
299 }
300 
301 PSIn TFIn::New(const TStr& FNm, bool& OpenedP){
302  return PSIn(new TFIn(FNm, OpenedP));
303 }
304 
306  if (FileId!=NULL){
307  EAssertR(fclose(FileId)==0, "Can not close file '"+GetSNm()+"'.");}
308  if (Bf!=NULL){delete[] Bf;}
309 }
310 
311 int TFIn::GetBf(const void* LBf, const TSize& LBfL){
312  int LBfS=0;
313  if (TSize(BfC+LBfL)>TSize(BfL)){
314  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
315  if (BfC==BfL){FillBf();}
316  LBfS+=((char*)LBf)[LBfC]=Bf[BfC++];}
317  } else {
318  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
319  LBfS+=(((char*)LBf)[LBfC]=Bf[BfC++]);}
320  }
321  return LBfS;
322 }
323 
324 // Gets the next line to LnChA.
325 // Returns true, if LnChA contains a valid line.
326 // Returns false, if LnChA is empty, such as end of file was encountered.
327 
328 bool TFIn::GetNextLnBf(TChA& LnChA) {
329  int Status;
330  int BfN; // new pointer to the end of line
331  int BfP; // previous pointer to the line start
332  bool CrEnd; // last character in previous buffer was CR
333 
334  LnChA.Clr();
335 
336  CrEnd = false;
337  do {
338  if (BfC >= BfL) {
339  // reset the current pointer, FindEol() will read a new buffer
340  BfP = 0;
341  } else {
342  BfP = BfC;
343  }
344  Status = FindEol(BfN,CrEnd);
345  if (Status >= 0) {
346  if (BfN-BfP > 0) {
347  LnChA.AddBf(&Bf[BfP],BfN-BfP);
348  }
349  if (Status == 1) {
350  // got a complete line
351  return true;
352  }
353  }
354  // get more data, if the line is incomplete
355  } while (Status == 0);
356 
357  // eof or the last line has no newline
358  return !LnChA.Empty();
359 }
360 
361 // Sets BfN to the end of line or end of buffer. Reads more data, if needed.
362 // Returns 1, when an end of line was found, BfN is end of line.
363 // Returns 0, when an end of line was not found and more data is required,
364 // BfN is end of buffer.
365 // Returns -1, when an end of file was found, BfN is not defined.
366 
367 int TFIn::FindEol(int& BfN, bool& CrEnd) {
368  char Ch;
369 
370  if (BfC >= BfL) {
371  // read more data, check for eof
372  if (Eof()) {
373  return -1;
374  }
375  if (CrEnd && Bf[BfC]=='\n') {
376  BfC++;
377  BfN = BfC-1;
378  return 1;
379  }
380  }
381 
382  CrEnd = false;
383  while (BfC < BfL) {
384  Ch = Bf[BfC++];
385  if (Ch=='\n') {
386  BfN = BfC-1;
387  return 1;
388  }
389  if (Ch=='\r') {
390  if (BfC == BfL) {
391  CrEnd = true;
392  BfN = BfC-1;
393  return 0;
394  } else if (Bf[BfC]=='\n') {
395  BfC++;
396  BfN = BfC-2;
397  return 1;
398  }
399  }
400  }
401  BfN = BfC;
402 
403  return 0;
404 }
405 
407 // Output-File
408 const TSize TFOut::MxBfL=16*1024;;
409 
411  EAssertR(
412  fwrite(Bf, 1, BfL, FileId)==BfL,
413  "Error writting to the file '"+GetSNm()+"'.");
414  BfL=0;
415 }
416 
417 TFOut::TFOut(const TStr& FNm, const bool& Append):
418  TSBase(FNm.CStr()), TSOut(FNm), FileId(NULL), Bf(NULL), BfL(0){
419  if (FNm.GetUc()=="CON"){
420  FileId=stdout;
421  } else {
422  if (Append){FileId=fopen(FNm.CStr(), "a+b");}
423  else {FileId=fopen(FNm.CStr(), "w+b");}
424  EAssertR(FileId!=NULL, "Can not open file '"+FNm+"'.");
425  Bf=new char[MxBfL]; BfL=0;
426  }
427 }
428 
429 TFOut::TFOut(const TStr& FNm, const bool& Append, bool& OpenedP):
430  TSBase(FNm.CStr()), TSOut(FNm), FileId(NULL), Bf(NULL), BfL(0){
431  if (FNm.GetUc()=="CON"){
432  FileId=stdout;
433  } else {
434  if (Append){FileId=fopen(FNm.CStr(), "a+b");}
435  else {FileId=fopen(FNm.CStr(), "w+b");}
436  OpenedP=(FileId!=NULL);
437  if (OpenedP){
438  Bf=new char[MxBfL]; BfL=0;}
439  }
440 }
441 
442 PSOut TFOut::New(const TStr& FNm, const bool& Append){
443  return PSOut(new TFOut(FNm, Append));
444 }
445 
446 PSOut TFOut::New(const TStr& FNm, const bool& Append, bool& OpenedP){
447  PSOut SOut=PSOut(new TFOut(FNm, Append, OpenedP));
448  if (OpenedP){return SOut;} else {return NULL;}
449 }
450 
452  if (FileId!=NULL){FlushBf();}
453  if (Bf!=NULL){delete[] Bf;}
454  if (FileId!=NULL){
455  EAssertR(fclose(FileId)==0, "Can not close file '"+GetSNm()+"'.");}
456 }
457 
458 int TFOut::PutCh(const char& Ch){
459  if (BfL==TSize(MxBfL)){FlushBf();}
460  return Bf[BfL++]=Ch;
461 }
462 
463 int TFOut::PutBf(const void* LBf, const TSize& LBfL){
464  int LBfS=0;
465  if (BfL+LBfL>MxBfL){
466  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
467  LBfS+=PutCh(((char*)LBf)[LBfC]);}
468  } else {
469  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
470  LBfS+=(Bf[BfL++]=((char*)LBf)[LBfC]);}
471  }
472  return LBfS;
473 }
474 
476  FlushBf();
477  EAssertR(fflush(FileId)==0, "Can not flush file '"+GetSNm()+"'.");
478 }
479 
481 // Input-Output-File
482 TFInOut::TFInOut(const TStr& FNm, const TFAccess& FAccess, const bool& CreateIfNo) :
483  TSBase(TSStr(FNm.CStr())), FileId(NULL) {
484  switch (FAccess){
485  case faCreate: FileId=fopen(FNm.CStr(), "w+b"); break;
486  case faUpdate: FileId=fopen(FNm.CStr(), "r+b"); break;
487  case faAppend: FileId=fopen(FNm.CStr(), "r+b");
488  if (FileId!=NULL){fseek(FileId, SEEK_END, 0);} break;
489  case faRdOnly: FileId=fopen(FNm.CStr(), "rb"); break;
490  default: Fail;
491  }
492  if ((FileId==NULL)&&(CreateIfNo)){FileId=fopen(FNm.CStr(), "w+b");}
493  IAssert(FileId!=NULL);
494 }
495 
496 PSInOut TFInOut::New(const TStr& FNm, const TFAccess& FAccess, const bool& CreateIfNo) {
497  return PSInOut(new TFInOut(FNm, FAccess, CreateIfNo));
498 }
499 
500 int TFInOut::GetSize() const {
501  const int FPos = GetPos();
502  IAssert(fseek(FileId, 0, SEEK_END) == 0);
503  const int FLen = GetPos();
504  IAssert(fseek(FileId, FPos, SEEK_SET) == 0);
505  return FLen;
506 }
507 
508 int TFInOut::PutBf(const void* LBf, const TSize& LBfL) {
509  int LBfS = 0;
510  for (TSize i = 0; i < LBfL; i++) {
511  LBfS += ((char *)LBf)[i];
512  }
513  IAssert(fwrite(LBf, sizeof(char), LBfL, FileId) == (size_t) LBfL);
514  return LBfS;
515 }
516 
517 int TFInOut::GetBf(const void* LBf, const TSize& LBfL) {
518  IAssert(fread((void *)LBf, sizeof(char), LBfL, FileId) == (size_t) LBfL);
519  int LBfS = 0;
520  for (TSize i = 0; i < LBfL; i++) {
521  LBfS += ((char *)LBf)[i];
522  }
523  return LBfS;
524 }
525 
527  // not implemented
528  FailR(TStr::Fmt("TFInOut::GetNextLnBf: not implemented").CStr());
529  return false;
530 }
531 
533  return GetSNm();
534 }
535 
537 // Input-Memory
538 TMIn::TMIn(const void* _Bf, const uint64& _BfL, const bool& TakeBf):
539  TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(_BfL), IsMemoryMapped(false){
540  if (TakeBf){
541  Bf=(char*)_Bf;
542  } else {
543  Bf=new char[static_cast<size_t>(BfL)]; memmove(Bf, _Bf, static_cast<size_t>(BfL));
544  }
545 }
546 
548  TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){
549  BfL=SIn.Len(); Bf=new char[static_cast<size_t>(BfL)];
550  for (uint64 BfC=0; BfC<BfL; BfC++){Bf[BfC]=SIn.GetCh();}
551 }
552 
553 TMIn::TMIn(const char* CStr):
554  TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){
555  BfL=uint64(strlen(CStr)); Bf=new char[static_cast<size_t>(BfL+1)]; strcpy(Bf, CStr);
556 }
557 
558 /* GLib_LINUX should be defined if FromFile is true */
559 TMIn::TMIn(const TStr& Str, bool FromFile):
560  TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0){
561  if (FromFile == false) {
562  BfL=Str.Len(); Bf=new char[static_cast<size_t>(BfL)]; strncpy(Bf, Str.CStr(), static_cast<size_t>(BfL));
563  IsMemoryMapped = false;
564  }
565  else {
566 #ifdef GLib_LINUX
567  TStr FNm = Str;
568  TFileId FileId;
569  int fd;
570  uint64 FLen;
571  EAssertR(!FNm.Empty(), "Empty file-name.");
572  FileId=fopen(FNm.CStr(), "rb");
573  fd = fileno(FileId);
574 
575  EAssertR(FileId!=NULL, "Can not open file '"+FNm+"'.");
576 
577  EAssertR(
578  fseek(FileId, 0, SEEK_END)==0,
579  "Error seeking into file '"+TStr(FNm)+"'.");
580  FLen=(uint64)ftell(FileId);
581  EAssertR(
582  fseek(FileId, 0, SEEK_SET)==0,
583  "Error seeking into file '"+TStr(FNm)+"'.");
584 
585  // memory map contents of file
586  char *mapped;
587  mapped = (char *) mmap (0, FLen, PROT_READ, MAP_PRIVATE, fd, 0);
588  IsMemoryMapped = true;
589 
590  if (mapped == MAP_FAILED) {
591  printf("mmap failed: %d %s\n", fd, strerror (errno));
592  Bf = NULL;
593  BfC = BfL = 0;
594  }
595  else {
596  Bf = mapped;
597  BfC = 0;
598  BfL = FLen;
599  }
600  IsMemoryMapped = true;
601 #else
602  TExcept::Throw("TMIn::TMIn(TStr, Bool): GLib_LINUX undefined.\n");
603 #endif
604  }
605 }
606 
607 TMIn::TMIn(const TChA& ChA):
608  TSBase("Input-Memory"), TSIn("Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){
609  BfL=ChA.Len(); Bf=new char[static_cast<size_t>(BfL)]; strncpy(Bf, ChA.CStr(), static_cast<size_t>(BfL));
610 }
611 
612 PSIn TMIn::New(const void* _Bf, const uint64& _BfL, const bool& TakeBf){
613  return PSIn(new TMIn(_Bf, _BfL, TakeBf));
614 }
615 
616 PSIn TMIn::New(const char* CStr){
617  return PSIn(new TMIn(CStr));
618 }
619 
620 PSIn TMIn::New(const TStr& Str){
621  return PSIn(new TMIn(Str));
622 }
623 
624 PMIn TMIn::New(const TStr& Str, bool FromFile){
625  return new TMIn(Str, FromFile);
626 }
627 
628 PSIn TMIn::New(const TChA& ChA){
629  return PSIn(new TMIn(ChA));
630 }
631 
633  if (Bf!=NULL){
634  if (IsMemoryMapped) {
635 #ifdef GLib_LINUX
636  munmap(Bf, BfL);
637 #endif
638  }
639  else {
640  delete[] Bf;
641  }
642  }
643 }
644 
645 char TMIn::GetCh(){
646  EAssertR(BfC<BfL, "Reading beyond the end of stream.");
647  return Bf[BfC++];
648 }
649 
651  EAssertR(BfC<BfL, "Reading beyond the end of stream.");
652  return Bf[BfC];
653 }
654 
655 int TMIn::GetBf(const void* LBf, const TSize& LBfL){
656  EAssertR(TSize(BfC+LBfL)<=TSize(BfL), "Reading beyond the end of stream.");
657  int LBfS=0;
658  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
659  LBfS+=(((char*)LBf)[LBfC]=Bf[BfC++]);}
660  return LBfS;
661 }
662 
663 // Sets BfN to the end of line or end of buffer.
664 // Returns 1, when an end of line was found, BfN is end of line.
665 // Returns 0, when an end of line was not found and more data is required,
666 // BfN is end of buffer.
667 // Returns -1, when an end of file was found, BfN is not defined.
668 
669 int TMIn::FindEol(uint64& BfN, bool& CrEnd) {
670  char Ch;
671  if (BfC >= BfL) {
672  // read more data, check for eof
673  if (Eof()) {
674  return -1;
675  }
676  if (CrEnd && Bf[BfC]=='\n') {
677  BfC++;
678  BfN = BfC-1;
679  return 1;
680  }
681  }
682 
683  CrEnd = false;
684  while (BfC < BfL) {
685  Ch = Bf[BfC++];
686  if (Ch=='\n') {
687  BfN = BfC-1;
688  return 1;
689  }
690  if (Ch=='\r') {
691  if (BfC == BfL) {
692  CrEnd = true;
693  BfN = BfC-1;
694  return 0;
695  } else if (Bf[BfC]=='\n') {
696  BfC++;
697  BfN = BfC-2;
698  return 1;
699  }
700  }
701  }
702  BfN = BfC;
703 
704  return 0;
705 }
706 
707 bool TMIn::GetNextLnBf(TChA& LnChA){
708  // not implemented
709  FailR(TStr::Fmt("TMIn::GetNextLnBf: not implemented").CStr());
710  return false;
711 }
712 
714  return BfC;
715 }
716 
718  return BfL;
719 }
720 
721 void TMIn::SetBfC(uint64 Pos) {
722  BfC = Pos;
723 }
724 
725 // Assumes that lines end in '\n'
727  uint64 Cnt = 0;
728  if (Lb >= BfL) {
729  return 0;
730  }
731  for (uint64 i = Lb; i < Ub; i++) {
732  if (Bf[i] == '\n') {
733  Cnt += 1;
734  }
735  }
736  return Cnt;
737 }
738 
740  while (Ind > 0 && Bf[Ind-1] != '\n') {
741  Ind--;
742  }
743  return Ind;
744 }
745 
747  while (Ind < BfL && Bf[Ind] != '\n') {
748  Ind++;
749  }
750  if (Ind == BfL) Ind--;
751  return Ind;
752 }
753 
754 char* TMIn::GetLine(uint64 Index) {
755  return &Bf[Index];
756 }
757 
759  while (BfC < BfL && TCh::IsHashCh(Bf[BfC])) {
760  while (BfC < BfL && Bf[BfC] != '\n') {
761  BfC++;
762  }
763  BfC++;
764  }
765 }
766 
768 // Output-Memory
769 void TMOut::Resize(const int& ReqLen){
770  IAssert(OwnBf&&(BfL==MxBfL || ReqLen >= 0));
771  if (Bf==NULL){
772  IAssert(MxBfL==0);
773  if (ReqLen < 0) Bf=new char[MxBfL=1024];
774  else Bf=new char[MxBfL=ReqLen];
775  } else {
776  if (ReqLen < 0){ MxBfL*=2; }
777  else if (ReqLen < MxBfL){ return; } // nothing to do
778  else { MxBfL=(2*MxBfL < ReqLen ? ReqLen : 2*MxBfL); }
779  char* NewBf=new char[MxBfL];
780  memmove(NewBf, Bf, BfL); delete[] Bf; Bf=NewBf;
781  }
782 }
783 
784 TMOut::TMOut(const int& _MxBfL):
785  TSBase("Output-Memory"), TSOut("Output-Memory"),
786  Bf(NULL), BfL(0), MxBfL(0), OwnBf(true){
787  MxBfL=_MxBfL>0?_MxBfL:1024;
788  Bf=new char[MxBfL];
789 }
790 
791 TMOut::TMOut(char* _Bf, const int& _MxBfL):
792  TSBase("Output-Memory"), TSOut("Output-Memory"),
793  Bf(_Bf), BfL(0), MxBfL(_MxBfL), OwnBf(false){}
794 
795 void TMOut::AppendBf(const void* LBf, const TSize& LBfL) {
796  Resize(Len() + (int)LBfL);
797  memcpy(Bf + BfL, LBf, LBfL);
798  BfL += (int)LBfL;
799 }
800 
801 int TMOut::PutBf(const void* LBf, const TSize& LBfL){
802  int LBfS=0;
803  if (TSize(BfL+LBfL)>TSize(MxBfL)){
804  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
805  LBfS+=PutCh(((char*)LBf)[LBfC]);}
806  } else {
807  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
808  LBfS+=(Bf[BfL++]=((char*)LBf)[LBfC]);}
809  }
810  return LBfS;
811 }
812 
814  TChA ChA(BfL);
815  for (int BfC=0; BfC<BfL; BfC++){ChA+=Bf[BfC];}
816  return ChA;
817 }
818 
819 void TMOut::CutBf(const int& CutBfL){
820  IAssert((0<=CutBfL)&&(CutBfL<=BfL));
821  if (CutBfL==BfL){BfL=0;}
822  else {memmove(Bf, Bf+CutBfL, BfL-CutBfL); BfL=BfL-CutBfL;}
823 }
824 
825 PSIn TMOut::GetSIn(const bool& IsCut, const int& CutBfL){
826  IAssert((CutBfL==-1)||((0<=CutBfL)));
827  int SInBfL= (CutBfL==-1) ? BfL : TInt::GetMn(BfL, CutBfL);
828  PSIn SIn;
829  if (OwnBf&&IsCut&&(SInBfL==BfL)){
830  SIn=PSIn(new TMIn(Bf, SInBfL, true));
831  Bf=NULL; BfL=MxBfL=0; OwnBf=true;
832  } else {
833  SIn=PSIn(new TMIn(Bf, SInBfL, false));
834  if (IsCut){CutBf(SInBfL);}
835  }
836  return SIn;
837 }
838 
839 bool TMOut::IsCrLfLn() const {
840  for (int BfC=0; BfC<BfL; BfC++){
841  if ((Bf[BfC]==TCh::CrCh)&&((BfC+1<BfL)&&(Bf[BfC+1]==TCh::LfCh))){return true;}}
842  return false;
843 }
844 
846  IAssert(IsCrLfLn());
847  TChA Ln;
848  for (int BfC=0; BfC<BfL; BfC++){
849  char Ch=Bf[BfC];
850  if ((Ch==TCh::CrCh)&&((BfC+1<BfL)&&(Bf[BfC+1]==TCh::LfCh))){
851  Ln+=TCh::CrCh; Ln+=TCh::LfCh; CutBf(BfC+1+1); break;
852  } else {
853  Ln+=Ch;
854  }
855  }
856  return Ln;
857 }
858 
859 bool TMOut::IsEolnLn() const {
860  for (int BfC=0; BfC<BfL; BfC++){
861  if ((Bf[BfC]==TCh::CrCh)||(Bf[BfC]==TCh::LfCh)){return true;}
862  }
863  return false;
864 }
865 
866 TStr TMOut::GetEolnLn(const bool& DoAddEoln, const bool& DoCutBf){
867  IAssert(IsEolnLn());
868  int LnChs=0; TChA Ln;
869  for (int BfC=0; BfC<BfL; BfC++){
870  char Ch=Bf[BfC];
871  if ((Ch==TCh::CrCh)||(Ch==TCh::LfCh)){
872  LnChs++; if (DoAddEoln){Ln+=Ch;}
873  if (BfC+1<BfL){
874  char NextCh=Bf[BfC+1];
875  if (((Ch==TCh::CrCh)&&(NextCh==TCh::LfCh))||
876  ((Ch==TCh::LfCh)&&(NextCh==TCh::CrCh))){
877  LnChs++; if (DoAddEoln){Ln+=NextCh;}
878  }
879  }
880  break;
881  } else {
882  LnChs++; Ln+=Ch;
883  }
884  }
885  if (DoCutBf){
886  CutBf(LnChs);
887  }
888  return Ln;
889 }
890 
892  if (!IsEolnLn()){
894 }
895 
897 // Line-Returner
898 // J: after talking to BlazF -- can be removed from GLib
899 bool TLnRet::NextLn(TStr& LnStr) {
900  if (SIn->Eof()) { return false; }
901  TChA LnChA; char Ch = TCh::EofCh;
902  while (!SIn->Eof() && ((Ch=SIn->GetCh())!='\n')) {
903  if (Ch != '\r') { LnChA += Ch; }
904  }
905  LnStr = LnChA; return true;
906 }
907 
909 // fseek-Constants-Definitions
910 // because of strange Borland CBuilder behaviour in sysdefs.h
911 #ifndef SEEK_SET
912 #define SEEK_CUR 1
913 #define SEEK_END 2
914 #define SEEK_SET 0
915 #endif
916 
918 // Random-File
920  EAssertR(
921  fseek(FileId, 0, SEEK_CUR)==0,
922  "Error seeking into file '"+TStr(FNm)+"'.");
923 }
924 
925 TFRnd::TFRnd(const TStr& _FNm, const TFAccess& FAccess,
926  const bool& CreateIfNo, const int& _HdLen, const int& _RecLen):
927  FileId(NULL), FNm(_FNm.CStr()),
928  RecAct(false), HdLen(_HdLen), RecLen(_RecLen){
929  RecAct=(HdLen>=0)&&(RecLen>0);
930  switch (FAccess){
931  case faCreate: FileId=fopen(FNm.CStr(), "w+b"); break;
932  case faUpdate: FileId=fopen(FNm.CStr(), "r+b"); break;
933  case faAppend: FileId=fopen(FNm.CStr(), "r+b");
934  if (FileId!=NULL){fseek(FileId, SEEK_END, 0);} break;
935  case faRdOnly: FileId=fopen(FNm.CStr(), "rb"); break;
936  default: Fail;
937  }
938  if ((FileId==NULL)&&(CreateIfNo)){
939  FileId=fopen(FNm.CStr(), "w+b");}
940  EAssertR(FileId!=NULL, "Can not open file '"+_FNm+"'.");
941 }
942 
944  EAssertR(fclose(FileId)==0, "Can not close file '"+TStr(FNm)+"'.");
945 }
946 
948  return FNm.CStr();
949 }
950 
951 void TFRnd::SetFPos(const int& FPos){
952  EAssertR(
953  fseek(FileId, FPos, SEEK_SET)==0,
954  "Error seeking into file '"+TStr(FNm)+"'.");
955 }
956 
957 void TFRnd::MoveFPos(const int& DFPos){
958  EAssertR(
959  fseek(FileId, DFPos, SEEK_CUR)==0,
960  "Error seeking into file '"+TStr(FNm)+"'.");
961 }
962 
964  int FPos= (int) ftell(FileId);
965  EAssertR(FPos!=-1, "Error seeking into file '"+TStr(FNm)+"'.");
966  return FPos;
967 }
968 
970  int FPos=GetFPos();
971  EAssertR(
972  fseek(FileId, 0, SEEK_END)==0,
973  "Error seeking into file '"+TStr(FNm)+"'.");
974  int FLen=GetFPos(); SetFPos(FPos); return FLen;
975 }
976 
977 void TFRnd::SetRecN(const int& RecN){
978  IAssert(RecAct);
979  SetFPos(HdLen+RecN*RecLen);
980 }
981 
983  IAssert(RecAct);
984  int FPos=GetFPos()-HdLen;
985  EAssertR(FPos%RecLen==0, "Invalid position in file'"+TStr(FNm)+"'.");
986  return FPos/RecLen;
987 }
988 
990  IAssert(RecAct);
991  int FLen=GetFLen()-HdLen;
992  EAssertR(FLen%RecLen==0, "Invalid length of file'"+TStr(FNm)+"'.");
993  return FLen/RecLen;
994 }
995 
996 void TFRnd::GetBf(void* Bf, const TSize& BfL){
997  RefreshFPos();
998  EAssertR(
999  fread(Bf, 1, BfL, FileId)==BfL,
1000  "Error reading file '"+TStr(FNm)+"'.");
1001 }
1002 
1003 void TFRnd::PutBf(const void* Bf, const TSize& BfL){
1004  RefreshFPos();
1005  EAssertR(
1006  fwrite(Bf, 1, BfL, FileId)==BfL,
1007  "Error writting to the file '"+TStr(FNm)+"'.");
1008 }
1009 
1011  EAssertR(fflush(FileId)==0, "Can not flush file '"+TStr(FNm)+"'.");
1012 }
1013 
1014 void TFRnd::PutCh(const char& Ch, const int& Chs){
1015  if (Chs>0){
1016  char* CStr=new char[Chs];
1017  for (int ChN=0; ChN<Chs; ChN++){CStr[ChN]=Ch;}
1018  PutBf(CStr, Chs);
1019  delete[] CStr;
1020  }
1021 }
1022 
1023 void TFRnd::PutStr(const TStr& Str){
1024  PutBf(Str.CStr(), Str.Len()+1);
1025 }
1026 
1027 TStr TFRnd::GetStr(const int& StrLen, bool& IsOk){
1028  IsOk=false; TStr Str;
1029  if (GetFPos()+StrLen+1<=GetFLen()){
1030  char* CStr=new char[StrLen+1];
1031  GetBf(CStr, StrLen+1);
1032  if (CStr[StrLen+1-1]==TCh::NullCh){IsOk=true; Str=CStr;}
1033  delete[] CStr;
1034  }
1035  return Str;
1036 }
1037 
1038 TStr TFRnd::GetStr(const int& StrLen){
1039  TStr Str;
1040  char* CStr=new char[StrLen+1];
1041  GetBf(CStr, StrLen+1);
1042  EAssertR(CStr[StrLen+1-1]==TCh::NullCh, "Error reading file '"+TStr(FNm)+"'.");
1043  Str=CStr;
1044  delete[] CStr;
1045  return Str;
1046 }
1047 
1048 void TFRnd::PutSIn(const PSIn& SIn, TCs& Cs){
1049  int BfL=SIn->Len();
1050  char* Bf=new char[BfL];
1051  SIn->GetBf(Bf, BfL);
1052  Cs=TCs::GetCsFromBf(Bf, BfL);
1053  PutBf(Bf, BfL);
1054  delete[] Bf;
1055 }
1056 
1057 PSIn TFRnd::GetSIn(const int& BfL, TCs& Cs){
1058  char* Bf=new char[BfL];
1059  GetBf(Bf, BfL);
1060  Cs=TCs::GetCsFromBf(Bf, BfL);
1061  PSIn SIn=PSIn(new TMIn(Bf, BfL, true));
1062  return SIn;
1063 }
1064 
1066  switch (FAccess){
1067  case faCreate: return "Create";
1068  case faUpdate: return "Update";
1069  case faAppend: return "Append";
1070  case faRdOnly: return "ReadOnly";
1071  case faRestore: return "Restore";
1072  default: Fail; return TStr();
1073  }
1074 }
1075 
1077  TStr UcStr=Str.GetUc();
1078  if (UcStr=="CREATE"){return faCreate;}
1079  if (UcStr=="UPDATE"){return faUpdate;}
1080  if (UcStr=="APPEND"){return faAppend;}
1081  if (UcStr=="READONLY"){return faRdOnly;}
1082  if (UcStr=="RESTORE"){return faRestore;}
1083 
1084  if (UcStr=="NEW"){return faCreate;}
1085  if (UcStr=="CONT"){return faUpdate;}
1086  if (UcStr=="CONTINUE"){return faUpdate;}
1087  if (UcStr=="REST"){return faRestore;}
1088  if (UcStr=="RESTORE"){return faRestore;}
1089  return faUndef;
1090 }
1091 
1093 // Files
1094 const TStr TFile::TxtFExt=".Txt";
1095 const TStr TFile::HtmlFExt=".Html";
1096 const TStr TFile::HtmFExt=".Htm";
1097 const TStr TFile::GifFExt=".Gif";
1098 const TStr TFile::JarFExt=".Jar";
1099 
1100 bool TFile::Exists(const TStr& FNm){
1101  if (FNm.Empty()) { return false; }
1102  bool DoExists;
1103  TFIn FIn(FNm, DoExists);
1104  return DoExists;
1105 }
1106 
1107 #if defined(GLib_WIN32)
1108 
1109 void TFile::Copy(const TStr& SrcFNm, const TStr& DstFNm,
1110  const bool& ThrowExceptP, const bool& FailIfExistsP){
1111  if (ThrowExceptP){
1112  if (CopyFile(SrcFNm.CStr(), DstFNm.CStr(), FailIfExistsP) == 0) {
1113  int ErrorCode = (int)GetLastError();
1115  "Error %d copying file '%s' to '%s'.",
1116  ErrorCode, SrcFNm.CStr(), DstFNm.CStr()));
1117  }
1118  } else {
1119  CopyFile(SrcFNm.CStr(), DstFNm.CStr(), FailIfExistsP);
1120  }
1121 }
1122 
1123 #elif defined(GLib_LINUX)
1124 
1125 void TFile::Copy(const TStr& SrcFNm, const TStr& DstFNm,
1126  const bool& ThrowExceptP, const bool& FailIfExistsP){
1127  int input, output;
1128  size_t filesize;
1129  void *source, *target;
1130 
1131  if( (input = open(SrcFNm.CStr(), O_RDONLY)) == -1) {
1132  if (ThrowExceptP) {
1134  "Error copying file '%s' to '%s': cannot open source file for reading.",
1135  SrcFNm.CStr(), DstFNm.CStr()));
1136  } else {
1137  return;
1138  }
1139  }
1140 
1141 
1142  if( (output = open(DstFNm.CStr(), O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1143  close(input);
1144 
1145  if (ThrowExceptP) {
1147  "Error copying file '%s' to '%s': cannot open destination file for writing.",
1148  SrcFNm.CStr(), DstFNm.CStr()));
1149  } else {
1150  return;
1151  }
1152  }
1153 
1154 
1155  filesize = lseek(input, 0, SEEK_END);
1156  lseek(output, filesize - 1, SEEK_SET);
1157  write(output, '\0', 1);
1158 
1159  if((source = mmap(0, filesize, PROT_READ, MAP_SHARED, input, 0)) == (void *) -1) {
1160  close(input);
1161  close(output);
1162  if (ThrowExceptP) {
1164  "Error copying file '%s' to '%s': cannot mmap input file.",
1165  SrcFNm.CStr(), DstFNm.CStr()));
1166  } else {
1167  return;
1168  }
1169  }
1170 
1171  if((target = mmap(0, filesize, PROT_WRITE, MAP_SHARED, output, 0)) == (void *) -1) {
1172  munmap(source, filesize);
1173  close(input);
1174  close(output);
1175  if (ThrowExceptP) {
1177  "Error copying file '%s' to '%s': cannot mmap output file.",
1178  SrcFNm.CStr(), DstFNm.CStr()));
1179  } else {
1180  return;
1181  }
1182  }
1183 
1184  memcpy(target, source, filesize);
1185 
1186  munmap(source, filesize);
1187  munmap(target, filesize);
1188 
1189  close(input);
1190  close(output);
1191 
1192 }
1193 
1194 
1195 
1196 #endif
1197 
1198 void TFile::Del(const TStr& FNm, const bool& ThrowExceptP){
1199  if (ThrowExceptP){
1200  EAssertR(
1201  remove(FNm.CStr())==0,
1202  "Error removing file '"+FNm+"'.");
1203  } else {
1204  remove(FNm.CStr());
1205  }
1206 }
1207 
1208 void TFile::DelWc(const TStr& WcStr, const bool& RecurseDirP){
1209  // collect file-names
1210  TStrV FNmV;
1211  TFFile FFile(WcStr, RecurseDirP); TStr FNm;
1212  while (FFile.Next(FNm)){
1213  FNmV.Add(FNm);}
1214  // delete files
1215  for (int FNmN=0; FNmN<FNmV.Len(); FNmN++){
1216  Del(FNmV[FNmN], false);}
1217 }
1218 
1219 void TFile::Rename(const TStr& SrcFNm, const TStr& DstFNm){
1220  EAssertR(
1221  rename(SrcFNm.CStr(), DstFNm.CStr())==0,
1222  "Error renaming file '"+SrcFNm+"' to "+DstFNm+"'.");
1223 }
1224 
1226  // <name>.#.txt --> <name>.<num>.txt
1227  int Cnt=1; int ch;
1228  TStr NewFNm; TStr TmpFNm=FNm;
1229  if (FNm.SearchCh('#') == -1) {
1230  for (ch = FNm.Len()-1; ch >= 0; ch--) if (FNm[ch] == '.') break;
1231  if (ch != -1) TmpFNm.InsStr(ch, ".#");
1232  else TmpFNm += ".#";
1233  }
1234  forever{
1235  NewFNm=TmpFNm;
1236  NewFNm.ChangeStr("#", TStr::Fmt("%03d", Cnt)); Cnt++;
1237  if (!TFile::Exists(NewFNm)){break;}
1238  }
1239  return NewFNm;
1240 }
1241 
1242 #ifdef GLib_WIN
1243 
1244 uint64 TFile::GetSize(const TStr& FNm) {
1245  // open
1246  HANDLE hFile = CreateFile(
1247  FNm.CStr(), // file to open
1248  GENERIC_READ, // open for reading
1249  FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading
1250  NULL, // default security
1251  OPEN_EXISTING, // existing file only
1252  FILE_ATTRIBUTE_NORMAL, // normal file
1253  NULL); // no attr. template
1254  // check if we could open it
1255  if (hFile == INVALID_HANDLE_VALUE) {
1256  TExcept::Throw("Can not open file " + FNm + "!"); }
1257  // read file times
1258  LARGE_INTEGER lpFileSizeHigh;
1259  if (!GetFileSizeEx(hFile, &lpFileSizeHigh)) {
1260  TExcept::Throw("Can not read size of file " + FNm + "!"); }
1261  // close file
1262  CloseHandle(hFile);
1263  // convert to uint64
1264  return uint64(lpFileSizeHigh.QuadPart);
1265 }
1266 
1267 uint64 TFile::GetCreateTm(const TStr& FNm) {
1268  // open
1269  HANDLE hFile = CreateFile(
1270  FNm.CStr(), // file to open
1271  GENERIC_READ, // open for reading
1272  FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading
1273  NULL, // default security
1274  OPEN_EXISTING, // existing file only
1275  FILE_ATTRIBUTE_NORMAL, // normal file
1276  NULL); // no attr. template
1277  // check if we could open it
1278  if (hFile == INVALID_HANDLE_VALUE) {
1279  TExcept::Throw("Can not open file " + FNm + "!"); }
1280  // read file times
1281  FILETIME lpCreationTime;
1282  if (!GetFileTime(hFile, &lpCreationTime, NULL, NULL)) {
1283  TExcept::Throw("Can not read time from file " + FNm + "!"); }
1284  // close file
1285  CloseHandle(hFile);
1286  // convert to uint64
1287  TUInt64 UInt64(uint(lpCreationTime.dwHighDateTime),
1288  uint(lpCreationTime.dwLowDateTime));
1289  return UInt64.Val / uint64(10000);
1290 }
1291 
1292 uint64 TFile::GetLastAccessTm(const TStr& FNm) {
1293  // open
1294  HANDLE hFile = CreateFile(
1295  FNm.CStr(), // file to open
1296  GENERIC_READ, // open for reading
1297  FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading
1298  NULL, // default security
1299  OPEN_EXISTING, // existing file only
1300  FILE_ATTRIBUTE_NORMAL, // normal file
1301  NULL); // no attr. template
1302  // check if we could open it
1303  if (hFile == INVALID_HANDLE_VALUE) {
1304  TExcept::Throw("Can not open file " + FNm + "!"); }
1305  // read file times
1306  FILETIME lpLastAccessTime;
1307  if (!GetFileTime(hFile, NULL, &lpLastAccessTime, NULL)) {
1308  TExcept::Throw("Can not read time from file " + FNm + "!"); }
1309  // close file
1310  CloseHandle(hFile);
1311  // convert to uint64
1312  TUInt64 UInt64(uint(lpLastAccessTime.dwHighDateTime),
1313  uint(lpLastAccessTime.dwLowDateTime));
1314  return UInt64.Val / uint64(10000);
1315 }
1316 
1317 uint64 TFile::GetLastWriteTm(const TStr& FNm) {
1318  // open
1319  HANDLE hFile = CreateFile(
1320  FNm.CStr(), // file to open
1321  GENERIC_READ, // open for reading
1322  FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading
1323  NULL, // default security
1324  OPEN_EXISTING, // existing file only
1325  FILE_ATTRIBUTE_NORMAL, // normal file
1326  NULL); // no attr. template
1327  // check if we could open it
1328  if (hFile == INVALID_HANDLE_VALUE) {
1329  TExcept::Throw("Can not open file " + FNm + "!"); }
1330  // read file times
1331  FILETIME lpLastWriteTime;
1332  if (!GetFileTime(hFile, NULL, NULL, &lpLastWriteTime)) {
1333  TExcept::Throw("Can not read time from file " + FNm + "!"); }
1334  // close file
1335  CloseHandle(hFile);
1336  // convert to uint64
1337  TUInt64 UInt64(uint(lpLastWriteTime.dwHighDateTime),
1338  uint(lpLastWriteTime.dwLowDateTime));
1339  return UInt64.Val / uint64(10000);
1340 }
1341 
1342 #elif defined(GLib_LINUX)
1343 
1344 uint64 TFile::GetSize(const TStr& FNm) {
1345  Fail; return 0;
1346 }
1347 
1348 uint64 TFile::GetCreateTm(const TStr& FNm) {
1349  return GetLastWriteTm(FNm);
1350 }
1351 
1352 uint64 TFile::GetLastWriteTm(const TStr& FNm) {
1353  struct stat st;
1354  if (stat(FNm.CStr(), &st) != 0) {
1355  TExcept::Throw("Cannot read tile from file " + FNm + "!");
1356  }
1357  return uint64(st.st_mtime);
1358 }
1359 
1360 
1361 #endif
int PutMem(const TMem &Mem)
Definition: fl.cpp:79
#define IAssert(Cond)
Definition: bd.h:262
Definition: bd.h:514
~TFOut()
Definition: fl.cpp:451
int SearchCh(const char &Ch, const int &BChN=0) const
Definition: dt.cpp:1043
TStr GetStr(const int &StrLen)
Definition: fl.cpp:1038
int GetBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:655
char * CStr()
Definition: bd.h:531
TStr GetStr() const
Definition: dt.h:1107
static PSOut New(const TStr &FNm, const bool &Append=false)
Definition: fl.cpp:442
Definition: xfl.h:30
int Len() const
Definition: dt.h:487
Definition: fl.h:347
TStr GetFNm() const
Definition: fl.cpp:532
int PutStrFmtLn(const char *FmtStr,...)
Definition: fl.cpp:145
int GetBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:311
int BfL
Definition: fl.h:445
virtual int PutCh(const char &Ch)=0
int BfL
Definition: fl.h:280
virtual int PutBf(const void *LBf, const TSize &LBfL)=0
static void Rename(const TStr &SrcFNm, const TStr &DstFNm)
Definition: fl.cpp:1219
static bool Exists(const TStr &FNm)
Definition: fl.cpp:1100
#define SEEK_END
Definition: fl.cpp:913
TMOut(const TMOut &)
static const TStr GifFExt
Definition: fl.h:592
TStdOut()
Definition: fl.cpp:213
int PutCh(const char &Ch)
Definition: fl.cpp:458
#define forever
Definition: bd.h:6
void AppendBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:795
int Len() const
Definition: dt.h:134
unsigned int uint
Definition: bd.h:11
bool RecAct
Definition: fl.h:520
#define SEEK_CUR
Definition: fl.cpp:912
PSIn GetSIn(const bool &IsCut=true, const int &CutBfL=-1)
Definition: fl.cpp:825
void Flush()
Definition: fl.cpp:1010
int PutBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:508
bool Empty() const
Definition: dt.h:260
void FillBf()
Definition: fl.cpp:264
#define Fail
Definition: bd.h:238
int FindEol(int &BfN, bool &CrEnd)
Definition: fl.cpp:367
int GetFLen()
Definition: fl.cpp:969
TStr GetUc() const
Definition: dt.h:493
uint64 GetBfL()
Definition: fl.cpp:717
static const char NullCh
Definition: dt.h:943
static bool IsHashCh(const char &Ch)
Definition: dt.h:968
int PutCh(const char &Ch)
Definition: fl.h:458
bool IsEolnLn() const
Definition: fl.cpp:859
TFileId FileId
Definition: fl.h:518
void Clr()
Definition: dt.h:258
int LnLen
Definition: fl.h:130
void AddCh(const char &Ch, const int &MxLen=-1)
Definition: dt.h:271
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TStdIn()
Definition: fl.cpp:63
void MkEolnLn()
Definition: fl.cpp:891
void CutBf(const int &CutBfL)
Definition: fl.cpp:819
TFileId FileId
Definition: fl.h:278
#define SEEK_SET
Definition: fl.cpp:914
void PutSIn(const PSIn &SIn, TCs &Cs)
Definition: fl.cpp:1048
int PutBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:463
virtual int GetBf(const void *Bf, const TSize &BfL)=0
static const TStr HtmlFExt
Definition: fl.h:590
Definition: fl.h:347
int Len() const
Definition: dt.h:259
TStr GetCrLfLn()
Definition: fl.cpp:845
int PutBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:232
~TFIn()
Definition: fl.cpp:305
~TFRnd()
Definition: fl.cpp:943
char PeekCh()
Definition: fl.cpp:650
bool GetNextLnBf(TChA &LnChA)
Definition: fl.cpp:707
int PutSepLn(const int &Lns=0)
Definition: fl.cpp:182
TStr GetFNm() const
Definition: fl.cpp:947
int PutLn(const int &Lns=1)
Definition: fl.cpp:158
Definition: fl.h:275
TStr GetAsStr() const
Definition: fl.cpp:813
Definition: fl.h:40
void LoadCs()
Definition: fl.cpp:28
bool GetNextLnBf(TChA &LnChA)
Definition: fl.cpp:328
char * Bf
Definition: fl.h:323
int GetFPos() const
Definition: fl.cpp:249
Definition: fl.h:384
void FlushBf()
Definition: fl.cpp:410
Definition: fl.h:58
TFileId FileId
Definition: fl.h:322
int UpdateLnLen(const int &StrLen, const bool &ForceInLn=false)
Definition: fl.cpp:70
Definition: dt.h:77
static const char EofCh
Definition: dt.h:947
static uint64 GetSize(const TStr &FNm)
void Flush()
Definition: fl.cpp:475
char * Bf
Definition: fl.h:386
Definition: fl.h:240
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
void SkipCommentLines()
Move stream pointer along until a non commented line is found.
Definition: fl.cpp:758
int GetBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:217
static const int MxMask
Definition: fl.h:13
static const TStr TxtFExt
Definition: fl.h:589
static TCs GetCsFromBf(char *Bf, const int &BfL)
Definition: fl.cpp:12
static void DelWc(const TStr &WcStr, const bool &RecurseDirP=false)
Definition: fl.cpp:1208
static int GetMn(const int &Int1, const int &Int2)
Definition: dt.h:1090
char * CStr()
Definition: dt.h:255
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
Definition: fl.h:347
static TStr GetUniqueFNm(const TStr &FNm)
Definition: fl.cpp:1225
bool GetNextLnBf(TChA &LnChA)
Definition: fl.cpp:224
virtual bool Eof()=0
void MoveFPos(const int &DFPos)
Definition: fl.cpp:957
static const TStr HtmFExt
Definition: fl.h:591
unsigned long long uint64
Definition: bd.h:38
uint64 BfL
Definition: fl.h:387
void Load(bool &Bool)
Definition: fl.h:84
TFRnd(const TFRnd &)
TStr GetStr() const
Definition: dt.h:1189
size_t TSize
Definition: bd.h:58
int PutBool(const bool &Bool)
Definition: fl.cpp:89
static void Copy(const TStr &SrcFNm, const TStr &DstFNm, const bool &ThrowExceptP=true, const bool &FailIfExistsP=false)
char * Bf
Definition: fl.h:279
static void Del(const TStr &FNm, const bool &ThrowExceptP=true)
Definition: fl.cpp:1198
Definition: fl.h:347
uint64 CountNewLinesInRange(uint64 Lb, uint64 Ub)
Finds number of new line chars in interval [Lb, Ub)
Definition: fl.cpp:726
int PutBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:801
int PutSep(const int &NextStrLen=0)
Definition: fl.cpp:170
int PutInt(const int &Int)
Definition: fl.cpp:93
bool Eof()
Definition: fl.h:298
int MxBfL
Definition: fl.h:445
uint64 GetBfC()
Definition: fl.cpp:713
void SetFPos(const int &FPos)
Definition: fl.cpp:951
bool IsMemoryMapped
Definition: fl.h:388
#define EFailR(Reason)
Definition: bd.h:246
TPt< TSInOut > PSInOut
Definition: fl.h:236
TFileId FileId
Definition: fl.h:351
#define FailR(Reason)
Definition: bd.h:240
void PutCh(const char &Ch)
Definition: fl.h:566
static const TPt< TSOut > StdOut
Definition: fl.h:208
int FindEol(uint64 &BfN, bool &CrEnd)
Definition: fl.cpp:669
Definition: fl.h:128
int GetRecs()
Definition: fl.cpp:989
bool OwnBf
Definition: fl.h:446
int PutIndent(const int &IndentLev=1)
Definition: fl.cpp:154
static PSIn New(const void *_Bf, const uint64 &_BfL, const bool &TakeBf=false)
Definition: fl.cpp:612
int PutFlt(const double &Flt)
Definition: fl.cpp:109
Definition: fl.h:260
static const char LfCh
Definition: dt.h:945
TStr GetStr() const
Definition: dt.h:678
int GetRecN()
Definition: fl.cpp:982
void Save(const bool &Bool)
Definition: fl.h:173
void PutStr(const TStr &Str)
Definition: fl.cpp:1023
static PSInOut New(const TStr &FNm, const TFAccess &FAccess, const bool &CreateIfNo)
Definition: fl.cpp:496
uint64 GetLineEndPos(uint64 Ind)
Finds end of line in which Ind is present.
Definition: fl.cpp:746
int BfC
Definition: fl.h:280
Definition: fl.h:347
Definition: dt.h:201
uint64 GetLineStartPos(uint64 Ind)
Finds beginning of line in which Ind is present.
Definition: fl.cpp:739
int PutDosLn(const int &Lns=1)
Definition: fl.cpp:164
char * GetLine(uint64 Ind)
Definition: fl.cpp:754
static const TPt< TSIn > StdIn
Definition: fl.h:116
static const TSize MxBfL
Definition: fl.h:321
void AddBf(char *NewBf, const int &BfS)
Definition: dt.h:275
static TStr GetStrFromFAccess(const TFAccess &FAccess)
Definition: fl.cpp:1065
TSIn()
Definition: fl.h:65
char GetCh()
Definition: fl.h:250
int GetBf(const void *LBf, const TSize &LBfL)
Definition: fl.cpp:517
int MxLnLen
Definition: fl.h:130
int GetPos() const
Definition: fl.h:373
Definition: dt.h:412
bool Eof()
Definition: fl.h:411
bool Empty() const
Definition: dt.h:488
static TFAccess GetFAccessFromStr(const TStr &Str)
Definition: fl.cpp:1076
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
int PutStr(const char *CStr)
Definition: fl.cpp:117
PSIn SIn
Definition: fl.h:506
static const char CrCh
Definition: dt.h:946
FILE * TFileId
Definition: bd.h:17
uint64 BfC
Definition: fl.h:387
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
PSIn GetSIn(const int &SInLen, TCs &Cs)
Definition: fl.cpp:1057
static TStr GetStr(const bool &Val)
Definition: dt.h:918
virtual TStr GetSNm() const
Definition: fl.cpp:20
int PutCh(const char &Ch)
Definition: fl.h:268
static uint64 GetLastWriteTm(const TStr &FNm)
static uint64 GetCreateTm(const TStr &FNm)
void SetBfC(uint64 Pos)
Definition: fl.cpp:721
static const int MxBfL
Definition: fl.h:277
TStr GetStr() const
Definition: dt.h:1369
TSOut()
Definition: fl.h:136
int GetSize() const
Definition: fl.cpp:500
TPt< TSIn > PSIn
Definition: fl.h:119
void Resize(const int &ReqLen=-1)
Definition: fl.cpp:769
bool IsCrLfLn() const
Definition: fl.cpp:839
bool Next(TStr &FNm)
TSize BfL
Definition: fl.h:324
char * Bf
Definition: fl.h:444
void SetRecN(const int &RecN)
Definition: fl.cpp:977
virtual char GetCh()=0
TSStr FNm
Definition: fl.h:519
int HdLen
Definition: fl.h:521
virtual char PeekCh()=0
void PutBf(const void *Bf, const TSize &BfL)
Definition: fl.cpp:1003
int GetFPos()
Definition: fl.cpp:963
char * CStr()
Definition: dt.h:476
int Len() const
Definition: fl.h:464
virtual int Len() const =0
int PutStrLn(const TStr &Str, const bool &ForceInLn=false)
Definition: fl.h:161
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int GetFLen() const
Definition: fl.cpp:255
Definition: fl.h:11
int PutStrFmt(const char *FmtStr,...)
Definition: fl.cpp:136
void GetBf(void *Bf, const TSize &BfL)
Definition: fl.cpp:996
void InsStr(const int &BChN, const TStr &Str)
Definition: dt.cpp:825
~TMIn()
Definition: fl.cpp:632
TSOut & operator<<(const bool &Bool)
Definition: fl.h:191
TFAccess
Definition: fl.h:347
int PutUInt(const uint &Int)
Definition: fl.cpp:101
int RecLen
Definition: fl.h:521
bool GetNextLnBf(TChA &LnChA)
Definition: fl.cpp:526
TPt< TSOut > PSOut
Definition: fl.h:211
Definition: fl.h:347
TStr GetEolnLn(const bool &DoAddEoln, const bool &DoCutBf)
Definition: fl.cpp:866
TSStr SNm
Definition: fl.h:43
char GetCh()
Definition: fl.cpp:645
void SetFPos(const int &FPos) const
Definition: fl.cpp:243
TCs Cs
Definition: fl.h:44
Definition: dt.h:1225
static const TStr JarFExt
Definition: fl.h:593
void RefreshFPos()
Definition: fl.cpp:919
int ChangeStr(const TStr &SrcStr, const TStr &DstStr, const int &BChN=0)
Definition: dt.cpp:1130
bool NextLn(TStr &LnStr)
Definition: fl.cpp:899
bool GetNextLn(TStr &LnStr)
Definition: fl.cpp:43
static uint64 GetLastAccessTm(const TStr &FNm)