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 #ifndef OUTPUT_HPP
00035 #define OUTPUT_HPP
00036
00037 #include <string.h>
00038 #include <stdlib.h>
00039
00040 #ifndef WIN32
00041 #include <unistd.h>
00042 #else
00043 #include <stdio.h>
00044 #include <fcntl.h>
00045 #include <io.h>
00046 #endif
00047
00048 #include "Types.hpp"
00049 #include "Error.hpp"
00050
00051 extern char usedosnewline;
00052
00053
00054
00055
00056 #ifdef SET_OUTPUT_STATIC
00057 #define OUTPUT_STATIC static
00058 #else
00059 #define OUTPUT_STATIC
00060 #endif
00061
00062 class Output
00063 {
00064 OUTPUT_STATIC FILE *output;
00065 OUTPUT_STATIC char *buf;
00066 OUTPUT_STATIC char *savefilename;
00067 OUTPUT_STATIC int bufsize,curpos;
00068 OUTPUT_STATIC int overallsize;
00069
00070 public:
00071 char OUTPUT_STATIC CreateFile(char *filename,int mybufsize=65536)
00072
00073
00074 {
00075 if(filename==NULL)
00076 {
00077 savefilename=NULL;
00078 output=stdout;
00079
00080
00081 buf=(char *)malloc(mybufsize);
00082 if(buf==NULL)
00083 ExitNoMem();
00084
00085 #ifdef WIN32
00086 _setmode(_fileno(stdout),_O_BINARY);
00087 #endif
00088 }
00089 else
00090 {
00091
00092 buf=(char *)malloc(mybufsize+strlen(filename)+1);
00093 if(buf==NULL)
00094 ExitNoMem();
00095
00096 savefilename=buf+mybufsize;
00097 strcpy(savefilename,filename);
00098
00099
00100
00101
00102 if(strlen(filename)!=0)
00103 {
00104 output=fopen(filename,"wb");
00105 if(output==NULL)
00106 return 0;
00107 }
00108 else
00109 output=NULL;
00110 }
00111 bufsize=mybufsize;
00112 curpos=0;
00113 overallsize=0;
00114 return 1;
00115 }
00116
00117 void OUTPUT_STATIC CloseFile()
00118
00119 {
00120 Flush();
00121 if(savefilename!=NULL)
00122 {
00123 if(output!=NULL)
00124 fclose(output);
00125 }
00126 free(buf);
00127 }
00128
00129 void OUTPUT_STATIC CloseAndDeleteFile()
00130
00131 {
00132 Flush();
00133 if(savefilename!=NULL)
00134 {
00135 if(output!=NULL)
00136 fclose(output);
00137 unlink(savefilename);
00138 }
00139 free(buf);
00140 return;
00141 }
00142
00143 void OUTPUT_STATIC Flush()
00144
00145 {
00146 overallsize+=curpos;
00147
00148 if(output==NULL)
00149 {
00150 curpos=0;
00151 return;
00152 }
00153
00154 char *ptr=buf;
00155 int byteswritten;
00156
00157
00158
00159
00160
00161 while(curpos>0)
00162 {
00163 byteswritten=fwrite(ptr,1,(curpos<30000) ? curpos : 30000,output);
00164 if(byteswritten==0)
00165 {
00166 Error("Could not write output file!");
00167 Exit();
00168 }
00169 curpos-=byteswritten;
00170 ptr+=byteswritten;
00171 }
00172 }
00173
00174 char OUTPUT_STATIC *GetBufPtr(int *len)
00175
00176 {
00177 *len=bufsize-curpos;
00178 return buf+curpos;
00179 }
00180
00181 void OUTPUT_STATIC SaveBytes(int bytecount)
00182
00183
00184 {
00185 curpos+=bytecount;
00186 }
00187
00188 int OUTPUT_STATIC GetCurFileSize() { return overallsize+curpos; }
00189
00190
00191
00192
00193 void OUTPUT_STATIC StoreData(char *ptr,int len)
00194
00195 {
00196 while(bufsize-curpos<len)
00197 {
00198 mymemcpy(buf+curpos,ptr,bufsize-curpos);
00199 len-=bufsize-curpos;
00200 ptr+=bufsize-curpos;
00201 curpos=bufsize;
00202 Flush();
00203 }
00204 mymemcpy(buf+curpos,ptr,len);
00205 curpos+=len;
00206 }
00207
00208 void OUTPUT_STATIC StoreInt32(int val)
00209
00210 {
00211 if(bufsize-curpos<4)
00212 Flush();
00213
00214 *(int *)(buf+curpos)=val;
00215 curpos+=4;
00216 }
00217
00218 void OUTPUT_STATIC StoreChar(char c)
00219
00220 {
00221 if(bufsize-curpos<1)
00222 Flush();
00223
00224 buf[curpos]=c;
00225 curpos++;
00226 }
00227
00228 void OUTPUT_STATIC StoreNewline()
00229
00230 {
00231 if(usedosnewline)
00232 {
00233 if(bufsize-curpos<2)
00234 Flush();
00235
00236 buf[curpos++]='\r';
00237 }
00238 else
00239 {
00240 if(bufsize-curpos<1)
00241 Flush();
00242 }
00243 buf[curpos++]='\n';
00244 }
00245
00246 char OUTPUT_STATIC *GetDataPtr(int len)
00247
00248
00249 {
00250 if(bufsize-curpos<len)
00251 {
00252 Flush();
00253 if(bufsize-curpos<len)
00254 {
00255 Error("Fatal Error !");
00256 Exit();
00257 }
00258 }
00259 curpos+=len;
00260 return buf+curpos-len;
00261 }
00262
00263
00264
00265 };
00266
00267 inline char FileExists(char *filename)
00268 {
00269 #ifdef WIN32
00270 _finddata_t fileinfo;
00271 long handle=_findfirst(filename,&fileinfo);
00272 if(handle==-1)
00273 return 0;
00274 _findclose(handle);
00275 return 1;
00276 #else
00277 FILE *file=fopen(filename,"r");
00278 if(file==NULL)
00279 return 0;
00280 fclose(file);
00281 return 1;
00282 #endif
00283 }
00284
00285 inline void RemoveFile(char *filename)
00286 {
00287 unlink(filename);
00288 }
00289 #endif