00001 #ifndef gFILE_X_H
00002 #define gFILE_X_H
00003
00004 #include <stdio.h>
00005
00006
00007 #ifdef gDOS_SPEC
00008 ;
00009 #else
00010 #include <unistd.h>
00011 #include <sys/types.h>
00012 #endif //gDOS_SPEC (~)
00013
00014
00015 #include "gstring.h"
00016 #include "gcontrol.h"
00017
00018 #define DEF_FIL_BUFSIZE (4*4096)
00019
00020 #define FL_FILE_TO_READ 2
00021 #define FL_FILE_TO_WRITE 4
00022
00023 #define FILENM_STRICT_LVL1 1 // Highly strict chrs on file names
00024 #define FILENM_STRICT_LVL2 2
00025 #define FILENM_STRICT_LVL3 3
00026 #define FILENM_STRICT_LVL4 4 // Regular level for chrs...
00027 #define FILENM_STRICT_LVL5 5 // may have blanks
00028 #define FILENM_STRICT_LVL6 6
00029 #define FILENM_STRICT_LVL7 7
00030 #define FILENM_STRICT_LVL8 8 // Paths can have '\' or '/'
00031 #define FILENM_STRICT_LVL9 9
00032
00033 #define GX_SIGHUP 1
00034 #define GX_SIGINT 2
00035 #define GX_SIGQUIT 3
00036 #define GX_SIGTRAP 5
00037 #define GX_SIGKILL 9
00038 #define GX_SIGUSR1 10
00039 #define GX_SIGSEGV 11
00040 #define GX_SIGUSR2 12
00041 #define GX_SIGPIPE 13
00042 #define GX_SIGALRM 14
00043 #define GX_SIGTERM 15
00044 #define GX_SIGCHLD 17
00045
00046
00047 struct sSignalTrap {
00048 t_int16 sigId;
00049 t_int16 handled;
00050 char* shortName;
00051 };
00052
00053
00054 extern sSignalTrap global_signal_traps[];
00055
00056
00057 extern int gglobal_EventTerminate (int v) ;
00058
00059
00060
00061
00062 class gList;
00063
00064
00065 class gFileOut : public gControl {
00066 public:
00067 gFileOut (FILE* aFile=nil, bool isModeDOS=false) ;
00068 virtual ~gFileOut () ;
00069
00070 FILE* f;
00071
00072
00073 virtual char* NewLine () {
00074 return sNL.Str();
00075 }
00076
00077 virtual int MinimumStorageSize () {
00078 return 0;
00079 }
00080 virtual int MaximumStorageSize () {
00081 return -1;
00082 }
00083
00084
00085 void SetModeDOS (bool isDOS) ;
00086 virtual void SetNL (gString& aNL) {
00087 sNL = aNL;
00088 }
00089
00090 protected:
00091 gString sNL;
00092
00093 private:
00094
00095 gFileOut (gFileOut& ) ;
00096 gFileOut& operator= (gFileOut& ) ;
00097 };
00098
00099 class gFileControl : public gControl {
00100 public:
00101 ~gFileControl () ;
00102
00103 static gFileControl& Self () {
00104 return myself;
00105 }
00106
00107
00108 int lastOpError;
00109 short isCharOnFile[256];
00110 short charOnFileRelax;
00111 char tmpPath[300];
00112 char tmpPrefix[10];
00113 char tmpSuffix[10];
00114 t_uint16 userId;
00115 FILE* fReport;
00116 int exitCodeOnSignal;
00117 gList* tempL;
00118
00119
00120 bool IsOkChar (t_uchar c) {
00121 return isCharOnFile[c]!=0;
00122 }
00123 bool IsOkStrict (t_uchar c, bool isStrict) ;
00124
00125 bool IsOkPathname (char* s) ;
00126
00127 int CtrlGetTempPath (char* resPathStr) ;
00128
00129 t_uint32 CtrlGetPid () ;
00130 gString& CtrlGetUserName () {
00131 return sUName;
00132 }
00133 t_uint32 GetCurrentEpoch () ;
00134 gString& GetUniqueName (char* s) ;
00135
00136 char* ErrorStr (int aErrorNo) ;
00137
00138 bool IsTerminated () {
00139 return isTerminated;
00140 }
00141
00142
00143 void InitCharOnFile (t_uchar* s) ;
00144 bool AddTempFile (gString& s) ;
00145
00146
00147 int Init () ;
00148 int End (bool doReportObjs=true) ;
00149 int RemoveTemp (bool doReport=true) ;
00150 int RemoveTemp (unsigned& nTemp, bool doReport) ;
00151 int Release (bool doReport) ;
00152
00153 t_int16 IsSignaled (t_int16 iSignalId) ;
00154 t_int16 SetSignaled (t_int16 iSignalId, t_int16 handled) ;
00155 t_int16 SetSignaledByStr (char* shortStr, t_int16 handled) ;
00156
00157 void SignalHandler (int signalId) ;
00158 int OnEventTerminate (int (*DoSomething)(int)) ;
00159
00160
00161 bool Write (int fHandle, t_uchar* uBuf, unsigned nBytes) ;
00162
00163
00164 void GetCwd (gString& s) ;
00165 int NanoSleep (t_uint32 uSec, t_uint32 nSec) ;
00166 int NanoSleep (t_uint32 nSec) {
00167 return NanoSleep( 0, nSec );
00168 }
00169 int SecSleep (t_uint32 aSec) ;
00170 int MiliSecSleep (t_uint32 aSec) ;
00171
00172 protected:
00173 gFileControl () ;
00174
00175 gString sName;
00176 gString sUName;
00177
00178 int thisInit () ;
00179 int thisInitCharOnFile (bool doDefault) ;
00180 int thisGetUniqueName (char* s,
00181 t_uint32& aStamp,
00182 t_uint32& aRand,
00183 gString& sRes) ;
00184
00185 private:
00186 static gFileControl myself;
00187 static bool isTerminated;
00188 bool isInit;
00189 gString sErrorRef;
00190
00191
00192 gFileControl (gFileControl& ) ;
00193 gFileControl& operator= (gFileControl& ) ;
00194 };
00195
00196 class gFile {
00197 public:
00198
00199 enum eFileKind {
00200 e_Text,
00201 e_Binary
00202 };
00203
00204 enum eDeviceKind {
00205 e_fDevOther,
00206 e_fStdin,
00207 e_fStdout,
00208 e_fStderr
00209 };
00210
00211 gFile (eFileKind aFKind, char* fName, bool doOpenToRead, bool isTmpFile=false) ;
00212 virtual ~gFile () ;
00213
00214
00215 int lastOpError;
00216
00217
00218 virtual bool IsOk () {
00219 return lastOpError==0 && IsOpened();
00220 }
00221
00222 eFileKind FileKind () {
00223 return fKind;
00224 }
00225
00226 eDeviceKind Device () {
00227 return dKind;
00228 }
00229
00230 virtual bool IsDevice () {
00231 return dKind!=e_fDevOther;
00232 }
00233
00234 virtual bool IsOpened () {
00235 return f!=NULL;
00236 }
00237 FILE* Stream () {
00238 return f;
00239 }
00240 t_uint16 Mode () {
00241 return fMode;
00242 }
00243 char* LastErrorStr () ;
00244 char* ErrorStr (int aErrorNo) ;
00245
00246
00247 virtual bool OpenDevice (eDeviceKind aDKind) ;
00248 virtual bool OpenToRead (char* fName) ;
00249 virtual bool Overwrite (char* fName) ;
00250 virtual bool Close () ;
00251
00252 bool ReadData (void* buf, t_uint16 bufSize) ;
00253 virtual bool ReadBuffer (void* buf, t_uint16 bufSize, t_uint16& nBytes) ;
00254 virtual bool Read (void* buf, t_uint16 bufSize, t_uint16& nBytes) ;
00255
00256 protected:
00257 FILE* f;
00258 eFileKind fKind;
00259 eDeviceKind dKind;
00260 t_uint16 fMode;
00261 char lastErrorMsg[1024];
00262
00263 bool thisRead (int fd, void* buf, t_uint16 bufSize, t_uint16& nBytes) ;
00264
00265 private:
00266
00267 gFile (gFile& ) ;
00268 gFile& operator= (gFile& ) ;
00269 };
00270
00271 class gFileStream : public gFile {
00272 public:
00273 gFileStream (char* fName, bool doOpenToRead=true) ;
00274 gFileStream (gFile::eFileKind aFKind, char* fName, bool doOpenToRead=true, bool isTmpFile=false) ;
00275 virtual ~gFileStream () ;
00276
00277
00278 virtual bool IsBufferOk () {
00279 return isBufferOk;
00280 }
00281 virtual t_uint16 BufferSize () {
00282 return bufferSize;
00283 }
00284 virtual t_uint32 SeekPos () {
00285 return (t_uint32)seekPos;
00286 }
00287 virtual t_uint32 Size () {
00288 return (t_uint32)seekEnd;
00289 }
00290 virtual char* Buffer () ;
00291 virtual t_uchar* UBuffer () ;
00292
00293
00294 virtual bool Overwrite (char* fName) ;
00295
00296 virtual bool ReadBuffer (void* buf, t_uint16 bufSize, t_uint16& nBytes) ;
00297 virtual bool Read (void* buf, t_uint16 bufSize, t_uint16& nBytes) ;
00298
00299 virtual bool Rewind () ;
00300
00301 protected:
00302 bool isOpOk, isFileChanged;
00303 bool isBufferOk;
00304 off_t seekPos, seekEnd;
00305 t_uint16 bufferSize;
00306 t_uchar* buffer;
00307
00308 int thisAllocateBuffer (t_uint16 aBufferSize) ;
00309
00310 private:
00311 int thisGetStreamSize (int fd) ;
00312
00313
00314 gFileStream (gFileStream& ) ;
00315 gFileStream& operator= (gFileStream& ) ;
00316 };
00317
00318 class gFileText : public gFileStream {
00319 public:
00320 gFileText (char* fName, bool doOpenToRead=true) ;
00321 virtual ~gFileText () ;
00322
00323
00324 bool ReadLine () ;
00325 bool ReadLine (bool& isOk) ;
00326 bool ReadLine (bool& isOk, bool& hasNewLine) ;
00327
00328 protected:
00329 bool isOpOk, isFileChanged;
00330 off_t seekPos, seekEnd;
00331
00332 private:
00333
00334 gFileText (gFileText& ) ;
00335 gFileText& operator= (gFileText& ) ;
00336 };
00337
00338 class gFileTemp : public gFileStream {
00339 public:
00340
00341 enum eTempMethod {
00342 e_NameStd,
00343 e_NamePre
00344 };
00345
00346 gFileTemp (char* strTemplate, gFile::eFileKind aFKind=gFile::e_Binary) ;
00347 gFileTemp (gFile::eFileKind aFKind) ;
00348 virtual ~gFileTemp () ;
00349
00350
00351 int fHandle;
00352 gString sTempName;
00353
00354
00355 virtual bool IsOpened () ;
00356
00357
00358 virtual bool Rewind () ;
00359
00360 protected:
00361 eTempMethod tempMethod;
00362 bool isHandledFStream;
00363
00364 int thisOverwrite (char* fName) ;
00365
00366 private:
00367
00368 gFileTemp (gFileTemp& ) ;
00369 gFileTemp& operator= (gFileTemp& ) ;
00370 };
00371
00372 #endif //gFILE_X_H
00373