00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_serializer.hpp"
00012
00013 #include <string.h>
00014
00015 namespace Xrb
00016 {
00017
00018 Serializer::Serializer ()
00019 :
00020 m_is_open(false),
00021 m_io_direction(IOD_NONE),
00022 m_error(IOE_NONE)
00023 {
00024 }
00025
00026 Serializer::~Serializer ()
00027 {
00028 ASSERT1(!IsOpen() && "A Serializer must not be in an open state upon destruction.");
00029 ASSERT1(GetIODirection() == IOD_NONE);
00030 }
00031
00032 Uint32 Serializer::ReadString (char **const destination)
00033 {
00034 ASSERT1(destination != NULL);
00035
00036 char buffer[MAX_SUPPORTED_STRING_BUFFER_SIZE];
00037 Uint32 const read_string_length =
00038 ReadBufferString(buffer, MAX_SUPPORTED_STRING_BUFFER_SIZE);
00039 if (Error() == IOE_NONE)
00040 {
00041 *destination = new char[read_string_length + 1];
00042 memcpy(*destination, buffer, read_string_length + 1);
00043 }
00044 return read_string_length;
00045 }
00046
00047 Uint32 Serializer::WriteString (char const *const source)
00048 {
00049 ASSERT1(source != NULL);
00050
00051 Uint32 actual_string_length = static_cast<Uint32>(strlen(source));
00052 Uint32 written_string_length =
00053 WriteBufferString(
00054 source,
00055 Min(actual_string_length + 1, static_cast<Uint32>(MAX_SUPPORTED_STRING_BUFFER_SIZE)));
00056 return written_string_length;
00057 }
00058
00059 void Serializer::ReadStdString (std::string *const destination, Uint32 *const string_length)
00060 {
00061 ASSERT1(destination != NULL);
00062
00063 char buffer[MAX_SUPPORTED_STRING_BUFFER_SIZE];
00064 Uint32 const read_string_length =
00065 ReadBufferString(buffer, MAX_SUPPORTED_STRING_BUFFER_SIZE);
00066 if (string_length != NULL)
00067 *string_length = read_string_length;
00068 if (Error() != IOE_NONE)
00069 return;
00070 *destination = buffer;
00071 }
00072
00073 void Serializer::WriteStdString (std::string const &source, Uint32 *const string_length)
00074 {
00075 Uint32 written_string_length =
00076 WriteBufferString(
00077 source.c_str(),
00078 static_cast<Uint32>(source.length()+1));
00079 if (string_length != NULL)
00080 *string_length = written_string_length;
00081 }
00082
00083 }