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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00047
#include "pl_io_deprecated.h"
00048
#include <plearn/base/stringutils.h>
00049
#include "pl_streambuf.h"
00050
00051
namespace PLearn {
00052
using namespace std;
00053
00054
00055 void writeHeader(ostream& out,
const string& classname,
int version)
00056 { out <<
"<" << classname <<
":" << version <<
">\n"; }
00057
00058 void writeFooter(ostream& out,
const string& classname)
00059 { out <<
"</" << classname <<
">\n"; }
00060
00061 int readHeader(istream& in,
const string& classname)
00062 {
00063
string header;
00064
#if defined(_MINGW_) || defined(WIN32)
00065
in.tellg();
00066
00067
#endif
00068
in >>
header;
00069
int headerlen = (
int)
header.length();
00070 in.get();
00071
int classnamelen = (
int)classname.length();
00072
if ( headerlen<classnamelen+2
00073 ||
header[0]!=
'<' ||
header.substr(1,classnamelen)!=classname
00074 || (
header[1+classnamelen]!=
'>' &&
header[1+classnamelen]!=
':') )
00075
PLERROR(
"In Object::readHeader WRONG HEADER: %s (SHOULD BE {s:version>)",
header.c_str(),classname.c_str());
00076
if (
header[1+classnamelen]==
':')
00077
return toint(
header.substr(2+classnamelen, headerlen-classnamelen-2));
00078
else return 0;
00079 }
00080
00081 void readFooter(istream& in,
const string& classname)
00082 {
00083
string footer;
00084 in >> footer;
00085
string correctfooter =
string(
"</")+classname+
">";
00086
if(footer != correctfooter)
00087
PLERROR(
"In Object::readFooter WRONG FOOTER: %s (SHOULD BE %s)",footer.c_str(),correctfooter.c_str());
00088 in.get();
00089 }
00090
00091
00092 void writeFieldName(ostream& out,
const string& fieldname)
00093 { out << fieldname <<
": "; }
00094
00095 bool readFieldName(istream& in,
const string& fieldname,
bool force)
00096 {
00097
pl_streambuf* buffer = dynamic_cast<pl_streambuf*>(in.rdbuf());
00098
pl_streammarker fence(buffer);
00099
string word;
00100 in >> word;
00101
if(word != fieldname+
":")
00102 {
00103
if (force)
00104
PLERROR(
"In readFieldName read %s while expected fieldname was %s",word.c_str(),fieldname.c_str());
00105
else {
00106
00107
00108
00109
00110
00111
00112
00113
00114 buffer->
seekmark(fence);
00115
return false;
00116 }
00117 }
00118 in.get();
00119
return true;
00120
00121 }
00122
00123
00124
00125
00126
00127
00128 void fwrite_int(FILE *f,
const int* ptr,
int n,
bool is_file_bigendian)
00129 {
00130
#ifdef LITTLEENDIAN
00131
if(is_file_bigendian)
00132 {
00133
reverse_int(ptr,n);
00134 fwrite(ptr,
sizeof(
int),n,f);
00135
reverse_int(ptr,n);
00136 }
00137
else
00138 fwrite(ptr,
sizeof(
int),n,f);
00139
#endif
00140
#ifdef BIGENDIAN
00141
if(is_file_bigendian)
00142 fwrite(ptr,
sizeof(
int),n,f);
00143
else
00144 {
00145
reverse_int(ptr,n);
00146 fwrite(ptr,
sizeof(
int),n,f);
00147
reverse_int(ptr,n);
00148 }
00149
#endif
00150
}
00151
00152 void fwrite_float(FILE *f,
const float* ptr,
int n,
bool is_file_bigendian)
00153 {
00154
#ifdef LITTLEENDIAN
00155
if(is_file_bigendian)
00156 {
00157
reverse_float(ptr,n);
00158 fwrite(ptr,
sizeof(
float),n,f);
00159
reverse_float(ptr,n);
00160 }
00161
else
00162 fwrite(ptr,
sizeof(
float),n,f);
00163
#endif
00164
#ifdef BIGENDIAN
00165
if(is_file_bigendian)
00166 fwrite(ptr,
sizeof(
float),n,f);
00167
else
00168 {
00169
reverse_float(ptr,n);
00170 fwrite(ptr,
sizeof(
float),n,f);
00171
reverse_float(ptr,n);
00172 }
00173
#endif
00174
}
00175
00176 void fwrite_float(FILE *f,
const double* ptr,
int n,
bool is_file_bigendian)
00177 {
00178
float* fptr =
new float[n];
00179
for(
int i=0; i<n; i++)
00180 fptr[i] = float(ptr[i]);
00181
fwrite_float(f,fptr,n,is_file_bigendian);
00182
delete[] fptr;
00183 }
00184
00185 void fwrite_double(FILE *f,
const double* ptr,
int n,
bool is_file_bigendian)
00186 {
00187
#ifdef LITTLEENDIAN
00188
if(is_file_bigendian)
00189 {
00190
reverse_double(ptr,n);
00191 fwrite(ptr,
sizeof(
double),n,f);
00192
reverse_double(ptr,n);
00193 }
00194
else
00195 fwrite(ptr,
sizeof(
double),n,f);
00196
#endif
00197
#ifdef BIGENDIAN
00198
if(is_file_bigendian)
00199 fwrite(ptr,
sizeof(
double),n,f);
00200
else
00201 {
00202
reverse_double(ptr,n);
00203 fwrite(ptr,
sizeof(
double),n,f);
00204
reverse_double(ptr,n);
00205 }
00206
#endif
00207
}
00208
00209 void fwrite_double(FILE *f,
const float* ptr,
int n,
bool is_file_bigendian)
00210 {
00211
double* dptr =
new double[n];
00212
for(
int i=0; i<n; i++)
00213 dptr[i] = double(ptr[i]);
00214
fwrite_double(f,dptr,n,is_file_bigendian);
00215
delete[] dptr;
00216 }
00217
00218
00219
00220 void fread_int(FILE *f,
int* ptr,
int n,
bool is_file_bigendian)
00221 {
00222 fread(ptr,
sizeof(
int),n,f);
00223
#ifdef LITTLEENDIAN
00224
if(is_file_bigendian)
00225
reverse_int(ptr,n);
00226
#endif
00227
#ifdef BIGENDIAN
00228
if(!is_file_bigendian)
00229
reverse_int(ptr,n);
00230
#endif
00231
}
00232
00233 void fread_float(FILE *f,
float* ptr,
int n,
bool is_file_bigendian)
00234 {
00235 fread(ptr,
sizeof(
float),n,f);
00236
#ifdef LITTLEENDIAN
00237
if(is_file_bigendian)
00238
reverse_float(ptr,n);
00239
#endif
00240
#ifdef BIGENDIAN
00241
if(!is_file_bigendian)
00242
reverse_float(ptr,n);
00243
#endif
00244
}
00245
00246 void fread_float(FILE *f,
double* ptr,
int n,
bool is_file_bigendian)
00247 {
00248
float* fptr =
new float[n];
00249
fread_float(f,fptr,n,is_file_bigendian);
00250
for(
int i=0; i<n; i++)
00251 ptr[i] = double(fptr[i]);
00252
delete[] fptr;
00253 }
00254
00255 void fread_double(FILE *f,
double* ptr,
int n,
bool is_file_bigendian)
00256 {
00257 fread(ptr,
sizeof(
double),n,f);
00258
#ifdef LITTLEENDIAN
00259
if(is_file_bigendian)
00260
reverse_double(ptr,n);
00261
#endif
00262
#ifdef BIGENDIAN
00263
if(!is_file_bigendian)
00264
reverse_double(ptr,n);
00265
#endif
00266
}
00267
00268 void fread_double(FILE *f,
float* ptr,
int n,
bool is_file_bigendian)
00269 {
00270
double* dptr =
new double[n];
00271
fread_double(f,dptr,n,is_file_bigendian);
00272
for(
int i=0; i<n; i++)
00273 ptr[i] = float(dptr[i]);
00274
delete[] dptr;
00275 }
00276
00277 void fread_short(FILE *f,
unsigned short* ptr,
int n,
bool is_file_bigendian)
00278 {
00279 fread(ptr,
sizeof(
unsigned short),n,f);
00280
#ifdef LITTLEENDIAN
00281
if(is_file_bigendian)
00282
reverse_ushort(ptr,n);
00283
#endif
00284
#ifdef BIGENDIAN
00285
if(!is_file_bigendian)
00286
reverse_ushort(ptr,n);
00287
#endif
00288
}
00289
00290 void write_int(ostream& out,
const int* ptr,
int n,
bool is_file_bigendian)
00291 {
00292
#ifdef LITTLEENDIAN
00293
if(is_file_bigendian)
00294 {
00295
reverse_int(ptr,n);
00296 out.write((
char*)ptr,n*
sizeof(
int));
00297
reverse_int(ptr,n);
00298 }
00299
else
00300 out.write((
char*)ptr,n*
sizeof(
int));
00301
#endif
00302
#ifdef BIGENDIAN
00303
if(is_file_bigendian)
00304 out.write((
char*)ptr,n*
sizeof(
int));
00305
else
00306 {
00307
reverse_int(ptr,n);
00308 out.write((
char*)ptr,n*
sizeof(
int));
00309
reverse_int(ptr,n);
00310 }
00311
#endif
00312
}
00313
00314 void write_short(ostream& out,
const short* ptr,
int n,
bool is_file_bigendian)
00315 {
00316
#ifdef LITTLEENDIAN
00317
if(is_file_bigendian)
00318 {
00319
reverse_short(ptr,n);
00320 out.write((
char*)ptr,n*
sizeof(
short));
00321
reverse_short(ptr,n);
00322 }
00323
else
00324 out.write((
char*)ptr,n*
sizeof(
short));
00325
#endif
00326
#ifdef BIGENDIAN
00327
if(is_file_bigendian)
00328 out.write((
char*)ptr,n*
sizeof(
short));
00329
else
00330 {
00331
reverse_short(ptr,n);
00332 out.write((
char*)ptr,n*
sizeof(
short));
00333
reverse_short(ptr,n);
00334 }
00335
#endif
00336
}
00337
00338 void write_double(ostream& out,
const double* ptr,
int n,
bool is_file_bigendian)
00339 {
00340
#ifdef LITTLEENDIAN
00341
if(is_file_bigendian)
00342 {
00343
reverse_double(ptr,n);
00344 out.write((
char*)ptr,n*
sizeof(
double));
00345
reverse_double(ptr,n);
00346 }
00347
else
00348 out.write((
char*)ptr,n*
sizeof(
double));
00349
#endif
00350
#ifdef BIGENDIAN
00351
if(is_file_bigendian)
00352 out.write((
char*)ptr,n*
sizeof(
double));
00353
else
00354 {
00355
reverse_double(ptr,n);
00356 out.write((
char*)ptr,n*
sizeof(
double));
00357
reverse_double(ptr,n);
00358 }
00359
#endif
00360
}
00361
00362
00363 void write_float(ostream& out,
const float* ptr,
int n,
bool is_file_bigendian)
00364 {
00365
#ifdef LITTLEENDIAN
00366
if(is_file_bigendian)
00367 {
00368
reverse_float(ptr,n);
00369 out.write((
char*)ptr,n*
sizeof(
float));
00370
reverse_float(ptr,n);
00371 }
00372
else
00373 out.write((
char*)ptr,n*
sizeof(
float));
00374
#endif
00375
#ifdef BIGENDIAN
00376
if(is_file_bigendian)
00377 out.write((
char*)ptr,n*
sizeof(
float));
00378
else
00379 {
00380
reverse_float(ptr,n);
00381 out.write((
char*)ptr,n*
sizeof(
float));
00382
reverse_float(ptr,n);
00383 }
00384
#endif
00385
}
00386
00387
00388
00389
00390 void read_int(istream& in,
int* ptr,
int n,
bool is_file_bigendian)
00391 {
00392 in.read((
char *)ptr,n*
sizeof(
int));
00393
#ifdef LITTLEENDIAN
00394
if(is_file_bigendian)
00395
reverse_int(ptr,n);
00396
#endif
00397
#ifdef BIGENDIAN
00398
if(!is_file_bigendian)
00399
reverse_int(ptr,n);
00400
#endif
00401
}
00402
00403 void read_short(istream& in,
short* ptr,
int n,
bool is_file_bigendian)
00404 {
00405 in.read((
char *)ptr,n*
sizeof(
short));
00406
#ifdef LITTLEENDIAN
00407
if(is_file_bigendian)
00408
reverse_short(ptr,n);
00409
#endif
00410
#ifdef BIGENDIAN
00411
if(!is_file_bigendian)
00412
reverse_short(ptr,n);
00413
#endif
00414
}
00415
00416 void read_float(istream& in,
float* ptr,
int n,
bool is_file_bigendian)
00417 {
00418 in.read((
char *)ptr,n*
sizeof(
float));
00419
#ifdef LITTLEENDIAN
00420
if(is_file_bigendian)
00421
reverse_float(ptr,n);
00422
#endif
00423
#ifdef BIGENDIAN
00424
if(!is_file_bigendian)
00425
reverse_float(ptr,n);
00426
#endif
00427
}
00428
00429 void read_double(istream& in,
double* ptr,
int n,
bool is_file_bigendian)
00430 {
00431 in.read((
char *)ptr,n*
sizeof(
double));
00432
#ifdef LITTLEENDIAN
00433
if(is_file_bigendian)
00434
reverse_double(ptr,n);
00435
#endif
00436
#ifdef BIGENDIAN
00437
if(!is_file_bigendian)
00438
reverse_double(ptr,n);
00439
#endif
00440
}
00441
00442
00443
00444 }
00445