gstorage.cpp

Go to the documentation of this file.
00001 // gstorage.cpp -- Version 0.2
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include "gstorage.h"
00006 ////////////////////////////////////////////////////////////
00007 // Static members
00008 // ---------------------------------------------------------
00009 short gTop::nObjHistogram[MAX_INTSTGKIND];
00010 int gTop::nObjs=0;
00011 gUCharBuffer gStorage::oneCharBuf( 20 );
00012 gStorage::eStoreMethod gStorage::globalStoreMethod=gStorage::e_StgGlobalString;
00013 
00014 const sStorageDesc gStorage::storeKindDesc[]={
00015     {"NIL",     0},
00016     {"RSVD",    1},       //e_ResvdStore  = 1
00017     {"UCHAR",   2},       //e_UChar       = 2
00018     {"SCHAR",   3},       //e_SChar       = 3
00019     {"UINT",    4},       //e_UInt        = 4
00020     {"SINT",    5},       //e_SInt        = 5
00021     {"ULINT",   6},       //e_ULongInt    = 6
00022     {"SLINT",   7},       //e_SLongInt    = 7
00023     {"RSVD08",  0},       // = 8
00024     {"RSVD09",  0},       // = 9
00025     {"REAL",   10},       //e_Real        = 10
00026     {"LIST",   11},       //e_List        = 11
00027     {"RSVD12",  0},       // = 12
00028     {"RSVD13",  0},       // = 13
00029     {"RSVD14",  0},       // = 14
00030     {"RSVD15",  0},       // = 15
00031     {"RSVD16",  0},       // = 16
00032     {"RSVD17",  0},       // = 17
00033     {"RSVD18",  0},       // = 18
00034     {"RSVD19",  0},       // = 19
00035     {"STR",    20},       //e_String      = 20
00036     {"UNISTR", 21},       //e_StrUnicode  = 21
00037     {"RSVD22",  0},       // = 22
00038     {"RSVD23",  0},       // = 23
00039     {"RSVD24",  0},       // = 24
00040     {"RSVD25",  0},       // = 25
00041     {"RSVD26",  0},       // = 26
00042     {"RSVD27",  0},       // = 27
00043     {"RSVD28",  0},       // = 28
00044     {"RSVD29",  0},       // = 29
00045     {"RSVD30",  0},       // = 30
00046     {"EXT___", 31},       //e_StoreExtend  = 31
00047     {"CTRL",   32},       //e_Control
00048 };
00049 ////////////////////////////////////////////////////////////
00050 // gTop - Generic control for objects
00051 // ---------------------------------------------------------
00052 gTop::gTop (t_uint16 intStgKind)
00053     : stgKind( intStgKind )
00054 {
00055 #ifdef DEBUG_MIN
00056  static int dbgInst;
00057 #endif //DEBUG_MIN
00058 
00059  nObjs++;
00060  ASSERTION(stgKind>=1 && stgKind<MAX_INTSTGKIND,"gTop::gTop");
00061  nObjHistogram[stgKind]++;
00062  ///if ( stgKind==gStorage::e_List ) printf("DBG:::+ %d lists (%d)\n",nObjHistogram[stgKind],nObjs)
00063  DBGPRINT_MIN("DBG:::+ (#%d)\tKIND=%u KIND#%d  (%d)\n",nObjs,stgKind,nObjHistogram[stgKind],dbgInst++);
00064 }
00065 
00066 gTop::~gTop ()
00067 {
00068  nObjs--;
00069  DBGPRINT_MIN("DBG: gTop::~gTop: nObjs=%d, stgKind=%d\n",nObjs,stgKind);
00070 #ifdef DEBUG
00071  ASSERTION(stgKind>=1 && stgKind<MAX_INTSTGKIND,"gTop::~gTop");  // only debug: too much consuming
00072 #endif //DEBUG
00073  nObjHistogram[stgKind]--;
00074  ///if ( stgKind==gStorage::e_List ) printf("DBG:::- %d lists (%d)\n",nObjHistogram[stgKind],nObjs)
00075  //printf("DBG:::- (#%d)\tKIND=%u KIND#%d\n",nObjs,stgKind,nObjHistogram[stgKind]);
00076 }
00077 ////////////////////////////////////////////////////////////
00078 gUCharBuffer::gUCharBuffer (t_uint16 bufSize)
00079     : size( bufSize ),
00080       uBuf( nil )
00081 {
00082  if ( size==0 ) size = GENUCHAR_USU_BUFSIZE;
00083  thisAllocate( size );
00084 }
00085 
00086 gUCharBuffer::~gUCharBuffer ()
00087 {
00088  delete[] uBuf;
00089 }
00090 
00091 void gUCharBuffer::Copy (t_uchar* s)
00092 {
00093  thisCopy( s, strlen((char*)s) );
00094 }
00095 
00096 void gUCharBuffer::Copy (char* s)
00097 {
00098  thisCopy( (t_uchar*)s, strlen(s) );
00099 }
00100 
00101 void gUCharBuffer::Clear ()
00102 {
00103  memset( uBuf, 0x0, (size_t)size );
00104 }
00105 
00106 void gUCharBuffer::thisAllocate (t_uint16 bufSize)
00107 {
00108  delete[] uBuf;
00109  uBuf = new t_uchar[size=bufSize];
00110  ASSERTION(uBuf!=nil,"gUCharBuffer::thisAllocate");
00111  Clear();
00112 }
00113 
00114 void gUCharBuffer::thisCopy (t_uchar* s, t_uint16 len)
00115 {
00116  if ( len+1>=size ) {
00117      // Buffer somehow short, rebuilding it
00118      thisAllocate( len*2 );
00119  }
00120  Clear();
00121  for (t_uint16 iter=0; iter<len; iter++) uBuf[iter] = s[iter];
00122 }
00123 ////////////////////////////////////////////////////////////
00124 // gStorage - Generic storage handling
00125 // ---------------------------------------------------------
00126 gStorage::gStorage (gStorage::eStorage aKind,
00127                     gStorage::eStoreMethod aMethod)
00128     : gTop( (t_uint16)aKind ),
00129       kind( aKind ),
00130       storeMethod( aMethod )
00131 {
00132 }
00133 
00134 gStorage::~gStorage ()
00135 {
00136 }
00137 
00138 char* gStorage::StorageName ()
00139 {
00140  ASSERTION(kind>=0 && kind<e_UnusedStore,"StorageName(1)");
00141  return storeKindDesc[ kind ].descStr;
00142 }
00143 
00144 char* gStorage::Str ()
00145 {
00146  return (char*)ToString( oneCharBuf.uBuf );
00147 }
00148 
00149 int gStorage::SetError (int opError)
00150 {
00151  if ( opError==0 ) return 0;
00152  //printf("DBG: SETERROR(%d:%s)\n",opError,StorageName());
00153  //fprintf(stderr,"@STG: opError=%d\n",opError);
00154  return opError;
00155 }
00156 
00157 void gStorage::SetStoreMethod (gStorage::eStoreMethod aMethod)
00158 {
00159  eStoreMethod resMethod( e_StgDefault );
00160  bool isOk = thisSetStoreMethod(aMethod,storeMethod);
00161  ASSERTION(isOk,"gStorage::SetStoreMethod");
00162  storeMethod = resMethod;
00163 }
00164 
00165 bool gStorage::EndGuts ()
00166 {
00167  ;
00168  // Return true if this object is 'active'
00169  return false;
00170 }
00171 
00172 void gStorage::Show (bool doShowAll)
00173 {
00174  short refKind;
00175  if ( kind>=e_UnusedStore ) {
00176      printf("[unknown]");
00177      return;
00178  }
00179  printf("[%s]",storeKindDesc[kind].descStr);
00180  refKind = storeKindDesc[kind].refKind;
00181 #ifdef DEBUG_IO
00182  if ( refKind==0 ) fprintf(stderr,"DBG: refKind=0, kind=%d (%s)\n",(int)kind,storeKindDesc[kind].descStr);
00183 #endif
00184 
00185  ASSERTION(refKind==0 || refKind==kind,"...refKind==kind");
00186 }
00187 
00188 bool gStorage::thisSetStoreMethod (gStorage::eStoreMethod aMethod,
00189                                    gStorage::eStoreMethod& resMethod)
00190 {
00191  resMethod = aMethod;
00192 
00193  switch ( aMethod ) {
00194  case gStorage::e_StgNoStore:
00195      break;
00196  case gStorage::e_StgGlobalString:
00197      globalStoreMethod = aMethod;
00198      break;
00199  case gStorage::e_StgGlobalFlat:
00200      globalStoreMethod = aMethod;
00201      break;
00202  case gStorage::e_StgString:
00203  case gStorage::e_StgFlat:
00204  case gStorage::e_StgDefault:
00205      break;
00206  default:
00207      return false;
00208  }
00209  return true;
00210 }
00211 
00212 bool gStorage::CanSave (FILE* f)
00213 {
00214  return CanRestore( f );
00215 }
00216 
00217 bool gStorage::CanRestore (FILE* f)
00218 {
00219  if ( globalStoreMethod==e_StgNoStore ) return false;
00220  return f!=NULL;
00221 }
00222 ////////////////////////////////////////////////////////////
00223 sServiceNode::~sServiceNode ()
00224 {
00225  // This is anoying for forked programs: uncomment yourself! (ugly currently)
00226  //if ( used>=0 ) {
00227      //fprintf(stderr,"@STG: Undeleted node (%s#%d): used=%d, handle=%d\n",shortName,(int)index,(int)used,handle);
00228  //}
00229 }
00230 
00231 void sServiceNode::Copy (sServiceNode& copy)
00232 {
00233  ASSERTION(copy.shortName[0]!=0,"invalid-copy(1)"); //Must copy a valid name
00234  used = copy.used;
00235  handle = copy.handle;
00236  strcpy( shortName, copy.shortName );
00237  pStorage = copy.pStorage;
00238 }
00239 
00240 void sServiceNode::Delete ()
00241 {
00242  used = -1;
00243  index = 0;
00244  handle = -1;
00245  memset( shortName, 0x0, sizeof(shortName) );
00246  pStorage = nil;
00247 }
00248 
00249 sServiceArray::sServiceArray (unsigned max)
00250     : n( 0 ),
00251       maxN( max ),
00252       pNodes( nil )
00253 {
00254  Allocate( maxN );
00255 }
00256 
00257 sServiceArray::~sServiceArray ()
00258 {
00259  Release();
00260 }
00261 
00262 void sServiceArray::Release ()
00263 {
00264  if ( pNodes!=nil ) delete[] pNodes;
00265  pNodes = nil;
00266  n = 0;
00267 }
00268 
00269 void sServiceArray::Allocate (unsigned nAlloc)
00270 {
00271 // Release();  :: CHECK THIS, use Release to allow multiple allocation
00272  ASSERTION(nAlloc>0,"nAlloc>0");
00273  // .
00274  ASSERTION(pNodes==nil,"pNodes==nil");
00275  pNodes = new sServiceNode[ (maxN = nAlloc)+1 ];
00276  ASSERTION(pNodes!=nil,"pNodes!=nil");
00277 }
00278 
00279 bool sServiceArray::Add (sServiceNode& aNode)
00280 {
00281  unsigned idx;
00282  n++;
00283  idx = n;
00284  if ( n>maxN ) {
00285      idx = FindFreeNode();
00286      if ( idx==0 ) return false;
00287      n = maxN;
00288  }
00289  ASSERTION(idx>=1 && idx<=maxN,"idx<=maxN");
00290  ASSERTION(pNodes!=nil,"pNodes!=nil");
00291  pNodes[ idx ].Copy( aNode );
00292  pNodes[ idx ].index = idx;
00293  return true;
00294 }
00295 
00296 bool sServiceArray::Delete (gStorage* pStorage)
00297 {
00298  unsigned idx;
00299  ASSERTION(pStorage!=nil,"pStorage!=nil");
00300  ASSERTION(n>0,"n>0");
00301  // Find the storage in the nodes
00302  for (idx=1; idx<=maxN; idx++) {
00303      if ( pNodes[idx].pStorage==pStorage ) {
00304          pStorage->EndGuts();
00305          pNodes[idx].Delete();
00306          return true;
00307      }
00308  }
00309  return false;
00310 }
00311 
00312 unsigned sServiceArray::FindFreeNode ()
00313 {
00314  unsigned idx;
00315  for (idx=1; idx<=maxN; idx++) {
00316      if ( pNodes[idx].used==-1 ) return idx;
00317  }
00318  return 0;
00319 }
00320 ////////////////////////////////////////////////////////////
00321 sServiceRegister::~sServiceRegister ()
00322 {
00323  ReleaseDescriptor();
00324  if ( data!=nil ) ReleaseData();
00325 }
00326 
00327 int sServiceRegister::AllocateNodes ()
00328 {
00329  unsigned n = nMediumNodes;
00330  ASSERTION(data==nil,"data==nil");
00331  ASSERTION(n>1,"n>1"); // ...more than 1 node
00332  data = new sServiceArray( n );
00333  ASSERTION(data!=nil,"data!=nil");
00334  return (int)n;
00335 }
00336 
00337 void sServiceRegister::ReleaseDescriptor ()
00338 {
00339  delete[] descriptor.descStr;
00340  descriptor.descStr = nil;
00341 }
00342 
00343 void sServiceRegister::ReleaseData ()
00344 {
00345  ASSERTION(data!=nil,"data!=nil");
00346  delete data;
00347  data = nil;
00348 }
00349 ////////////////////////////////////////////////////////////
00350 void gUChar::Reset ()
00351 {
00352  gStorage::Reset();
00353  c = 0;
00354 }
00355 
00356 gStorage* gUChar::NewObject ()
00357 {
00358  gUChar* a = new gUChar( c );
00359  return a;
00360 }
00361 
00362 t_uchar* gUChar::ToString (t_uchar* uBuf)
00363 {
00364  if ( uBuf==nil ) return nil;
00365  sprintf( (char*)uBuf, "%c", c );
00366  return uBuf;
00367 }
00368 
00369 bool gUChar::SaveGuts (FILE* f)
00370 {
00371  if ( CanSave( f )==false ) return false;
00372  return fprintf( f,"%c", c );
00373 }
00374 
00375 bool gUChar::RestoreGuts (FILE* f)
00376 {
00377  if ( CanRestore( f )==false ) return false;
00378  return fscanf( f, "%c", &c )>=1;
00379 }
00380 ////////////////////////////////////////////////////////////
00381 void gInt::Reset ()
00382 {
00383  gStorage::Reset();
00384  c = 0;
00385 }
00386 
00387 gStorage* gInt::NewObject ()
00388 {
00389  gInt* a = new gInt( c );
00390  return a;
00391 }
00392 
00393 t_uchar* gInt::ToString (t_uchar* uBuf)
00394 {
00395  if ( uBuf==nil ) return nil;
00396  sprintf( (char*)uBuf, "%d", c);
00397  return uBuf;
00398 }
00399 
00400 bool gInt::SaveGuts (FILE* f)
00401 {
00402  if ( CanSave( f )==false ) return false;
00403  return fprintf( f, "%d", c );
00404 }
00405 
00406 bool gInt::RestoreGuts (FILE* f)
00407 {
00408  if ( CanRestore( f )==false ) return false;
00409  return fscanf( f, "%d", &c )>=1;
00410 }
00411 
00412 void gInt::Show (bool doShowAll)
00413 {
00414  printf("%s%d",doShowAll?"'":"\0",c);
00415 }
00416 ////////////////////////////////////////////////////////////
00417 void gUInt::Reset ()
00418 {
00419  gStorage::Reset();
00420  c = 0;
00421 }
00422 
00423 gStorage* gUInt::NewObject ()
00424 {
00425  gUInt* a = new gUInt( c );
00426  return a;
00427 }
00428 
00429 t_uchar* gUInt::ToString (t_uchar* uBuf)
00430 {
00431  if ( uBuf==nil ) return nil;
00432  sprintf( (char*)uBuf, "%u", c);
00433  return uBuf;
00434 }
00435 
00436 bool gUInt::SaveGuts (FILE* f)
00437 {
00438  if ( CanSave( f )==false ) return false;
00439  return fprintf( f, "%u", c );
00440 }
00441 
00442 bool gUInt::RestoreGuts (FILE* f)
00443 {
00444  if ( CanRestore( f )==false ) return false;
00445  return fscanf( f, "%u", &c )>=1;
00446 }
00447 
00448 void gUInt::Show (bool doShowAll)
00449 {
00450  printf("%s%u",doShowAll?"'":"\0",c);
00451 }
00452 ////////////////////////////////////////////////////////////
00453 void gReal::Reset ()
00454 {
00455  gStorage::Reset();
00456  c = 0.0;
00457 }
00458 
00459 gStorage* gReal::NewObject ()
00460 {
00461  gReal* a = new gReal( (float)c );
00462  return a;
00463 }
00464 
00465 t_uchar* gReal::ToString (t_uchar* uBuf)
00466 {
00467  if ( uBuf==nil ) return nil;
00468  sprintf( (char*)uBuf, "%f", c);
00469  return uBuf;
00470 }
00471 
00472 bool gReal::SaveGuts (FILE* f)
00473 {
00474  if ( CanSave( f )==false ) return false;
00475  return fprintf( f, "%f", c );
00476 }
00477 
00478 bool gReal::RestoreGuts (FILE* f)
00479 {
00480  bool isOk;
00481  float val;
00482  if ( CanRestore( f )==false ) return false;
00483  isOk = fscanf( f, "%f", &val );
00484  if ( isOk ) c = val;
00485  return isOk;
00486 }
00487 
00488 void gReal::Show (bool doShowAll)
00489 {
00490  printf("%s%f",doShowAll?"`":"\0",c);
00491 }
00492 ////////////////////////////////////////////////////////////
00493 

Generated on Sat Aug 18 02:40:58 2007 for xpfweb_v2x lib by  doxygen 1.4.2