Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Output.hpp

Go to the documentation of this file.
00001 /*
00002 This product contains certain software code or other information
00003 ("AT&T Software") proprietary to AT&T Corp. ("AT&T").  The AT&T
00004 Software is provided to you "AS IS".  YOU ASSUME TOTAL RESPONSIBILITY
00005 AND RISK FOR USE OF THE AT&T SOFTWARE.  AT&T DOES NOT MAKE, AND
00006 EXPRESSLY DISCLAIMS, ANY EXPRESS OR IMPLIED WARRANTIES OF ANY KIND
00007 WHATSOEVER, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00008 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WARRANTIES OF
00009 TITLE OR NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS, ANY
00010 WARRANTIES ARISING BY USAGE OF TRADE, COURSE OF DEALING OR COURSE OF
00011 PERFORMANCE, OR ANY WARRANTY THAT THE AT&T SOFTWARE IS "ERROR FREE" OR
00012 WILL MEET YOUR REQUIREMENTS.
00013 
00014 Unless you accept a license to use the AT&T Software, you shall not
00015 reverse compile, disassemble or otherwise reverse engineer this
00016 product to ascertain the source code for any AT&T Software.
00017 
00018 (c) AT&T Corp. All rights reserved.  AT&T is a registered trademark of AT&T Corp.
00019 
00020 ***********************************************************************
00021 
00022 History:
00023 
00024       24/11/99  - initial release by Hartmut Liefke, liefke@seas.upenn.edu
00025                                      Dan Suciu,      suciu@research.att.com
00026 */
00027 
00028 //**************************************************************************
00029 //**************************************************************************
00030 
00031 // This module contains the output file management
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 // For now, we do not have a static implementation
00054 //#define SET_OUTPUT_STATIC
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;        // The output file handler
00065    OUTPUT_STATIC char  *buf;           // the output buffer
00066    OUTPUT_STATIC char  *savefilename;  // the name of the output file
00067    OUTPUT_STATIC int   bufsize,curpos; // buffer size and current position
00068    OUTPUT_STATIC int   overallsize;    // the accumulated size of the output data
00069 
00070 public:
00071    char OUTPUT_STATIC CreateFile(char *filename,int mybufsize=65536)
00072       // Creates the output file. If 'filename==NULL', then the standard
00073       // output is used.
00074    {
00075       if(filename==NULL)
00076       {
00077          savefilename=NULL;
00078          output=stdout;
00079 
00080          // We allocate the memory
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          // We allocate the output buffer memory and a little more for the filename
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          // We only open the output file, if strlen(filename)>0.
00100          // Otherwise, no output is performed at all.
00101          // (This is the test-case)
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       // Writes the remaining output to the file and closes the file
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       // Writes the remaining data and closes the file and removes it
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       // Flushes the output file
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       // We write the output between '0' and 'curpos'
00158       // We only write at most 30000 bytes - to avoid some glitches
00159       // in the implementation of 'fwrite'.
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       // Returns the current empty buffer space and its size
00176    {
00177       *len=bufsize-curpos;
00178       return buf+curpos;
00179    }
00180 
00181    void OUTPUT_STATIC SaveBytes(int bytecount)
00182       // After the user put some data into the empty buffer space,
00183       // it must be acknowledges with 'SaveBytes'
00184    {
00185       curpos+=bytecount;
00186    }
00187 
00188    int OUTPUT_STATIC GetCurFileSize() {  return overallsize+curpos; }
00189       // Returns the current file size
00190 
00191 //********************************************
00192 
00193    void OUTPUT_STATIC StoreData(char *ptr,int len)
00194       // Stores the data at position 'ptr' of length 'len'
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       // Stores a simple uncompressed integer
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       // Stores a character
00220    {
00221       if(bufsize-curpos<1)
00222          Flush();
00223 
00224       buf[curpos]=c;
00225       curpos++;
00226    }
00227 
00228    void OUTPUT_STATIC StoreNewline()
00229       // Stores a new line
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       // Returns a buffer space big enough to contain 'len' bytes
00248       // The buffer pointer is automatically updated
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

Generated on Sat Oct 13 16:08:39 2001 for XMILL by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001