bundle.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <iostream>
  9. #include <memory.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define deltaProcLines 20
  13. /* Allocates memory for a new bundle and initializes it to zero. */
  14. void newBundle (bundle *)
  15. {
  16. }
  17. /* Increments the size of the table strTab by deltaProcLines and copies all
  18. * the strings to the new table. */
  19. static void incTableSize (strTable *strTab)
  20. {
  21. strTab->resize(strTab->size()+deltaProcLines);
  22. }
  23. /* Appends the new line (in printf style) to the string table strTab. */
  24. void appendStrTab (strTable *strTab, const char *format, ...)
  25. {
  26. va_list args;
  27. char buf[lineSize];
  28. va_start (args, format);
  29. vsprintf (buf, format, args);
  30. strTab->push_back(buf);
  31. va_end (args);
  32. }
  33. /* Returns the next available index into the table */
  34. Int nextBundleIdx (strTable *strTab)
  35. {
  36. return (strTab->size());
  37. }
  38. /* Adds the given label to the start of the line strTab[idx]. The first
  39. * tab is removed and replaced by this label */
  40. void addLabelBundle (strTable &strTab, Int idx, Int label)
  41. {
  42. char s[lineSize];
  43. sprintf (s, "l%ld: %s", label, strTab[idx].c_str()+4);
  44. strTab[idx] = s;
  45. }
  46. /* Writes the contents of the string table on the file fp. */
  47. static void writeStrTab (std::ostream &ios, strTable &strTab)
  48. {
  49. Int i;
  50. for (i = 0; i < strTab.size(); i++)
  51. ios << strTab[i];
  52. }
  53. /* Writes the contents of the bundle (procedure code and declaration) to
  54. * a file. */
  55. void writeBundle (std::ostream &ios, bundle procCode)
  56. {
  57. writeStrTab (ios, procCode.decl);
  58. if (procCode.decl[procCode.decl.size() - 1][0] != ' ')
  59. ios << "\n";
  60. writeStrTab (ios, procCode.code);
  61. }
  62. /* Frees the storage allocated by the string table. */
  63. static void freeStrTab (strTable &strTab)
  64. {
  65. strTab.clear();
  66. }
  67. void freeBundle (bundle *procCode)
  68. /* Deallocates the space taken by the bundle procCode */
  69. {
  70. freeStrTab (procCode->decl);
  71. freeStrTab (procCode->code);
  72. }
  73. void bundle::appendCode(const char *format,...)
  74. {
  75. va_list args;
  76. char buf[lineSize]={0};
  77. va_start (args, format);
  78. vsprintf (buf, format, args);
  79. code.push_back(buf);
  80. va_end (args);
  81. }
  82. void bundle::appendDecl(const char *format,...)
  83. {
  84. va_list args;
  85. char buf[lineSize]={0};
  86. va_start (args, format);
  87. vsprintf (buf, format, args);
  88. decl.push_back(buf);
  89. va_end (args);
  90. }