00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef __MMDB_IO_File__
00040 #define __MMDB_IO_File__
00041
00042 #include <stdio.h>
00043
00044 #include "mmdb_mattype.h"
00045
00046 namespace mmdb {
00047
00048 namespace io {
00049
00050
00051
00052 enum GZ_MODE {
00053 GZM_NONE = 0,
00054 GZM_CHECK = 1,
00055 GZM_ENFORCE = 2,
00056 GZM_ENFORCE_GZIP = 2,
00057 GZM_ENFORCE_COMPRESS = 3
00058 };
00059
00060 enum FILE_ERROR {
00061 FileError_NoMemory = 110,
00062 FileError_ShortData = 111,
00063 FileError_NoDataFound = 112,
00064 FileError_NoColumn = 113,
00065 FileError_BadData = 114,
00066 FileError_WrongMemoryAllocation = 115
00067 };
00068
00069 enum SYSKEY {
00070 syskey_unix = 1,
00071 syskey_win = 2,
00072 syskey_all = 3
00073 };
00074
00075 extern const char _dir_sep_c;
00076 extern cpstr _dir_sep;
00077
00078
00079
00080
00081 extern cpstr GetFPath ( pstr FilePath, SYSKEY syskey=syskey_unix );
00082 extern cpstr GetFName ( cpstr FilePath, SYSKEY syskey=syskey_unix );
00083 extern cpstr GetFExt ( cpstr FilePath );
00084 extern cpstr ChangeExt ( pstr FilePath, cpstr newExt,
00085 SYSKEY syskey=syskey_unix );
00086 extern cpstr FileError ( int ErrCode );
00087 extern void RemoveDelimiters ( pstr S, int SLen );
00088 extern void PickOutNumber ( cpstr S, pstr SV, int SLen, int & j );
00089
00090
00091
00092
00093 DefineClass(File);
00094
00095 class File {
00096
00097 public :
00098
00099 File ( word BufSize=4096 );
00100 virtual ~File();
00101
00102
00103
00104
00105 void assign ( cpstr FileName,
00106 bool Text=false,
00107 bool UniB=false,
00108 GZ_MODE gzMode=GZM_NONE );
00109
00110 void assign ( word poolSize, word sizeInc, pstr filePool );
00111 void GetFilePool ( pstr & filePool, word & fileSize );
00112
00113 inline cpstr FileName() { return FName; }
00114 bool reset ( bool ReadOnly=false, int retry=0 );
00115
00116 bool erase ();
00117 bool exists ();
00118 bool parse ( cpstr FileName );
00119 bool rename ( cpstr NewFileName );
00120 bool rewrite ();
00121 bool append ();
00122 bool isOpen ();
00123 long Position ();
00124 inline long FileLength () { return FLength; }
00125 bool seek ( long Position );
00126 bool FileEnd ();
00127 inline bool Success () { return IOSuccess; }
00128 inline void SetSuccess() { IOSuccess = true; }
00129 void shut ();
00130
00131
00132 word ReadFile ( void * Buffer, word Count );
00133 word CreateRead ( pstr & Line );
00134 word ReadTerLine ( pstr Line, bool longLine=false );
00135 bool WriteFile ( const void * Buffer, word Count );
00136 bool CreateWrite ( cpstr Line );
00137 bool WriteTerLine ( cpstr Line, bool longLine=false );
00138
00139
00140 bool WriteReal ( realtype * V );
00141 bool WriteFloat ( realtype * V );
00142 bool WriteInt ( int * I );
00143 bool WriteShort ( short * S );
00144 bool WriteLong ( long * L );
00145 bool WriteBool ( bool * B );
00146 bool WriteByte ( byte * B );
00147 bool WriteWord ( word * W );
00148 bool ReadReal ( realtype * V );
00149 bool ReadFloat ( realtype * V );
00150 bool ReadInt ( int * I );
00151 bool ReadShort ( short * S );
00152 bool ReadLong ( long * L );
00153 bool ReadBool ( bool * B );
00154 bool ReadByte ( byte * B );
00155 bool ReadWord ( word * B );
00156 bool AddReal ( realtype * V );
00157 bool AddFloat ( realtype * V );
00158 bool AddInt ( int * I );
00159 bool AddShort ( short * S );
00160 bool AddLong ( long * L );
00161 bool AddByte ( byte * B );
00162 bool AddWord ( word * B );
00163
00164
00165 bool WriteVector ( rvector V, int len, int Shift );
00166 bool WriteVector ( ivector iV, int len, int Shift );
00167 bool WriteVector ( lvector lV, int len, int Shift );
00168 bool WriteVector ( bvector B, int len, int Shift );
00169 bool ReadVector ( rvector V, int maxlen, int Shift );
00170 bool ReadVector ( ivector iV, int maxlen, int Shift );
00171 bool ReadVector ( lvector lV, int maxlen, int Shift );
00172 bool ReadVector ( bvector B, int maxlen, int Shift );
00173 bool CreateReadVector ( rvector & V, int & len, int Shift );
00174 bool CreateReadVector ( ivector & iV, int & len, int Shift );
00175 bool CreateReadVector ( lvector & lV, int & len, int Shift );
00176 bool CreateReadVector ( bvector & B, int & len, int Shift );
00177 bool CreateReadVector ( rvector & V, int Shift );
00178 bool CreateReadVector ( ivector & iV, int Shift );
00179 bool CreateReadVector ( lvector & lV, int Shift );
00180 bool CreateReadVector ( bvector & B, int Shift );
00181 bool WriteMatrix ( rmatrix & A, int N, int M,
00182 int ShiftN, int ShiftM );
00183 bool CreateReadMatrix ( rmatrix & A, int ShiftN, int ShiftM );
00184 bool CreateReadMatrix ( rmatrix & A, int & N, int & M,
00185 int ShiftN, int ShiftM );
00186
00188 bool Write ( cpstr Line );
00189 bool Write ( realtype V, int length=10 );
00190 bool Write ( int iV, int length=5 );
00191 bool WriteLine ( cpstr Line );
00192 bool LF ();
00193 word ReadLine ( pstr Line, word MaxLen=255 );
00194 word ReadNonBlankLine ( pstr S, word MaxLen=255 );
00195
00197
00198
00199 bool WriteDataLine ( realtype X, realtype Y, int length=10 );
00200
00201 bool WriteParameter ( cpstr S, realtype X,
00202 int ParColumn=40,
00203 int length=10 );
00204
00205
00206 bool WriteParameters ( cpstr S, int n_X,
00207 rvector X,
00208 int ParColumn=40,
00209 int length=10 );
00210
00211 bool ReadParameter ( pstr S, realtype & X,
00212 int ParColumn=40 );
00213 bool ReadParameter ( pstr S, int & X,
00214 int ParColumn=40 );
00215
00216 bool ReadParameters ( pstr S, int & n_X,
00217 rvector X,
00218 int MaxLen=255,
00219 int ParColumn=40 );
00220
00221
00222
00223
00224
00225
00226
00227 bool WriteColumns ( rvector X, rvector Y, rvector Z,
00228 int len, int Shift, int MLength );
00229 bool WriteColumns ( rvector X, rvector Y,
00230 int len, int Shift, int MLength );
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 int ReadColumns ( int maxlen, rvector X, rvector Y, rvector Z,
00241 int xCol, int yCol, int zCol, int Shift );
00242 int ReadColumns ( int maxlen, rvector X, rvector Y,
00243 int xCol, int yCol, int Shift );
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 int CreateReadColumns ( rvector & X, rvector & Y, rvector & Z,
00256 int xCol, int yCol, int zCol, int Shift );
00257 int CreateReadColumns ( rvector & X, rvector & Y,
00258 int xCol, int yCol, int Shift );
00259
00260
00261 realtype GetNumber ( cpstr S );
00262 FILE * GetHandle () { return hFile; }
00263
00264 protected :
00265 word Buf_Size;
00266 bool TextMode,UniBin;
00267 GZ_MODE gzipMode;
00268 pstr IOBuf;
00269 word BufCnt,BufLen,BufInc;
00270 FILE * hFile;
00271 bool EofFile;
00272 pstr FName;
00273 long FLength;
00274 bool IOSuccess;
00275 int ErrCode;
00276
00277 void FreeBuffer ();
00278 void _ReadColumns ( int & DLen, pstr S, int SLen,
00279 rvector X, rvector Y, rvector Z,
00280 int xCol, int yCol, int zCol, int Shift );
00281
00282 private :
00283 int gzipIO;
00284 bool StdIO,memIO;
00285
00286 };
00287
00288
00289 extern void SetGZIPPath ( pstr gzipPath, pstr ungzipPath );
00290 extern void SetCompressPath ( pstr compressPath, pstr uncompressPath );
00291
00292 extern bool FileExists ( cpstr FileName, PFile f=NULL );
00293
00294 }
00295
00296 }
00297
00298
00299 #endif
00300