bundle.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. writeStrTab (ios, procCode.code);
  42. }
  43. /* Frees the storage allocated by the string table. */
  44. static void freeStrTab (strTable &strTab)
  45. {
  46. strTab.clear();
  47. }
  48. /* Deallocates the space taken by the bundle procCode */
  49. void freeBundle (bundle *procCode)
  50. {
  51. freeStrTab (procCode->decl);
  52. freeStrTab (procCode->code);
  53. }
  54. void bundle::appendCode(const char *format,...)
  55. {
  56. va_list args;
  57. char buf[lineSize]={0};
  58. va_start (args, format);
  59. vsprintf (buf, format, args);
  60. code.push_back(buf);
  61. va_end (args);
  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. }