Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / mbk / mbk_vector.c
1 /****************************************************************************/
2 /* */
3 /* Chaine de CAO & VLSI AVERTEC */
4 /* */
5 /* Produit : AVERTEC global tools */
6 /* Fichier : mbk_vector.c */
7 /* */
8 /* © copyright 2000 AVERTEC */
9 /* Tous droits reserves */
10 /* */
11 /* Auteur(s) : Antony PINTO */
12 /* */
13 /****************************************************************************/
14
15 #include <string.h>
16 #include "mbk_vector.h"
17 #include AVT_H
18
19 static int MBV_ELT_SIZE = 0;
20 static AdvancedNameAllocator *MBV_NAME_ALLOCATOR = NULL;
21 static char *MBV_NO_CORRESP = 0;
22
23 static inline void addAtIndex(ht *tbl, char *key, char *corresp, int index);
24 static inline mbv_tab *newmbv_tab(ht *tbl, char *key, int left, int right);
25 static inline long getElt(mbv_tab *table, int index);
26 static inline void addElt(mbv_tab *table, int index, long elt);
27 static inline void addName(mbv_tab *table, int index, char *corresp, int nameallocate);
28 static inline char *transName(mbv_tab *table, int index, char *buf);
29 static inline char *getCorresp(ht *tbl, mbv_tab *table, int index, char *name, char *buf, int del);
30 static inline void addInHt(ht *tbl, char *key, mbv_tab *table);
31 static inline void resize(mbv_tab *table, int index, int dep);
32 static inline void reverseTbl(mbv_tab *table);
33
34 mbv_tab *mbv_resize(ht *tbl, char *key, int left, int right);
35
36 /****************************************************************************/
37 /*{{{ Static */
38 /****************************************************************************/
39 /*{{{ addElt() */
40 /* */
41 /* */
42 /****************************************************************************/
43 static inline void addElt(mbv_tab *table, int index, long elt)
44 {
45 table->CPT ++;
46
47 if (MBV_NAME_ALLOCATOR)
48 table->TABLE.NUM[index] = (int)elt;
49 else
50 table->TABLE.NOM[index] = (char*)elt;
51 }
52
53 /*}}}************************************************************************/
54 /*{{{ addName() */
55 /* */
56 /* */
57 /****************************************************************************/
58 static inline void addName(mbv_tab *table, int index, char *corresp,
59 int nameallocate)
60 {
61 table->CPT ++;
62 if (MBV_NAME_ALLOCATOR)
63 table->TABLE.NUM[index] = AdvancedNameAlloc(MBV_NAME_ALLOCATOR,corresp) + 1;
64 else if (nameallocate)
65 table->TABLE.NOM[index] = namealloc(corresp);
66 else
67 table->TABLE.NOM[index] = corresp;
68 }
69
70 /*}}}************************************************************************/
71 /*{{{ getElt() */
72 /* */
73 /* */
74 /****************************************************************************/
75 static inline long getElt(mbv_tab *table, int index)
76 {
77 if (MBV_NAME_ALLOCATOR)
78 return (long)table->TABLE.NUM[index];
79 else
80 return (long)table->TABLE.NOM[index];
81 }
82
83 /*}}}************************************************************************/
84 /*{{{ resize() */
85 /* */
86 /* */
87 /****************************************************************************/
88 static inline void resize(mbv_tab *table, int index, int dep)
89 {
90 int size, olds;
91 void *str;
92
93 if (table->RIGHT < index)
94 {
95 olds = table->RIGHT - table->LEFT + 1;
96 table->RIGHT = index + dep;
97 size = table->RIGHT - table->LEFT + 1;
98 str = mbkalloc(MBV_ELT_SIZE*size);
99 //memset(str + olds,0,MBV_ELT_SIZE*(size - olds));
100 memset(str,0,MBV_ELT_SIZE*size);
101 memcpy(str,table->TABLE.TAB,MBV_ELT_SIZE*olds);
102 mbkfree(table->TABLE.TAB);
103 table->TABLE.TAB = str;
104 }
105 else if (index < table->LEFT)
106 {
107 olds = table->RIGHT - table->LEFT + 1;
108 table->LEFT = (index > dep) ? index - dep : 0;
109 size = table->RIGHT - table->LEFT + 1;
110 str = mbkalloc(MBV_ELT_SIZE*size);
111 memset(str,0,MBV_ELT_SIZE*(size - olds));
112 //memcpy(str + size - olds,table->TABLE.TAB,MBV_ELT_SIZE*olds);
113 memcpy(str + (size - olds)*MBV_ELT_SIZE,table->TABLE.TAB,MBV_ELT_SIZE*olds);
114 mbkfree(table->TABLE.TAB);
115 table->TABLE.TAB = str;
116 }
117 }
118
119 /*}}}************************************************************************/
120 /*{{{ reverseTbl() */
121 /* */
122 /* */
123 /****************************************************************************/
124 static inline void reverseTbl(mbv_tab *table)
125 {
126 long elt;
127 int size, i;
128
129 size = table->RIGHT - table->LEFT + 1;
130 size /= 2;
131 for (i = 0; i < size; i ++)
132 {
133 elt = getElt(table,i);
134 addElt(table,i,getElt(table,(size-1) - i));
135 addElt(table,(size-1) - i,elt);
136 }
137 }
138
139 /*}}}************************************************************************/
140 /*{{{ addInHt() */
141 /* */
142 /* */
143 /****************************************************************************/
144 static inline void addInHt(ht *tbl, char *key, mbv_tab *table)
145 {
146 addhtitem(tbl,key,(long)table);
147 }
148
149 static inline int addInHtIfNew(ht *tbl, char *key, mbv_tab *table)
150 {
151 if (gethtitem(tbl,key)!=EMPTYHT) return 1;
152 addhtitem(tbl,key,(long)table);
153 return 0;
154 }
155
156 /*}}}************************************************************************/
157 /*{{{ newmbv_tab() */
158 /* */
159 /* */
160 /****************************************************************************/
161 static inline mbv_tab *newmbv_tab(ht *tbl, char *key, int left, int right)
162 {
163 int size;
164 mbv_tab *table;
165
166 if (!MBV_ELT_SIZE)
167 MBV_ELT_SIZE = sizeof(char*);
168
169 size = right - left + 1;
170 table = mbkalloc(sizeof(mbv_tab));
171 table->LEFT = left;
172 table->RIGHT = right;
173 table->CPT = 0;
174 table->TABLE.TAB = mbkalloc(MBV_ELT_SIZE * size);
175
176 memset(table->TABLE.TAB,0,MBV_ELT_SIZE * size);
177
178 if (tbl && key)
179 addInHt(tbl,key,table);
180
181 return table;
182 }
183
184 /*}}}************************************************************************/
185 /*{{{ dupmbv_tab() */
186 /* */
187 /* */
188 /****************************************************************************/
189 static inline mbv_tab *dupmbv_tab(mbv_tab *table, char *prefix)
190 {
191 mbv_tab *res;
192 int i, size;
193 char buf[1024], buf2[1024], *corresp;
194
195 res = newmbv_tab(NULL,NULL,table->LEFT,table->RIGHT);
196 size = table->RIGHT - table->LEFT + 1;
197 if (!prefix)
198 for (i = 0; i < size; i++)
199 addElt(res,i,getElt(table,i));
200 else
201 for (i = 0; i < size; i++)
202 if ((corresp = transName(table,i,buf)) == MBV_NO_CORRESP ||
203 !corresp)
204 addName(res,i,MBV_NO_CORRESP,1);
205 else
206 {
207 sprintf(buf2,"%s%c%s",prefix,SEPAR,corresp);
208 addName(res,i,buf2,1);
209 }
210
211 return res;
212 }
213
214 /*}}}************************************************************************/
215 /*{{{ addAtIndex() */
216 /* */
217 /* */
218 /****************************************************************************/
219 static inline void addAtIndex(ht *tbl, char *key, char *corresp, int index)
220 {
221 mbv_tab *table;
222
223 if (!(table = mbv_get(tbl,key)))
224 table = newmbv_tab(tbl,key,index,index);
225
226 mbv_addAtIndex(table,corresp,index);
227 }
228
229 /*}}}************************************************************************/
230 /*{{{ transName() */
231 /* */
232 /* */
233 /****************************************************************************/
234 static inline char *transName(mbv_tab *table, int index, char *buf)
235 {
236 if (MBV_NAME_ALLOCATOR)
237 {
238 int name;
239
240 if ((name = table->TABLE.NUM[index]))
241 {
242 AdvancedNameAllocName(MBV_NAME_ALLOCATOR,name-1,buf);
243
244 return buf;
245 }
246 else
247 return NULL;
248 }
249 else
250 {
251 char *name;
252
253 if ((name = table->TABLE.NOM[index]))
254 {
255 buf[0] = '\0';
256
257 return name;
258 }
259 else
260 return NULL;
261 }
262 }
263
264 /*}}}************************************************************************/
265 /*{{{ getCorresp() */
266 /* */
267 /* */
268 /****************************************************************************/
269 static inline char *getCorresp(ht *tbl, mbv_tab *table, int index, char *key,
270 char *buf, int del)
271 {
272 char *res;
273
274 if (index == -1)
275 {
276 res = transName(table,0,buf);
277 if (del)
278 mbv_free(tbl,key);
279 }
280 else
281 {
282 res = transName(table,index - table->LEFT,buf);
283 if (del)
284 {
285 table->CPT --;
286 if (!table->CPT)
287 mbv_free(tbl,key);
288 }
289 }
290
291 return res;
292 }
293
294 /*}}}************************************************************************/
295 /*}}}************************************************************************/
296 /*{{{ Extern */
297 /****************************************************************************/
298 /*{{{ mbv_prefix() */
299 /* */
300 /* */
301 /****************************************************************************/
302 void mbv_prefix(ht *tbl, char *prefix)
303 {
304 htitem *hi;
305 long val;
306 int i;
307
308 for (i = 0; i < tbl->length; i ++)
309 {
310 hi =& tbl->pElem[i];
311 val = hi->value;
312 if (val != EMPTYHT && val != DELETEHT)
313 hi->value = (long)dupmbv_tab((mbv_tab*)val,prefix);
314 }
315 }
316
317 /*}}}************************************************************************/
318 /*{{{ mbv_setNoCorrepondance() */
319 /* */
320 /* */
321 /****************************************************************************/
322 void mbv_setNoCorrepondance(char *noCorrespString)
323 {
324 MBV_NO_CORRESP = noCorrespString;
325 }
326
327 /*}}}************************************************************************/
328 ///*{{{ mbv_changeRef() */
329 ///* */
330 ///* */
331 ///****************************************************************************/
332 //void mbv_changeRef(ht *oht, ht *nht, chain_list *old, chain_list *new)
333 //{
334 // int nleft, nright, oleft, oright, nm, om;
335 // int size, k, ol, or, nl, nr;
336 // long elt;
337 // char nname[1024], oname[1024], buf[1024], *nkey, *okey;
338 // mbv_tab *otable, *ntable;
339 //
340 //
341 // for (; old && new; old = delchain(old,old), new = delchain(new,new))
342 // {
343 // nm = sscanf(new->DATA,"%s%d:%d",nname,&nleft,&nright);
344 // om = sscanf(old->DATA,"%s%d:%d",oname,&oleft,&oright);
345 //
346 // ol = (oleft <= oright) ? oleft : oright;
347 // or = (oleft <= oright) ? oright : oleft;
348 // nl = (nleft <= nright) ? nleft : nright;
349 // nr = (nleft <= nright) ? nright : nleft;
350 //
351 // nkey = namealloc(nname);
352 // okey = namealloc(oname);
353 //
354 // if (nm != om)
355 // {
356 // //&& (otable = mbv_get(oht,okey)))
357 // printf("[BVL_WAR] botched name changing\b");
358 // mbkexit(0);
359 // /*
360 // if ((ntable = mbv_get(nht,nkey )))
361 // mbv_addAtIndex(ntable,transName(otable,0,buf),nleft);
362 // else
363 // {
364 // otable->LEFT = nleft;
365 // otable->RIGHT = nleft;
366 // addInHt(nht,nkey ,otable);
367 // delhtitem(oht,old->DATA);
368 // }
369 // */
370 // }
371 // else
372 // {
373 // if ((otable = mbv_get(oht,okey )))
374 // {
375 // switch (nm)
376 // {
377 // case 3 :
378 // ntable = mbv_get(nht,nkey);
379 // if (!ntable && oleft == nleft && oright == nright)
380 // addInHt(nht,nkey ,otable);
381 // else if (!ntable && oleft-oright == nleft-nright)
382 // {
383 // addInHt(nht,nkey ,otable);
384 // otable->LEFT = nl;
385 // otable->RIGHT = nr;
386 // }
387 // else if (!ntable && oleft-oright == nright-nleft)
388 // {
389 // addInHt(nht,nkey ,otable);
390 // otable->LEFT = nl;
391 // otable->RIGHT = nr;
392 // size = or - ol + 1;
393 // size /= 2;
394 // for (k = 0; k < size; k ++)
395 // {
396 // elt = getElt(otable,k);
397 // addElt(otable,k,getElt(otable,(size-1) - k));
398 // addElt(otable,(size-1) - k,elt);
399 // }
400 // }
401 // else
402 // {
403 // if (!(ntable = mbv_initName(nht,nkey ,nl,nr)))
404 // if (!(ntable = mbv_resize(nht,nkey ,nl,nr)))
405 // return;
406 //
407 // size = or - ol + 1;
408 //
409 // for (k = 0; k < size; k ++)
410 // if (nleft <= nright && oleft <= oright)
411 // addElt(ntable,k + (nl-ntable->LEFT),getElt(otable,k));
412 // else
413 // addElt(ntable,(nr-ntable->RIGHT) - k,getElt(otable,k));
414 //
415 // mbkfree(otable->TABLE);
416 // mbkfree(otable);
417 // }
418 // break;
419 // case 1 :
420 // addInHt(nht,nkey ,otable);
421 // break;
422 // default :
423 // fprintf(stderr,"[MBV_ERR] Bad number of argument to data\n");
424 // }
425 // delhtitem(oht,old->DATA);
426 // }
427 // else
428 // fprintf(stderr,"[MBV_ERR] Bad old name\n");
429 //
430 // }
431 // }
432 //}
433 //
434
435
436 ht *mbv_duplicateht(ht *source)
437 {
438 chain_list *cl, *ch;
439 char *key;
440 ht *newht;
441
442 newht=addht(source->count);
443
444 cl=ch=GetAllHTKeys(source);
445 while (cl!=NULL)
446 {
447 key=(char *)cl->DATA;
448 addInHt(newht, key,
449 dupmbv_tab(mbv_get(source,key), NULL));
450 cl=cl->NEXT;
451 }
452 freechain(ch);
453 return newht;
454 }
455
456
457 ///*}}}************************************************************************/
458 /*{{{ mbv_changeRef() */
459 /* */
460 /* */
461 /****************************************************************************/
462
463 void mbv_changeRef(ht *oht, ht *nht, chain_list *old, chain_list *new)
464 {
465 int nleft, nright, oleft, oright, nm, om;
466 int size, k = 0, ol, or, nl, nr, shift;
467 char nname[1024], oname[1024], buf[1024], *nkey, *okey;
468 mbv_tab *otable, *ntable;
469 long elt;
470
471
472 for (; old && new; old = delchain(old,old), new = delchain(new,new))
473 {
474 nm = sscanf(new->DATA,"%s%d:%d",nname,&nleft,&nright);
475 om = sscanf(old->DATA,"%s%d:%d",oname,&oleft,&oright);
476
477 nkey = namealloc(nname);
478 okey = namealloc(oname);
479 otable = mbv_get(oht,okey);
480 ntable = mbv_get(nht,nkey);
481
482 delhtitem(oht,okey);
483
484 if (!otable)
485 {
486 if (strncasecmp(okey,"ext@",4)!=0)
487 avt_error("mbv", 1, AVT_WAR,"could not find correspondance for name '%s'\n",oname);
488 }
489 else if (nm != om)
490 {
491 if (ntable)
492 {
493 mbv_addAtIndex(ntable,transName(otable,0,buf),nleft);
494 mbkfree(otable->TABLE.TAB);
495 mbkfree(otable);
496 }
497 else
498 {
499 ntable = otable;
500 ntable->LEFT = nleft;
501 ntable->RIGHT = nleft;
502 addInHt(nht,nkey ,ntable);
503 }
504 }
505 else if (nm == 1)
506 {
507 if (addInHtIfNew(nht,nkey,otable))
508 {
509 mbkfree(otable->TABLE.TAB);
510 mbkfree(otable);
511 }
512 }
513 else if (nm == 2)
514 {
515 fprintf(stderr,"[MBV_WAR] contact support@avertec.com\n");
516 addInHt(nht,nkey,otable);
517 }
518 else if (nm == 3)
519 {
520 ol = (oleft <= oright) ? oleft : oright;
521 or = (oleft <= oright) ? oright : oleft;
522 nl = (nleft <= nright) ? nleft : nright;
523 nr = (nleft <= nright) ? nright : nleft;
524
525 if (or - ol != nr - nl)
526 fprintf(stderr,"[MBV_ERR] Botched name\n");
527 else
528 {
529 // ntable = mbv_get(nht,nkey);
530
531 if (!ntable && oleft == nleft && oright == nright)
532 ntable = otable;
533 else if (!ntable && oleft-oright == nleft-nright)
534 {
535 ntable = otable;
536 ntable->LEFT = nl;
537 ntable->RIGHT = nr;
538 }
539 else if (!ntable && oleft-oright == nright-nleft)
540 {
541 ntable = otable;
542 ntable->LEFT = nl;
543 ntable->RIGHT = nr;
544 reverseTbl(ntable);
545 }
546 else
547 {
548 // if needed the correspondence table is resized
549 // or created
550 if (!(ntable && ntable->LEFT <= nl && ntable->RIGHT >= nr)
551 && !(ntable = mbv_initName(nht,nkey ,nl,nr))
552 && !(ntable = mbv_resize(nht,nkey ,nl,nr)) )
553 return;
554 /*
555 size = or - otable->LEFT + 1;
556 shift = nl - ntable->LEFT;
557 */
558 for (k = ol; k <= or/* size*/; k ++)
559 if ((elt = getElt(otable,k-otable->LEFT)))
560 addElt(ntable,nl+(k-ol)-ntable->LEFT /*k+shift*/,elt);
561
562 mbkfree(otable->TABLE.TAB);
563 mbkfree(otable);
564 }
565 addInHt(nht,nkey ,ntable);
566 }
567 }
568 else
569 fprintf(stderr,"[MBV_ERR] Bad number of argument to data\n");
570 }
571 }
572
573 /*}}}************************************************************************/
574 /*{{{ mbv_useAdvancedNameAllocator() */
575 /* */
576 /* */
577 /****************************************************************************/
578 void mbv_useAdvancedNameAllocator()
579 {
580 if (!MBV_ELT_SIZE)
581 {
582 MBV_ELT_SIZE = sizeof(int);
583 MBV_NAME_ALLOCATOR = CreateAdvancedNameAllocator(CASE_SENSITIVE);
584 }
585 }
586
587 /*}}}************************************************************************/
588 /*{{{ mbv_addAtIndex() */
589 /* */
590 /* */
591 /****************************************************************************/
592 void mbv_addAtIndex(mbv_tab *table, char *corresp, int index)
593 {
594 resize(table,index,16);
595 if (index < 0)
596 index = 0;
597 else
598 index = index - table->LEFT;
599
600 if (corresp)
601 addName(table,index,corresp,0);
602 }
603
604 /*}}}************************************************************************/
605 /*{{{ mbv_resize() */
606 /* */
607 /* */
608 /****************************************************************************/
609 mbv_tab *mbv_resize(ht *tbl, char *key, int left, int right)
610 {
611 mbv_tab *table;
612 int tmp;
613
614 if (left > right)
615 {
616 tmp = left;
617 left = right;
618 right = tmp;
619 }
620
621 if (!(table = mbv_get(tbl,key)))
622 return NULL;
623 else if (right < table->LEFT)
624 {
625 resize(table,left,0);
626
627 return table;
628 }
629 else if (table->RIGHT < left)
630 {
631 resize(table,right,0);
632
633 return table;
634 }
635 else
636 {
637 for (tmp = left; tmp <= right; tmp ++)
638 if (tmp > table->RIGHT || table->LEFT > tmp)
639 continue;
640 else if (getElt(table,tmp - table->LEFT))
641 {
642 avt_error("mbv", 1, AVT_ERR, "Conflict on %s(%d to %d) to merge with %s(%d to %d), cannot resize\n", key, left, right, key, table->LEFT, table->RIGHT);
643 // fprintf(stderr,"[MBV_ERR] Conflict can not resize\n");
644 return NULL;
645 }
646 resize(table,left,0);
647 resize(table,right,0);
648
649 return table;
650 }
651 }
652
653 /*}}}************************************************************************/
654 /*{{{ mbv_initName() */
655 /* */
656 /* */
657 /****************************************************************************/
658 mbv_tab *mbv_initName(ht *tbl, char *key, int left, int right)
659 {
660 mbv_tab *table;
661 int tmp;
662
663 if (left > right)
664 {
665 tmp = left;
666 left = right;
667 right = tmp;
668 }
669
670 if (!(table = mbv_get(tbl,key)))
671 return newmbv_tab(tbl,key,left,right);
672 else
673 return NULL;
674 }
675
676 /*}}}************************************************************************/
677 /*{{{ mbv_addCorresp() */
678 /* */
679 /* */
680 /****************************************************************************/
681 int mbv_addCorresp(ht *tbl, char *key, char *corresp)
682 {
683 char buf[1024], test;
684 int index, res;
685
686 switch (sscanf(key,"%s %d%c",buf,&index,&test))
687 {
688 case 1 :
689 index = -1;
690 case 2 :
691 key = namealloc(buf);
692 addAtIndex(tbl,key,corresp,index);
693 res = 1;
694 break;
695 default :
696 res = 0;
697 }
698
699 return res;
700 }
701
702 /*}}}************************************************************************/
703 /*{{{ mbv_get() */
704 /* */
705 /* */
706 /****************************************************************************/
707 mbv_tab *mbv_get(ht *tbl, char *key)
708 {
709 mbv_tab *table;
710
711 if ((table=(mbv_tab*)gethtitem(tbl,key)) == (mbv_tab*)EMPTYHT ||
712 table == (mbv_tab*)DELETEHT)
713 return NULL;
714 else
715 return table;
716 }
717
718 ht *mbv_buildglobalht(chain_list *htl)
719 {
720 ht *h=addht(10000), *j;
721 chain_list *entries;
722 while (htl!=NULL)
723 {
724 j=(ht *)htl->DATA;
725 entries=GetAllHTKeys(j);
726 while (entries!=NULL)
727 {
728 addhtitem(h, entries->DATA, gethtitem(j, entries->DATA));
729 entries=delchain(entries, entries);
730 }
731 htl=htl->NEXT;
732 }
733 return h;
734 }
735 /*}}}************************************************************************/
736 ///*{{{ mbv_getFromVect() */
737 ///* */
738 ///* */
739 ///****************************************************************************/
740 //mbv_tab *mbv_getFromVect(ht *tbl, char *key)
741 //{
742 // mbv_tab *table;
743 // char rad[1024];
744 //
745 // sscanf(key,"%s",rad);
746 // if ((table = (mbv_tab*)gethtitem(tbl,namealloc(rad))) == (mbv_tab*)EMPTYHT)
747 // return NULL;
748 // else
749 // return table;
750 //}
751 //
752 ///*}}}************************************************************************/
753 ///*{{{ mbv_addFromVect() */
754 ///* */
755 ///* */
756 ///****************************************************************************/
757 //void mbv_addFromVect(ht *tbl, char *key, mbv_tab *data)
758 //{
759 // char rad[1024];
760 //
761 // sscanf(key,"%s",rad);
762 // addhtitem(tbl,namealloc(rad),(long)data);
763 //}
764 //
765 ///*}}}************************************************************************/
766 ///*{{{ mbv_del() */
767 ///* */
768 ///* */
769 ///****************************************************************************/
770 //void mbv_del(ht *tbl, char *key)
771 //{
772 // mbv_tab *table;
773 // char buf[1024], test, *res;
774 // int index;
775 //
776 // switch (sscanf(key,"%s %d%c",buf,&index,&test))
777 // {
778 // case 1 :
779 // index = -1;
780 // case 2 :
781 // if ((table = mbv_get(tbl,buf)))
782 // {
783 // if (index == -1)
784 // res = table->TABLE[0];
785 // else
786 // res = table->TABLE[index - table->LEFT];
787 // }
788 // else
789 // res = NULL;
790 // break;
791 // default :
792 // res = NULL;
793 // }
794 //}
795 //
796 ///*}}}************************************************************************/
797 /*{{{ mbv_free() */
798 /* */
799 /* */
800 /****************************************************************************/
801 void mbv_free(ht *tbl, char *key)
802 {
803 mbv_tab *table;
804
805 if ((table = mbv_get(tbl,key)))
806 {
807 mbkfree(table->TABLE.TAB);
808 mbkfree(table);
809 }
810 delhtitem(tbl,key);
811 }
812
813 void mbv_freeht(ht *source)
814 {
815 chain_list *cl, *ch;
816 char *key;
817
818 cl=ch=GetAllHTKeys(source);
819 while (cl!=NULL)
820 {
821 key=(char *)cl->DATA;
822 mbv_free(source, key);
823 cl=cl->NEXT;
824 }
825 freechain(ch);
826 delht(source);
827 }
828
829 /*}}}************************************************************************/
830 /*{{{ mbv_getCorresp() */
831 /* */
832 /* */
833 /****************************************************************************/
834 char *mbv_getCorresp(ht *tbl, char *key, char *buf)
835 {
836 mbv_tab *table;
837 char name[1024], test, *res;
838 int index;
839
840 switch (sscanf(key,"%s %d%c",name,&index,&test))
841 {
842 case 1 :
843 index = -1;
844 case 2 :
845 key = namealloc(name);
846 if ((table = mbv_get(tbl,key )))
847 res = getCorresp(tbl,table,index,key,buf,0);
848 else
849 res = NULL;
850 break;
851 default :
852 res = NULL;
853 }
854
855 return res;
856 }
857
858 /*}}}************************************************************************/
859 /*{{{ mbv_getCorrespAndDel() */
860 /* */
861 /* */
862 /****************************************************************************/
863 char *mbv_getCorrespAndDel(ht *tbl, char *key, char *buf)
864 {
865 mbv_tab *table;
866 char name[1024], test, *res;
867 int index;
868
869 switch (sscanf(key,"%s %d%c",name,&index,&test))
870 {
871 case 1 :
872 index = -1;
873 case 2 :
874 key = namealloc(name);
875 if ((table = mbv_get(tbl,key)))
876 res = getCorresp(tbl,table,index,key,buf,1);
877 else
878 res = NULL;
879 break;
880 default :
881 res = NULL;
882 }
883
884 return res;
885 }
886
887 /*}}}************************************************************************/
888 /*{{{ mbv_dumpCorresp() */
889 /* */
890 /* */
891 /****************************************************************************/
892 void mbv_dumpCorresp(ht *table)
893 {
894 htitem *hi;
895 long val;
896 int i, j, size;
897 mbv_tab *tab;
898 char buf[1024], *name;
899
900 for (i = 0; i < table->length; i ++)
901 {
902 hi = &(table->pElem[i]);
903 val = hi->value;
904 if (val != EMPTYHT && val != DELETEHT)
905 {
906 tab = (mbv_tab*)val;
907 size = tab->RIGHT - tab->LEFT + 1;
908 for (j = 0; j < size; j ++)
909 {
910 name = transName(tab,j,buf);
911 name = name ? name : "nothing";
912 printf("%s(%d) -> %s\n", (char*)hi->key,j+tab->LEFT,name);
913 }
914 }
915 }
916 }
917
918 /*}}}************************************************************************/
919 /*}}}************************************************************************/
920 #if 0
921 /*{{{ Test functions */
922 /****************************************************************************/
923 #include <stdlib.h>
924
925 /****************************************************************************/
926 /*{{{ */
927 /* */
928 /* void mbv_changeRef(ht *orig, ht *dest, chain_list *old, chain_list *new) */
929 /* */
930 /****************************************************************************/
931 void testchangeref()
932 {
933 #define TSIZE 16
934 ht *old[TSIZE], *new[TSIZE], *top;
935 //int mem[] = {1024,3145728};
936 //int test = 0;
937 int i, j;
938 char buf[TSIZE], *corresp[TSIZE], *oname[TSIZE], *nname;
939 chain_list *och, *nch;
940
941
942 top = addht(15);
943 for (j = 0; j < TSIZE; j ++)
944 {
945 new[j] = addht(15);
946 mbv_initName(new[j],namealloc("mem"),0,TSIZE-1);
947
948 sprintf(buf,"mem_%d",j);
949 oname[j] = mbkstrdup(buf);
950
951 for (i = 0; i < TSIZE; i ++)
952 {
953 old[i] = addht(15);
954 sprintf(buf,"cell_%d_mem",i+j*TSIZE);
955 corresp[i] = mbkstrdup(buf);
956
957 mbv_addCorresp(old[i],oname[j],corresp[i]);
958
959 //printf("drive %d : ",i);
960 //mbv_dumpCorresp(old[i]);
961 }
962 for (i = 0; i < TSIZE; i ++)
963 {
964 och = addchain(NULL,oname[j]);
965
966 sprintf(buf,"mem %d",i);
967 nname = mbkstrdup(buf);
968 nch = addchain(NULL,nname);
969
970 mbv_changeRef(old[i],new[j],och,nch);
971
972 mbkfree(nname);
973 }
974
975 mbkfree(oname[j]);
976
977
978 sprintf(buf,"mem %d:%d",0,TSIZE -1);
979 oname[0] = mbkstrdup(buf);
980 och = addchain(NULL,oname[0]);
981
982 if (j%2)
983 sprintf(buf,"mem %d:%d",(j+1)*TSIZE -1,j*TSIZE);
984 else
985 sprintf(buf,"mem %d:%d",j*TSIZE,(j+1)*TSIZE -1);
986 nname = mbkstrdup(buf);
987 nch = addchain(NULL,nname);
988
989 mbv_changeRef(new[j],top,och,nch);
990
991 //mbv_dumpCorresp(new[j]);
992 }
993 printf("top ---\n");
994 mbv_dumpCorresp(top);
995 /*
996 for (i = 0; i < TSIZE; i ++)
997 {
998 mbkfree(corresp[i]);
999 delht(old[i]);
1000 }
1001 */
1002 }
1003
1004 /*}}}************************************************************************/
1005 /*{{{ main() */
1006 /* */
1007 /* />gcc -o main -L$AVT_DISTRIB_DIR/lib -Xlinker -z -Xlinker allextract \
1008 -lMut325 -lstdc++ -lnsl -ldl -lm -Xlinker -z -Xlinker defaultextract */
1009 /* />main */
1010 /****************************************************************************/
1011 int main(int argc, char *argv[])
1012 {
1013 char *testbench [] =
1014 {
1015 "sig0" ,"testsig0" ,
1016 "sig 0" ,"testsig(0)" ,
1017 "sig(0)" ,"testsig(0)" ,
1018 "sig 10" ,"testsig(10)" ,
1019 "sig 58" ,"testsig(58)" ,
1020 "sig 15" ,"testsig(15)" ,
1021 "sig 13" ,"testsig(13)" ,
1022 "sig 45" ,"testsig(45)" ,
1023 "sig1 58" ,"testsig1(58)" ,
1024 "sig1 98" ,"testsig1(98)" ,
1025 "sig1 10" ,"testsig1(10)" ,
1026 };
1027 // chain_list *old, *new;
1028 char *res, buf[1024];
1029 int i, nbbench, nberr = 0;
1030 ht* tbl = addht(50);
1031
1032 mbkenv();
1033
1034 // mbv_useAdvancedNameAllocator();
1035 tbl = addht(50);
1036 nbbench = (int)(sizeof(testbench)/sizeof(char*))/2;
1037 for (i = 0; i < nbbench; i ++ )
1038 mbv_addCorresp(tbl,testbench[i*2],testbench[i*2+1]);
1039
1040 // mbv_prefix(tbl,"prefix");
1041 for (i = 0; i < nbbench; i ++ )
1042 {
1043 res = mbv_getCorresp(tbl,testbench[i*2],buf);
1044 if (!res)
1045 {
1046 nberr ++;
1047 printf("[MBV_ERR] not found corresp for : %s\n",testbench[i*2]);
1048 }
1049 else if (strcmp(res,testbench[i*2+1]))
1050 {
1051 nberr ++;
1052 printf("[MBV_ERR] expected : %s obtained : %s\n",testbench[i*2+1],res);
1053 }
1054 }
1055 printf("[MBV_TEST] %d/%d errors\n",nberr,nbbench);
1056
1057 printf("--- testing changeref ---\n\n");
1058 testchangeref();
1059 printf("\n--- done ---\n");
1060
1061
1062 return EXIT_SUCCESS;
1063 argc = 0;
1064 argv = NULL;
1065 }
1066
1067 /*}}}************************************************************************/
1068 #endif