home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dccsrcoo.zip / bundle.cpp < prev    next >
C/C++ Source or Header  |  1997-04-09  |  3KB  |  114 lines

  1. /*****************************************************************************
  2.  *
  3.  * File: bundle.c
  4.  * Module that handles the bundle type (array of pointers to strings).
  5.  *
  6.  ****************************************************************************/
  7.  
  8. #include "dcc.h"
  9. #include <stdarg.h>
  10. #include <memory.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #define deltaProcLines  20
  15.  
  16.  
  17. void newBundle (bundle *procCode)
  18. /* Allocates memory for a new bundle and initializes it to zero.    */
  19. {
  20.     memset (&(procCode->decl), 0, sizeof(strTable));
  21.     memset (&(procCode->code), 0, sizeof(strTable));
  22. }
  23.  
  24.  
  25. static void incTableSize (strTable *strTab)
  26. /* Increments the size of the table strTab by deltaProcLines and copies all 
  27.  * the strings to the new table.        */
  28. {
  29.     strTab->allocLines += deltaProcLines;
  30.     strTab->str = (char**)reallocVar (strTab->str, strTab->allocLines*sizeof(char *));
  31.     memset (&strTab->str[strTab->allocLines - deltaProcLines], 0,
  32.             deltaProcLines * sizeof(char *));
  33. }
  34.  
  35.  
  36. void appendStrTab (strTable *strTab, char *format, ...) 
  37. /* Appends the new line (in printf style) to the string table strTab.   */
  38. {  va_list args;
  39.  
  40.     va_start (args, format);
  41.     if (strTab->numLines == strTab->allocLines)
  42.     {
  43.         incTableSize (strTab);
  44.     }
  45.     strTab->str[strTab->numLines] = (char *)malloc(lineSize * sizeof(char));
  46.     if (strTab->str == NULL)
  47.     {
  48.         fatalError(MALLOC_FAILED, lineSize * sizeof(char));
  49.     }
  50.     vsprintf (strTab->str[strTab->numLines], format, args); 
  51.     strTab->numLines++;
  52.     va_end (args);
  53. }
  54.  
  55.  
  56. Int nextBundleIdx (strTable *strTab)
  57. /* Returns the next available index into the table */
  58. {
  59.     return (strTab->numLines);
  60. }
  61.  
  62.  
  63. void addLabelBundle (strTable *strTab, Int idx, Int label)
  64. /* Adds the given label to the start of the line strTab[idx].  The first
  65.  * tab is removed and replaced by this label */
  66. { char s[lineSize];
  67.  
  68.     sprintf (s, "l%ld: %s", label, &strTab->str[idx][4]);
  69.     strcpy (strTab->str[idx], s);
  70. }
  71.  
  72.  
  73. static void writeStrTab (FILE *fp, strTable strTab)
  74. /* Writes the contents of the string table on the file fp.  */
  75. { Int i;
  76.  
  77.     for (i = 0; i < strTab.numLines; i++)
  78.         fprintf (fp, "%s", strTab.str[i]); 
  79. }
  80.  
  81.  
  82. void writeBundle (FILE *fp, bundle procCode)
  83. /* Writes the contents of the bundle (procedure code and declaration) to
  84.  * a file.          */
  85. {
  86.     writeStrTab (fp, procCode.decl);
  87.     if (procCode.decl.str[procCode.decl.numLines - 1][0] != ' ')
  88.        fprintf (fp, "\n");
  89.     writeStrTab (fp, procCode.code);
  90. }
  91.  
  92.  
  93. static void freeStrTab (strTable *strTab)
  94. /* Frees the storage allocated by the string table. */
  95. { Int i;
  96.  
  97.     if (strTab->allocLines > 0)  {
  98.        for (i = 0; i < strTab->numLines; i++)
  99.          free (strTab->str[i]); 
  100.        free (strTab->str);
  101.        memset (strTab, 0, sizeof(strTable));
  102.     }
  103. }
  104.  
  105.  
  106. void freeBundle (bundle *procCode)
  107. /* Deallocates the space taken by the bundle procCode */
  108.     freeStrTab (&(procCode->decl));
  109.     freeStrTab (&(procCode->code));
  110. }
  111.  
  112.  
  113.