|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | + |
| 6 | +#ifndef __STACKSTRING_H_ |
| 7 | +#define __STACKSTRING_H_ |
| 8 | + |
| 9 | +#include "malloc.h" |
| 10 | + |
| 11 | +template <SIZE_T STACKCOUNT, class T> |
| 12 | +class StackString |
| 13 | +{ |
| 14 | +private: |
| 15 | + T m_innerBuffer[STACKCOUNT + 1]; |
| 16 | + T * m_buffer; |
| 17 | + SIZE_T m_size; // actual allocated size |
| 18 | + SIZE_T m_count; // actual length of string |
| 19 | + |
| 20 | + void NullTerminate() |
| 21 | + { |
| 22 | + m_buffer[m_count] = 0; |
| 23 | + } |
| 24 | + |
| 25 | + void DeleteBuffer() |
| 26 | + { |
| 27 | + if (m_innerBuffer != m_buffer) |
| 28 | + free(m_buffer); |
| 29 | + |
| 30 | + m_buffer = NULL; |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + void ReallocateBuffer(SIZE_T count) |
| 35 | + { |
| 36 | + // count is always > STACKCOUNT here. |
| 37 | + T * newBuffer = (T *)malloc((count + 1) * sizeof(T)); |
| 38 | + if (NULL == newBuffer) |
| 39 | + { |
| 40 | + SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
| 41 | + |
| 42 | + DeleteBuffer(); |
| 43 | + m_count = 0; |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + DeleteBuffer(); |
| 49 | + m_buffer = newBuffer; |
| 50 | + m_count = count; |
| 51 | + m_size = count+1; |
| 52 | + |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + void Resize(SIZE_T count) |
| 57 | + { |
| 58 | + if (NULL == m_buffer) |
| 59 | + { |
| 60 | + if (count > STACKCOUNT) |
| 61 | + { |
| 62 | + ReallocateBuffer(count); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + m_size = STACKCOUNT+1; |
| 67 | + m_buffer = m_innerBuffer; |
| 68 | + m_count = count; |
| 69 | + } |
| 70 | + } |
| 71 | + else if (m_innerBuffer == m_buffer) |
| 72 | + { |
| 73 | + if (count > STACKCOUNT) |
| 74 | + { |
| 75 | + ReallocateBuffer(count); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + m_count = count; |
| 80 | + m_size = STACKCOUNT+1; |
| 81 | + } |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + ReallocateBuffer(count); |
| 86 | + } |
| 87 | + |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + StackString(const StackString &s) |
| 92 | + { |
| 93 | + Set(s); |
| 94 | + } |
| 95 | + |
| 96 | +public: |
| 97 | + StackString() |
| 98 | + : m_buffer(m_innerBuffer), m_count(0) |
| 99 | + { |
| 100 | + } |
| 101 | + |
| 102 | + BOOL Set(const T * buffer, SIZE_T count) |
| 103 | + { |
| 104 | + Resize(count); |
| 105 | + if (NULL == m_buffer) |
| 106 | + return FALSE; |
| 107 | + |
| 108 | + CopyMemory(m_buffer, buffer, (count + 1) * sizeof(T)); |
| 109 | + NullTerminate(); |
| 110 | + return TRUE; |
| 111 | + } |
| 112 | + |
| 113 | + BOOL Set(const StackString &s) |
| 114 | + { |
| 115 | + return Set(s.m_buffer, s.m_count); |
| 116 | + } |
| 117 | + |
| 118 | + SIZE_T GetCount() const |
| 119 | + { |
| 120 | + return m_count; |
| 121 | + } |
| 122 | + |
| 123 | + SIZE_T GetAllocationCount() const |
| 124 | + { |
| 125 | + return m_size; |
| 126 | + } |
| 127 | + |
| 128 | + SIZE_T GetSizeOf() const |
| 129 | + { |
| 130 | + return m_size * sizeof(T); |
| 131 | + } |
| 132 | + |
| 133 | + CONST T * GetString() const |
| 134 | + { |
| 135 | + return (const T *)m_buffer; |
| 136 | + } |
| 137 | + |
| 138 | + T * ExtractString() |
| 139 | + { |
| 140 | + T * ret_buffer; |
| 141 | + |
| 142 | + if (m_innerBuffer != m_buffer) |
| 143 | + { |
| 144 | + //it is heap allocated just return the memory |
| 145 | + ret_buffer = m_buffer; |
| 146 | + m_buffer = NULL; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + ret_buffer = (T *)malloc((m_count + 1) * sizeof(T)); |
| 151 | + if (NULL == ret_buffer) |
| 152 | + { |
| 153 | + SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
| 154 | + return NULL; |
| 155 | + } |
| 156 | + CopyMemory(ret_buffer, m_buffer, (m_count + 1) * sizeof(T)); |
| 157 | + ret_buffer[m_count] = 0; |
| 158 | + } |
| 159 | + return ret_buffer; |
| 160 | + } |
| 161 | + |
| 162 | + T * OpenStringBuffer(SIZE_T count) |
| 163 | + { |
| 164 | + Resize(count); |
| 165 | + return (T *)m_buffer; |
| 166 | + } |
| 167 | + |
| 168 | + void Clear() |
| 169 | + { |
| 170 | + m_count = 1; |
| 171 | + NullTerminate(); |
| 172 | + return; |
| 173 | + } |
| 174 | + void CloseBuffer(SIZE_T count) |
| 175 | + { |
| 176 | + if (m_count > count) |
| 177 | + m_count = count; |
| 178 | + |
| 179 | + NullTerminate(); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + ~StackString() |
| 184 | + { |
| 185 | + DeleteBuffer(); |
| 186 | + } |
| 187 | +}; |
| 188 | + |
| 189 | +#if _DEBUG |
| 190 | +typedef StackString<32, CHAR> PathCharString; |
| 191 | +typedef StackString<32, WCHAR> PathWCharString; |
| 192 | +#else |
| 193 | +typedef StackString<260, CHAR> PathCharString; |
| 194 | +typedef StackString<260, WCHAR> PathWCharString; |
| 195 | +#endif |
| 196 | +#endif |
0 commit comments