home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctb_291.zip / source / table.cpp < prev    next >
C/C++ Source or Header  |  1996-08-14  |  9KB  |  359 lines

  1. /*
  2. ** Module   :TABLE.CPP
  3. ** Abstract :Name table implementation
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. ** Log:
  7. ** Last update Sat  11-05-96
  8. */
  9.  
  10. #include <table.h>
  11. #include <dlc.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #define INFO_PLACE  50
  15. extern char print_declarators;
  16. extern char print_methods;
  17. extern char show_all;
  18. extern char draw_tree;
  19.  
  20. Table::Table(int sz)
  21. {
  22.     if (sz <= 0)
  23.     {
  24.         if (sz)
  25.             sz = -sz;
  26.         else
  27.             sz = 29;
  28.     }
  29.     tbl = new Name *[sz];
  30.     size = sz;
  31.     for (int i = 0; i < sz; i++)
  32.         tbl[i] = NULL;
  33. }
  34. Table::~Table()
  35. {
  36.     for (int i = 0; i < size; i++)
  37.     {
  38.         Name *nx;
  39.         for (Name * n = tbl[i]; n; n = nx)
  40.         {
  41.             nx = n->next;
  42.             delete n;
  43.         }
  44.     }
  45.     delete tbl;
  46. }
  47.  
  48. Name *Table::look(char *p, int ins)
  49. {
  50.     int ii = 0;
  51.     char *pp = p;
  52.     while (*pp)
  53.         ii = ii << 1 ^ *pp++;
  54.     if (ii < 0)
  55.         ii = -ii;
  56.     ii %= size;
  57.     for (Name * n = tbl[ii]; n; n = n->next)
  58.         if (!strcmp(p, n->str))
  59.             return n;
  60.     if (ins)
  61.     {
  62.         Name *nn = new Name(p, tbl[ii]);
  63.         tbl[ii] = nn;
  64.         return nn;
  65.     }
  66.     return NULL;
  67. }
  68. Name::Name(char *p, Name * n)
  69. {
  70.     next = n;
  71.     str = new char[strlen(p) + 1];
  72.     strcpy(str, p);
  73.     base = NULL;
  74.     childs = NULL;
  75.     used = 0;
  76.     funcs = 0;
  77.     declaration_file[0] = 0;
  78.     declaration_line = 0;
  79. }
  80.  
  81. Name::~Name()
  82. {
  83.     delete str;
  84.     for(method* tmp = 0; funcs; funcs = tmp)
  85.     {
  86.         tmp = funcs->next;
  87.         delete funcs;
  88.     }
  89. }
  90.  
  91. link *lmerge (link *p, link *q)
  92. {
  93.     link *r;
  94.     link head(0, 0);
  95.  
  96.     for ( r = &head; p && q; )
  97.     {
  98.         if ( strcmp(p->name->str, q->name->str) < 0 )
  99.         {
  100.             r = r->next = p;
  101.             p = p->next;
  102.         }
  103.         else
  104.         {
  105.             r = r->next = q;
  106.             q = q->next;
  107.         }
  108.     }
  109.     r->next = (p ? p : q);
  110.     return head.next;
  111. }
  112.  
  113. link* Sort(link *p)
  114. {
  115.     link *q, *r;
  116.  
  117.     if(p)
  118.     {
  119.         q = p;
  120.         for ( r = q->next; r && (r = r->next) != NULL; r = r->next )
  121.             q = q->next;
  122.         r = q->next;
  123.         q->next = NULL;
  124.  
  125.         if ( r )
  126.             p = lmerge(Sort(r), Sort(p));
  127.     }
  128.     return p;
  129. }
  130. Table::Prepare(Name * nm)
  131. {
  132.     if (nm)
  133.     {
  134.         if (nm->base)
  135.         {
  136.             nm->used = 1;
  137.             nm->base = Sort(nm->base);
  138.         }
  139.         for (link * lnk = nm->base; lnk; lnk = lnk->next)
  140.         {
  141.             if (!lnk->name->childs)
  142.             {
  143.                 lnk->name->childs = new link(nm);
  144.             }
  145.             else
  146.             {
  147.                 for (link * lnk2 = lnk->name->childs; lnk2; lnk2 = lnk2->next)
  148.                 {
  149.                     if (lnk2->name == nm)
  150.                         break;
  151.                     else
  152.                         lnk2->name->used = 1;
  153.                 }
  154.                 if (!lnk2)
  155.                     lnk->name->childs = new link(nm, lnk->name->childs);
  156.             }
  157.         }
  158.     }
  159.     return 0;
  160. }
  161.  
  162. static int level = 1;
  163. static char *bar;
  164. static char *outbuffer;
  165.  
  166. void Table::show_tree(Name * nm)
  167. {
  168.     if(nm->childs)
  169.         nm->childs = Sort(nm->childs);
  170.     for (link * lnk = nm->childs; lnk; lnk = lnk->next)
  171.     {
  172.         bar[level * 2] = (lnk->next) ? '├' : '└';
  173.         bar[level * 2 + 1] = 0;
  174.         int pos = sprintf(outbuffer, "%s─%s", bar, lnk->name->str);
  175.         if (lnk->name->base && lnk->name->base->next)
  176.         {
  177.             pos += sprintf(&outbuffer[pos], "(");                      ///////////////////////////
  178.             for (link * ptr = lnk->name->base; ptr; ptr = ptr->next)
  179.             {
  180.                 if(ptr->name == nm)
  181.                     continue;
  182.  
  183.                 pos += sprintf(&outbuffer[pos], ptr->name->str);
  184.                 if (ptr->next && ptr->next->name != nm)
  185.                     pos += sprintf(&outbuffer[pos], ",");
  186.             }
  187.             pos += sprintf(&outbuffer[pos], ")");
  188.         }
  189.         if(print_declarators)
  190.         {
  191.             while(pos < INFO_PLACE)
  192.                 outbuffer[pos++] = ' ';
  193.             pos += sprintf(&outbuffer[pos], " -[%-12s (%4ld)]",lnk->name->declaration_file,lnk->name->declaration_line);
  194.         }
  195.         sprintf(&outbuffer[pos], "\r\n");
  196.  
  197.         if (OUT.ok())
  198.             OUT.put(outbuffer);
  199.         else
  200.             printf(outbuffer);
  201.  
  202.         if (lnk->name->childs)
  203.             bar[level * 2] = (lnk->next) ? '│' : ' ';
  204.         bar[level * 2 + 1] = ' ';
  205.         level++;
  206.         show_tree(lnk->name);
  207.         level--;
  208.     }
  209. }
  210. void Table::show_tree(char *obj, char *file)
  211. {
  212.     int pos = 0;
  213.     bar       = new char[1024];
  214.     outbuffer = new char[1024];
  215.     bar[0] = ' ';
  216.     bar[1] = ' ';
  217.     bar[2] = ' ';
  218.     for (int i = 0; i < size; i++)
  219.     {
  220.         for (Name * nx = tbl[i]; nx; nx = nx->next)
  221.             Prepare(nx);
  222.     }
  223.     if (file)
  224.     {
  225.         OUT.open(file, ".def", WR);
  226.     }
  227.     if (obj)
  228.     {
  229.         Name *o = look(obj);
  230.         if (o)
  231.         {
  232.             pos = sprintf(outbuffer, "Class hierarchy from base class %s\r\n", o->str);
  233.             if (OUT.ok())
  234.                 OUT.put(outbuffer);
  235.             else
  236.                 printf(outbuffer);
  237.  
  238.             pos = sprintf(outbuffer, " %s", o->str);
  239.             if(print_declarators)
  240.             {
  241.                 while(pos < INFO_PLACE)
  242.                     outbuffer[pos++] = ' ';
  243.                 pos += sprintf(&outbuffer[pos], " -[%-12s (%4ld)]",o->declaration_file,o->declaration_line);
  244.             }
  245.             sprintf(&outbuffer[pos], "\r\n");
  246.             if (OUT.ok())
  247.                 OUT.put(outbuffer);
  248.             else
  249.                 printf(outbuffer);
  250.             show_tree(o);
  251.             delete bar;
  252.             return;
  253.         }
  254.         else
  255.             printf("Error: Class '%s' not found.\n", obj);
  256.     }
  257.  
  258.     sprintf(outbuffer, "\r\nClass hierarchy\r\n");
  259.  
  260.     if (OUT.ok())
  261.         OUT.put(outbuffer);
  262.     else
  263.         printf(outbuffer);
  264.  
  265.     for (i = 0; i < size; i++)
  266.     {
  267.         for (Name * nx = tbl[i]; nx; nx = nx->next)
  268.         {
  269.             if (!nx->used)
  270.             {
  271.                 if(show_all || nx->childs)
  272.                 {
  273.                     pos = sprintf(outbuffer, " %s", nx->str);
  274.                     if(print_declarators)
  275.                     {
  276.                         while(pos < INFO_PLACE)
  277.                             outbuffer[pos++] = ' ';
  278.                         pos += sprintf(&outbuffer[pos], " -[%-12s (%4ld)]",nx->declaration_file,nx->declaration_line);
  279.                     }
  280.                     sprintf(&outbuffer[pos], "\n\r");
  281.                     if (OUT.ok())
  282.                         OUT.put(outbuffer);
  283.                     else
  284.                         printf(outbuffer);
  285.  
  286.                     if(nx->funcs && print_methods)
  287.                     {
  288.                         if (OUT.ok())
  289.                             OUT.put("\tMethods:\n\r");
  290.                         else
  291.                             printf("\tMethods:\n");
  292.                         for(method *tmp=nx->funcs; tmp; tmp = tmp->next)
  293.                         {
  294.                             if (OUT.ok())
  295.                             {
  296.                                 OUT.put("\t\t");
  297.                                 OUT.put(tmp->name);
  298.                             }
  299.                             else
  300.                                 printf("\t\t%s", tmp->name);
  301.  
  302.                             if(tmp->flags)
  303.                             {
  304.                                 if(OUT.ok())
  305.                                 {
  306.                                     OUT.put(" - ");
  307.                                     if(tmp->flags & 1)
  308.                                         OUT.put("static ");
  309.                                     if(tmp->flags & 2)
  310.                                         OUT.put("virtual ");
  311.                                     if(tmp->flags & 4)
  312.                                         OUT.put("destuctor ");
  313.                                     if(tmp->flags & 8)
  314.                                         OUT.put("constructor");
  315.                                     OUT.put("\n\r");
  316.                                 }
  317.                                 else
  318.                                     printf(" - %s%s%s%s\n",
  319.                                     (tmp->flags & 1) ? "static " :"",
  320.                                     (tmp->flags & 2) ? "virtual ":"",
  321.                                     (tmp->flags & 4) ? "destuctor ":"",
  322.                                     (tmp->flags & 8) ? "constructor":""
  323.                                     );
  324.                             }
  325.                             else
  326.                             {
  327.                                 if (OUT.ok())
  328.                                     OUT.put("\n\r");
  329.                                 else
  330.                                     printf("\n");
  331.                             }
  332.                         }
  333.                         if (OUT.ok())
  334.                             OUT.put("\n\r");
  335.                         else
  336.                             printf("\n");
  337.                         if(draw_tree)
  338.                             if (OUT.ok())
  339.                                 OUT.put(outbuffer);
  340.                             else
  341.                                 printf(outbuffer);
  342.                     }
  343.                     if(draw_tree)
  344.                         show_tree(nx);
  345.                 }
  346.             }
  347.         }
  348.     }
  349.     delete bar;
  350. }
  351.  
  352. method::method(char *txt, method * nxt)
  353. {
  354.     name = new char[strlen(txt) + 1];
  355.     strcpy(name, txt);
  356.     next = nxt;
  357. }
  358.  
  359.