22 #include "usModuleResourceBuffer_p.h"
30 #ifdef US_PLATFORM_WINDOWS
31 #define DATA_NEEDS_NEWLINE_CONVERSION 1
32 #undef REMOVE_LAST_NEWLINE_IN_TEXT_MODE
34 #undef DATA_NEEDS_NEWLINE_CONVERSION
35 #define REMOVE_LAST_NEWLINE_IN_TEXT_MODE 1
40 class ModuleResourceBufferPrivate
44 ModuleResourceBufferPrivate(
void* data, std::size_t size,
const char* begin, std::ios_base::openmode mode)
49 , uncompressedData(reinterpret_cast<unsigned char*>(data))
50 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
56 ~ModuleResourceBufferPrivate()
58 free(uncompressedData);
61 const char*
const begin;
62 const char*
const end;
65 const std::ios_base::openmode mode;
67 unsigned char* uncompressedData;
69 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
71 std::streambuf::pos_type pos;
76 ModuleResourceBuffer::ModuleResourceBuffer(
void* data, std::size_t _size,
77 std::ios_base::openmode mode)
83 char* begin =
reinterpret_cast<char*
>(data);
84 std::size_t size = _size;
86 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
87 if (data != NULL && !(mode & std::ios_base::binary) && begin[0] ==
'\r')
94 #ifdef REMOVE_LAST_NEWLINE_IN_TEXT_MODE
95 if (data != NULL && !(mode & std::ios_base::binary) && begin[size-1] ==
'\n')
101 d =
new ModuleResourceBufferPrivate(data, size, begin, mode);
104 ModuleResourceBuffer::~ModuleResourceBuffer()
109 ModuleResourceBuffer::int_type ModuleResourceBuffer::underflow()
111 if (d->current == d->end)
112 return traits_type::eof();
114 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
115 char c = *d->current;
116 if (!(d->mode & std::ios_base::binary))
120 if (d->current + 1 == d->end)
122 return traits_type::eof();
127 return traits_type::to_int_type(c);
129 return traits_type::to_int_type(*d->current);
133 ModuleResourceBuffer::int_type ModuleResourceBuffer::uflow()
135 if (d->current == d->end)
136 return traits_type::eof();
138 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
139 char c = *d->current++;
140 if (!(d->mode & std::ios_base::binary))
144 if (d->current == d->end)
146 return traits_type::eof();
151 return traits_type::to_int_type(c);
153 return traits_type::to_int_type(*d->current++);
157 ModuleResourceBuffer::int_type ModuleResourceBuffer::pbackfail(int_type ch)
160 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
161 if (!(d->mode & std::ios_base::binary))
163 while ((d->current - backOffset) >= d->begin && d->current[backOffset] ==
'\r')
170 if (d->current == d->begin || (ch != traits_type::eof() && ch != d->current[backOffset]))
172 return traits_type::eof();
175 d->current += backOffset;
176 return traits_type::to_int_type(*d->current);
179 std::streamsize ModuleResourceBuffer::showmanyc()
181 assert(d->current <= d->end);
183 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
184 std::streamsize ssize = 0;
185 std::size_t chunkSize = d->end - d->current;
186 for (std::size_t i = 0; i < chunkSize; ++i)
188 if (d->current[i] !=
'\r')
195 return d->end - d->current;
199 std::streambuf::pos_type ModuleResourceBuffer::seekoff(std::streambuf::off_type off,
200 std::ios_base::seekdir way,
201 std::ios_base::openmode )
203 #ifdef DATA_NEEDS_NEWLINE_CONVERSION
204 std::streambuf::off_type step = 1;
205 if (way == std::ios_base::beg)
207 d->current = d->begin;
209 else if (way == std::ios_base::end)
215 if (!(d->mode & std::ios_base::binary))
217 if (way == std::ios_base::beg)
221 else if (way == std::ios_base::end)
226 std::streambuf::off_type i = 0;
230 if (*d->current !=
'\r')
239 if (way == std::ios_base::end)
245 const std::streampos currInternalPos = d->current - d->begin;
246 while (i != currInternalPos)
248 if (d->begin[i] !=
'\r')
259 d->pos = d->current - d->begin;
263 if (way == std::ios_base::beg)
265 d->current = d->begin + off;
268 else if (way == std::ios_base::cur)
271 return d->current - d->begin;
275 d->current = d->end + off;
276 return d->current - d->begin;
281 std::streambuf::pos_type ModuleResourceBuffer::seekpos(std::streambuf::pos_type sp,
282 std::ios_base::openmode )
284 return this->seekoff(sp, std::ios_base::beg);