Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

ORESX.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ORESX.CPP

Go to the documentation of this file.
00001 //Filename    : ORESX.CPP
00002 //Description : Object of resource library with name index
00003 
00004 #include <string.h>
00005 
00006 #include <ALL.H>
00007 #include <OSYS.H>
00008 #include <ORESX.H>
00009 
00010 //--------- Format of RES file ------------//
00011 //
00012 // In the resource file, contain many data unit.
00013 //
00014 // <int>  = no. of data records in this RES file
00015 //
00016 // Index area
00017 // char[8] = data record name
00018 // <long>  = pointer of corresponding data unit in this file
00019 //   .
00020 //   .
00021 // char[9] = the last index is a virtual index which is used to calculate
00022 // <long>    the size of the last data unit
00023 //
00024 //
00025 // Data area
00026 // <char..>  = data
00027 //
00028 //---------------------------------------------
00029 //
00030 // size of data unit = next offset - current offset
00031 //
00032 //---------------------------------------------//
00033 
00034 //---------- Begin of function void ResourceIdx::init ---------//
00042 void ResourceIdx::init(char* resName, int readAll, int useCommonBuf) {
00043     long dataSize;
00044 
00045     if( init_flag )                                 // if a resource is already opened, close it first
00046         deinit();
00047 
00048     //------------- open resource file ----------//
00049 
00050     file_open( resName );
00051 
00052     //------------ Init vars ---------------------//
00053 
00054     read_all      = readAll;
00055     use_common_buf   = useCommonBuf;
00056     data_buf      = NULL;
00057     data_buf_size = 0;
00058 
00059     rec_count     = file_get_short();
00060     cur_rec_no    = -1;
00061 
00062     user_data_buf       = NULL;
00063     user_data_buf_size  = 0;
00064     user_start_read_pos = 0;
00065 
00066     //---------- Read in record index -------------//
00067 
00068     index_buf = (ResIndex*) mem_add( (rec_count+1) * sizeof(ResIndex) );
00069 
00070     // rec_count+1 is the last index pointer for calculating last record size
00071 
00072     file_read( index_buf, sizeof(ResIndex) * (rec_count+1) );
00073 
00074     //---------- Read in record data -------------//
00075 
00076     if( read_all ) {
00077         dataSize = index_buf[rec_count].pointer - index_buf[0].pointer;
00078 
00079         data_buf = mem_add( dataSize );
00080 
00081         file_read( data_buf, dataSize );
00082         file_close();
00083     }
00084     else {
00085         if( use_common_buf )
00086             data_buf = sys.common_data_buf;
00087     }
00088 
00089     init_flag = 1;
00090 }
00091 
00092 //----------- End of function ResourceIdx::init ------------//
00093 
00094 //---------- Begin of function ResourceIdx::deinit ---------//
00096 void ResourceIdx::deinit() {
00097     if( init_flag ) {
00098         if( index_buf ) {
00099             mem_del(index_buf);
00100             index_buf = NULL;
00101         }
00102 
00103         if( data_buf && data_buf != sys.common_data_buf ) {
00104             mem_del(data_buf);
00105             data_buf = NULL;
00106         }
00107 
00108         if( !read_all )
00109             file_close();
00110 
00111         init_flag=0;
00112     }
00113 }
00114 
00115 //----------- End of function ResourceIdx::~ResourceIdx ------------//
00116 
00117 //---------- Begin of function ResourceIdx::read_into_user_buf ----------//
00131 int ResourceIdx::read_into_user_buf(char* dataName, char* userBuf, int userBufSize, int userStartReadPos) {
00132     err_when( !init_flag || !dataName );
00133 
00134     int indexId = get_index(dataName);
00135 
00136     if( !indexId )
00137         return 0;
00138 
00139     //----- set the user buffer and read into the data -----//
00140 
00141     set_user_buf(userBuf, userBufSize, userStartReadPos);
00142 
00143     int rc = get_data(indexId)!=NULL;               // ==NULL if actual data size > buffer size
00144 
00145     reset_user_buf();
00146 
00147     return rc;
00148 }
00149 
00150 //----------- End of function ResourceIdx::read_into_user_buf -------------//
00151 
00152 //---------- Begin of function ResourceIdx::read ----------//
00166 char* ResourceIdx::read(char* dataName) {
00167     err_when( !init_flag || !dataName );
00168 
00169     int indexId = get_index(dataName);
00170 
00171     if( indexId )
00172         return get_data(indexId);
00173     else
00174         return NULL;
00175 }
00176 
00177 //----------- End of function ResourceIdx::read -------------//
00178 
00179 //---------- Begin of function ResourceIdx::get_index ----------//
00189 int ResourceIdx::get_index(char* dataName) {
00190     err_when( !init_flag || !dataName );
00191 
00192     int i;
00193 
00194     for( i=0 ; i<rec_count ; i++ ) {
00195         if( strcmp( index_buf[i].name, dataName ) == 0 )
00196             return i+1;
00197     }
00198 
00199     return 0;
00200 }
00201 
00202 //----------- End of function ResourceIdx::get_index -------------//
00203 
00204 //---------- Begin of function ResourceIdx::get_data ----------//
00215 char* ResourceIdx::get_data(int indexId) {
00216     err_when( !init_flag );
00217 
00218     unsigned dataSize;
00219 
00220     err_when( indexId < 1 || indexId > rec_count );
00221 
00222     indexId--;                                      // make it start from 0, instead of starting from 1 for easy array accessing
00223 
00224     //------ all data pre-loaded to memory ------//
00225 
00226     if( read_all )
00227         return data_buf + index_buf[indexId].pointer - index_buf[0].pointer;
00228 
00229     //------ all data NOT pre-loaded to memory -------//
00230 
00231     if( indexId == cur_rec_no && !use_common_buf )
00232         return data_buf;
00233 
00234     dataSize = index_buf[indexId+1].pointer - index_buf[indexId].pointer;
00235 
00236     file_seek( index_buf[indexId].pointer );
00237 
00238     //--- if the user has custom assigned a buffer, read into that buffer ---//
00239 
00240     if( user_data_buf ) {
00241         if( user_start_read_pos > 0 ) {
00242             file_seek(user_start_read_pos,FILE_CURRENT);// skip the width and height info
00243             dataSize -= user_start_read_pos;
00244         }
00245 
00246         if( dataSize > user_data_buf_size )
00247             return NULL;
00248 
00249         file_read( user_data_buf, dataSize );
00250 
00251         return user_data_buf;
00252     }
00253     else {
00254         err_when( use_common_buf && dataSize > COMMON_DATA_BUF_SIZE );
00255 
00256         if( !use_common_buf && data_buf_size < dataSize )
00257             data_buf = mem_resize( data_buf, dataSize );
00258 
00259         data_buf_size = dataSize;
00260 
00261         file_read( data_buf, dataSize );
00262 
00263         return data_buf;
00264     }
00265 }
00266 
00267 //----------- End of function ResourceIdx::get_data -------------//
00268 
00269 //---------- Begin of function ResourceIdx::get_file ----------//
00282 File* ResourceIdx::get_file(char* dataName, int& dataSize) {
00283     err_when( !init_flag || !dataName || read_all);
00284 
00285     int i;
00286 
00287     //-------- Search for data name ----------//
00288 
00289     for( i=0 ; i<rec_count ; i++ ) {
00290         if( strcmp( index_buf[i].name, dataName ) == 0 ) {
00291             file_seek( index_buf[i].pointer );
00292 
00293             dataSize = index_buf[i+1].pointer - index_buf[i].pointer;
00294 
00295             return this;
00296         }
00297     }
00298 
00299     return NULL;
00300 }
00301 
00302 //----------- End of function ResourceIdx::get_file -------------//
00303 
00304 //---------- Begin of function ResourceIdx::get_file ----------//
00317 File* ResourceIdx::get_file(int bitmapId, int& dataSize) {
00318     err_when( !init_flag || !bitmapId || read_all );
00319 
00320     //-------- Search for data name ----------//
00321 
00322     file_seek( index_buf[bitmapId-1].pointer );
00323 
00324     dataSize = index_buf[bitmapId].pointer - index_buf[bitmapId-1].pointer;
00325 
00326     return this;
00327 }
00328 
00329 //----------- End of function ResourceIdx::get_file -------------//
00330 
00331 //---------- Begin of function ResourceIdx::set_user_buf ----------//
00341 void ResourceIdx::set_user_buf(char* userDataBuf, int bufSize, int userStartReadPos) {
00342     user_data_buf       = userDataBuf;
00343     user_data_buf_size  = bufSize;
00344     user_start_read_pos = userStartReadPos;
00345 }
00346 
00347 //----------- End of function ResourceIdx::set_user_buf -------------//
00348 
00349 //---------- Begin of function ResourceIdx::reset_user_buf ----------//
00353 void ResourceIdx::reset_user_buf() {
00354     user_data_buf      = NULL;
00355     user_data_buf_size = 0;
00356 }
00357 
00358 //----------- End of function ResourceIdx::reset_user_buf -------------//
00359 
00360 //---------- Begin of function ResourceIdx::data_name ----------//
00361 char *ResourceIdx::data_name(int i) {
00362     if( i < 1 || i > rec_count)
00363         return NULL;
00364 
00365     return( index_buf[i-1].name );
00366 }
00367 
00368 //---------- End of function ResourceIdx::data_name ----------//

Generated on Fri Aug 23 01:38:18 2002 for VirtualU by doxygen1.2.17