bundle.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /* Returns the next available index into the table */
  18. int nextBundleIdx (strTable *strTab)
  19. {
  20. return (strTab->size());
  21. }
  22. /* Adds the given label to the start of the line strTab[idx]. The first
  23. * tab is removed and replaced by this label */
  24. void addLabelBundle (strTable &strTab, int idx, int label)
  25. {
  26. char s[lineSize];
  27. sprintf (s, "l%ld: %s", label, strTab[idx].c_str()+4);
  28. strTab[idx] = s;
  29. }
  30. /* Writes the contents of the string table on the file fp. */
  31. static void writeStrTab (std::ostream &ios, strTable &strTab)
  32. {
  33. for (size_t i = 0; i < strTab.size(); i++)
  34. ios << strTab[i];
  35. }
  36. /* Writes the contents of the bundle (procedure code and declaration) to
  37. * a file. */
  38. void writeBundle (std::ostream &ios, bundle procCode)
  39. {
  40. writeStrTab (ios, procCode.decl);
  41. if (procCode.decl[procCode.decl.size() - 1][0] != ' ')
  42. ios << "\n";
  43. writeStrTab (ios, procCode.code);
  44. }
  45. /* Frees the storage allocated by the string table. */
  46. static void freeStrTab (strTable &strTab)
  47. {
  48. strTab.clear();
  49. }
  50. void freeBundle (bundle *procCode)
  51. /* Deallocates the space taken by the bundle procCode */
  52. {
  53. freeStrTab (procCode->decl);
  54. freeStrTab (procCode->code);
  55. }
  56. void bundle::appendCode(const char *format,...)
  57. {
  58. va_list args;
  59. char buf[lineSize]={0};
  60. va_start (args, format);
  61. vsprintf (buf, format, args);
  62. code.push_back(buf);
  63. va_end (args);
  64. }
  65. void bundle::appendDecl(const char *format,...)
  66. {
  67. va_list args;
  68. char buf[lineSize]={0};
  69. va_start (args, format);
  70. vsprintf (buf, format, args);
  71. decl.push_back(buf);
  72. va_end (args);
  73. }