14 for (
int BfC=0; BfC<BfL; BfC++){Cs+=Bf[BfC];}
30 Cs+=
GetBf(&TestCs,
sizeof(TestCs));
31 EAssertR(CurCs==TestCs,
"Invalid checksum reading '"+
GetSNm()+
"'.");
38 CStr=
new char[CStrLen+1];
39 if (CStrLen>0){
Cs+=
GetBf(CStr, CStrLen);}
53 const char Ch=
GetCh();
54 if (Ch==
'\n'){
return true;}
55 if (Ch==
'\r' &&
PeekCh()==
'\n'){
GetCh();
return true;}
58 return !LnChA.
Empty();
68 TSBase(Str.CStr()), MxLnLen(-1), LnLen(0){}
85 for (
int ChN=0; ChN<Chs; ChN++){Cs+=
PutCh(Ch);}
119 return Cs+
PutBf(CStr,
int(strlen(CStr)));
139 va_start(valist, FmtStr);
140 const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
148 va_start(valist, FmtStr);
149 const int RetVal=vsnprintf(Bf, 10*1024-2, FmtStr, valist);
155 return PutCh(
' ', IndentLev*2);
160 for (
int LnN=0; LnN<Lns; LnN++){Cs+=
PutCh(
'\n');}
190 int CStrLen=int(strlen(CStr));
193 if (CStrLen>0){
Cs+=
PutBf(CStr, CStrLen);}
207 operator<<((
char)SIn.
GetCh());
219 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
220 LBfS+=(((
char*)LBf)[LBfC]=
GetCh());}
234 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
235 LBfS+=
PutCh(((
char*)LBf)[LBfC]);}
246 "Error seeking into file '"+
GetSNm()+
"'.");
250 const int FPos=(int)ftell(
FileId);
259 "Error seeking into file '"+
GetSNm()+
"'.");
267 "Error reading file '"+
GetSNm()+
"'.");
274 TSBase(FNm.CStr()),
TSIn(FNm), FileId(NULL), Bf(NULL), BfC(0), BfL(0){
277 EAssertR(FileId!=NULL,
"Can not open file '"+FNm+
"'.");
282 TSBase(FNm.CStr()),
TSIn(FNm), FileId(NULL), Bf(NULL), BfC(0), BfL(0){
285 OpenedP=(FileId!=NULL);
294 printf(
"*** Exception: %s\n", Except->GetMsgStr().CStr());
295 EFailR(Except->GetMsgStr());
302 return PSIn(
new TFIn(FNm, OpenedP));
308 if (
Bf!=NULL){
delete[]
Bf;}
314 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
316 LBfS+=((
char*)LBf)[LBfC]=
Bf[
BfC++];}
318 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
319 LBfS+=(((
char*)LBf)[LBfC]=
Bf[
BfC++]);}
355 }
while (Status == 0);
358 return !LnChA.
Empty();
375 if (CrEnd &&
Bf[
BfC]==
'\n') {
394 }
else if (
Bf[
BfC]==
'\n') {
413 "Error writting to the file '"+
GetSNm()+
"'.");
418 TSBase(FNm.CStr()),
TSOut(FNm), FileId(NULL), Bf(NULL), BfL(0){
419 if (FNm.
GetUc()==
"CON"){
430 TSBase(FNm.CStr()),
TSOut(FNm), FileId(NULL), Bf(NULL), BfL(0){
431 if (FNm.
GetUc()==
"CON"){
448 if (OpenedP){
return SOut;}
else {
return NULL;}
453 if (
Bf!=NULL){
delete[]
Bf;}
466 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
467 LBfS+=
PutCh(((
char*)LBf)[LBfC]);}
469 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
470 LBfS+=(
Bf[
BfL++]=((
char*)LBf)[LBfC]);}
501 const int FPos =
GetPos();
503 const int FLen =
GetPos();
510 for (
TSize i = 0; i < LBfL; i++) {
511 LBfS += ((
char *)LBf)[i];
513 IAssert(fwrite(LBf,
sizeof(
char), LBfL,
FileId) == (
size_t) LBfL);
518 IAssert(fread((
void *)LBf,
sizeof(
char), LBfL,
FileId) == (
size_t) LBfL);
520 for (
TSize i = 0; i < LBfL; i++) {
521 LBfS += ((
char *)LBf)[i];
539 TSIn(
"Input-Shared_Memory"), TotalLength(0),
548 FileId=fopen(FNm.
CStr(),
"rb");
550 EAssertR(FileId!=NULL,
"Can not open file '"+FNm+
"'.");
553 "Error seeking into file '"+
TStr(FNm)+
"'.");
554 FLen=(
uint64)ftell(FileId);
557 "Error seeking into file '"+
TStr(FNm)+
"'.");
559 Mapped = (
char *) mmap (0, FLen, PROT_READ, MAP_PRIVATE, fd, 0);
560 EAssertR(Mapped!=MAP_FAILED,
"mmap failed in TShMIn.");
567 TExcept::Throw(
"TMIn::TMIn(TStr, Bool): GLib_LINUX undefined.\n");
572 TSIn(
"Input-Shared_Memory"), TotalLength(_BfL), SizeLeft(_BfL), IsMemoryMapped(false) {
595 TSBase(
"Input-Memory"),
TSIn(
"Input-Memory"), Bf(NULL), BfC(0), BfL(_BfL), IsMemoryMapped(false){
599 Bf=
new char[
static_cast<size_t>(
BfL)]; memmove(
Bf, _Bf, static_cast<size_t>(
BfL));
604 TSBase(
"Input-Memory"),
TSIn(
"Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){
605 BfL=SIn.
Len();
Bf=
new char[
static_cast<size_t>(
BfL)];
610 TSBase(
"Input-Memory"),
TSIn(
"Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){
611 BfL=
uint64(strlen(CStr));
Bf=
new char[
static_cast<size_t>(
BfL+1)]; strcpy(
Bf, CStr);
616 TSBase(
"Input-Memory"),
TSIn(
"Input-Memory"), Bf(NULL), BfC(0), BfL(0){
617 if (FromFile ==
false) {
618 BfL=Str.
Len();
Bf=
new char[
static_cast<size_t>(
BfL)]; strncpy(
Bf, Str.
CStr(),
static_cast<size_t>(
BfL));
628 FileId=fopen(FNm.
CStr(),
"rb");
631 EAssertR(FileId!=NULL,
"Can not open file '"+FNm+
"'.");
635 "Error seeking into file '"+
TStr(FNm)+
"'.");
636 FLen=(
uint64)ftell(FileId);
639 "Error seeking into file '"+
TStr(FNm)+
"'.");
643 mapped = (
char *) mmap (0, FLen, PROT_READ, MAP_PRIVATE, fd, 0);
646 if (mapped == MAP_FAILED) {
647 printf(
"mmap failed: %d %s\n", fd, strerror (errno));
658 TExcept::Throw(
"TMIn::TMIn(TStr, Bool): GLib_LINUX undefined.\n");
664 TSBase(
"Input-Memory"),
TSIn(
"Input-Memory"), Bf(NULL), BfC(0), BfL(0), IsMemoryMapped(false){
665 BfL=ChA.
Len();
Bf=
new char[
static_cast<size_t>(
BfL)]; strncpy(
Bf, ChA.
CStr(),
static_cast<size_t>(
BfL));
669 return PSIn(
new TMIn(_Bf, _BfL, TakeBf));
681 return new TMIn(Str, FromFile);
714 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
715 LBfS+=(((
char*)LBf)[LBfC]=
Bf[
BfC++]);}
732 if (CrEnd &&
Bf[
BfC]==
'\n') {
751 }
else if (
Bf[
BfC]==
'\n') {
787 for (
uint64 i = Lb; i < Ub; i++) {
796 while (Ind > 0 &&
Bf[Ind-1] !=
'\n') {
803 while (Ind <
BfL &&
Bf[Ind] !=
'\n') {
806 if (Ind ==
BfL) Ind--;
816 while (BfC <
BfL &&
Bf[BfC] !=
'\n') {
829 if (ReqLen < 0)
Bf=
new char[
MxBfL=1024];
830 else Bf=
new char[MxBfL=ReqLen];
832 if (ReqLen < 0){
MxBfL*=2; }
833 else if (ReqLen <
MxBfL){
return; }
835 char* NewBf=
new char[
MxBfL];
836 memmove(NewBf,
Bf,
BfL);
delete[]
Bf;
Bf=NewBf;
842 Bf(NULL), BfL(0), MxBfL(0), OwnBf(true){
843 MxBfL=_MxBfL>0?_MxBfL:1024;
849 Bf(_Bf), BfL(0), MxBfL(_MxBfL), OwnBf(false){}
853 memcpy(
Bf +
BfL, LBf, LBfL);
860 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
861 LBfS+=
PutCh(((
char*)LBf)[LBfC]);}
863 for (
TSize LBfC=0; LBfC<LBfL; LBfC++){
864 LBfS+=(
Bf[
BfL++]=((
char*)LBf)[LBfC]);}
871 for (
int BfC=0; BfC<
BfL; BfC++){ChA+=
Bf[BfC];}
882 IAssert((CutBfL==-1)||((0<=CutBfL)));
890 if (IsCut){
CutBf(SInBfL);}
896 for (
int BfC=0; BfC<
BfL; BfC++){
904 for (
int BfC=0; BfC<
BfL; BfC++){
916 for (
int BfC=0; BfC<
BfL; BfC++){
924 int LnChs=0;
TChA Ln;
925 for (
int BfC=0; BfC<
BfL; BfC++){
928 LnChs++;
if (DoAddEoln){Ln+=Ch;}
930 char NextCh=
Bf[BfC+1];
933 LnChs++;
if (DoAddEoln){Ln+=NextCh;}
956 if (
SIn->
Eof()) {
return false; }
959 if (Ch !=
'\r') { LnChA += Ch; }
961 LnStr = LnChA;
return true;
978 "Error seeking into file '"+
TStr(
FNm)+
"'.");
982 const bool& CreateIfNo,
const int& _HdLen,
const int& _RecLen):
983 FileId(NULL), FNm(_FNm.CStr()),
984 RecAct(false), HdLen(_HdLen), RecLen(_RecLen){
994 if ((
FileId==NULL)&&(CreateIfNo)){
1010 "Error seeking into file '"+
TStr(
FNm)+
"'.");
1016 "Error seeking into file '"+
TStr(
FNm)+
"'.");
1020 int FPos= (int) ftell(
FileId);
1029 "Error seeking into file '"+
TStr(
FNm)+
"'.");
1055 fread(Bf, 1, BfL,
FileId)==BfL,
1056 "Error reading file '"+
TStr(
FNm)+
"'.");
1062 fwrite(Bf, 1, BfL,
FileId)==BfL,
1063 "Error writting to the file '"+
TStr(
FNm)+
"'.");
1072 char* CStr=
new char[Chs];
1073 for (
int ChN=0; ChN<Chs; ChN++){CStr[ChN]=Ch;}
1084 IsOk=
false;
TStr Str;
1086 char* CStr=
new char[StrLen+1];
1087 GetBf(CStr, StrLen+1);
1088 if (CStr[StrLen+1-1]==
TCh::NullCh){IsOk=
true; Str=CStr;}
1096 char* CStr=
new char[StrLen+1];
1097 GetBf(CStr, StrLen+1);
1106 char* Bf=
new char[BfL];
1107 SIn->
GetBf(Bf, BfL);
1114 char* Bf=
new char[BfL];
1134 if (UcStr==
"CREATE"){
return faCreate;}
1135 if (UcStr==
"UPDATE"){
return faUpdate;}
1136 if (UcStr==
"APPEND"){
return faAppend;}
1137 if (UcStr==
"READONLY"){
return faRdOnly;}
1138 if (UcStr==
"RESTORE"){
return faRestore;}
1140 if (UcStr==
"NEW"){
return faCreate;}
1141 if (UcStr==
"CONT"){
return faUpdate;}
1142 if (UcStr==
"CONTINUE"){
return faUpdate;}
1144 if (UcStr==
"RESTORE"){
return faRestore;}
1157 if (FNm.
Empty()) {
return false; }
1159 TFIn FIn(FNm, DoExists);
1163 #if defined(GLib_WIN32)
1166 const bool& ThrowExceptP,
const bool& FailIfExistsP){
1168 if (CopyFile(SrcFNm.
CStr(), DstFNm.
CStr(), FailIfExistsP) == 0) {
1169 int ErrorCode = (int)GetLastError();
1171 "Error %d copying file '%s' to '%s'.",
1172 ErrorCode, SrcFNm.
CStr(), DstFNm.
CStr()));
1175 CopyFile(SrcFNm.
CStr(), DstFNm.
CStr(), FailIfExistsP);
1179 #elif defined(GLib_LINUX)
1182 const bool& ThrowExceptP,
const bool& FailIfExistsP){
1185 void *source, *target;
1187 if( (input = open(SrcFNm.
CStr(), O_RDONLY)) == -1) {
1190 "Error copying file '%s' to '%s': cannot open source file for reading.",
1198 if( (output = open(DstFNm.
CStr(), O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1203 "Error copying file '%s' to '%s': cannot open destination file for writing.",
1211 filesize = lseek(input, 0,
SEEK_END);
1212 lseek(output, filesize - 1,
SEEK_SET);
1213 write(output,
'\0', 1);
1215 if((source = mmap(0, filesize, PROT_READ, MAP_SHARED, input, 0)) == (
void *) -1) {
1220 "Error copying file '%s' to '%s': cannot mmap input file.",
1227 if((target = mmap(0, filesize, PROT_WRITE, MAP_SHARED, output, 0)) == (
void *) -1) {
1228 munmap(source, filesize);
1233 "Error copying file '%s' to '%s': cannot mmap output file.",
1240 memcpy(target, source, filesize);
1242 munmap(source, filesize);
1243 munmap(target, filesize);
1257 remove(FNm.
CStr())==0,
1258 "Error removing file '"+FNm+
"'.");
1268 while (FFile.
Next(FNm)){
1271 for (
int FNmN=0; FNmN<FNmV.
Len(); FNmN++){
1272 Del(FNmV[FNmN],
false);}
1277 rename(SrcFNm.
CStr(), DstFNm.
CStr())==0,
1278 "Error renaming file '"+SrcFNm+
"' to "+DstFNm+
"'.");
1286 for (ch = FNm.
Len()-1; ch >= 0; ch--)
if (FNm[ch] ==
'.')
break;
1287 if (ch != -1) TmpFNm.
InsStr(ch,
".#");
1288 else TmpFNm +=
".#";
1302 HANDLE hFile = CreateFile(
1305 FILE_SHARE_READ | FILE_SHARE_WRITE,
1308 FILE_ATTRIBUTE_NORMAL,
1311 if (hFile == INVALID_HANDLE_VALUE) {
1314 LARGE_INTEGER lpFileSizeHigh;
1315 if (!GetFileSizeEx(hFile, &lpFileSizeHigh)) {
1320 return uint64(lpFileSizeHigh.QuadPart);
1325 HANDLE hFile = CreateFile(
1328 FILE_SHARE_READ | FILE_SHARE_WRITE,
1331 FILE_ATTRIBUTE_NORMAL,
1334 if (hFile == INVALID_HANDLE_VALUE) {
1337 FILETIME lpCreationTime;
1338 if (!GetFileTime(hFile, &lpCreationTime, NULL, NULL)) {
1343 TUInt64 UInt64(
uint(lpCreationTime.dwHighDateTime),
1344 uint(lpCreationTime.dwLowDateTime));
1345 return UInt64.Val /
uint64(10000);
1350 HANDLE hFile = CreateFile(
1353 FILE_SHARE_READ | FILE_SHARE_WRITE,
1356 FILE_ATTRIBUTE_NORMAL,
1359 if (hFile == INVALID_HANDLE_VALUE) {
1362 FILETIME lpLastAccessTime;
1363 if (!GetFileTime(hFile, NULL, &lpLastAccessTime, NULL)) {
1368 TUInt64 UInt64(
uint(lpLastAccessTime.dwHighDateTime),
1369 uint(lpLastAccessTime.dwLowDateTime));
1370 return UInt64.Val /
uint64(10000);
1375 HANDLE hFile = CreateFile(
1378 FILE_SHARE_READ | FILE_SHARE_WRITE,
1381 FILE_ATTRIBUTE_NORMAL,
1384 if (hFile == INVALID_HANDLE_VALUE) {
1387 FILETIME lpLastWriteTime;
1388 if (!GetFileTime(hFile, NULL, NULL, &lpLastWriteTime)) {
1393 TUInt64 UInt64(
uint(lpLastWriteTime.dwHighDateTime),
1394 uint(lpLastWriteTime.dwLowDateTime));
1395 return UInt64.Val /
uint64(10000);
1398 #elif defined(GLib_LINUX)
1410 if (stat(FNm.
CStr(), &st) != 0) {
1413 return uint64(st.st_mtime);
int PutMem(const TMem &Mem)
int SearchCh(const char &Ch, const int &BChN=0) const
TStr GetStr(const int &StrLen)
int GetBf(const void *LBf, const TSize &LBfL)
virtual int Len() const =0
static PSOut New(const TStr &FNm, const bool &Append=false)
int PutStrFmtLn(const char *FmtStr,...)
int GetBf(const void *LBf, const TSize &LBfL)
virtual int PutCh(const char &Ch)=0
virtual int PutBf(const void *LBf, const TSize &LBfL)=0
static void Rename(const TStr &SrcFNm, const TStr &DstFNm)
static bool Exists(const TStr &FNm)
static const TStr GifFExt
int PutCh(const char &Ch)
void AppendBf(const void *LBf, const TSize &LBfL)
PSIn GetSIn(const bool &IsCut=true, const int &CutBfL=-1)
int PutBf(const void *LBf, const TSize &LBfL)
int FindEol(int &BfN, bool &CrEnd)
static bool IsHashCh(const char &Ch)
int PutCh(const char &Ch)
void AddCh(const char &Ch, const int &MxLen=-1)
TSizeTy Len() const
Returns the number of elements in the vector.
void CutBf(const int &CutBfL)
void PutSIn(const PSIn &SIn, TCs &Cs)
int PutBf(const void *LBf, const TSize &LBfL)
virtual int GetBf(const void *Bf, const TSize &BfL)=0
static const TStr HtmlFExt
int PutBf(const void *LBf, const TSize &LBfL)
bool GetNextLnBf(TChA &LnChA)
int PutSepLn(const int &Lns=0)
int PutLn(const int &Lns=1)
bool GetNextLnBf(TChA &LnChA)
int UpdateLnLen(const int &StrLen, const bool &ForceInLn=false)
static uint64 GetSize(const TStr &FNm)
static PSIn New(const TStr &FNm)
void SkipCommentLines()
Move stream pointer along until a non commented line is found.
int GetBf(const void *LBf, const TSize &LBfL)
static const TStr TxtFExt
static TCs GetCsFromBf(char *Bf, const int &BfL)
static void DelWc(const TStr &WcStr, const bool &RecurseDirP=false)
static int GetMn(const int &Int1, const int &Int2)
static void Throw(const TStr &MsgStr)
static TStr GetUniqueFNm(const TStr &FNm)
bool GetNextLnBf(TChA &LnChA)
void MoveFPos(const int &DFPos)
static const TStr HtmFExt
unsigned long long uint64
int PutBool(const bool &Bool)
static void Copy(const TStr &SrcFNm, const TStr &DstFNm, const bool &ThrowExceptP=true, const bool &FailIfExistsP=false)
static void Del(const TStr &FNm, const bool &ThrowExceptP=true)
uint64 CountNewLinesInRange(uint64 Lb, uint64 Ub)
Finds number of new line chars in interval [Lb, Ub)
int PutBf(const void *LBf, const TSize &LBfL)
int PutSep(const int &NextStrLen=0)
int PutInt(const int &Int)
void SetFPos(const int &FPos)
void PutCh(const char &Ch)
static const TPt< TSOut > StdOut
int FindEol(uint64 &BfN, bool &CrEnd)
int PutIndent(const int &IndentLev=1)
static PSIn New(const void *_Bf, const uint64 &_BfL, const bool &TakeBf=false)
int PutFlt(const double &Flt)
void Save(const bool &Bool)
void PutStr(const TStr &Str)
static PSInOut New(const TStr &FNm, const TFAccess &FAccess, const bool &CreateIfNo)
uint64 GetLineEndPos(uint64 Ind)
Finds end of line in which Ind is present.
uint64 GetLineStartPos(uint64 Ind)
Finds beginning of line in which Ind is present.
int PutDosLn(const int &Lns=1)
char * GetLine(uint64 Ind)
static const TPt< TSIn > StdIn
void AddBf(char *NewBf, const int &BfS)
static TStr GetStrFromFAccess(const TFAccess &FAccess)
int GetBf(const void *LBf, const TSize &LBfL)
static TFAccess GetFAccessFromStr(const TStr &Str)
static TStr Fmt(const char *FmtStr,...)
int PutStr(const char *CStr)
#define EAssertR(Cond, MsgStr)
PSIn GetSIn(const int &SInLen, TCs &Cs)
static TStr GetStr(const bool &Val)
virtual TStr GetSNm() const
int PutCh(const char &Ch)
static uint64 GetLastWriteTm(const TStr &FNm)
static uint64 GetCreateTm(const TStr &FNm)
void Resize(const int &ReqLen=-1)
void SetRecN(const int &RecN)
void PutBf(const void *Bf, const TSize &BfL)
void CloseMapping()
munmap the mapping. Note that munmap is not called by the destructor
int PutStrLn(const TStr &Str, const bool &ForceInLn=false)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
int PutStrFmt(const char *FmtStr,...)
void GetBf(void *Bf, const TSize &BfL)
void InsStr(const int &BChN, const TStr &Str)
TSOut & operator<<(const bool &Bool)
int PutUInt(const uint &Int)
bool GetNextLnBf(TChA &LnChA)
TStr GetEolnLn(const bool &DoAddEoln, const bool &DoCutBf)
void SetFPos(const int &FPos) const
static const TStr JarFExt
int ChangeStr(const TStr &SrcStr, const TStr &DstStr, const int &BChN=0)
bool GetNextLn(TStr &LnStr)
static uint64 GetLastAccessTm(const TStr &FNm)