bundle.cpp 2.9 KB

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