Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <vector>
- #include <iostream> //std::cout, std::endl
- /////////////////////////////////////////////////////////////////
- /*
- / The class is templates, put both declaration and definition in headers
- / (either both in the same file or #include the template header after the declaration)
- /
- / !There might be errors, haven't slept the night :/
- /
- / *required includes:
- / - vector
- / - windows.h
- */
- // ------------- Declaration -------------
- template <typename _TValueType = TCHAR> //Default it with TCHAR
- class C_Strings
- {
- public:
- using value_type = std::remove_reference <
- std::remove_const<_TValueType>::type > ::type;
- using result_type = value_type;
- private:
- std::vector<result_type*> m_Queue;
- result_type *AddToQueue(value_type *pValue);
- result_type *GetLastItem();
- public:
- C_Strings() = default;
- ~C_Strings() {
- this->ClearQueue();
- }
- //Returns the number of elements it deleted
- size_t ClearQueue();
- //Formats a new string
- result_type *Format(value_type *fmt, ...);
- //Formats and appends to an existing string (allocates a new object, doesn't free the old one)
- result_type *FormatAppend(value_type *destination, value_type *fmt, ...);
- };
- // ------------- Definition -------------
- template <typename _TValueType>
- auto C_Strings<_TValueType>::AddToQueue(value_type *pValue) -> result_type * {
- this->m_Queue.push_back(pValue);
- return pValue;
- }
- template <typename _TValueType>
- auto C_Strings<_TValueType>::GetLastItem() -> result_type * {
- return this->m_Queue.back();
- }
- template <typename _TValueType>
- size_t C_Strings<_TValueType>::ClearQueue() {
- size_t counter = 0;
- for(result_type *p : m_Queue) {
- free(p);
- counter++;
- }
- return counter;
- }
- template <typename _TValueType>
- auto C_Strings<_TValueType>::Format(value_type *fmt, ...) -> result_type * {
- va_list va;
- va_start(va, fmt);
- size_t requiredBufferSize = static_cast<size_t>(vsnprintf(nullptr, 0, fmt, va)) + 1;
- if(requiredBufferSize) {
- vsnprintf(this->AddToQueue(static_cast<value_type*>(memset(new value_type[requiredBufferSize], 0, requiredBufferSize))),
- requiredBufferSize, fmt, va);
- return this->GetLastItem();
- }
- return nullptr;
- }
- template <typename _TValueType>
- auto C_Strings<_TValueType>::FormatAppend(value_type *destination, value_type *fmt, ...) -> result_type * {
- va_list va;
- va_start(va, fmt);
- size_t existingBufferSize = strlen(destination);
- size_t requiredBufferSize = static_cast<size_t>(vsnprintf(nullptr, 0, fmt, va)) + 1;
- if(requiredBufferSize) {
- this->AddToQueue(static_cast<value_type*>(memset(new value_type[existingBufferSize + requiredBufferSize], 0, existingBufferSize + requiredBufferSize)));
- value_type *pBuffer = this->GetLastItem();
- value_type *pFmtBuffer = pBuffer + existingBufferSize;
- strncpy(pBuffer, destination, existingBufferSize);
- vsnprintf(pFmtBuffer, requiredBufferSize, fmt, va);
- return pBuffer;
- }
- return nullptr;
- }
- ///////////////////////////////////////////////////////
- //! Simple tests.
- int main() {
- C_Strings <> x;
- std::cout << x.Format("Fmt: %s %d", "test", 1337) << std::endl;
- std::cout << x.FormatAppend("Append ", "Fmt: %s %d", "test", 1337) << std::endl;
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement