13 #if !defined(XPACE_FILE_H) 23 # pragma warning(push) 24 # pragma warning(disable : 4251) 113 virtual void fromHandle
122 virtual bool operator!
137 virtual size_t getBufSize
166 template <
typename T>
173 template <
typename T>
184 size_t maxLen = 0x100000);
190 (
size_t maxLen = 0x100000);
203 template <
typename T>
220 template <
typename T>
229 template <
typename T>
237 template <
typename T>
243 virtual Position getLength
250 virtual Position getLength
255 virtual Position getPos
302 enum { SEQ_BUF_SIZE = 65536, RAND_BUF_SIZE = 16384 };
317 virtual bool seekFile
319 virtual size_t fillBuffer
324 virtual size_t fileRead
327 virtual size_t fileWrite
337 enum { OS_BUF_SIZE = 4096 };
340 std::vector<byte> g_buffer;
341 std::vector<byte> temp;
350 Exception(
"Can't open file \"%1\" for %2.")
356 addParam(
"sequential read");
359 addParam(
"random read");
362 addParam(
"sequential write");
365 addParam(
"sequential write new)");
368 addParam(
"random write");
371 addParam(
"random write (new)");
382 Exception(
"Can't read file \"%1\" at position \"%2\".")
394 Exception(
"Can't write file \"%1\" at position \"%2\".")
406 Exception(
"File \"%1\" corrupt at position \"%2\".")
434 return (hd == INVALID_HANDLE_VALUE);
461 size_t bytes_to_read = std::min(static_cast<size_t>(end - current), bytes);
462 memcpy(dest, current, bytes_to_read);
463 if (bytes_to_read == bytes)
469 + fileRead(reinterpret_cast<byte*>(dest) + bytes_to_read,
470 bytes - bytes_to_read);
473 template <
typename T>
478 return (read(reinterpret_cast<byte*>(dest),
sizeof(T)) ==
sizeof(T));
481 template <
typename T>
492 template <
typename T>
498 size_t bytes(count *
sizeof(T));
499 size_t bytes_to_get = std::min(static_cast<size_t>(end - current), bytes);
500 if (bytes_to_get == bytes)
502 *dest =
reinterpret_cast<T*
>(current);
507 if ((current == end) && (bytes <= bufSize))
510 bufOffset += bufSize;
511 *dest =
reinterpret_cast<T*
>(buffer);
512 size_t ret(std::min(bytes, fillBuffer()));
513 current = buffer + ret;
514 return ret /
sizeof(T);
517 if (bytes > g_buffer.size())
518 g_buffer.resize(bytes);
521 bytes = read(d = &g_buffer[0], bytes);
522 *dest =
reinterpret_cast<T*
>(d);
523 return bytes /
sizeof(T);
527 template <
typename T>
533 byte* start(current);
534 T* t(reinterpret_cast<T*>(current));
540 if (t >= reinterpret_cast<T*>(end))
542 uint copy_size(end - start);
545 temp.resize(copy_size);
546 memcpy(&temp[0], start, copy_size);
551 bufOffset += end - buffer;
557 t =
reinterpret_cast<T*
>(buffer);
565 current =
reinterpret_cast<byte*
>(t);
570 ret = temp.size() + current - buffer;
571 if (ret > g_buffer.size())
572 g_buffer.resize(ret);
573 memcpy(&g_buffer[0], &temp[0], temp.size());
574 if (current - buffer)
575 memcpy(&g_buffer[temp.size()], buffer, current - buffer);
576 *dest =
reinterpret_cast<T*
>(&g_buffer[0]);
580 *dest =
reinterpret_cast<T*
>(start);
581 ret = current - start;
585 current +=
sizeof(T);
587 return ret /
sizeof(T);
590 template <
typename T>
596 return (
get(reinterpret_cast<byte**>(&t),
sizeof(T)) ==
sizeof(T))
608 size_t bytes_to_copy = std::min(bufSize - static_cast<size_t>(current - buffer), bytes);
609 memcpy(current, src, bytes_to_copy);
612 if (bytes_to_copy == bytes)
615 bytes = bytes_to_copy
616 + fileWrite(reinterpret_cast<const byte*>(src) + bytes_to_copy,
617 bytes - bytes_to_copy);
619 end = std::max(end, current);
624 template <
typename T>
629 return (write(&src,
sizeof(T)) ==
sizeof(T));
638 if (static_cast<int>(buffer - current) <= dist)
646 if (end - current >= dist)
652 return seek(getPos() + dist);
660 return end - current;
666 # pragma warning(pop) const Xpace_Char16 Xpace_Data_Type type
size_t bufSize
actual buffer size
create for (primarily) random writing
size_t write(const void *src, size_t bytes)
Write some data to the file.
byte * current
current position in buffer
an untyped file from an external source
Type type
the file's type
create for (primarily) sequential writing
A string, Unicode UTF-16 and reference-counted.
void readString(MemBitStream *b, CH *ch, size_t length)
const Position offset
start offset of this ref list
virtual size_t getBufSize() const
bool seekRel(Distance dist)
Move current position.
virtual Position getPos() const
Get current position.
unsigned long long uint64
bool writeString(const CH *str, size_t length, MemBitStream *b)
match any type (except raw)
uint64 bufOffset
offset of buffer in file
bool bufDirty
is buffer modified
size_t bufRemains() const
const Xpace_Char16 * name
Sink callbacks for table data.
T read()
Read a T from the file.
open existing for (primarily) random reading
bool seekable
is not a special file
bool writable
opened for write
T * get()
Get a T by direct access to the file buffer return not guaranteed to be valid after successive calls...
thrown if either of the above two fails e.g., if rhs is not read-only
byte * end
end of data in buffer
Segment(File *file, Position offset, Position size)
static const Position errorPosition
const Position size
length of this ref list
open existing or new for (primarily) sequential writing
virtual String getName() const
Xpace project main namespace
open existing or new for (primarily) random writing
open existing for (primarily) sequential reading
size_t getUntil(T **dest, T delim)
Get bytes until a delimiter.