00001 
00012 #include  "tlist.h"
00013 #include  "jbxl_state.h"
00014 
00015 
00017 
00018 
00019 
00020 
00028 tList_data  init_tList_data(void)
00029 {
00030     tList_data pp;
00031 
00032     memset(&pp, 0, sizeof(tList_data));
00033     return pp;
00034 }
00035 
00036 
00037 
00053 tList_data  make_tList_data(int id, int lv, Buffer key, Buffer val, void* ptr, int sz)
00054 {
00055     tList_data pp;
00056 
00057     memset(&pp, 0, sizeof(tList_data));
00058 
00059     pp.id  = id;
00060     pp.lv  = lv;
00061     pp.sz  = sz;
00062     pp.key = dup_Buffer(key);    
00063     pp.val = dup_Buffer(val);
00064 
00065     if (sz>0) {
00066         pp.ptr = (void*)malloc(sz);
00067         if (pp.ptr!=NULL) {
00068             if (ptr!=NULL) memcpy(pp.ptr, ptr, sz);
00069             else           memset(pp.ptr, 0,   sz);
00070         }
00071     }
00072     
00073     return pp;
00074 }
00075 
00076 
00077 
00093 tList_data  make_tList_data_bystr(int id, int lv, const char* key, const char* val, void* ptr, int sz)
00094 {
00095     tList_data pp;
00096     
00097     memset(&pp, 0, sizeof(tList_data));
00098 
00099     pp.id  = id;
00100     pp.lv  = lv;
00101     pp.sz  = sz;
00102     pp.key = make_Buffer_bystr(key);    
00103     pp.val = make_Buffer_bystr(val);
00104 
00105     if (sz>0) {
00106         pp.ptr = (void*)malloc(sz);
00107         if (pp.ptr!=NULL) {
00108             if (ptr!=NULL) memcpy(pp.ptr, ptr, sz);
00109             else           memset(pp.ptr, 0,   sz);
00110         }
00111     }
00112 
00113     return pp;
00114 }
00115 
00116 
00117 
00125 void  free_tList_data(tList_data* ldat)
00126 {
00127     if (ldat==NULL) return;
00128 
00129     ldat->id = 0;
00130     ldat->lv = 0;
00131     ldat->sz = 0;
00132 
00133     free_Buffer(&(ldat->key));    
00134     free_Buffer(&(ldat->val));
00135     if (ldat->ptr!=NULL) free(ldat->ptr);
00136     del_all_tList(&(ldat->lst));
00137 
00138     ldat->key = init_Buffer();
00139     ldat->val = init_Buffer();
00140     ldat->ptr = NULL;
00141     ldat->lst = NULL;
00142 
00143     return;
00144 }
00145 
00146 
00147 
00155 void  del_tList_data(tList_data** ldat)
00156 {
00157     if (ldat==NULL || *ldat==NULL) return;
00158 
00159     free_tList_data(*ldat);
00160     *ldat = NULL;
00161 
00162     return;
00163 }
00164 
00165 
00166 
00176 tList_data  dup_tList_data(tList_data ldat)
00177 {
00178     tList_data dup;    
00179 
00180     memcpy(&dup, &ldat, sizeof(tList_data));
00181     dup.key = dup_Buffer(ldat.key);
00182     dup.val = dup_Buffer(ldat.val);
00183     if (ldat.ptr!=NULL && ldat.sz>0) {
00184         dup.ptr = (void*)malloc(ldat.sz);
00185         if (dup.ptr!=NULL) memcpy(dup.ptr, ldat.ptr, ldat.sz);
00186     }
00187     dup.lst = dup_tList(ldat.lst);
00188     
00189     return dup;
00190 }
00191 
00192 
00193 
00194 
00196 
00197 
00198 
00206 tList*  new_tList_node(void)
00207 {
00208     tList* pp;
00209 
00210     pp = (tList*)malloc(sizeof(tList));
00211     if (pp==NULL) return NULL;
00212     memset(pp, 0, sizeof(tList));
00213     pp->ldat  = init_tList_data();
00214     pp->state = JBXL_NORMAL;
00215 
00216     return pp;
00217 }
00218 
00219 
00220 
00232 tList  make_tList_node(tList_data ldat)
00233 {
00234     tList pp;
00235 
00236     memset(&pp, 0, sizeof(tList));
00237     pp.ldat  = ldat;    
00238     pp.state = JBXL_NORMAL;
00239 
00240     return pp;
00241 }
00242 
00243 
00244 
00253 tList*  free_tList_node(tList* node) 
00254 {    
00255     if (node==NULL) return NULL;
00256 
00257     free_tList_data(&(node->ldat));
00258 
00259     tList* pp = NULL;
00260     if (node->prev!=NULL) node->prev->next = node->next;
00261     if (node->next!=NULL) {
00262         node->next->prev = node->prev;
00263         pp = node->next;
00264     }
00265 
00266     return pp;
00267 }
00268 
00269 
00270 
00281 tList*  del_tList_node(tList** node)
00282 {
00283     if (node==NULL || *node==NULL) return NULL;
00284 
00285     tList* pp = free_tList_node(*node);
00286     free(*node);
00287     *node = NULL;
00288 
00289     return pp;
00290 }    
00291 
00292 
00293 
00299 tList*  dup_tList_node(tList* node)
00300 {
00301     tList* pp;
00302     int  sz;
00303 
00304     if (node==NULL) return NULL;
00305 
00306     sz = sizeof(tList);
00307     pp = (tList*)malloc(sz);
00308     if (pp==NULL) return NULL;
00309 
00310     memcpy(pp, node, sz);
00311     pp->ldat = dup_tList_data(node->ldat);
00312     pp->next = NULL;
00313     pp->prev = NULL;
00314     pp->altp = NULL;
00315     pp->yngr = NULL;
00316     pp->esis = NULL;
00317     pp->ysis = NULL;
00318 
00319     return pp;
00320 }
00321 
00322 
00323 
00334 tList*  move_tList_node(tList* pp, tList* node)
00335 {
00336     if (pp==NULL || node==NULL) return NULL;
00337     
00338     if (node->prev!=NULL) node->prev->next = node->next;
00339     if (node->next!=NULL) node->next->prev = node->prev;
00340 
00341     node->prev = pp;
00342     node->next = pp->next;
00343     if (pp->next!=NULL) pp->next->prev = node;
00344     pp->next   = node;
00345     
00346     return node;
00347 }
00348 
00349 
00350 
00356 tList*  swap_tList_node(tList* pp1, tList* pp2)
00357 {
00358     if (pp1==NULL || pp2==NULL) return NULL;
00359 
00360     tList_data swp = pp1->ldat;
00361     pp1->ldat = pp2->ldat;
00362     pp2->ldat = swp;
00363 
00364     return pp1;
00365 
00366 
00367 
00368 
00369 
00370 
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 }
00383 
00384 
00385 
00386 tList*  new_tList_anchor_node(void)
00387 {
00388     tList* pp;
00389 
00390     pp = (tList*)malloc(sizeof(tList));
00391     if (pp==NULL) return NULL;
00392     memset(pp, 0, sizeof(tList));
00393     pp->ldat    = init_tList_data();
00394     pp->ldat.id = TLIST_ANCHOR_NODE;
00395     pp->depth   = -1;
00396     pp->state   = JBXL_STATE_ANCHOR;    
00397 
00398     return pp;
00399 }
00400 
00401 
00402 
00403 tList*  del_tList_anchor_node(tList* node)
00404 {
00405     tList* pp = node;
00406 
00407     if (node!=NULL && node->ldat.id==TLIST_ANCHOR_NODE) {
00408         pp = node->next;
00409         free_tList_node(node);
00410         free(node);
00411     }
00412 
00413     return pp;
00414 }
00415 
00416 
00417 
00419 
00433 tList*  add_tList_node_bydata(tList* pp, tList_data ldat) 
00434 {
00435     tList* pt;
00436 
00437     pt = new_tList_node();
00438     pt->ldat = ldat;
00439 
00440     if (pp==NULL) return pt;
00441 
00442     pt->next = pp->next;
00443     pt->prev = pp;
00444     pp->next = pt;
00445     if (pt->next!=NULL) (pt->next)->prev = pt;    
00446 
00447     return pt;
00448 }
00449 
00450 
00451 
00470 tList*  add_tList_node_bystr(tList* pp, int id, int lv, const char* key, const char* val, void* ptr, int sz) 
00471 {
00472     tList* pt;
00473     tList_data ldat;
00474 
00475     ldat = make_tList_data_bystr(id, lv, key, val, ptr, sz);
00476     pt   = add_tList_node_bydata(pp, ldat);
00477 
00478     return pt;
00479 }
00480 
00481 
00482 
00501 tList*  add_tList_node_byBuffer(tList* pp, int id, int lv, Buffer key, Buffer val, void* ptr, int sz) 
00502 {
00503     tList* pt;
00504     tList_data ldat;
00505 
00506     ldat = make_tList_data(id, lv, key, val, ptr, sz);
00507     pt   = add_tList_node_bydata(pp, ldat);
00508 
00509     return pt;
00510 }
00511 
00512 
00513 
00523 void  set_tList_node_bydata(tList* node, tList_data dat)
00524 {
00525     if (node==NULL) return;
00526 
00527     free_tList_data(&(node->ldat));
00528     node->ldat = dat;
00529 }
00530 
00531 
00532 
00547 void  set_tList_node_bystr(tList* pp, int id, int lv, const char* key, const char* val, void* ptr, int sz)
00548 {
00549     if (pp==NULL) return;
00550 
00551     pp->ldat.id = id;
00552     pp->ldat.lv = lv;
00553     pp->ldat.sz = sz;
00554 
00555     if (key!=NULL) {
00556         free_Buffer(&(pp->ldat.key));
00557         pp->ldat.key = make_Buffer_bystr(key);
00558     }
00559     if (val!=NULL) {
00560         free_Buffer(&(pp->ldat.val));
00561         pp->ldat.val = make_Buffer_bystr(val);
00562     }
00563 
00564     if (sz>0 && ptr!=NULL) {
00565         if (pp->ldat.ptr!=NULL) free(pp->ldat.ptr);
00566         pp->ldat.ptr = (void*)malloc(sz);
00567         if (pp->ldat.ptr!=NULL) memcpy(pp->ldat.ptr, ptr, sz);
00568     }
00569 }
00570 
00571 
00572 
00587 void  set_tList_node_byBuffer(tList* pp, int id, int lv, Buffer key, Buffer val, void* ptr, int sz)
00588 {
00589     if (pp==NULL) return;
00590 
00591     pp->ldat.id = id;
00592     pp->ldat.lv = lv;
00593 
00594     if (pp->ldat.key.buf!=NULL) {
00595         free_Buffer(&(pp->ldat.key));
00596         pp->ldat.key = dup_Buffer(key);
00597     }
00598 
00599     if (pp->ldat.val.buf!=NULL) {
00600         free_Buffer(&(pp->ldat.val));
00601         pp->ldat.val = dup_Buffer(val);
00602     }
00603 
00604     if (sz>0 && ptr!=NULL) {
00605         if (pp->ldat.ptr!=NULL) free(pp->ldat.ptr);
00606         pp->ldat.ptr = (void*)malloc(sz);
00607         if (pp->ldat.ptr!=NULL) memcpy(pp->ldat.ptr, ptr, sz);
00608     }
00609 }
00610 
00611 
00612 
00622 tList*  update_tList_node(tList* pp, tList* pt)
00623 {
00624     tList* pm;
00625     tList_data ldat;
00626 
00627     if (pt==NULL) return pp;
00628     
00629     ldat = pp->ldat;
00630     pm = update_tList_node_byBuffer(pp, ldat.id, ldat.lv, ldat.key, ldat.val, ldat.ptr, ldat.sz);
00631     
00632     return pm;
00633 }
00634 
00635 
00636 
00649 tList*  update_tList_node_bydata(tList* pp, char* srch, tList_data ldat)
00650 {
00651     tList* pm = NULL;
00652 
00653     if (pp==NULL || srch==NULL) return NULL;
00654 
00655     pm = strncmp_tList(pp, srch, 0, 1);
00656     if (pm!=NULL) set_tList_node_bydata(pm, ldat);
00657     else {
00658         pm = find_tList_end(pp);
00659         pm = add_tList_node_bydata(pm, ldat);
00660     }
00661     
00662     return pm;
00663 }
00664 
00665 
00666 
00683 tList*  update_tList_node_bystr(tList* pp, int id, int lv, const char* key, const char* val, void* ptr, int sz)
00684 {
00685     tList* pm = NULL;
00686 
00687     if (pp==NULL || key==NULL) return NULL;
00688 
00689     pm = strncmp_tList(pp, key, 0, 1);
00690     if (pm!=NULL) {
00691         set_tList_node_bystr(pm, id, lv, NULL, val, ptr, sz);
00692     }
00693     else {
00694         pm = find_tList_end(pp);
00695         pm = add_tList_node_bystr(pm, id, lv, key, val, ptr, sz);
00696     }
00697     
00698     return pm;
00699 }
00700 
00701 
00702 
00719 tList*  update_tList_node_byBuffer(tList* pp, int id, int lv, Buffer key, Buffer val, void* ptr, int sz)
00720 {
00721     tList* pm = NULL;
00722 
00723     if (pp==NULL || key.buf==NULL) return NULL;
00724 
00725     pm = strncmp_tList(pp, (char*)key.buf, 0, 1);
00726     if (pm!=NULL) set_tList_node_byBuffer(pm, id, lv, key, val, ptr, sz);
00727     else {
00728         pm = find_tList_end(pp);
00729         pm = add_tList_node_byBuffer(pm, id, lv, key, val, ptr, sz);
00730     }
00731     
00732     return pm;
00733 }
00734 
00735 
00736 
00737 
00739 
00740 
00741 
00752 tList*  del_tList(tList** pp) 
00753 {    
00754     tList* pt;
00755     tList* pm;
00756     tList* pw;
00757 
00758     if (pp==NULL || *pp==NULL) return NULL;
00759 
00760     pt = (*pp)->prev;
00761     if (pt!=NULL) pt->next = NULL;
00762 
00763     pm = *pp;
00764     while (pm!=NULL) {
00765         pw = pm;
00766         pm = pm->next;
00767         free_tList_data(&(pw->ldat));
00768         free(pw);
00769     }
00770     *pp = NULL;
00771 
00772     return pt;
00773 }
00774 
00775 
00786 void  del_all_tList(tList** pp)
00787 {
00788     tList* pm;
00789     tList* pv;
00790 
00791     if (pp==NULL || *pp==NULL) return;
00792 
00793     pm = *pp;
00794     pv = (*pp)->prev;
00795 
00796     do {
00797         pm = del_tList_node(&pm);
00798     } while (pm!=NULL);
00799 
00800     pm = pv;
00801     while (pm!=NULL) {
00802         pv = pm->prev;
00803         del_tList_node(&pm);
00804         pm = pv;
00805     }
00806 
00807     *pp = NULL;
00808 }
00809 
00810 
00811 
00823 int  del_tList_key(tList** pl, const char* key, int no)
00824 {       
00825     int dl = 0;
00826     int nn = 0;
00827     if (no<0) no = 0;
00828 
00829     tList* pp = *pl;
00830 
00831     while (pp!=NULL) {
00832         if (ex_strncasecmp((char*)(pp->ldat).key.buf, key, 0)) {
00833             nn++;
00834             if (no==0 || no==nn) {
00835                 if (pp->prev==NULL) *pl = pp->next;
00836                 pp = del_tList_node(&pp);    
00837                 dl++;
00838                 if (no!=0) break;
00839             }
00840             else {
00841                 pp = pp->next;
00842             }
00843         }
00844         else {
00845             pp = pp->next;
00846         }
00847     }
00848 
00849     return dl;
00850 }
00851 
00852 
00853 
00862 tList*  dup_tList(tList* pp)
00863 {
00864     tList* pt;
00865     tList* pl;
00866     tList* tt;
00867 
00868     if (pp==NULL) return NULL;
00869 
00870     pt = pl = dup_tList_node(pp);
00871     pp = pp->next;
00872 
00873     while(pp!=NULL) {
00874         tt = dup_tList_node(pp);
00875         pl = insert_tList(pl, tt);
00876         pp = pp->next;
00877     }
00878 
00879     return pt;
00880 }
00881 
00882 
00883 
00897 tList*  add_tList_end(tList* pp, tList* pt)
00898 {
00899     if (pt==NULL) return pp;
00900     if (pp==NULL) return pt;
00901 
00902     tList* pe = find_tList_end(pp);
00903     pe->next = pt;
00904     pt->prev = pe;
00905 
00906     return pp;
00907 }
00908 
00909 
00910 
00924 tList*  insert_tList(tList* pp, tList* pt)
00925 {
00926     tList* pe;
00927     
00928     if (pt==NULL) return pp;
00929     if (pp==NULL) return pt;
00930 
00931     pe = find_tList_end(pt);
00932     if (pp->next!=NULL) pp->next->prev = pe;
00933     pe->next = pp->next;
00934     pp->next = pt;
00935     pt->prev = pp;
00936     
00937     return pt;
00938 }
00939 
00940 
00941 
00950 void  print_tList(FILE* fp, tList* pp)
00951 {
00952     if (fp==NULL) fp = stderr;
00953 
00954     if (pp!=NULL) {
00955         while(pp!=NULL) {
00956             tList_data ld = pp->ldat;
00957             fprintf(fp, "[%d] [%d] [%s] [%s]\n", ld.id, ld.lv, ld.key.buf, ld.val.buf);
00958             
00959             pp = pp->next;
00960         }
00961     }
00962     else {
00963         fprintf(fp, "(List is NULL)\n");
00964     }
00965     return;
00966 }
00967 
00968 
00974 void  dump_tList(FILE* fp, tList* pp)
00975 {
00976     if (fp==NULL) fp = stderr;
00977 
00978     if (pp!=NULL) {
00979         while(pp!=NULL) {
00980             tList_data ld = pp->ldat;
00981             fprintf(fp, "[%d] [%d] [%s] [%d]\n", ld.id, ld.lv, ld.key.buf, ld.val.vldsz);
00982             fdump(fp, (unsigned char*)ld.val.buf, ld.val.vldsz);
00983             pp = pp->next;
00984         }
00985     }
00986     else {
00987         fprintf(fp, "(List is NULL)\n");
00988     }
00989     return;
00990 }
00991 
00992 
00993 
01002 int  count_tList(tList* pp)
01003 {
01004     int cnt = 0;
01005 
01006     while (pp!=NULL) {
01007         cnt++;
01008         pp = pp->next;
01009     }    
01010     return cnt;
01011 }
01012 
01013 
01014 
01015 
01017 
01026 tList*  find_tList_top(tList* pl)
01027 {
01028     if (pl==NULL) return NULL;
01029 
01030     while (pl->prev!=NULL) pl = pl->prev;
01031     return pl;
01032 }
01033 
01034 
01035 
01048 tList*  find_tList_end(tList* pl)
01049 {
01050     if (pl==NULL) return NULL;
01051 
01052     while (pl->next!=NULL) pl = pl->next;
01053     return pl;
01054 }
01055 
01056 
01057 
01058 
01060 
01061 
01062 
01082 tList*  strncmp_tList(tList* pl, const char* key, int len, int no)
01083 {
01084     int nn = 0;
01085 
01086     if (pl==NULL || key==NULL) return NULL;
01087     if (len<=-3) return NULL;
01088     if (no<=0) no = 1;
01089 
01090     while (pl!=NULL) {
01091         if (ex_strncmp((char*)pl->ldat.key.buf, key, len)) {
01092             nn++;
01093             if (no==nn) return pl;
01094         }
01095         pl = pl->next;
01096     }
01097     return NULL;
01098 }
01099 
01100 
01101 
01121 tList*  strncasecmp_tList(tList* pl, const char* key, int len, int no)
01122 {
01123     int nn = 0;
01124 
01125     if (pl==NULL || key==NULL) return NULL;
01126     if (len<=-3) return NULL;
01127     if (no<=0) no = 1;
01128 
01129     while (pl!=NULL) {
01130         if (ex_strncasecmp((char*)(pl->ldat).key.buf, key, len)) {
01131             nn++;
01132             if (no==nn) return pl;
01133         }
01134         pl = pl->next;
01135     }
01136     return NULL;
01137 }
01138 
01139 
01140 
01160 tList*  strnrvscmp_tList(tList* pl, const char* key, int len, int no)
01161 {
01162     int nn = 0;
01163 
01164     if (pl==NULL || key==NULL) return NULL;
01165     if (len<=-3) return NULL;
01166     if (no<=0) no = 1;
01167 
01168     while (pl!=NULL) {
01169         if (ex_strnrvscmp((char*)(pl->ldat).key.buf, key, len)) {
01170             nn++;
01171             if (no==nn) return pl;
01172         }
01173         pl = pl->next;
01174     }
01175     return NULL;
01176 }
01177 
01178 
01179 
01199 tList*  strncaservscmp_tList(tList* pl, const char* key, int len, int no)
01200 {
01201     int nn = 0;
01202 
01203     if (pl==NULL || key==NULL) return NULL;
01204     if (len<=-3) return NULL;
01205     if (no<=0) no = 1;
01206 
01207     while (pl!=NULL) {
01208         if (ex_strncaservscmp((char*)(pl->ldat).key.buf, key, len)) {
01209             nn++;
01210             if (no==nn) return pl;
01211         }
01212         pl = pl->next;
01213     }
01214     return NULL;
01215 }
01216 
01217 
01218 
01236 tList*  strstr_tList(tList* pl, const char* key, int len, int no)
01237 {
01238     int nn = 0;
01239 
01240     if (pl==NULL || key==NULL) return NULL;
01241     if (no<=0) no = 1;
01242 
01243     while (pl!=NULL) {
01244         if (len>=0) {
01245             if (strstr((char*)(pl->ldat).key.buf, key)!=NULL) {
01246                 nn++;
01247                 if (no==nn) return pl;
01248             }
01249         }
01250         else if (len<0) {
01251             if (strstr(key, (char*)(pl->ldat).key.buf)!=NULL) {
01252                 nn++;
01253                 if (no==nn) return pl;
01254             }
01255         }
01256 
01257         pl = pl->next;
01258     }
01259     return NULL;
01260 }
01261 
01262 
01263 
01281 tList*  strstrcase_tList(tList* pl, const char* key, int len, int no)
01282 {
01283     int nn = 0;
01284 
01285     if (pl==NULL || key==NULL) return NULL;
01286     if (no<=0) no = 1;
01287 
01288     while (pl!=NULL) {
01289         if (len>=0) {
01290             if (strstrcase((char*)(pl->ldat).key.buf, key)!=NULL) {
01291                 nn++;
01292                 if (no==nn) return pl;
01293             }
01294         }
01295         else if (len<0) {
01296             if (strstrcase(key, (char*)(pl->ldat).key.buf)!=NULL) {
01297                 nn++;
01298                 if (no==nn) return pl;
01299             }
01300         }
01301 
01302         pl = pl->next;
01303     }
01304     return NULL;
01305 }
01306 
01307 
01308 
01309 
01311 
01312 
01313 
01331 tList*  strncmp_back_tList(tList* pl, const char* key, int len, int no)
01332 {
01333     int nn = 0;
01334 
01335     if (pl==NULL || key==NULL) return NULL;
01336     if (len<=-3) return NULL;
01337     if (no<=0) no = 1;
01338 
01339     pl = find_tList_end(pl);
01340     
01341     while (pl!=NULL) {
01342         if (ex_strncmp((char*)(pl->ldat).key.buf, key, len)) {
01343             nn++;
01344             if (no==nn) return pl;
01345         }
01346         pl = pl->prev;
01347     }
01348     return NULL;
01349 }
01350 
01351 
01352 
01370 tList*  strncasecmp_back_tList(tList* pl, const char* key, int len, int no)
01371 {
01372     int nn = 0;
01373 
01374     if (pl==NULL || key==NULL) return NULL;
01375     if (len<=-3) return NULL;
01376     if (no<=0) no = 1;
01377 
01378     pl = find_tList_end(pl);
01379 
01380     while (pl!=NULL) {
01381         if (ex_strncasecmp((char*)(pl->ldat).key.buf, key, len)) {
01382             nn++;
01383             if (no==nn) return pl;
01384         }
01385         pl = pl->prev;
01386     }
01387     return NULL;
01388 }
01389 
01390 
01391 
01409 tList*  strnrvscmp_back_tList(tList* pl, const char* key, int len, int no)
01410 {
01411     int nn = 0;
01412 
01413     if (pl==NULL || key==NULL) return NULL;
01414     if (len<=-3) return NULL;
01415     if (no<=0) no = 1;
01416 
01417     pl = find_tList_end(pl);
01418 
01419     while (pl!=NULL) {
01420         if (ex_strnrvscmp((char*)(pl->ldat).key.buf, key, len)) {
01421             nn++;
01422             if (no==nn) return pl;
01423         }
01424         pl = pl->prev;
01425     }
01426     return NULL;
01427 }
01428 
01429 
01430 
01448 tList*  strncaservscmp_back_tList(tList* pl, const char* key, int len, int no)
01449 {
01450     int nn = 0;
01451 
01452     if (pl==NULL || key==NULL) return NULL;
01453     if (len<=-3) return NULL;
01454     if (no<=0) no = 1;
01455 
01456     pl = find_tList_end(pl);
01457 
01458     while (pl!=NULL) {
01459         if (ex_strncaservscmp((char*)(pl->ldat).key.buf, key, len)) {
01460             nn++;
01461             if (no==nn) return pl;
01462         }
01463         pl = pl->prev;
01464     }
01465     return NULL;
01466 }
01467 
01468 
01469 
01487 tList*  strstr_back_tList(tList* pl, const char* key, int len, int no)
01488 {
01489     int nn = 0;
01490 
01491     if (pl==NULL || key==NULL) return NULL;
01492     if (no<=0) no = 1;
01493 
01494     pl = find_tList_end(pl);
01495 
01496     while (pl!=NULL) {
01497         if (len>=0) {
01498             if (strstr((char*)(pl->ldat).key.buf, key)!=NULL) {
01499                 nn++;
01500                 if (no==nn) return pl;
01501             }
01502         }
01503         else if (len<0) {
01504             if (strstr(key, (char*)(pl->ldat).key.buf)!=NULL) {
01505                 nn++;
01506                 if (no==nn) return pl;
01507             }
01508         }
01509 
01510         pl = pl->prev;
01511     }
01512     return NULL;
01513 }
01514 
01515 
01516 
01534 tList*  strstrcase_back_tList(tList* pl, const char* key, int len, int no)
01535 {
01536     int nn = 0;
01537 
01538     if (pl==NULL || key==NULL) return NULL;
01539     if (no<=0) no = 1;
01540 
01541     pl = find_tList_end(pl);
01542 
01543     while (pl!=NULL) {
01544         if (len>=0) {
01545             if (strstrcase((char*)(pl->ldat).key.buf, key)!=NULL) {
01546                 nn++;
01547                 if (no==nn) return pl;
01548             }
01549         }
01550         else if (len<0) {
01551             if (strstrcase(key, (char*)(pl->ldat).key.buf)!=NULL) {
01552                 nn++;
01553                 if (no==nn) return pl;
01554             }
01555         }
01556 
01557         pl = pl->prev;
01558     }
01559     return NULL;
01560 }
01561 
01562 
01563 
01564 
01566 
01579 tList*  search_id_tList(tList* pl, int id, int no) 
01580 {
01581     int nn = 0;
01582 
01583     if (pl==NULL) return NULL;
01584     if (no<=0) no = 1;
01585 
01586     while (pl!=NULL) {
01587         if (pl->ldat.id == id) {
01588             nn++;
01589             if (no==nn) return pl;
01590         }
01591         pl = pl->next;
01592     }
01593     return NULL;
01594 }
01595 
01596 
01597 
01610 tList*  search_key_tList(tList* pl, const char* key, int no) 
01611 {
01612     tList* pp;
01613     if (pl==NULL || key==NULL) return NULL;
01614     if (no<=0) no = 1;
01615     
01616     pp = strncasecmp_tList(pl, key, 0, no);        
01617     
01618     return pp;
01619 }
01620 
01621 
01622 
01635 Buffer  buffer_key_tList(tList* list, const char* key, int no) 
01636 {
01637     tList* pp;
01638     Buffer buf;
01639 
01640     buf = init_Buffer();
01641     if (list==NULL || key==NULL) return buf;
01642     if (no<=0) no = 1;
01643     
01644     pp = strncasecmp_tList(list, key, 0, no);        
01645     if (pp!=NULL) {
01646         buf = dup_Buffer(pp->ldat.val);
01647     }
01648     
01649     return buf;
01650 }
01651 
01652 
01653 
01667 Buffer  buffer_key_value_tList(tList* list, const char* key, char* data, int no) 
01668 {
01669     tList* pp;
01670     Buffer buf;
01671     char*  str;
01672     int    len;
01673 
01674     buf = init_Buffer();
01675     if (list==NULL || key==NULL) return buf;
01676     if (no<=0) no = 1;
01677 
01678     if (data==NULL) {
01679         buf = buffer_key_tList(list, key, no);
01680         return buf;
01681     }
01682 
01683     buf = init_Buffer();
01684     len = (int)strlen(data); 
01685     
01686     pp = strncasecmp_tList(list, key, 0, no);
01687     if (pp!=NULL) {
01688         str = (char*)pp->ldat.val.buf;
01689         if (str!=NULL && !strncasecmp(str, data, len)) {
01690             buf = make_Buffer_bystr(str);
01691             return buf;
01692         }
01693     }
01694 
01695     return buf;
01696 }
01697 
01698 
01699 
01700 
01701 
01702 
01703 
01704 
01705 
01706 
01707 
01708 
01709 
01710 
01711 
01712 
01713 
01714 
01715 
01716 
01717 int  set_value_tList(tList* list, const char* key, int no, const char* value, int add_mode)
01718 {
01719     int  cn = 0;
01720     tList* pm;
01721 
01722     if (list==NULL || key==NULL || value==NULL) return JBXL_ARGS_ERROR;
01723 
01724     if (no>0) {
01725         pm = strncasecmp_tList(list, key, 0, no);
01726         if (pm!=NULL) {
01727             int rep = set_value_tList_node(pm, value);
01728             if (rep) cn = 1;
01729         }
01730     }
01731     else {        
01732         int nn = 1;
01733         cn = 0;
01734         pm = strncasecmp_tList(list, key, 0, nn);
01735         while (pm!=NULL) {
01736             int rep = set_value_tList_node(pm, value);
01737             if (rep) cn++;
01738             pm = strncasecmp_tList(list, key, 0, ++nn);
01739         }
01740     }
01741 
01742     
01743     if (add_mode==ON && cn==0) {
01744         add_tList_node_str(list, key, value);
01745     }
01746 
01747     return cn;
01748 }
01749 
01750 
01751 
01757 int  set_value_tList_node(tList* lp, const char* value)
01758 {
01759     if (lp==NULL || value==NULL) return FALSE;
01760     
01761     Buffer buf = make_Buffer_bystr(value);
01762     free_Buffer(&lp->ldat.val);
01763     lp->ldat.val = buf;
01764 
01765     return TRUE;
01766 }
01767 
01768 
01769 
01786 int  replace_value_tList(tList* list, const char* key, int no, const char* srcval, char* value)
01787 {
01788     int  cn = 0;
01789     tList* pm;
01790 
01791     if (list==NULL || key==NULL || value==NULL) return JBXL_ARGS_ERROR;
01792     if (srcval==NULL) {
01793         return set_value_tList(list, key, no, value, OFF);
01794     }
01795 
01796     if (no>0) {
01797         pm = strncasecmp_tList(list, key, 0, no);
01798         if (pm!=NULL) {
01799             int rep = replace_value_tList_node(pm, srcval, value);
01800             if (rep) cn = 1;
01801         }
01802     }
01803     else {      
01804         int nn = 1;
01805         cn = 0;
01806         pm = strncasecmp_tList(list, key, 0, nn);
01807         while (pm!=NULL) {
01808             int rep = replace_value_tList_node(pm, srcval, value);
01809             if (rep) cn++;
01810             pm = strncasecmp_tList(list, key, 0, ++nn);
01811         }
01812     }
01813     
01814     return cn;
01815 }
01816 
01817 
01818 
01824 int  replace_value_tList_node(tList* lp, const char* srcval, const char* value)
01825 {
01826     if (lp==NULL || value==NULL) return FALSE;
01827     if (srcval==NULL) {
01828         return set_value_tList_node(lp, value);
01829     }
01830 
01831     Buffer buf = replace_sBuffer(lp->ldat.val, srcval, value);
01832     free_Buffer(&lp->ldat.val);
01833     lp->ldat.val = buf;
01834     
01835     return TRUE;
01836 }
01837     
01838 
01839 
01851 tList*  awk_tList(char* str, char cc)
01852 {
01853     int  nn = 1;
01854     char*  item;
01855     tList* lp = NULL;
01856 
01857     if (str==NULL) return NULL;
01858 
01859     lp = add_tList_node_bystr(NULL, 1, 0, NULL, NULL, NULL, 0);
01860 
01861     
01862     item = awk(str, cc, nn);
01863     if (item==NULL) return lp;
01864     lp->ldat.key = make_Buffer_bystr(item);
01865 
01866     
01867     item = awk(str, cc, ++nn);
01868     while (item!=NULL) {
01869         lp = add_tList_node_bystr(lp, nn, 0, item, NULL, NULL, 0);
01870         free(item);
01871         item = awk(str, cc, ++nn);
01872     }
01873 
01874     if (lp!=NULL) lp = find_tList_top(lp);
01875     return lp;
01876 }
01877 
01878 
01879 
01891 tList*  cawk_tList(char* str, char cc)
01892 {
01893     int  nn = 1;
01894     char*  item;
01895     tList* lp = NULL;
01896 
01897     if (str==NULL) return NULL;
01898 
01899     lp = add_tList_node_bystr(NULL, 1, 0, NULL, NULL, NULL, 0);
01900 
01901     
01902     item = cawk(str, cc, nn);
01903     if (item==NULL) return lp;
01904     lp->ldat.key = make_Buffer_bystr(item);
01905 
01906     
01907     item = cawk(str, cc, ++nn);
01908     while (item!=NULL) {
01909         lp = add_tList_node_bystr(lp, nn, 0, item, NULL, NULL, 0);
01910         free(item);
01911         item = cawk(str, cc, ++nn);
01912     }
01913 
01914     if (lp!=NULL) lp = find_tList_top(lp);
01915     return lp;
01916 }
01917 
01918 
01919 
01931 tList*  awk_Buffer_tList(Buffer buf, char cc)
01932 {
01933     int  nn = 1;
01934     Buffer item;
01935     tList* lp = NULL;
01936 
01937     if (buf.buf==NULL) return NULL;
01938 
01939     item = awk_Buffer(buf, cc, nn);
01940     while (item.buf!=NULL) {
01941         lp = add_tList_node_bystr(lp, nn, 0, (char*)item.buf, NULL, NULL, 0);
01942         free_Buffer(&item);
01943         item = awk_Buffer(buf, cc, ++nn);
01944     }
01945 
01946     if (lp!=NULL) lp = find_tList_top(lp);
01947     return lp;
01948 }
01949 
01950 
01951 
01963 tList*  cawk_Buffer_tList(Buffer buf, char cc)
01964 {
01965     int  nn = 1;
01966     Buffer item;
01967     tList* lp = NULL;
01968 
01969     if (buf.buf==NULL) return NULL;
01970 
01971     item = cawk_Buffer(buf, cc, nn);
01972     while (item.buf!=NULL) {
01973         lp = add_tList_node_bystr(lp, nn, 0, (char*)item.buf, NULL, NULL, 0);
01974         free_Buffer(&item);
01975         item = cawk_Buffer(buf, cc, ++nn);
01976     }
01977 
01978     if (lp!=NULL) lp = find_tList_top(lp);
01979     return lp;
01980 
01981 }
01982 
01983 
01990 char*  get_str_join_tList(tList* lp, const char* deli)
01991 {
01992     Buffer buf = get_Buffer_join_tList(lp, deli);
01993     return (char*)buf.buf;
01994 }
01995 
01996 
02002 Buffer  get_Buffer_join_tList(tList* lp, const char* deli)
02003 {
02004     Buffer buf;
02005     
02006     buf = init_Buffer();
02007     if (lp==NULL) return buf;
02008 
02009     buf = make_Buffer(LBUF);
02010 
02011     if (lp!=NULL && lp->ldat.key.buf!=NULL) {
02012         cat_s2Buffer((char*)lp->ldat.key.buf, &buf);
02013         lp = lp->next;
02014 
02015         while (lp!=NULL && lp->ldat.key.buf!=NULL) {
02016             if (deli!=NULL) cat_s2Buffer(deli, &buf);
02017             cat_s2Buffer((char*)lp->ldat.key.buf, &buf);
02018             lp = lp->next;
02019         }
02020     }
02021 
02022     return buf;
02023 }
02024 
02025 
02026 
02027 
02029 
02030 
02031 
02044 char*  get_str_param_tList(tList* lt, const char* key, const char* dflt)
02045 {
02046     Buffer buf;
02047 
02048     buf = buffer_key_tList(lt, key, 1);
02049     if (buf.buf==NULL) buf = make_Buffer_bystr(dflt);
02050         
02051     return (char*)buf.buf;
02052 }
02053 
02054 
02066 int  get_int_param_tList(tList* lt, const char* key, int dflt)
02067 {
02068     Buffer buf;
02069 
02070     buf = buffer_key_tList(lt, key, 1);
02071     if (buf.buf!=NULL) {
02072         int ret = atoi((char*)buf.buf);
02073         free_Buffer(&buf);
02074         return ret;
02075     }
02076     return dflt;
02077 }
02078 
02079 
02091 double  get_double_param_tList(tList* lt, const char* key, double dflt)
02092 {
02093     Buffer buf;
02094 
02095     buf = buffer_key_tList(lt, key, 1);
02096     if (buf.buf!=NULL) {
02097         double ret = atof((char*)buf.buf);
02098         free_Buffer(&buf);
02099         return ret;
02100     }
02101     return dflt;
02102 }
02103 
02104 
02116 float  get_float_param_tList(tList* lt, const char* key, float dflt)
02117 {
02118     Buffer buf;
02119 
02120     buf = buffer_key_tList(lt, key, 1);
02121     if (buf.buf!=NULL) {
02122         float ret = (float)atof((char*)buf.buf);
02123         free_Buffer(&buf);
02124         return ret;
02125     }
02126     return dflt;
02127 }
02128 
02129 
02141 int  get_bool_param_tList(tList* lt, const char* key, int dflt)
02142 {
02143     int  ret = dflt;
02144     char* val = NULL;
02145 
02146     if (dflt) val = get_str_param_tList(lt, key, "true");
02147     else      val = get_str_param_tList(lt, key, "false");
02148 
02149     if (val!=NULL) {
02150         if (!strcasecmp("true", val)) ret = 1;       
02151         else ret = 0;                                
02152         free(val);
02153     }
02154 
02155     return ret;
02156 }
02157 
02158 
02159 
02160 
02162 
02163 
02164 
02178 tList*  read_tList_file(const char* fname, int mode) 
02179 {
02180     tList* lp = NULL;
02181     FILE* fp;
02182 
02183     fp = fopen(fname, "rb");
02184     if (fp!=NULL) {
02185         lp = read_tList_fp(fp, mode);
02186         fclose(fp);
02187     }
02188 
02189     return lp;
02190 }
02191 
02192 
02193 
02210 tList*  read_tList_fp(FILE* fp, int mode) 
02211 {
02212     char    val[LBUF+1];
02213     char*   str;
02214     tList*  lp = NULL;
02215     tList*  lt = NULL;
02216 
02217     if (fp==NULL) return NULL;
02218 
02219     str = fgets(val, LBUF, fp);     
02220     while (!feof(fp)) {
02221         if (mode>0) {
02222             if (mode>1) {
02223                 int i;
02224                 for (i=0; i<(int)strlen(val); i++) {
02225                     if (val[i]=='#') {
02226                         val[i] = '\0';
02227                         break;
02228                     }
02229                     if (i>=LBUF) break;
02230                 }
02231             }
02232             str = pack_char(val, ' ');
02233         }
02234         else {
02235             str = (char*)malloc(LBUF+1);
02236             if (str!=NULL) strncpy(val, str, LBUF);
02237         }
02238 
02239         if (str!=NULL) {
02240             if (strlen(str)>0) {    
02241                 if (mode==0 || str[0]!='#') {
02242                     lt = add_tList_node_str(lt, str, NULL);
02243                     if (lp==NULL) lp = lt;
02244                 }
02245             }
02246             free(str);
02247         }
02248         str = fgets(val, LBUF, fp);     
02249     }
02250     
02251     return lp;
02252 }
02253 
02254 
02255 
02268 tList*  read_index_tList_file(const char* fname, char deli) 
02269 {
02270     tList* lp = NULL;
02271     FILE* fp;
02272 
02273     fp = fopen(fname, "rb");
02274     if (fp!=NULL) {
02275         lp = read_index_tList_fp(fp, deli);
02276         fclose(fp);
02277     }
02278     return lp;
02279 }
02280 
02281 
02282 
02295 tList*  read_index_tList_fp(FILE* fp, char deli)
02296 {
02297     Buffer key, val;
02298     Buffer fst, snd;
02299     tList* pl;
02300     tList* pp;
02301     tList* lt = NULL;
02302 
02303     pp = pl = read_tList_fp(fp, 1);
02304     while (pp!=NULL) {
02305         fst = awk_Buffer(pp->ldat.key, deli, 1);
02306         snd = awk_Buffer(pp->ldat.key, deli, 2);
02307         key = pack_Buffer(fst, ' ');
02308         val = pack_Buffer(snd, ' ');
02309         if (lt==NULL) lt = add_tList_node_byBuffer(NULL, 0, 0, key, val, NULL, 0);
02310         else               add_tList_node_byBuffer(lt,   0, 0, key, val, NULL, 0);
02311     
02312         free_Buffer(&key);
02313         free_Buffer(&val);
02314         free_Buffer(&fst);
02315         free_Buffer(&snd);
02316         
02317         pp = pp->next;
02318     }
02319     del_all_tList(&pl);
02320 
02321     return lt;
02322 }
02323 
02324 
02325 
02334 tList*  read_Buffer_tList_file(const char* fname) 
02335 {
02336     tList* lp = NULL;
02337     FILE* fp;
02338 
02339     fp = fopen(fname, "rb");
02340     if (fp!=NULL) {
02341         lp = read_Buffer_tList_fp(fp);
02342         fclose(fp);
02343     }
02344     return lp;
02345 }
02346 
02347 
02348 
02357 tList*  read_Buffer_tList_fp(FILE* fp) 
02358 {
02359     int  cc;
02360     tList*  lp = NULL;
02361     tList*  lt = NULL;
02362     Buffer key, val;
02363 
02364     if (fp==NULL) return NULL;
02365 
02366     cc = read_Buffer2_fp(&key, &val, fp);
02367     while (!feof(fp) && cc) {
02368         lt = add_tList_node_Buffer(lt, key, val);
02369         if (lp==NULL) lp = lt;
02370         free_Buffer(&key);    
02371         free_Buffer(&val);    
02372         cc = read_Buffer2_fp(&key, &val, fp);
02373     }
02374     
02375     free_Buffer(&key);    
02376     free_Buffer(&val);    
02377     return lp;
02378 }
02379 
02380 
02381 
02394 int  save_Buffer_tList_file(const char* fname, tList* lp) 
02395 {
02396     int  ret=FALSE;
02397     FILE* fp;
02398 
02399     fp = fopen(fname, "ab");
02400     if (fp!=NULL) {
02401         ret = save_Buffer_tList_fp(fp, lp);
02402         fclose(fp);
02403     }
02404     return ret;
02405 }
02406 
02407 
02408 
02421 int  save_Buffer_tList_fp(FILE* fp, tList* lp) 
02422 {
02423     int cc=TRUE;
02424 
02425     if (fp==NULL) return FALSE;
02426 
02427     while (lp!=NULL && cc) {
02428         cc = save_Buffer2_fp(lp->ldat.key, lp->ldat.val, fp);    
02429         lp = lp->next;
02430     }
02431 
02432     if (!cc) return FALSE;
02433     return TRUE;
02434 }
02435