00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "data/stream.hpp"
00035 #include "data/exception.hpp"
00036 #include "data/string.hpp"
00037
00038 #include <cstdio>
00039 #include <cwchar>
00040 #include <cctype>
00041
00042 #ifdef WIN32
00043 #pragma warning (disable : 4996)
00044 #define snprintf _snprintf
00045 #define snscanf _snscanf
00046 #endif
00047
00048 namespace gsgl
00049 {
00050
00051 namespace io
00052 {
00053
00054 static const int BUF_LEN = 64;
00055
00056 text_stream::text_stream()
00057 {}
00058
00059 text_stream::~text_stream()
00060 {}
00061
00062 text_stream & text_stream::operator<< (const wchar_t * str)
00063 {
00064 this->write(str, (gsgl::index_t) ::wcslen(str));
00065 return *this;
00066 }
00067
00068 text_stream & text_stream::operator<< (const wchar_t & ch)
00069 {
00070
00071 this->write(&ch, 1);
00072 return *this;
00073 }
00074
00075 text_stream & text_stream::operator<< (const int & n)
00076 {
00077 wchar_t buf[BUF_LEN];
00078 buf[BUF_LEN-1] = 0;
00079 swprintf(buf, BUF_LEN-1, L"%d", n);
00080 this->write(buf, (gsgl::index_t) ::wcslen(buf));
00081 return *this;
00082 }
00083
00084 text_stream & text_stream::operator<< (const double & n)
00085 {
00086 wchar_t buf[BUF_LEN];
00087 buf[BUF_LEN-1] = 0;
00088 swprintf(buf, BUF_LEN-1, L"%f", n);
00089 this->write(buf, (gsgl::index_t) ::wcslen(buf));
00090 return *this;
00091 }
00092
00093 text_stream & text_stream::operator<< (const float & n)
00094 {
00095 wchar_t buf[BUF_LEN];
00096 buf[BUF_LEN-1] = 0;
00097 swprintf(buf, BUF_LEN-1, L"%f", n);
00098 this->write(buf, (gsgl::index_t) ::wcslen(buf));
00099 return *this;
00100 }
00101
00102 text_stream & text_stream::operator<< (const char *str)
00103 {
00104 return *this << gsgl::string(str);
00105 }
00106
00107
00108 text_stream & operator<< (text_stream & s, const printable & p)
00109 {
00110 p.to_stream(s);
00111 return s;
00112 }
00113
00114
00115
00116
00117 text_stream & text_stream::operator>> (wchar_t & ch)
00118 {
00119
00120 this->read(&ch, 1);
00121 return *this;
00122 }
00123
00124 text_stream & text_stream::operator>> (int & n)
00125 {
00126 wchar_t buf[BUF_LEN];
00127 int pos = 0;
00128 buf[pos] = 0;
00129
00130 do
00131 {
00132 this->read(&buf[pos++], 1);
00133 }
00134 while (pos < BUF_LEN && !this->at_end() && isdigit(buf[pos-1]));
00135 buf[pos] = 0;
00136
00137 swscanf(buf, L"%d", &n);
00138 return *this;
00139 }
00140
00141 text_stream & text_stream::operator>> (double & n)
00142 {
00143 wchar_t buf[BUF_LEN];
00144 int pos = 0;
00145 buf[pos] = 0;
00146
00147 do
00148 {
00149 this->read(&buf[pos++], 1);
00150 }
00151 while (pos < BUF_LEN && !this->at_end() && !isspace(buf[pos-1]));
00152 buf[pos] = 0;
00153
00154 swscanf(buf, L"%f", &n);
00155 return *this;
00156 }
00157
00158 text_stream & text_stream::operator>> (float & n)
00159 {
00160 double dd;
00161 *this >> dd;
00162 n = (float) dd;
00163 return *this;
00164 }
00165
00166
00167 text_stream & operator>> (text_stream & s, printable & p)
00168 {
00169 p.from_stream(s);
00170 return s;
00171 }
00172
00173
00174
00175
00176 data_stream::data_stream()
00177 {
00178 }
00179
00180
00181 data_stream::~data_stream()
00182 {
00183 }
00184
00185
00186 data_stream & data_stream::operator<< (const unsigned char & c)
00187 {
00188 const size_t bytes_written = write(&c, sizeof(unsigned char));
00189
00190 if (bytes_written != sizeof(unsigned char))
00191 throw io_exception(L"Error writing char.");
00192 return *this;
00193 }
00194
00195
00196 data_stream & data_stream::operator<< (const int & n)
00197 {
00198 const size_t bytes_written = write(reinterpret_cast<const unsigned char *>(&n), sizeof(int));
00199
00200 if (bytes_written != sizeof(int))
00201 throw io_exception(L"Error writing int.");
00202
00203 return *this;
00204 }
00205
00206
00207 data_stream & data_stream::operator<< (const double & n)
00208 {
00209 const size_t bytes_written = write(reinterpret_cast<const unsigned char *>(&n), sizeof(double));
00210
00211 if (bytes_written != sizeof(double))
00212 throw io_exception(L"Error writing double.");
00213 return *this;
00214 }
00215
00216
00217 data_stream & data_stream::operator<< (const float & n)
00218 {
00219 const size_t bytes_written = write(reinterpret_cast<const unsigned char *>(&n), sizeof(float));
00220
00221 if (bytes_written != sizeof(float))
00222 throw io_exception(L"Error writing float.");
00223 return *this;
00224 }
00225
00226
00227 data_stream & operator<< (data_stream & st, const serializable & ss)
00228 {
00229 ss.to_stream(st);
00230 return st;
00231 }
00232
00233
00234
00235
00236 data_stream & data_stream::operator>> (unsigned char & ch)
00237 {
00238 const size_t bytes_read = read(&ch, sizeof(unsigned char));
00239
00240 if (bytes_read != sizeof(unsigned char))
00241 throw io_exception(L"Error reading char.");
00242
00243 return *this;
00244 }
00245
00246
00247 data_stream & data_stream::operator>> (int & n)
00248 {
00249 const size_t bytes_read = read(reinterpret_cast<unsigned char *>(&n), sizeof(int));
00250
00251 if (bytes_read != sizeof(int))
00252 throw io_exception(L"Error reading int.");
00253 return *this;
00254 }
00255
00256
00257 data_stream & data_stream::operator>> (double & n)
00258 {
00259 const size_t bytes_read = read(reinterpret_cast<unsigned char *>(&n), sizeof(double));
00260
00261 if (bytes_read != sizeof(double))
00262 throw io_exception(L"Error reading double.");
00263 return *this;
00264 }
00265
00266
00267 data_stream & data_stream::operator>> (float & n)
00268 {
00269 const size_t bytes_read = read(reinterpret_cast<unsigned char *>(&n), sizeof(float));
00270
00271 if (bytes_read != sizeof(float))
00272 throw io_exception(L"Error reading float.");
00273
00274 return *this;
00275 }
00276
00277
00278 data_stream & operator>> (data_stream & st, serializable & ss)
00279 {
00280 ss.from_stream(st);
00281 return st;
00282 }
00283
00284
00285 }
00286
00287 }