code_junkie

C++ string manipulation

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. char* mBaseName;
  2.  
  3. mBaseName = attributes->BaseName;
  4.  
  5. static const char* const suffixes[] = {"Version1", "Version", "Version3"}; //etc.
  6.  
  7. static char* GetSuffix()
  8. {
  9. int i = 0;
  10. //perform checks on some data structures
  11. i = somevalue;
  12. return suffixes[i];
  13. }
  14.  
  15. void LoadStuff()
  16. {
  17. char* suffix = GetSuffix();
  18. char* nameToUse = new char[50];
  19. sprintf(nameToUse, "%s%s",mBaseName,suffix);
  20.  
  21. LoadAndSetupData(nameToUse);
  22. }
  23.  
  24. LoadAndSetupData(mBaseName + GetSuffix());
  25.  
  26. template<class T>
  27. class ArrayGuard {
  28. public:
  29. ArrayGuard(T* ptr) { _ptr = ptr; }
  30. ~ArrayGuard() { delete[] _ptr; }
  31. private:
  32. T* _ptr;
  33. ArrayGuard(const ArrayGuard&);
  34. ArrayGuard& operator=(const ArrayGuard&);
  35. }
  36.  
  37. char* buffer = new char[50];
  38. ArrayGuard<char *> bufferGuard(buffer);
  39.  
  40. class StringPool
  41. {
  42. struct StringReference {
  43. char *buffer;
  44. time_t created;
  45. } *Pool;
  46.  
  47. size_t PoolSize;
  48. size_t Allocated;
  49.  
  50. static const size_t INITIAL_SIZE = 100;
  51.  
  52. void GrowBuffer()
  53. {
  54. StringReference *newPool = new StringReference[PoolSize * 2];
  55. for (size_t i = 0; i < Allocated; ++i)
  56. newPool[i] = Pool[i];
  57. StringReference *oldPool = Pool;
  58. Pool = newPool;
  59. delete[] oldPool;
  60. }
  61.  
  62. public:
  63.  
  64. StringPool() : Pool(new StringReference[INITIAL_SIZE]), PoolSize(INITIAL_SIZE)
  65. {
  66. }
  67.  
  68. ~StringPool()
  69. {
  70. ClearPool();
  71. delete[] Pool;
  72. }
  73.  
  74. char *GetBuffer(size_t size)
  75. {
  76. if (Allocated == PoolSize)
  77. GrowBuffer();
  78. Pool[Allocated].buffer = new char[size];
  79. Pool[Allocated].buffer = time(NULL);
  80. ++Allocated;
  81. }
  82.  
  83. void ClearPool()
  84. {
  85. for (size_t i = 0; i < Allocated; ++i)
  86. delete[] Pool[i].buffer;
  87. Allocated = 0;
  88. }
  89.  
  90. void ClearBefore(time_t knownCleared)
  91. {
  92. size_t newAllocated = 0;
  93. for (size_t i = 0; i < Allocated; ++i)
  94. {
  95. if (Pool[i].created < knownCleared)
  96. {
  97. delete[] Pool[i].buffer;
  98. }
  99. else
  100. {
  101. Pool[newAllocated] = Pool[i];
  102. ++newAllocated;
  103. }
  104. }
  105. Allocated = newAllocated;
  106. }
  107.  
  108. // This compares pointers, not strings!
  109. void ReleaseBuffer(char *knownCleared)
  110. {
  111. size_t newAllocated = 0;
  112. for (size_t i = 0; i < Allocated; ++i)
  113. {
  114. if (Pool[i].buffer == knownCleared)
  115. {
  116. delete[] Pool[i].buffer;
  117. }
  118. else
  119. {
  120. Pool[newAllocated] = Pool[i];
  121. ++newAllocated;
  122. }
  123. }
  124. Allocated = newAllocated;
  125. }
  126.  
  127. };
  128.  
  129. char * LoadStuff()
  130. {
  131. char* suffix = GetSuffix();
  132. char* nameToUse = new char[50];
  133. sprintf("%s%s",mBaseName,suffix);
  134.  
  135. LoadAndSetupData(nameToUse);
  136. return nameToUse;
  137. }
  138.  
  139. char *name = LoadStuff();
  140. // do whatever you need to do:
  141. delete [] name;
  142.  
  143. char nameToUse[50];
  144. snprintf(nameToUse, sizeof(nameToUse), "%s%s",mBaseName,suffix);
  145.  
  146. LoadAndSetupData(mBaseName + GetSuffix());
  147.  
  148. char nameToUse[50];
  149.  
  150. static char *nameToUse = new char[50];
Add Comment
Please, Sign In to add comment