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