Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* mBaseName;
- mBaseName = attributes->BaseName;
- static const char* const suffixes[] = {"Version1", "Version", "Version3"}; //etc.
- static char* GetSuffix()
- {
- int i = 0;
- //perform checks on some data structures
- i = somevalue;
- return suffixes[i];
- }
- void LoadStuff()
- {
- char* suffix = GetSuffix();
- char* nameToUse = new char[50];
- sprintf(nameToUse, "%s%s",mBaseName,suffix);
- LoadAndSetupData(nameToUse);
- }
- LoadAndSetupData(mBaseName + GetSuffix());
- template<class T>
- class ArrayGuard {
- public:
- ArrayGuard(T* ptr) { _ptr = ptr; }
- ~ArrayGuard() { delete[] _ptr; }
- private:
- T* _ptr;
- ArrayGuard(const ArrayGuard&);
- ArrayGuard& operator=(const ArrayGuard&);
- }
- char* buffer = new char[50];
- ArrayGuard<char *> bufferGuard(buffer);
- class StringPool
- {
- struct StringReference {
- char *buffer;
- time_t created;
- } *Pool;
- size_t PoolSize;
- size_t Allocated;
- static const size_t INITIAL_SIZE = 100;
- void GrowBuffer()
- {
- StringReference *newPool = new StringReference[PoolSize * 2];
- for (size_t i = 0; i < Allocated; ++i)
- newPool[i] = Pool[i];
- StringReference *oldPool = Pool;
- Pool = newPool;
- delete[] oldPool;
- }
- public:
- StringPool() : Pool(new StringReference[INITIAL_SIZE]), PoolSize(INITIAL_SIZE)
- {
- }
- ~StringPool()
- {
- ClearPool();
- delete[] Pool;
- }
- char *GetBuffer(size_t size)
- {
- if (Allocated == PoolSize)
- GrowBuffer();
- Pool[Allocated].buffer = new char[size];
- Pool[Allocated].buffer = time(NULL);
- ++Allocated;
- }
- void ClearPool()
- {
- for (size_t i = 0; i < Allocated; ++i)
- delete[] Pool[i].buffer;
- Allocated = 0;
- }
- void ClearBefore(time_t knownCleared)
- {
- size_t newAllocated = 0;
- for (size_t i = 0; i < Allocated; ++i)
- {
- if (Pool[i].created < knownCleared)
- {
- delete[] Pool[i].buffer;
- }
- else
- {
- Pool[newAllocated] = Pool[i];
- ++newAllocated;
- }
- }
- Allocated = newAllocated;
- }
- // This compares pointers, not strings!
- void ReleaseBuffer(char *knownCleared)
- {
- size_t newAllocated = 0;
- for (size_t i = 0; i < Allocated; ++i)
- {
- if (Pool[i].buffer == knownCleared)
- {
- delete[] Pool[i].buffer;
- }
- else
- {
- Pool[newAllocated] = Pool[i];
- ++newAllocated;
- }
- }
- Allocated = newAllocated;
- }
- };
- char * LoadStuff()
- {
- char* suffix = GetSuffix();
- char* nameToUse = new char[50];
- sprintf("%s%s",mBaseName,suffix);
- LoadAndSetupData(nameToUse);
- return nameToUse;
- }
- char *name = LoadStuff();
- // do whatever you need to do:
- delete [] name;
- char nameToUse[50];
- snprintf(nameToUse, sizeof(nameToUse), "%s%s",mBaseName,suffix);
- LoadAndSetupData(mBaseName + GetSuffix());
- char nameToUse[50];
- static char *nameToUse = new char[50];
Add Comment
Please, Sign In to add comment