Changeset 15598


Ignore:
Timestamp:
2008-05-20T15:00:37+12:00 (16 years ago)
Author:
mdewsnip
Message:

(Adding new DB support) Moved some non-GDBM-specific code out of gdbmclass and into dbclass.

Location:
gsdl/trunk/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/lib/dbclass.cpp

    r15442 r15598  
    2525
    2626#include "dbclass.h"
     27#include "OIDtools.h"
    2728
    2829
    2930dbclass::~dbclass() {}
     31
     32
     33// replaces the .fc, .lc, .pr, .rt, .ns and .ps syntax (first child,
     34// last child, parent, root, next sibling, previous sibling)
     35// it expects child, parent, etc. to exist if syntax has been used
     36// so you should test before using
     37text_t dbclass::translate_OID (const text_t &inOID, infodbclass &info)
     38{
     39  if (inOID.size() < 4) return inOID;
     40  if (findchar (inOID.begin(), inOID.end(), '.') == inOID.end()) return inOID;
     41
     42  text_t OID = inOID;
     43  text_tarray tailarray;
     44  text_t tail = substr (OID.end()-3, OID.end());
     45  if (tail == ".rt") {
     46    get_top (inOID, OID);
     47    return OID;
     48  }
     49  while (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
     50     tail == ".ns" || tail == ".ps") {
     51    tailarray.push_back(tail);
     52    OID.erase (OID.end()-3, OID.end());
     53    tail = substr (OID.end()-3, OID.end());
     54    if (tail == ".rt") {
     55      get_top (inOID, OID);
     56      return OID;
     57    }
     58  }
     59
     60  if (tailarray.empty()) return inOID;
     61  text_tarray::const_iterator begin = tailarray.begin();
     62  text_tarray::const_iterator here = tailarray.end() - 1;
     63
     64  while (here >= begin) {
     65
     66    if (*here == ".fc")
     67      get_first_child (OID, info);
     68    else if (*here == ".lc")
     69      get_last_child (OID, info);
     70    else if (*here == ".pr")
     71      OID = get_parent (OID);
     72    else if (*here == ".ns")
     73      get_next_sibling (OID, info);
     74    else if (*here == ".ps")
     75      get_previous_sibling (OID, info);
     76   
     77    if (here == begin)
     78      break;
     79    --here;
     80  }
     81
     82  return OID;
     83}
     84
     85
     86void dbclass::get_first_child (text_t &OID, infodbclass &info)
     87{
     88  text_t firstchild;
     89  if (getinfo (OID, info)) {
     90    text_t &contains = info["contains"];
     91    if (!contains.empty()) {
     92      text_t parent = OID;
     93      getdelimitstr (contains.begin(), contains.end(), ';', firstchild);
     94      if (firstchild.empty()) OID = contains;
     95      else OID = firstchild;
     96      if (*(OID.begin()) == '"') translate_parent (OID, parent);
     97    }
     98  }
     99}
     100
     101
     102void dbclass::get_last_child (text_t &OID, infodbclass &info)
     103{
     104  text_tarray children;
     105  if (getinfo (OID, info)) {
     106    text_t &contains = info["contains"];
     107    if (!contains.empty()) {
     108      text_t parent = OID;
     109      splitchar (contains.begin(), contains.end(), ';', children);
     110      OID = children.back();
     111      if (*(OID.begin()) == '"') translate_parent (OID, parent);
     112    }
     113  }
     114}
     115
     116 
     117void dbclass::get_next_sibling (text_t &OID, infodbclass &info)
     118{
     119  text_tarray siblings;
     120  text_t parent = get_parent (OID);
     121         
     122  if (getinfo (parent, info)) {
     123    text_t &contains = info["contains"];
     124    if (!contains.empty()) {
     125      splitchar (contains.begin(), contains.end(), ';', siblings);
     126      text_tarray::const_iterator here = siblings.begin();
     127      text_tarray::const_iterator end = siblings.end();
     128      text_t shrunk_OID = OID;
     129      shrink_parent (shrunk_OID);
     130      while (here != end) {
     131    if (*here == shrunk_OID && (here+1 != end)) {
     132      OID = *(here+1);
     133      if (*(OID.begin()) == '"') translate_parent (OID, parent);
     134      break;
     135    }
     136    ++here;
     137      }
     138    }
     139  }
     140}
     141
     142
     143void dbclass::get_previous_sibling (text_t &OID, infodbclass &info)
     144{
     145  text_tarray siblings;
     146  text_t parent = get_parent (OID);
     147
     148  if (getinfo (parent, info)) {
     149    text_t &contains = info["contains"];
     150    if (!contains.empty()) {
     151      splitchar (contains.begin(), contains.end(), ';', siblings);
     152      text_tarray::const_iterator here = siblings.begin();
     153      text_tarray::const_iterator end = siblings.end();
     154      text_t shrunk_OID = OID;
     155      shrink_parent (shrunk_OID);
     156      while (here != end) {
     157    if (*here == shrunk_OID && (here != siblings.begin())) {
     158      OID = *(here-1);
     159      if (*(OID.begin()) == '"') translate_parent (OID, parent);
     160      break;
     161    }
     162    ++here;
     163      }
     164    }
     165  }
     166}
  • gsdl/trunk/lib/dbclass.h

    r15585 r15598  
    6969  virtual bool setinfo(const text_t &key, const infodbclass &info) = 0;
    7070
    71   // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
    72   // it expects child, parent, etc. to exist if syntax has been used
    73   // so you should test before using
    74   virtual text_t translate_OID(const text_t &OID, infodbclass &info) = 0;
    75 
    7671  // getfirstkey and getnextkey are used for traversing the database
    7772  // no insertions or deletions should be carried out while traversing
     
    8378  virtual void deletekey(const text_t &key) = 0;
    8479
     80  // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
     81  // it expects child, parent, etc. to exist if syntax has been used
     82  // so you should test before using
     83  text_t translate_OID(const text_t &OID, infodbclass &info);
     84
    8585protected:
    8686  ostream *logout;
     87
     88  void get_first_child (text_t &OID, infodbclass &info);
     89  void get_last_child (text_t &OID, infodbclass &info);
     90  void get_next_sibling (text_t &OID, infodbclass &info);
     91  void get_previous_sibling (text_t &OID, infodbclass &info);
    8792};
    8893
  • gsdl/trunk/lib/gdbmclass.cpp

    r15557 r15598  
    285285
    286286
    287 // replaces the .fc, .lc, .pr, .rt, .ns and .ps syntax (first child,
    288 // last child, parent, root, next sibling, previous sibling)
    289 // it expects child, parent, etc. to exist if syntax has been used
    290 // so you should test before using
    291 text_t gdbmclass::translate_OID (const text_t &inOID, infodbclass &info)
    292 {
    293   if (inOID.size() < 4) return inOID;
    294   if (findchar (inOID.begin(), inOID.end(), '.') == inOID.end()) return inOID;
    295 
    296   text_t OID = inOID;
    297   text_tarray tailarray;
    298   text_t tail = substr (OID.end()-3, OID.end());
    299   if (tail == ".rt") {
    300     get_top (inOID, OID);
    301     return OID;
    302   }
    303   while (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
    304      tail == ".ns" || tail == ".ps") {
    305     tailarray.push_back(tail);
    306     OID.erase (OID.end()-3, OID.end());
    307     tail = substr (OID.end()-3, OID.end());
    308     if (tail == ".rt") {
    309       get_top (inOID, OID);
    310       return OID;
    311     }
    312   }
    313 
    314   if (tailarray.empty()) return inOID;
    315   text_tarray::const_iterator begin = tailarray.begin();
    316   text_tarray::const_iterator here = tailarray.end() - 1;
    317 
    318   while (here >= begin) {
    319 
    320     if (*here == ".fc")
    321       get_first_child (OID, info);
    322     else if (*here == ".lc")
    323       get_last_child (OID, info);
    324     else if (*here == ".pr")
    325       OID = get_parent (OID);
    326     else if (*here == ".ns")
    327       get_next_sibling (OID, info);
    328     else if (*here == ".ps")
    329       get_previous_sibling (OID, info);
    330    
    331     if (here == begin)
    332       break;
    333     --here;
    334   }
    335 
    336   return OID;
    337 }
    338 
    339 
    340 void gdbmclass::get_first_child (text_t &OID, infodbclass &info)
    341 {
    342   text_t firstchild;
    343   if (getinfo (OID, info)) {
    344     text_t &contains = info["contains"];
    345     if (!contains.empty()) {
    346       text_t parent = OID;
    347       getdelimitstr (contains.begin(), contains.end(), ';', firstchild);
    348       if (firstchild.empty()) OID = contains;
    349       else OID = firstchild;
    350       if (*(OID.begin()) == '"') translate_parent (OID, parent);
    351     }
    352   }
    353 }
    354 
    355 
    356 void gdbmclass::get_last_child (text_t &OID, infodbclass &info)
    357 {
    358   text_tarray children;
    359   if (getinfo (OID, info)) {
    360     text_t &contains = info["contains"];
    361     if (!contains.empty()) {
    362       text_t parent = OID;
    363       splitchar (contains.begin(), contains.end(), ';', children);
    364       OID = children.back();
    365       if (*(OID.begin()) == '"') translate_parent (OID, parent);
    366     }
    367   }
    368 }
    369 
    370  
    371 void gdbmclass::get_next_sibling (text_t &OID, infodbclass &info)
    372 {
    373   text_tarray siblings;
    374   text_t parent = get_parent (OID);
    375          
    376   if (getinfo (parent, info)) {
    377     text_t &contains = info["contains"];
    378     if (!contains.empty()) {
    379       splitchar (contains.begin(), contains.end(), ';', siblings);
    380       text_tarray::const_iterator here = siblings.begin();
    381       text_tarray::const_iterator end = siblings.end();
    382       text_t shrunk_OID = OID;
    383       shrink_parent (shrunk_OID);
    384       while (here != end) {
    385     if (*here == shrunk_OID && (here+1 != end)) {
    386       OID = *(here+1);
    387       if (*(OID.begin()) == '"') translate_parent (OID, parent);
    388       break;
    389     }
    390     ++here;
    391       }
    392     }
    393   }
    394 }
    395 
    396 
    397 void gdbmclass::get_previous_sibling (text_t &OID, infodbclass &info)
    398 {
    399   text_tarray siblings;
    400   text_t parent = get_parent (OID);
    401 
    402   if (getinfo (parent, info)) {
    403     text_t &contains = info["contains"];
    404     if (!contains.empty()) {
    405       splitchar (contains.begin(), contains.end(), ';', siblings);
    406       text_tarray::const_iterator here = siblings.begin();
    407       text_tarray::const_iterator end = siblings.end();
    408       text_t shrunk_OID = OID;
    409       shrink_parent (shrunk_OID);
    410       while (here != end) {
    411     if (*here == shrunk_OID && (here != siblings.begin())) {
    412       OID = *(here-1);
    413       if (*(OID.begin()) == '"') translate_parent (OID, parent);
    414       break;
    415     }
    416     ++here;
    417       }
    418     }
    419   }
    420 }
    421 
    422 
    423287// returns true on success
    424288bool gdbmclass::getinfo (const text_t& key, infodbclass &info)
  • gsdl/trunk/lib/gdbmclass.h

    r15557 r15598  
    6060  void closedatabase ();
    6161
    62   // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
    63   // it expects child, parent, etc. to exist if syntax has been used
    64   // so you should test before using
    65   text_t translate_OID (const text_t &OID, infodbclass &info);
    66 
    6762  // returns true on success
    6863  bool getinfo (const text_t& key, infodbclass &info);
     
    9287  GDBM_FILE gdbmfile;
    9388
    94   void get_first_child (text_t &OID, infodbclass &info);
    95   void get_last_child (text_t &OID, infodbclass &info);
    96   void get_next_sibling (text_t &OID, infodbclass &info);
    97   void get_previous_sibling (text_t &OID, infodbclass &info);
    98 
    99 
    10089  // returns true on success
    10190  bool getinfoline (text_t::iterator &here, text_t::iterator end,
Note: See TracChangeset for help on using the changeset viewer.