bundle.cpp 2.0 KB

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