00001 /********************************************************************** 00002 *< 00003 FILE: appio.h 00004 00005 DESCRIPTION: General chunk-ifying code: useful for writing 00006 hierarchical data structures to a linear stream, such as 00007 an AppData block. 00008 00009 CREATED BY: Dan Silva 00010 00011 HISTORY: created 3/24/97 00012 00013 *> Copyright (c) 1994, All Rights Reserved. 00014 **********************************************************************/ 00015 00016 #pragma once 00017 #include "coreexp.h" 00018 #include "maxheap.h" 00019 #include "strbasic.h" 00020 #include <WTypes.h> 00021 #include "maxtypes.h" 00022 00023 //------------------------------------------------------------------------ 00024 // AppSave will write hierarchical chunks into a private buffer, enlarging 00025 // it as needed. When completed, use the methods BufferPtr() and 00026 // NBytesWritten() to get at this buffer. ( AppSave will delete the buffer in 00027 // its DeleteThis() method , so you need to copy the buffer to save the data.) 00028 00029 // The chunk hierarchy should always have a single highest level chunk. 00030 // Chunks can be nested to any depth. 00031 // A Chunk can contain either sub-chunks, or data, but not both. 00032 00033 // For example: 00034 // 00035 // AppSave *asave = NewAppSave(1000); 00036 // asave->BeginChunk(MAIN_CHUNK); 00037 // asave->BeginChunk(CHUNK1); 00038 // .. write data 00039 // asave->EndChunk(); 00040 // 00041 // asave->BeginChunk(CHUNK2); 00042 // .. write data 00043 // asave->EndChunk(); 00044 // 00045 // asave->BeginChunk(CHUNK3); 00046 // .. write data 00047 // asave->EndChunk(); 00048 // asave->EndChunk(); // end MAIN_CHUNK 00049 00050 00082 class AppSave: public MaxHeapOperators { 00083 protected: 00084 virtual ~AppSave() {} 00085 public: 00087 virtual void DeleteThis()=0; 00088 00089 // After saving, use this to get pointer to the buffer created. 00092 virtual BYTE* BufferPtr()=0; 00093 00094 // This tells how many bytes were written in the buffer. 00097 virtual int NBytesWritten()=0; 00098 00099 // Begin a chunk. 00105 virtual void BeginChunk(USHORT id)=0; 00106 00107 // End a chunk, and back-patch the length. 00110 virtual void EndChunk()=0; 00111 00114 virtual int CurChunkDepth()=0; // for checking balanced BeginChunk/EndChunk 00115 00116 // write a block of bytes to the output stream. 00127 virtual IOResult Write(const void *buf, ULONG nbytes, ULONG *nwrit)=0; 00128 00129 // Write character strings 00136 virtual IOResult WriteWString(const char *str)=0; 00137 00144 virtual IOResult WriteWString(const mwchar_t *str)=0; 00152 virtual IOResult WriteCString(const char *str)=0; 00153 00160 virtual IOResult WriteCString(const mwchar_t *str)=0; 00161 00162 }; 00163 00164 //------------------------------------------------------------------------ 00165 // AppLoad takes a chunk-ified data stream, and provides routines for 00166 // decoding it. 00167 00174 class AppLoad: public MaxHeapOperators { 00175 protected: 00176 virtual ~AppLoad() {}; 00177 public: 00179 virtual void DeleteThis()=0; 00180 00181 // if OpenChunk returns IO_OK, use following 3 function to get the 00182 // info about the chunk. IO_END indicates no more chunks at this level 00192 virtual IOResult OpenChunk()=0; 00193 00194 // These give info about the most recently opened chunk 00197 virtual USHORT CurChunkID()=0; 00203 virtual ChunkType CurChunkType()=0; 00206 virtual ULONG CurChunkLength()=0; // chunk length NOT including header 00209 virtual int CurChunkDepth()=0; // for checking balanced OpenChunk/CloseChunk 00210 00211 // close the currently opened chunk, and position at the next chunk 00212 // return of IO_ERROR indicates there is no open chunk to close 00217 virtual IOResult CloseChunk()=0; 00218 00219 // Look at the next chunk ID without opening it. 00220 // returns 0 if no more chunks 00223 virtual USHORT PeekNextChunkID()=0; 00224 00225 // Read a block of bytes from the output stream. 00236 virtual IOResult Read(void *buf, ULONG nbytes, ULONG *nread )=0; 00237 00238 // Read a string from a string chunk assumes chunk is already open, 00239 // it will NOT close the chunk. Sets buf to point 00240 // to a char string. Don't delete buf: ILoad will take care of it. 00241 00242 // Read a string that was stored as Wide chars. 00251 virtual IOResult ReadWStringChunk(char** buf)=0; 00252 00261 virtual IOResult ReadWStringChunk(mwchar_t** buf)=0; 00262 00263 // Read a string that was stored as single byte chars 00271 virtual IOResult ReadCStringChunk(char** buf)=0; 00272 00281 virtual IOResult ReadCStringChunk(mwchar_t** buf)=0; 00282 }; 00283 00284 00285 // Create a new AppLoad for reading chunks out of buf: 00286 // bufSize specifies the number of bytes that are valid in 00287 // buf.. 00295 CoreExport AppLoad* NewAppLoad(BYTE* buf, int bufSize); 00296 00297 // Create a new AppSave for writing chunks 00298 // InitbufSize is the initial size the internal buffer is allocated to. 00299 // It will be enlarged if necessary. 00305 CoreExport AppSave* NewAppSave(int initBufSize); 00306