bundle.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if(at(idx).size()<4)
  22. at(idx)=s;
  23. else
  24. at(idx) = string(s)+at(idx).substr(4);
  25. }
  26. /* Writes the contents of the string table on the file fp. */
  27. static void writeStrTab (std::ostream &ios, strTable &strTab)
  28. {
  29. for (size_t i = 0; i < strTab.size(); i++)
  30. ios << strTab[i];
  31. }
  32. /* Writes the contents of the bundle (procedure code and declaration) to
  33. * a file. */
  34. void writeBundle (std::ostream &ios, bundle procCode)
  35. {
  36. writeStrTab (ios, procCode.decl);
  37. writeStrTab (ios, procCode.code);
  38. }
  39. /* Frees the storage allocated by the string table. */
  40. static void freeStrTab (strTable &strTab)
  41. {
  42. strTab.clear();
  43. }
  44. /* Deallocates the space taken by the bundle procCode */
  45. void freeBundle (bundle *procCode)
  46. {
  47. freeStrTab (procCode->decl);
  48. freeStrTab (procCode->code);
  49. }
  50. void bundle::appendCode(const char *format,...)
  51. {
  52. va_list args;
  53. char buf[lineSize]={0};
  54. va_start (args, format);
  55. vsprintf (buf, format, args);
  56. code.push_back(buf);
  57. va_end (args);
  58. }
  59. void bundle::appendCode(const std::string &s)
  60. {
  61. code.push_back(s);
  62. }
  63. void bundle::appendDecl(const char *format,...)
  64. {
  65. va_list args;
  66. char buf[lineSize]={0};
  67. va_start (args, format);
  68. vsprintf (buf, format, args);
  69. decl.push_back(buf);
  70. va_end (args);
  71. }
  72. void bundle::appendDecl(const std::string &v)
  73. {
  74. decl.push_back(v);
  75. }