glist.cpp

Go to the documentation of this file.
00001 // glist.cpp -- Version 0.2
00002 
00003 #include <string.h>
00004 #include "glist.h"
00005 #include "gstring.h"
00006 ////////////////////////////////////////////////////////////
00007 // Static members
00008 // ---------------------------------------------------------
00009 // void!
00010 
00011 ////////////////////////////////////////////////////////////
00012 // gElem - Generic elements of lists
00013 // ---------------------------------------------------------
00014 gElem::gElem ()
00015     : gStorage( e_ResvdStore ),
00016       prev( nil ),
00017       next( nil ),
00018       me( nil )
00019 {
00020 }
00021 
00022 gElem::gElem (gStorage* newObj)
00023     : gStorage( e_ResvdStore ),
00024       prev( nil ),
00025       next( nil ),
00026       me( newObj )
00027 {
00028  ASSERTION(me!=nil,"me!=nil");
00029 }
00030 
00031 gElem::~gElem ()
00032 {
00033  DBGPRINT("%s",me==nil?"DBG: BOGUS: ~:gElem->me...\n":"\0");
00034  //if ( me->Kind()==e_String ) {
00035  //    printf("ELEM(%u|%s):[%s]\n",me->Kind(),me->storeKindDesc[me->Kind()].descStr,((gString*)me)->Str());
00036  //}
00037 
00038  delete me;
00039  me = nil;
00040 }
00041 
00042 gStorage* gElem::NewObject ()
00043 {
00044  ASSERTION_FALSE("gElem::NewObject: Not used");
00045  return nil;
00046 }
00047 
00048 t_uchar* gElem::ToString (t_uchar* uBuf)
00049 {
00050  ASSERTION(me!=nil && uBuf!=nil,"me!=nil");
00051  return me->ToString( uBuf );
00052 }
00053 
00054 char* gElem::Str ()
00055 {
00056  ASSERTION(me!=nil,"me!=nil");
00057  return me->Str();
00058 }
00059 
00060 bool gElem::SaveGuts (FILE* f)
00061 {
00062  ASSERTION(me!=nil,"gElem::SaveGuts");
00063  return me->SaveGuts( f );
00064 }
00065 
00066 bool gElem::RestoreGuts (FILE* f)
00067 {
00068  ASSERTION(me!=nil,"gElem::SaveGuts");
00069  return me->SaveGuts( f );
00070 }
00071 ////////////////////////////////////////////////////////////
00072 // gListGeneric - Generic list handling
00073 // ---------------------------------------------------------
00074 gListGeneric::gListGeneric (eStorage aKind,
00075                             eListStrategy aStgy,
00076                             eListIndexes aLstIdxs)
00077     : gStorage( aKind, e_StgDefault ),
00078       size( 0 ),
00079       preMaxSize( 0/*aStgy==e_LstPointer ? 0 : LST_LIST_SIZE*/ ),
00080       maxSize( 0 ),
00081       lstIdxs( aLstIdxs ),
00082       stgy( aStgy ),
00083       pStart( nil ),
00084       pEnd( nil ),
00085       pCurrent( nil ),
00086       doIgnoreCase( false )
00087 {
00088  thisPreAllocate( preMaxSize );
00089  ASSERTION(lstIdxs==e_LstIdx1toN || lstIdxs==e_LstIdx1toNstrict,"gListGeneric:...");
00090 }
00091 
00092 gListGeneric::~gListGeneric ()
00093 {
00094  thisDelete();
00095 }
00096 
00097 bool gListGeneric::IsValidIndex (unsigned idx)
00098 {
00099  return thisIndex(idx);
00100 }
00101 
00102 char* gListGeneric::Str (unsigned idx)
00103 {
00104  thisIndex(idx);
00105  genUCharBuf.Clear();
00106  if ( pCurrent==nil ) return (char*)genUCharBuf.uBuf;
00107  return pCurrent->Str();
00108 }
00109 
00110 t_uchar* gListGeneric::UStr (unsigned idx)
00111 {
00112  /*
00113  if ( me->Kind()==e_String ) {
00114      char* dbgStr = me->Str();
00115      if ( dbgStr!=nil && strlen(dbgStr)>=genUCharBuf.size ) {
00116          printf("DBG: ToString([%s]), size-of-genUChar=%u, wanting %u chars!!!\n",dbgStr,genUCharBuf.size,strlen(dbgStr));
00117      }
00118  }
00119  return pCurrent->ToString( genUCharBuf.uBuf )  ==> Not used because of inexisting size check
00120  */
00121  return (t_uchar*)Str( idx );
00122 }
00123 
00124 gStorage::eStorage gListGeneric::ElementsKind ()
00125 {
00126  eStorage thisKind=e_NoStorage, lastKind=e_NoStorage;
00127  unsigned idx=1, nDiffs=0;
00128  if ( thisIndex( idx )==false ) return e_List;
00129  ASSERTION(size>0,"size>0");
00130  // Here, list is not empty:
00131  // it can be all-of-the-same-kind, or variously filled.
00132  for (pCurrent=pStart; pCurrent!=nil; pCurrent=pCurrent->next) {
00133      thisKind = pCurrent->me->Kind();
00134      if ( thisKind!=lastKind && lastKind!=e_NoStorage ) nDiffs++;
00135      lastKind = thisKind;
00136  }
00137  if ( nDiffs>0 ) return e_List;
00138  return thisKind;
00139 }
00140 
00141 gElem& gListGeneric::GetElement (unsigned idx)
00142 {
00143  static gElem tempElem;
00144  if ( thisIndex( idx ) ) return *pCurrent;
00145  return tempElem;
00146 }
00147 
00148 gElem* gListGeneric::GetElementPtr (unsigned idx)
00149 {
00150  if ( thisIndex( idx ) ) return pCurrent;
00151  return nil;
00152 }
00153 
00154 gStorage* gListGeneric::GetObjectPtr (unsigned idx)
00155 {
00156  gElem* pElem = GetElementPtr( idx );
00157  ASSERTION(pElem!=nil,"GetObjectPtr(1)");
00158  ASSERTION(pElem->me!=nil,"GetObjectPtr(2)");
00159  return pElem->me;
00160 }
00161 
00162 gStorage* gListGeneric::GetFirstObjectPtr ()
00163 {
00164  if ( pStart==nil ) return nil;
00165  return pStart->me;
00166 }
00167 
00168 gStorage* gListGeneric::GetLastObjectPtr ()
00169 {
00170  if ( pEnd==nil ) return nil;
00171  return pEnd->me;
00172 }
00173 
00174 unsigned gListGeneric::Delete (unsigned startPos, unsigned endPos)
00175 {
00176  unsigned
00177      counter=0,
00178      oldSize=N(),
00179      pIter,
00180      p0=startPos==0?1:startPos,
00181      p1=endPos==0?oldSize:gMIN(endPos,oldSize);
00182  gElem* pTemp;
00183  gElem* pNext;
00184 
00185  if ( startPos==0 && endPos==0 ) {
00186      thisDelete();
00187      return oldSize;
00188  }
00189 
00190  if ( p0>p1 ) return 0;
00191 
00192  // Delete only some positions
00193 
00194  // Place current pointer on first element to delete.
00195  thisIndex( p0 );
00196  ASSERTION(p0>=1 && p1<=oldSize,"Delete(1)");
00197 
00198  for (pIter=p0, pTemp=GetElementPtr(p0); pIter<=p1; pIter++) {
00199      ASSERTION(size>0,"Delete(2)");
00200      size--;
00201      counter++;
00202      pCurrent = pTemp;
00203      ASSERTION(pCurrent!=nil,"pCurrent!=nil");
00204      pNext = pTemp->next;
00205      if ( pTemp->prev==nil ) {
00206          // Start of list
00207          pStart = pNext;
00208          pTemp = pNext;
00209          if ( pTemp!=nil ) pTemp->prev = nil;
00210      }
00211      else {
00212          if ( pNext==nil ) {
00213              // At end of list
00214              pTemp = pTemp->prev;
00215              pTemp->next = nil;
00216              pEnd = pTemp;
00217          }
00218          else {
00219              // At middle of list
00220              // (pTemp->prev is not nil!)
00221              pTemp = pTemp->prev;
00222              pTemp->next = pNext;
00223              pNext->prev = pTemp;
00224              pTemp = pNext;  // Placed iterator in next position
00225          }
00226      }
00227      ASSERTION(pCurrent!=nil,"pCurrent!=nil");
00228      delete pCurrent;
00229      if ( pTemp==nil ) {
00230          pStart = pEnd = nil;
00231      }
00232      else {
00233          if ( pTemp->next==nil ) pEnd = pTemp;
00234          if ( pTemp->prev==nil ) pStart = pTemp;
00235      }
00236  }//end FOR
00237  pCurrent = nil;
00238  return counter;
00239 }
00240 
00241 gStorage* gListGeneric::NewObject ()
00242 {
00243  ASSERTION_FALSE("gListGeneric::NewObject: Not used");
00244  return nil;
00245 }
00246 
00247 t_uchar* gListGeneric::ToString (t_uchar* uBuf)
00248 {
00249  ;
00250  // Output notation:
00251  //     Empty list               ===>    ()
00252  //     List of strings          ===>    (me and you)
00253  //     List of strings and nums ===>    (I am '31 years old)
00254  //     List of sentences        ===>    ("I use a suit." "You use a dress.")
00255  //     List with some resvd chrs.==>    (123-a.com sold '100 domains)
00256  ;
00257  return nil;
00258 }
00259 
00260 bool gListGeneric::SaveGuts (FILE* f)
00261 {
00262  unsigned i, n=N();
00263  gString sBlank( ' ' );
00264 
00265  if ( CanSave( f )==false ) return false;
00266  for (i=1; i<=n; i++) {
00267      if ( i>1 ) sBlank.SaveGuts( f );
00268      GetObjectPtr( i )->SaveGuts( f );
00269  }
00270  return true;
00271 }
00272 
00273 bool gListGeneric::RestoreGuts (FILE* f)
00274 {
00275  return CanRestore( f );
00276 }
00277 
00278 void gListGeneric::thisPreAllocate (unsigned toSize)
00279 {
00280  if ( stgy==e_LstDefault ) stgy = e_LstPointer;
00281  ASSERTION(stgy==e_LstPointer,"stgy==e_LstPointer: TODO other");
00282 
00283  pCurrent = nil;
00284  //if ( preMaxSize==0 ) return;
00285 }
00286 
00287 unsigned gListGeneric::thisDelete ()
00288 {
00289  unsigned countCheck=0;
00290  gElem* pNext;
00291 
00292  for (pCurrent=pStart; pCurrent!=nil; )
00293  {
00294      pNext = pCurrent->next;
00295      delete pCurrent;
00296      pCurrent = pNext;
00297      countCheck++;
00298  }
00299  ASSERTION(countCheck==size,"countCheck==size");
00300  size = 0;
00301  pStart = pEnd = pCurrent = nil;
00302  return countCheck;
00303 }
00304 
00305 bool gListGeneric::thisIndex (unsigned& idx)
00306 {
00307  bool isOk = idx>=1 && idx<=size;
00308  unsigned i;
00309 
00310  pCurrent = nil;
00311  if ( lstIdxs==e_LstIdx1toNstrict ) {
00312      ASSERTION_USE(isOk,"gListGeneric::thisIndex");
00313      return true;
00314  }
00315  if ( idx<1 ) idx = 1;
00316  isOk = idx<=size;
00317  if ( idx>size ) idx = size;
00318  if ( isOk==false ) return false;
00319 
00320  for (i=1, pCurrent=pStart; i<idx; i++) {
00321      pCurrent = pCurrent->next;
00322      ASSERTION(pCurrent!=nil,"pCurrent!=nil");
00323  }
00324  ASSERTION(pCurrent!=nil,"pCurrent!=nil");
00325  ASSERTION(pCurrent->me!=nil,"pCurrent->me!=nil");
00326  return true;
00327 }
00328 
00329 bool gListGeneric::thisAppend (gStorage* newObj)
00330 {
00331  // Returns true iff new re-allocation performed.
00332  // Since no vector functions are present (yet),
00333  // returns always false.
00334  ;
00335  gElem* newElem;
00336 
00337  ASSERTION(newObj!=nil,"newObj!=nil");
00338  newElem = new gElem( newObj );
00339  ASSERTION(newElem!=nil,"newElem!=nil");
00340 
00341  if ( size==0 ) {
00342      ASSERTION(pStart==nil && pEnd==nil,"::thisAppend(1)");
00343      pStart = newElem;
00344  }
00345  else {
00346      newElem->prev = pEnd;
00347      pEnd->next = newElem;
00348  }
00349 
00350  pCurrent = pEnd = newElem;
00351  size++;
00352 
00353  return false;
00354 }
00355 ////////////////////////////////////////////////////////////
00356 gList::gList ()
00357     : gListGeneric(
00358         e_List,
00359         e_LstPointer,
00360         e_LstIdx1toN)
00361 {
00362 }
00363 
00364 gList::~gList ()
00365 {
00366 }
00367 
00368 int gList::GetInt (unsigned idx)
00369 {
00370  if ( thisIndex(idx)==false ) return 0;
00371  if ( pCurrent->me->Kind()!=e_SInt ) return 0;
00372  return ((gInt*)(pCurrent->me))->GetInt();
00373 }
00374 
00375 unsigned gList::GetUInt (unsigned idx)
00376 {
00377  if ( thisIndex(idx)==false ) return 0;
00378  if ( pCurrent->me->Kind()!=e_UInt ) return 0;
00379  return ((gUInt*)(pCurrent->me))->GetUInt();
00380 }
00381 
00382 unsigned gList::Match (char* s)
00383 {
00384  unsigned i, n=N();
00385  char* thisStr;
00386  for (i=1; i<=n; i++) {
00387      thisStr = Str( i );
00388      if ( thisStr==nil ) continue;
00389      gString sTemp( thisStr );
00390      if ( sTemp.Match( s, doIgnoreCase ) ) return i;
00391  }
00392  return 0;
00393 }
00394 
00395 unsigned gList::FindFirst (char* s,
00396                            unsigned strPos,
00397                            eFindCriteria findCriteria)
00398 {
00399  ;
00400  // Returns the index of first occurrence obeying 'findCriteria'
00401  // for the string 's' in list (on position strPos).
00402  ;
00403  gList posL;
00404  return thisFind( s, strPos, findCriteria, true, posL );
00405 }
00406 
00407 unsigned gList::Find (char* s,
00408                       unsigned strPos,
00409                       eFindCriteria findCriteria)
00410 {
00411  gList posL;
00412  return thisFind( s, strPos, findCriteria, false, posL );
00413 }
00414 
00415 unsigned gList::FindAny (char* s,
00416                          unsigned strPos,
00417                          eFindCriteria findCriteria,
00418                          gList& posL)
00419 {
00420  return thisFind( s, strPos, findCriteria, false, posL );
00421 }
00422 
00423 unsigned gList::Add (int v)
00424 {
00425  gInt* newObj;
00426  newObj = new gInt(v);
00427  thisAppend( newObj );
00428  return size;
00429 }
00430 
00431 unsigned gList::Add (unsigned v)
00432 {
00433  gUInt* newObj;
00434  newObj = new gUInt(v);
00435  thisAppend( newObj );
00436  return size;
00437 }
00438 
00439 unsigned gList::Add (gString& copy)
00440 {
00441  return Add( copy.Str() );
00442 }
00443 
00444 unsigned gList::Add (t_uchar* s)
00445 {
00446  gString* newObj;
00447  newObj = new gString( s );
00448  thisAppend( newObj );
00449  return size;
00450 }
00451 
00452 unsigned gList::Add (char* s)
00453 {
00454  return Add( (t_uchar*)s );
00455 }
00456 
00457 gList& gList::CopyList (gList& aL)
00458 {
00459  bool isOk;
00460  gStorage* aObj;
00461  gStorage* newObj;
00462  gElem* thisElem;
00463  unsigned i, n=aL.N();
00464 
00465  thisDelete();
00466  thisElem = aL.GetElementPtr(1);
00467  for (i=1; i<=n; i++) {
00468      ASSERTION(thisElem!=nil,"thisElem!=nil");
00469      aObj = thisElem->me;
00470      ASSERTION(aObj!=nil,"aObj!=nil");
00471      newObj = aObj->NewObject();
00472      isOk = AppendObject( newObj );
00473      ASSERTION(isOk,"isOk");
00474      thisElem = thisElem->next;
00475  }
00476  return *this;
00477 }
00478 
00479 unsigned gList::DeleteString (gString& s)
00480 {
00481  ;
00482  // Returns 0 if no string found, 1 otherwise
00483  // Delete the first string found.
00484  ;
00485  unsigned pos = Match( s.Str() );
00486  if ( pos==0 ) return 0;
00487  Delete( pos, pos );
00488  return 1;
00489 }
00490 
00491 unsigned gList::DeleteStrings (gString& s)
00492 {
00493  ;
00494  // Returns number of elements deleted.
00495  // Delete all strings found
00496  ;
00497  unsigned count=0, pos;
00498  while ( (pos = Match( s.Str() ))>0 ) {
00499      Delete( pos, pos );
00500      count++;
00501  }
00502  return count;
00503 }
00504 
00505 t_uchar* gList::ToString (t_uchar* uBuf)
00506 {
00507  return gListGeneric::ToString( uBuf );
00508 }
00509 
00510 bool gList::SaveGuts (FILE* f)
00511 {
00512  return gListGeneric::SaveGuts( f );
00513 }
00514 
00515 bool gList::RestoreGuts (FILE* f)
00516 {
00517  return gListGeneric::RestoreGuts( f );
00518 }
00519 
00520 void gList::Show (bool doShowAll)
00521 {
00522  unsigned i, n=N();
00523 
00524  if ( doShowAll ) printf("(");
00525  for (i=1; i<=n; i++) {
00526      gStorage* aObj;
00527      aObj = GetElementPtr(i)->me;
00528      ASSERTION(aObj!=nil,"aObj!=nil");
00529      if ( doShowAll ) {
00530          aObj->Show( true );
00531          printf("%s",i==n?"\0":" ");
00532      }
00533      else {
00534          printf("%s%s",Str(i),i==n?"\0":" ");
00535      }
00536  }
00537  if ( doShowAll ) printf(")\n");
00538 }
00539 
00540 unsigned gList::thisFind (char* s,
00541                           unsigned strPos,
00542                           eFindCriteria findCriteria,
00543                           bool doStopOnFirst,
00544                           gList& posL)
00545 {
00546  bool isOk;
00547  unsigned i, n=N(), pos, posFirst=0;
00548 
00549  for (i=1; i<=n; i++) {
00550      gString sTemp( Str( i ) );
00551      pos = sTemp.Find( s, doIgnoreCase );
00552      if ( pos==0 ) continue;
00553      switch ( findCriteria ) {
00554      case e_FindExactPosition:
00555          isOk = pos==strPos;
00556          break;
00557      case e_FindFromPosition:
00558          isOk = pos>=strPos;
00559          break;
00560      case e_FindBeforePosition:
00561          isOk = pos<strPos;
00562          break;
00563      default:
00564          return 0;
00565      }
00566      if ( isOk==false ) continue;
00567      if ( posFirst==0 ) posFirst = i;
00568      posL.Add( i );
00569      if ( doStopOnFirst ) return posFirst;
00570  }
00571  return posFirst;
00572 }
00573 ////////////////////////////////////////////////////////////
00574 gListInt::gListInt ()
00575 {
00576 }
00577 
00578 gListInt::~gListInt ()
00579 {
00580 }
00581 
00582 unsigned gListInt::FindInt (int v)
00583 {
00584  unsigned i, n = N();
00585  for (i=1; i<=n; i++) {
00586      if ( v==GetInt( i ) ) return i;
00587  }
00588  return 0;
00589 }
00590 
00591 bool gListInt::Append (int v)
00592 {
00593  gInt* newObj = new gInt( v );
00594  return AppendObject( newObj );
00595 }
00596 ////////////////////////////////////////////////////////////
00597 

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