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 #ifndef _LTI_IO_HANDLER_H_
00034 #define _LTI_IO_HANDLER_H_
00035
00036
00037 #include "ltiObject.h"
00038 #include "ltiTypes.h"
00039 #include <string>
00040 #include <iostream>
00041
00042 namespace lti {
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class ioHandler : public object {
00062 public:
00063
00064
00065
00066
00067 ioHandler();
00068
00069
00070
00071
00072 ioHandler(const ioHandler& other);
00073
00074
00075
00076
00077 virtual ~ioHandler();
00078
00079
00080
00081
00082 virtual const char* getTypeName() const;
00083
00084
00085
00086
00087
00088
00089 ioHandler& copy(const ioHandler& other);
00090
00091
00092
00093
00094 virtual ioHandler* clone() const = 0;
00095
00096
00097
00098
00099
00100
00101
00102 virtual void use(std::ostream& aStream) = 0;
00103
00104
00105
00106
00107
00108
00109
00110 virtual void use(std::istream& aStream) = 0;
00111
00112
00113
00114
00115 virtual bool writeBegin();
00116
00117
00118
00119
00120 virtual bool writeEnd();
00121
00122
00123
00124
00125 virtual bool readBegin();
00126
00127
00128
00129
00130 virtual bool readEnd();
00131
00132
00133
00134
00135
00136
00137
00138 virtual bool write(const std::string& data) = 0;
00139 virtual bool write(const char* data) = 0;
00140 virtual bool write(const double& data) = 0;
00141 virtual bool write(const float& data) = 0;
00142 virtual bool write(const int& data) = 0;
00143 virtual bool write(const unsigned int& data) = 0;
00144 virtual bool write(const char& data) = 0;
00145 virtual bool write(const byte& data) = 0;
00146 virtual bool write(const ubyte& data) = 0;
00147 virtual bool write(const bool& data) = 0;
00148 virtual bool write(const long& data) = 0;
00149 virtual bool write(const unsigned long& data) = 0;
00150 virtual bool write(const short& data) = 0;
00151 virtual bool write(const unsigned short& data) = 0;
00152
00153
00154
00155
00156
00157
00158 virtual bool write(const std::string& name,const std::string& data);
00159 virtual bool write(const std::string& name,const char* data);
00160 virtual bool write(const std::string& name,const double& data);
00161 virtual bool write(const std::string& name,const float& data);
00162 virtual bool write(const std::string& name,const int& data);
00163 virtual bool write(const std::string& name,const unsigned int& data);
00164 virtual bool write(const std::string& name,const char& data);
00165 virtual bool write(const std::string& name,const byte& data);
00166 virtual bool write(const std::string& name,const ubyte& data);
00167 virtual bool write(const std::string& name,const bool& data);
00168 virtual bool write(const std::string& name,const long& data);
00169 virtual bool write(const std::string& name,const unsigned long& data);
00170 virtual bool write(const std::string& name,const short& data);
00171 virtual bool write(const std::string& name,const unsigned short& data);
00172
00173
00174
00175
00176
00177
00178
00179
00180 virtual bool read(std::string& data) = 0;
00181 virtual bool read(double& data) = 0;
00182 virtual bool read(float& data) = 0;
00183 virtual bool read(int& data) = 0;
00184 virtual bool read(unsigned int& data) = 0;
00185 virtual bool read(char& data) = 0;
00186 virtual bool read(byte& data) = 0;
00187 virtual bool read(ubyte& data) = 0;
00188 virtual bool read(bool& data) = 0;
00189 virtual bool read(long& data) = 0;
00190 virtual bool read(unsigned long& data) = 0;
00191 virtual bool read(short& data) = 0;
00192 virtual bool read(unsigned short& data) = 0;
00193
00194
00195
00196
00197
00198
00199 virtual bool read(const std::string& name, std::string& data);
00200 virtual bool read(const std::string& name, double& data);
00201 virtual bool read(const std::string& name, float& data);
00202 virtual bool read(const std::string& name, int& data);
00203 virtual bool read(const std::string& name, unsigned int& data);
00204 virtual bool read(const std::string& name, char& data);
00205 virtual bool read(const std::string& name, byte& data);
00206 virtual bool read(const std::string& name, ubyte& data);
00207 virtual bool read(const std::string& name, bool& data);
00208 virtual bool read(const std::string& name, long& data);
00209 virtual bool read(const std::string& name, unsigned long& data);
00210 virtual bool read(const std::string& name, short& data);
00211 virtual bool read(const std::string& name, unsigned short& data);
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 virtual bool writeSymbol(const std::string& data) = 0;
00223
00224
00225
00226
00227
00228 virtual bool writeComment(const std::string& data) = 0;
00229
00230
00231
00232
00233
00234 virtual bool writeComment(const char* data) = 0;
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 virtual bool readSymbol(std::string& data) = 0;
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 virtual bool trySymbol(const std::string& data) = 0;
00258
00259
00260
00261
00262
00263
00264
00265 virtual bool tryBegin() = 0;
00266
00267
00268
00269
00270
00271
00272
00273 virtual bool tryEnd() = 0;
00274
00275
00276
00277
00278
00279
00280 virtual bool writeSpaces(const int& s=1) = 0;
00281
00282
00283
00284
00285
00286 virtual bool writeEOL() = 0;
00287
00288
00289
00290
00291
00292
00293 virtual bool writeKeyValueSeparator() = 0;
00294
00295
00296
00297
00298
00299 virtual bool writeDataSeparator() = 0;
00300
00301
00302
00303
00304 virtual bool readKeyValueSeparator() = 0;
00305
00306
00307
00308
00309 virtual bool readDataSeparator() = 0;
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 virtual const int& getLevel() const;
00330
00331
00332
00333
00334
00335 virtual bool restoreLevel() = 0;
00336
00337
00338
00339
00340
00341 virtual void resetLevel(const int& theLevel = 0);
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 virtual const char* getStatusString() const;
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 virtual void setStatusString(const char* msg) const;
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 virtual void setStatusString(const std::string& msg) const;
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 virtual void appendStatusString(const char* msg) const;
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 virtual void appendStatusString(const std::string& msg) const;
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 virtual void appendStatusString(const int& msg) const;
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 virtual void appendStatusString(const double& msg) const;
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 virtual void appendContextStatus() const;
00449
00450
00451 protected:
00452
00453
00454
00455 int level;
00456
00457 private:
00458
00459
00460
00461 mutable char* statusString;
00462
00463
00464
00465
00466 static const char *const emptyString;
00467
00468 };
00469
00470
00471
00472
00473
00474 bool read(ioHandler& handler,std::string& data);
00475 bool read(ioHandler& handler,double& data);
00476 bool read(ioHandler& handler,float& data);
00477 bool read(ioHandler& handler,int& data);
00478 bool read(ioHandler& handler,unsigned int& data);
00479 bool read(ioHandler& handler,char& data);
00480 bool read(ioHandler& handler,byte& data);
00481 bool read(ioHandler& handler,ubyte& data);
00482 bool read(ioHandler& handler,bool& data);
00483 bool read(ioHandler& handler,long& data);
00484 bool read(ioHandler& handler,unsigned long& data);
00485 bool read(ioHandler& handler,short& data);
00486 bool read(ioHandler& handler,unsigned short& data);
00487
00488
00489
00490
00491
00492
00493 bool write(ioHandler& handler,const std::string& data);
00494 bool write(ioHandler& handler,const char* data);
00495 bool write(ioHandler& handler,const double& data);
00496 bool write(ioHandler& handler,const float& data);
00497 bool write(ioHandler& handler,const int& data);
00498 bool write(ioHandler& handler,const unsigned int& data);
00499 bool write(ioHandler& handler,const char& data);
00500 bool write(ioHandler& handler,const byte& data);
00501 bool write(ioHandler& handler,const ubyte& data);
00502 bool write(ioHandler& handler,const bool& data);
00503 bool write(ioHandler& handler,const long& data);
00504 bool write(ioHandler& handler,const unsigned long& data);
00505 bool write(ioHandler& handler,const short& data);
00506 bool write(ioHandler& handler,const unsigned short& data);
00507
00508
00509
00510
00511
00512 template <class T>
00513 bool write(ioHandler& handler,const std::string& name,
00514 const T& data,const bool complete=true) {
00515
00516 if (complete) {
00517 handler.writeBegin();
00518 }
00519 handler.writeSymbol(name);
00520 handler.writeKeyValueSeparator();
00521 write(handler,data);
00522 if (complete) {
00523 handler.writeEnd();
00524 }
00525 return handler.writeEOL();
00526 };
00527
00528
00529
00530
00531 template <class T>
00532 bool read(ioHandler& handler,const std::string& name,
00533 T& data,const bool complete=true) {
00534 int level = handler.getLevel();
00535 bool result = true;
00536
00537 if (complete) {
00538 result = handler.readBegin();
00539 }
00540
00541 if (result && handler.trySymbol(name)) {
00542 result = result && handler.readKeyValueSeparator();
00543
00544 result = result && read(handler,data);
00545
00546 if (complete) {
00547 result = result && handler.readEnd();
00548 }
00549
00550 while (result && (handler.getLevel() > level)) {
00551 result = result && handler.readEnd();
00552 }
00553
00554 } else {
00555 result=false;
00556 handler.appendStatusString("\nSymbol " + name + " not found: ");
00557 handler.appendContextStatus();
00558 handler.restoreLevel();
00559 }
00560
00561 return result;
00562 };
00563
00564 }
00565
00566 #endif