bundle.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Int i;
  34. for (i = 0; i < strTab.size(); i++)
  35. ios << strTab[i];
  36. }
  37. /* Writes the contents of the bundle (procedure code and declaration) to
  38. * a file. */
  39. void writeBundle (std::ostream &ios, bundle procCode)
  40. {
  41. writeStrTab (ios, procCode.decl);
  42. if (procCode.decl[procCode.decl.size() - 1][0] != ' ')
  43. ios << "\n";
  44. writeStrTab (ios, procCode.code);
  45. }
  46. /* Frees the storage allocated by the string table. */
  47. static void freeStrTab (strTable &strTab)
  48. {
  49. strTab.clear();
  50. }
  51. void freeBundle (bundle *procCode)
  52. /* Deallocates the space taken by the bundle procCode */
  53. {
  54. freeStrTab (procCode->decl);
  55. freeStrTab (procCode->code);
  56. }
  57. void bundle::appendCode(const char *format,...)
  58. {
  59. va_list args;
  60. char buf[lineSize]={0};
  61. va_start (args, format);
  62. vsprintf (buf, format, args);
  63. code.push_back(buf);
  64. va_end (args);
  65. }
  66. void bundle::appendDecl(const char *format,...)
  67. {
  68. va_list args;
  69. char buf[lineSize]={0};
  70. va_start (args, format);
  71. vsprintf (buf, format, args);
  72. decl.push_back(buf);
  73. va_end (args);
  74. }