Changeset 241 in Sophya for trunk/SophyaLib
- Timestamp:
- Apr 21, 1999, 3:12:11 PM (26 years ago)
- Location:
- trunk/SophyaLib
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/SysTools.o.list
r219 r241 1 1 ctimer.o 2 exceptions.o2 md5.o 3 3 pdlmgr.o 4 4 periodic.o 5 pexceptions.o 5 6 ppersist.o 6 7 psighand.o -
trunk/SophyaLib/BaseTools/defs.h
r219 r241 1 #ifndef DEFS_SEEN 2 #define DEFS_SEEN 3 4 /*#define DEBUG*/ 5 6 #ifdef DEBUG 7 #define IMGRGCHECK 8 #define IMGVOIDCHECK 9 #endif 10 11 /* Sur mac, PP utilise fp.h plutot que math.h, et les deux sont incompatibles... */ 12 #ifdef __MWERKS__ 13 /* 14 #include <fp.h> 15 #define __MATH__ 16 */ 17 #endif 18 19 20 /********************************************************/ 21 /* Outil d'impressions pour debug */ 22 #define PR(_data_) {cout<<" "<<#_data_<<" "<<_data_;} 23 #define CR {cout<<"\n"<<flush;} 24 /* ********************************************************/ 25 26 /* Sur quelle machine sommes-nous ? */ 27 /* Il faudrait uniformiser tous les sources avec les memes conventions... */ 28 29 #if defined(__alpha) || defined(__alpha__) || defined(OSF1) || defined(DECALPHA) 30 #undef OSF1 31 #define OSF1 32 #undef DECALPHA 33 #define DECALPHA 34 #undef __alpha__ 35 #define __alpha__ 36 #endif 37 38 39 #if defined(_AIX) && !defined(AIX) 40 #define AIX 1 41 #endif 42 43 #if defined(__hpux__) && !defined(HPUX) 44 #define HPUX 1 45 #endif 46 47 48 #ifdef ultrix 49 #define DECULTRIX 1 50 #endif 51 52 #ifdef __ultrix__ 53 #define DECULTRIX 1 54 #endif 55 56 #ifdef THINK_CPLUS 57 #define __mac__ 58 #endif 59 60 #ifdef __MWERKS__ 61 #define __mac__ 62 #endif 63 64 /* Quelques variantes du C++ selon le compilateur */ 65 66 #define USESTRING 67 68 #define HAS_VEC_NEW 69 70 #ifdef __MWERKS__ 71 #define __ANSI_TEMPLATES__ 72 #undef HAS_VEC_NEW 73 #define COMPILER_EXCEPTIONS 74 #define NO_STRSTREAM 75 #define STREAMPOS_IS_CLASS 76 #define ITER_TAG 77 #endif 78 79 #ifdef __GNUG__ 80 #define __GNU_TEMPLATES__ 81 #endif 82 83 #ifdef __DECCXX 84 #if __DECCXX_VER > 60000000 85 #define CXX6 1 86 #define ITER_TAG 87 /* #define __GNU_TEMPLATES__ */ 88 #define __CXX_PRAGMA_TEMPLATES__ 89 #else 90 #define CXX5 1 91 #define __CXX_PRAGMA_TEMPLATES__ 92 /* Pour definir bool Reza 20/05/97 */ 93 #include <stl_macros> 94 #ifndef COMPILER_EXCEPTIONS 95 #define COMPILER_EXCEPTIONS 96 #endif 97 #endif 98 99 #endif 100 101 /* GCC 2.8.0 : exception, string::npos */ 102 #ifdef __GNUC__ 103 #if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8) 104 #undef COMPILER_EXCEPTIONS 105 #define COMPILER_EXCEPTIONS 106 #define NPOS string::npos 107 #endif 108 #endif 109 110 111 #ifndef __GNUG__ 112 /*typedef int bool;*/ 113 #define __PRETTY_FUNCTION__ __FILE__ " __LINE__ " 114 #ifndef __FUNCTION__ 115 #define __FUNCTION__ __FILE__ " __LINE__ " 116 #endif 117 #ifndef __KCC__ 118 #define __attribute__(_x_) 119 #endif 120 #endif 121 122 #ifdef __GNUG__ 123 #define HAS_NAMED_RETURN 0 124 #else 125 #define HAS_NAMED_RETURN 0 126 #endif 127 128 #ifdef THINK_CPLUS 129 #define ARG_FUNC_CPP_C 0 130 #else 131 #define ARG_FUNC_CPP_C 1 132 #endif 133 134 #ifdef __GNUG__ 135 #define HAS_EXPLICIT 136 #endif 137 138 #ifdef HAS_EXPLICIT 139 #define EXPLICIT explicit 140 #else 141 #define EXPLICIT 142 #endif 143 /* Quelques variantes de la librairie C++. */ 144 /* La librairie est AT&T par defaut, sauf si __ANSI_IO__ */ 145 146 /* $CHECK$ EA : a uniformiser */ 147 /* remplacer HAS_IOS_BIN par un selecteur qui indique si c'est ios::binary ou */ 148 /* ios::bin */ 149 150 #ifdef __MWERKS__ 151 /* #define __ANSI_IO__ */ 152 #endif 153 154 #if defined(THINK_CPLUS) 155 #define HAS_IOS_BIN 0 156 #define HAS_IOS_NOCREATE 1 157 #define FITS_IOS_IN_OPT ios::in | ios::nocreate 158 #define FITS_IOS_OUT_OPT ios::out 159 160 #elif defined(__MWERKS__) 161 #define HAS_IOS_BIN 1 162 #define HAS_IOS_NOCREATE 1 163 #define NPOS string::npos 164 165 #elif defined(__GNUG__) 166 #define HAS_IOS_BIN 1 167 #define HAS_IOS_NOCREATE 1 168 #define FITS_IOS_IN_OPT ios::in | ios::bin | ios::nocreate 169 #define FITS_IOS_OUT_OPT ios::out | ios::bin 170 171 #else 172 #define HAS_IOS_BIN 0 173 #define HAS_IOS_NOCREATE 0 174 #endif 175 176 #ifdef __ANSI_IO__ 177 #define seekg(p_, m_) rdbuf()->pubseekoff(p_,m_) 178 #define seekp(p_, m_) rdbuf()->pubseekoff(p_,m_) 179 #endif 180 181 182 #define STR2CH(x) ((string(x)).c_str()) 183 184 /* Des fonctions qui manquent sur certaines machines */ 185 186 #if defined(__DECCXX) 187 /* a cause des exceptions dans math.h */ 188 #define exception math_exception 189 #include <math.h> 190 #undef exception 191 #else 192 #include <math.h> 193 #endif 194 195 #if defined(__DECCXX) 196 /* Pour definir bool Reza 20/05/97 */ 197 #include <stl_macros> 198 #ifndef COMPILER_EXCEPTIONS 199 #define COMPILER_EXCEPTIONS 200 #endif 201 #endif 202 203 #if defined(__xlC) && defined (__cplusplus) 204 #include <bool.h> 205 #ifndef COMPILER_EXCEPTIONS 206 #define COMPILER_EXCEPTIONS 207 // NPOS - On utilise la STL de g++ 2.7.2 208 #endif 209 #endif 210 211 #if defined( __DECCXX ) || defined (__aCC__) 212 /* en fait si c'est RogueWave */ 213 /* Remis par Reza 20/05/97 */ 214 #define NPOS (size_t)-1 215 #endif 216 217 #if defined (__aCC__) 218 #ifndef COMPILER_EXCEPTIONS 219 #define COMPILER_EXCEPTIONS 220 #endif 221 #ifndef __ANSI_TEMPLATES__ 222 #define __ANSI_TEMPLATES__ 223 #endif 224 #endif 225 226 #if defined(__KCC__) 227 /* KCC V 3.2 ne compile pas i/ostream s ; long p = s.tellg(); OK en V 3.3 */ 228 #define STREAMPOS_IS_CLASS 229 #define NPOS string::npos 230 231 #ifndef COMPILER_EXCEPTIONS 232 #define COMPILER_EXCEPTIONS 233 #endif 234 #ifndef __ANSI_TEMPLATES__ 235 #define __ANSI_TEMPLATES__ 236 #endif 237 #endif 238 239 #ifndef M_PI 240 #define M_PI 3.1415926535 241 #endif 242 243 #ifdef __mac__ 244 #define hypot(_x_,_y_) sqrt((_x_)*(_x_) + (_y_)*(_y_)) 245 /* #define nintf(x) ((int) (x)) */ 246 #define random() (rand()*65538.0 + rand()*2. + rand()) 247 #define srandom srand 248 #define initstate(seed, tab, n) srand(seed) 249 #define nice(_x) 250 /*#define getpid() 1 */ 251 252 #ifdef __cplusplus 253 #include <string.h> 254 #include <string> 255 inline bool operator< (string const& s1, string const& s2) 256 { return (strcmp(s1.c_str(), s2.c_str()) < 0); } 257 #endif 258 #endif 259 260 261 262 #endif 263 1 #include "machdefs.h" -
trunk/SophyaLib/BaseTools/exceptions.h
r219 r241 1 // This may look like C code, but it is really -*- C++ -*- 2 #ifndef EXCEPTIONS_SEEN 3 #define EXCEPTIONS_SEEN 4 5 // Gestion des exceptions, conformemnt au manuel de reference C++ 6 // en attendant que les compilateurs le fassent tous seuls 7 // 8 // Eric Aubourg, la Silla fevrier 95 9 // Pour le moment, on ne sait gerer qu'un seul type d'exception, int. 10 // 11 // La syntaxe est 12 // TRY { 13 // .... 14 // } 15 // CATCH(i) { 16 // ... 17 // } 18 // ENDTRY 19 20 // ou bien CATCHALL a la place de CATCH. 21 // 22 23 // On peut faire THROW_ tout court, qui est pareil que THROW(0) 24 // ou bien THROW(n) 25 // ou bien THROW_SAME dans un bloc CATCH 26 27 // IMPORTANT 28 // Tout objet susceptible d'etre cree automatiquement au cours d'un bloc 29 // try doit etre declare EXC_AWARE (class x EXC_AWARE {...};) ou bien 30 // deriver d'une telle classe. 31 32 // Tout constructeur d'une classe qui contient d'autres objets parmi ses 33 // champs doit se terminer par END_CONSTRUCTOR. 34 35 #include "defs.h" 36 37 #include <iostream.h> 38 #include "peidainit.h" 39 40 //#define NO_EXCEPTIONS 41 #ifdef NO_EXCEPTIONS 42 43 #define EXC_ABORT_NEG(_x) 44 #define EXC_ABORT_ALL(_x) 45 46 #define EXC_AWARE 47 48 #define END_CONSTRUCTOR 49 50 #define TRY if (1) { 51 #define CATCH(_var) } else { long _var; 52 #define CATCHALL } else { 53 #define ENDTRY } 54 55 #define THROW(_i) \ 56 {cerr << "Throw " << _i << " file " << __FILE__ << " line " << __LINE__; \ 57 cerr << endl; \ 58 abort();} 59 60 #define THROW_ THROW(0) 61 62 #define THROW_SAME THROW(0) 63 64 #define ASSERT(_a_) if (!(_a_)) { \ 65 cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \ 66 << endl; \ 67 THROW_ } 68 69 #define RETURN(x) return(x) 70 #define RETURN_ return 71 72 #else 73 #ifdef COMPILER_EXCEPTIONS 74 75 #define EXC_ABORT_NEG(_x) 76 #define EXC_ABORT_ALL(_x) 77 78 #define EXC_AWARE 79 80 #define END_CONSTRUCTOR 81 82 #define TRY try 83 #define CATCH(_var) catch(long _var) 84 #define CATCHALL catch(...) 85 #define ENDTRY 86 87 #define THROW(_i) throw((long) _i); 88 89 #define THROW_ THROW(0) 90 91 #define THROW_SAME throw; 92 93 #define ASSERT(_a_) if (!(_a_)) { \ 94 cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \ 95 << endl; \ 96 THROW_ } 97 98 #define RETURN(x) return(x) 99 #define RETURN_ return 100 101 void InitFailNewHandler(); 102 #else 103 104 #include <setjmp.h> 105 #include <stdlib.h> 106 107 class ExcAwareObject; 108 109 class ExcHndl { 110 public: 111 ExcHndl(ExcHndl* up); 112 ~ExcHndl(); 113 114 void SetEnv(jmp_buf& theEnv) 115 #ifndef __GNUG__ 116 {memcpy(env, theEnv, sizeof(jmp_buf));} 117 #else 118 {env = theEnv;} 119 #endif 120 void Throw(int exc=0, char* file=0, int line=0); 121 void Add(ExcAwareObject*); 122 void Forget(ExcAwareObject*); 123 void Ignore(ExcAwareObject*); 124 void IgnoreTab(ExcAwareObject*, size_t); 125 void DelTab(ExcAwareObject*); 126 void EndConstructor(void*, size_t); 127 static void NoHandler(int exc=0, char* file=0, int line=0); 128 129 ExcHndl* nextHandler; 130 131 int theExc; 132 int hadExc; 133 134 private: 135 jmp_buf env; 136 int nObj, nObjAlloc; 137 ExcAwareObject** objects; 138 int nDyn, nDynAlloc; 139 ExcAwareObject** dynObjs; 140 int nDynTab, nDynTabAlloc; 141 ExcAwareObject** dynTabObjs; 142 size_t* dynTabSz; 143 }; 144 145 extern ExcHndl* topExcHndl; 146 extern int _ExcAbortNeg; 147 extern int _ExcAbortAll; 148 149 class ExcAwareObject { 150 friend class ExcHndl; 151 public: 152 ExcAwareObject() { 153 if (topExcHndl) topExcHndl->Add(this); 154 } 155 156 virtual ~ExcAwareObject() { 157 if (topExcHndl) topExcHndl->Forget(this); 158 } 159 160 #ifdef HAS_VEC_NEW 161 void* operator new(size_t s) { 162 void* p = ::operator new(s); 163 if (topExcHndl) topExcHndl->Ignore((ExcAwareObject*)p); 164 return p; 165 } 166 167 void* operator new(size_t /*s*/, void* p) { 168 if (topExcHndl) topExcHndl->Ignore((ExcAwareObject*)p); 169 return p; 170 } 171 172 void* operator new[](size_t s) { 173 void* p = ::operator new[](s); 174 if (topExcHndl) topExcHndl->IgnoreTab((ExcAwareObject*)p,s); 175 return p; 176 } 177 178 void operator delete[](void* p) { 179 if (topExcHndl) topExcHndl->DelTab((ExcAwareObject*)p); 180 ::operator delete[](p); 181 } 182 #else 183 void* operator new(size_t s) { 184 void* p = ::operator new(s); 185 if (topExcHndl) topExcHndl->IgnoreTab((ExcAwareObject*)p,s); 186 return p; 187 } 188 189 void* operator new(size_t s, void* p) { 190 if (topExcHndl) topExcHndl->IgnoreTab((ExcAwareObject*)p,s); 191 return p; 192 } 193 194 void operator delete(void* p) { 195 if (topExcHndl) topExcHndl->DelTab((ExcAwareObject*)p); 196 ::operator delete(p); 197 } 198 #endif 199 }; 200 201 //extern "C" void terminate(); 202 203 void InitFailNewHandler(); 204 205 // Et maintenant les macros! 206 207 // TRY cree un nouveau handler, et fait le setjmp 208 209 #define _UPHANDLER \ 210 { ExcHndl* tmp = topExcHndl->nextHandler; \ 211 delete topExcHndl; \ 212 topExcHndl = tmp; } 213 214 #define EXC_ABORT_NEG(_x) _ExcAbortNeg = _x; 215 #define EXC_ABORT_ALL(_x) _ExcAbortAll = _x; 216 217 #define EXC_AWARE : public virtual ExcAwareObject 218 219 #define END_CONSTRUCTOR \ 220 if (topExcHndl) topExcHndl->EndConstructor(this, sizeof(*this)); 221 222 #define TRY \ 223 { topExcHndl = new ExcHndl(topExcHndl); \ 224 jmp_buf _jmpBuffer; \ 225 volatile int _excCaught = 0; \ 226 if (!setjmp(_jmpBuffer)) { \ 227 topExcHndl->SetEnv(_jmpBuffer); 228 229 #define CATCH(_var) \ 230 _UPHANDLER \ 231 } else if (topExcHndl->hadExc) { \ 232 int _caughtException = topExcHndl->theExc; \ 233 int _var = _caughtException; \ 234 _excCaught = 1; \ 235 _UPHANDLER 236 237 #define CATCHALL \ 238 _UPHANDLER \ 239 } else if (topExcHndl->hadExc) { \ 240 int _caughtException = topExcHndl->theExc;\ 241 _excCaught = _caughtException; \ 242 _excCaught = 1; \ 243 _UPHANDLER 244 245 #define ENDTRY \ 246 } else _UPHANDLER\ 247 } 248 249 #define THROW(_i) \ 250 {if (!topExcHndl) ExcHndl::NoHandler(_i,__FILE__,__LINE__); \ 251 topExcHndl->Throw(_i,__FILE__,__LINE__);} 252 253 #define THROW_ THROW(0) 254 255 #define THROW_SAME THROW(_caughtException) 256 257 #define ASSERT(_a_) if (!(_a_)) { \ 258 cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \ 259 << endl; \ 260 THROW_ } 261 262 #define RETURN(x) { _UPHANDLER; return(x); } 263 #define RETURN_ { _UPHANDLER; return; } 264 265 #endif 266 #endif 267 #endif 1 #include "pexceptions.h" -
trunk/SophyaLib/BaseTools/machine.h
r229 r241 1 #ifndef MACHINE_H_SEEN2 #define MACHINE_H_SEEN3 4 #include "defs.h"5 6 #if defined(DECULTRIX) || defined(ULTRIX)7 8 typedef signed char int_1;9 typedef unsigned char uint_1;10 typedef long int_4;11 typedef unsigned long uint_4;12 typedef short int_2;13 typedef unsigned short uint_2;14 typedef float r_4;15 typedef double r_8;16 typedef long long int_8;17 typedef unsigned long long uint_8;18 19 #define SWAP 120 #define SWAPDEFAUT 121 22 #elif defined(DECALPHA) || defined(OSF1)23 24 typedef signed char int_1;25 typedef unsigned char uint_1;26 typedef int int_4;27 typedef unsigned int uint_4;28 typedef short int_2;29 typedef unsigned short uint_2;30 typedef float r_4;31 typedef double r_8;32 typedef long long int_8;33 typedef unsigned long long uint_8;34 35 #define SWAP 136 #define SWAPDEFAUT 137 38 #elif defined(HPUX)39 40 typedef signed char int_1;41 typedef unsigned char uint_1;42 typedef int int_4 ;43 typedef unsigned int uint_4;44 typedef short int_2;45 typedef unsigned short uint_2;46 typedef float r_4;47 typedef double r_8;48 typedef long long int_8;49 typedef unsigned long long uint_8;50 51 #define SWAP 052 #define SWAPDEFAUT 053 54 55 #elif defined( AIX )56 57 typedef signed char int_1;58 typedef unsigned char uint_1;59 typedef int int_4;60 typedef unsigned int uint_4;61 typedef short int_2;62 typedef unsigned short uint_2;63 typedef float r_4;64 typedef double r_8;65 typedef long long int_8;66 typedef unsigned long long uint_8;67 68 #define SWAP 069 #define SWAPDEFAUT 070 71 #elif defined( sun )72 73 typedef signed char int_1;74 typedef unsigned char uint_1;75 typedef int int_4;76 typedef unsigned int uint_4;77 typedef short int_2;78 typedef unsigned short uint_2;79 typedef float r_4;80 typedef double r_8;81 typedef long long int_8;82 typedef unsigned long long uint_8;83 84 #define SWAP 085 #define SWAPDEFAUT 086 87 #elif defined( IRIX64 )88 89 typedef signed char int_1;90 typedef unsigned char uint_1;91 typedef int int_4;92 typedef unsigned int uint_4;93 typedef short int_2;94 typedef unsigned short uint_2;95 typedef float r_4;96 typedef double r_8;97 typedef long long int_8;98 typedef unsigned long long uint_8;99 100 #define SWAP 0101 #define SWAPDEFAUT 0102 103 #elif defined(Linux) || defined(linux)104 105 typedef signed char int_1;106 typedef unsigned char uint_1;107 typedef int int_4;108 typedef unsigned int uint_4;109 typedef short int_2;110 typedef unsigned short uint_2;111 typedef float r_4;112 typedef double r_8;113 typedef long long int_8;114 typedef unsigned long long uint_8;115 116 #define SWAP 1117 #define SWAPDEFAUT 1118 119 #elif defined(__mac__)120 121 typedef signed char int_1;122 typedef unsigned char uint_1;123 typedef long int_4;124 typedef unsigned long uint_4;125 typedef short int_2;126 typedef unsigned short uint_2;127 typedef float r_4;128 typedef double r_8;129 typedef long long int_8;130 typedef unsigned long long uint_8;131 132 #define SWAP 0133 #define SWAPDEFAUT 0134 135 #else136 #error Undefined Machine type in machine.h137 #endif138 139 #endif140 -
trunk/SophyaLib/BaseTools/peidainit.h
r219 r241 5 5 #define PEIDAINIT_H_SEEN 6 6 7 #include " defs.h"7 #include "machdefs.h" 8 8 9 9 void PrintPeidaVersion(void); -
trunk/SophyaLib/BaseTools/perrors.h
r219 r241 1 #ifndef PERRORS_SEEN 2 #define PERRORS_SEEN 3 4 // Erreurs PEIDA 5 // Les erreurs graves sont negatives. Par defaut elles causent 6 // un abort, alors que les erreurs positives propagent une 7 // exception. 8 // voir les macros EXC_ABORT_NEG(flag) et EXC_ABORT_ALL(flag) 9 10 #include "exceptions.h" 11 enum { 12 allocationErr = -1, 13 nullPtrErr = -2, 14 sizeMismatchErr = -3, 15 rangeCheckErr = -4, 16 parmErr = -5, 17 forbiddenErr = -6, 18 assertionFailErr = -7, 19 20 typeMismatchErr = 1, 21 fileErr , 22 fitsImageErr , 23 endOfFileErr , 24 tapeErr , 25 tapeNoLogErr , 26 tapeNoLabelErr , 27 tapeEndMediaErr , 28 tapeNotReadyErr , 29 acqTapeErr , 30 acqRobotErr , 31 tarFileErr , 32 tarFileNoHeaderErr , 33 noStarsErr , 34 transfoErr , 35 fitsHeaderErr , 36 singMatxErr , 37 noCvgErr , 38 notFoundErr , 39 dupIdErr , 40 getEnvErr , 41 catchedSIGFPE , 42 catchedSIGSEGV , 43 catchedSIGINT , 44 catchedSIGQUIT , 45 catchedSIGUSR1 , 46 catchedSIGUSR2 , 47 inconsistentErr 48 }; 49 50 static inline char* PeidaExc(int i) { 51 switch (i) { 52 case allocationErr: return "Allocation Exception"; 53 case nullPtrErr: return "Null Pointer Exception"; 54 case sizeMismatchErr: return "Size Mismatch Exception"; 55 case rangeCheckErr: return "Range Check Exception"; 56 case parmErr: return "Invalid Parameter Exception"; 57 case forbiddenErr: return "Forbidden Function Exception"; 58 case assertionFailErr: return "Assertion Failed Exception"; 59 case typeMismatchErr: return "Type Mismatch Exception"; 60 case fileErr: return "File Error Exception"; 61 case fitsImageErr: return "FitsImage Error Exception"; 62 case endOfFileErr: return "End of File Error Exception"; 63 case tapeErr: return "Tape Error Exception"; 64 case tapeNoLogErr: return "Tape No Log File Exception"; 65 case tapeNoLabelErr: return "Tape No label Exception"; 66 case tapeEndMediaErr: return "Tape end data Exception"; 67 case tapeNotReadyErr: return "Tape not ready Exception"; 68 case acqTapeErr: return "Acq Tape Error Exception"; 69 case acqRobotErr: return "Acq Robot Error Exception"; 70 case tarFileErr: return "TarFile Error Exception"; 71 case tarFileNoHeaderErr: return "TarFile No Header Error Exception"; 72 case noStarsErr: return "No/Not Enough Stars Exception"; 73 case transfoErr: return "Transformation finding Exception"; 74 case fitsHeaderErr: return "Fits Header Exception"; 75 case singMatxErr: return "Singular Matrix Exception"; 76 case noCvgErr: return "No Convergence Exception"; 77 case notFoundErr: return "Not Found Exception"; 78 case dupIdErr: return "Duplicate Id Exception"; 79 case getEnvErr: return "GetEnv no var Exception"; 80 case inconsistentErr: return "Inconsistent Data Exception"; 81 case catchedSIGFPE: return "catched SIGFPE (floating point exception) Exception"; 82 case catchedSIGSEGV: return "catched SIGSEGV (segmentation fault) Exception"; 83 case catchedSIGINT: return "catched SIGINT (Interrupt signal) Exception"; 84 case catchedSIGQUIT: return "catched SIGQUIT (Quit signal) Exception"; 85 case catchedSIGUSR1: return "catched SIGUSR1 (User1 signal) Exception"; 86 case catchedSIGUSR2: return "catched SIGUSR2 (User2 signal) Exception"; 87 } 88 return "Unkown Exception"; 89 } 90 91 92 #define FAILNIL(_x_) \ 93 if (!(_x_)) THROW(nullPtrErr) 94 95 #define EXC_MSG(i) PeidaExc(i) << "(" << i << ")" 96 97 #ifdef DEBUG 98 #define DBFAILNIL(_x_) FAILNIL(_x_) 99 #define DBASSERT(_x_) ASSERT(_x_) 100 #else 101 #define DBFAILNIL(_x_) 102 #define DBASSERT(_x_) 103 #endif 104 105 #endif 1 #include "pexceptions.h" -
trunk/SophyaLib/BaseTools/ppersist.cc
r219 r241 1 #include "defs.h" 1 #include "machdefs.h" 2 #include "pexceptions.h" 2 3 #include <stdio.h> 3 4 #include "peidainit.h" 4 5 #include "ppersist.h" 5 #include "dates.h"6 7 #ifndef RFIO8 6 #include <fstream.h> 9 # endif7 #include <typeinfo> 10 8 11 9 … … 15 13 #endif 16 14 17 #if SWAP==1 18 #define IS_BIG_ENDIAN 0 19 #else 20 #define IS_BIG_ENDIAN 1 21 #endif 22 23 //++ 24 // Class PPersistMgr 15 using namespace PlanckDPC; 16 17 #define MAXTAGLEN 255 18 19 //++ 20 // Class PIOPersist 25 21 // Lib Outils++ 26 22 // include ppersist.h 27 23 // 28 // Classe gestionnaire d'objets persistants. Les méthodes statiques 29 // de cette classe permettent d'enregistrer des classes persistantes, 30 // et de relire automatiquement des objets. 31 //-- 32 33 //++ 34 // Links Voir 24 // Root class for persistant files. Handles the registration of 25 // persistant classes 26 //-- 27 28 //++ 29 // Links See 35 30 // PPersist 36 // PShPersist 37 //-- 38 39 PPersistMgr::ClassList* PPersistMgr::classList = NULL; 40 PShPersist::ObjList* PShPersist::objList = NULL; 41 42 //++ 43 void 44 PPersistMgr::RegisterClass(int_4 classId, ClassCreatorFunc f, bool shared) 45 // 46 // Enregistre une nouvelle classe auprès du gestionnaire. classId doit 47 // identifier de façon unique la classe. f doit créer un nouvel objet 48 // de cette classe (qui doit hériter de PPersist), non initialisé, et qui 49 // sera relu à partir du fichier d'objets persistants. 50 // 51 // Si shared est true, la classe doit hériter de PShPersist, et la persistance 52 // gère le fait que plusieurs objets Xi puissent faire référence à un même objet A. 53 // A, qui hérite donc de PShPersist, ne sera écrit qu'une seule fois dans le fichier 54 // lorsque les Xi sont écrits, et la référence à A sera mise à jour lors de la lecture 55 // des Xi... 56 //-- 57 { 58 DBASSERT(classList); 59 if (classList->size() && (classList->find(classId) != classList->end())) { 31 // PInPersist 32 // POutPersist 33 //-- 34 35 36 MD5_CTX PIOPersist::ctx; 37 PIOPersist::ClassList PIOPersist::classList; 38 39 40 //++ 41 void 42 PIOPersist::RegisterClass(uint_8 classId, ClassCreatorFunc f) 43 // 44 // Register a new persistant class. 45 // The classId is usually a hash of the class name, and this 46 // method is called only through the PPersistRegistrar template 47 // class, with the PPRegister(className) macro. 48 // 49 //-- 50 { 51 if (classList.size() && (classList.find(classId) != classList.end())) { 60 52 cerr << "RegisterClass : Error, " << hex << classId << dec 61 53 << " already registered." << endl; 62 THROW(dupIdErr);54 throw(DuplicateIdExc("PIOPersist::RegisterClass")); 63 55 } 64 56 65 ClassListEntry entry = {shared, f}; 66 (*classList)[classId] = entry; 67 } 68 69 70 PPersistMgr::ClassCreatorFunc 71 PPersistMgr::FindCreatorFunc(int_4 classId) 72 { 73 DBASSERT(classList); 74 return (*classList)[classId].f; 75 } 76 77 PPersistMgr::ClassCreatorFunc 78 PPersistMgr::FindCreatorFunc(int_4 classId, bool& isShared) 79 { 80 DBASSERT(classList); 81 ClassListEntry entry = (*classList)[classId]; 82 isShared = entry.shared; 83 return entry.f; 84 } 85 86 //++ 87 PPersist* 88 PPersistMgr::ReadObject(PInPersist& s) 89 // 90 // Lit un objet dans le stream s, en créant automatiquement un 91 // objet du bon type. 92 //-- 93 { 94 DBASSERT(classList); 95 DBASSERT(PShPersist::objList); 96 // On commence par lire le type de l'objet 97 int_4 classId; 98 s >> classId; 99 100 // On recupere le createur, et on regarde si l'objet est partage 101 bool shared; 102 if (!classList->size() || (classList->find(classId) == classList->end())) { 103 cerr << "PPersistMgr::ReadObject Error : Class " << classId << " not registered" << endl; 104 THROW(notFoundErr); 105 } 106 PPersistMgr::ClassCreatorFunc f = FindCreatorFunc(classId, shared); 107 108 if (!f) THROW(notFoundErr); 109 110 PPersist* object; 111 if (shared) { 112 // Si l'objet est partage, ce qui suit est son identificateur, sur 4 octets 113 // et un octet ensuite qui dit si l'objet est present. 114 115 int_4 objectId; 116 char objectSaved; 117 118 s >> objectId >> objectSaved; 119 120 #if 0 121 // On regarde si l'objet est deja en memoire 122 object = PShPersist::FindObject(objectId); 123 124 // On verifie qu'on est dans une situation coherente... 125 if ((!objectSaved) == (!object)) THROW(inconsistentErr); 126 #else 127 if (!objectSaved) { 128 object = PShPersist::FindObject(objectId); 129 if (!object) THROW(inconsistentErr); 130 } 131 #endif 132 if (objectSaved) { 133 object = f(); 134 object->ReadSelf(s); 135 ((PShPersist*)object)->mObjectID = objectId; 136 (*PShPersist::objList)[objectId] = (PShPersist*)object; 137 } 138 } else { 139 object = f(); 140 object->ReadSelf(s); 141 } 142 143 return object; 144 } 57 classList[classId] = f; 58 } 59 60 61 PIOPersist::ClassCreatorFunc 62 PIOPersist::FindCreatorFunc(uint_8 classId) 63 { 64 ClassList::iterator i = classList.find(classId); 65 if (i == classList.end()) throw(NotFoundExc("PIOPersist::FindCreatorFunc")); 66 return (*i).second; 67 } 68 145 69 146 70 //++ … … 168 92 169 93 //++ 170 // Links Voir94 // Links See 171 95 // PInPersist 172 96 // POutPersist 173 // PPersistMgr 174 // PShPersist 175 //-- 176 177 PShPersist* 178 PShPersist::FindObject(int_4 objId) 179 { 180 DBASSERT(objList); 181 return (*objList)[objId]; 182 } 97 // PIOPersist 98 //-- 183 99 184 100 //++ … … 213 129 //-- 214 130 { 215 // On doit tout d'abord ecrire notre type 216 s << ClassId(); 217 // Puis on s'ecrit, tout betement... 218 WriteSelf(s); 131 s.PutObject(this); 219 132 } 220 133 … … 226 139 // Relit l'objet dans le fichier ppersist. Il faut connaître a priori 227 140 // le type de l'objet. Pour une relecture avec création automatique du bon 228 // objet, utiliser PPersistMgr::ReadObject. 229 //-- 230 { 231 // A n'utiliser que si on connait a priori 232 // le vrai type de l'objet. 233 234 // On doit tout d'abord lire notre type 235 int_4 classId; 236 s >> classId; 237 if (classId != ClassId()) { 238 cerr << "PPersist::Read() Object type (=" << ClassId() << ") mismatch (type in file=" 239 << classId << ")" << endl; 240 THROW(typeMismatchErr); } 241 // Puis on se lit, tout betement... 242 243 ReadSelf(s); 244 } 245 246 //++ 247 int_4 248 PPersist::Write(POutPersist& s, int_4 key) const 249 // 250 // Ecrit l'objet dans le fichier PPersist avec un tag ayant "key" comme 251 // valeur du champ clé du tag. 252 //-- 253 { 254 int_4 rc; 255 // On ecrit le tag de positionnement et on recupere le numero de tag 256 rc = s.WriteTag(key, NULL); 257 // avant d'ecrire l'objet lui-meme 258 Write(s); 259 return(rc); 260 } 261 262 //++ 263 int_4 264 PPersist::Write(POutPersist& s, int_4 key, string& nom) const 265 // 266 // Ecrit l'objet dans le fichier PPersist avec un tag ayant "key", nom comme 267 // valeurs des champs clé et nom du tag. 268 //-- 269 { 270 int_4 rc; 271 // On ecrit le tag de positionnement et on recupere le numero de tag 272 rc = s.WriteTag(key, nom.c_str()); 273 // avant d'ecrire l'objet lui-meme 274 Write(s); 275 return(rc); 276 } 277 278 //++ 279 void 280 PPersist::ReadAtTag(PInPersist& s, int_4 tagid) 141 // objet, utiliser PInPersist::ReadObject. 142 // Il faut qu'on soit un objet ecrit 143 //-- 144 { 145 // We should be the exact type 146 // Check tag value 147 char ppstype; 148 s.GetRawByte(ppstype); 149 if (ppstype != PInPersist::PPS_OBJECT) { 150 throw TypeMismatchExc("PPersist::Read : not an object in flow"); 151 } 152 153 // Check class id 154 uint_8 classId; 155 s.GetRawU8(classId); 156 if (classId != PIOPersist::Hash(typeid(*this).name())) { 157 throw TypeMismatchExc("PPersist::Read : not the same object type"); 158 } 159 160 ReadSelf(s); 161 } 162 163 //++ 164 void 165 PPersist::Write(POutPersist& s, string const& tag) const 166 // 167 // Ecrit l'objet dans le fichier PPersist avec un tag 168 //-- 169 { 170 s.WriteTag(tag); 171 s.PutObject(this); 172 } 173 174 //++ 175 void 176 PPersist::ReadAtTag(PInPersist& s, string const& tag) 281 177 // 282 178 // Lit l'objet à la position du tag numéro "tagid". 283 179 //-- 284 180 { 285 if (s.GotoTag(tagid)) Read(s); 286 else { 287 cerr << "PInPersist::GotoTag() Error ! \n" << 288 "From PPersist::ReadAtTag(PInPersist, tag= " << tagid << ") " << endl; 289 THROW(fileErr); 290 } 291 return; 292 } 293 294 //++ 295 void 296 PPersist::ReadAtKey(PInPersist& s, int_4 key) 297 // 298 // Lit l'objet à la position du tag contenant la clé "key". 299 //-- 300 { 301 if (s.GotoKey(key)) Read(s); 302 else { 303 cerr << "PInPersist::GotoKey() Error ! \n" << 304 "From PPersist::ReadAtKey(PInPersist, key= " << key << ") " << endl; 305 THROW(fileErr); 306 } 307 return; 308 } 181 if (!s.GotoTag(tag)) 182 throw NotFoundExc("PPersist::ReadAtTag tag not found"); 183 Read(s); 184 } 185 309 186 310 187 //++ … … 320 197 321 198 322 //++323 // Class PShPersist324 // Lib Outils++325 // include ppersist.h326 //327 // Classe de base pour des objets persistants partagés.328 // Un tel objet ne sera écrit qu'une seule fois dans un fichier329 // donné. Les tentatives suivantes d'écriture n'écrivent qu'une330 // référence à la première écriture.331 //332 // Lors de la lecture, l'objet est créé lors de la première lecture,333 // puis l'adresse de cet objet est retournée lors des lectures suivantes.334 //--335 336 //++337 // Links Parents338 // PPersist339 //--340 341 //++342 void343 PShPersist::Write(POutPersist& s) const344 //345 // Ecrit l'objet dans le fichier s.346 //347 // En fait, l'objet est physiquement écrit une seule fois.348 //--349 {350 DBASSERT(objList);351 // On doit tout d'abord ecrire notre type352 353 s << ClassId();354 355 // On doit ensuite ecrire notre identificateur et le flag d'ecriture356 357 char write = (mObjectID == 0) ? 255 : 0;358 if (mObjectID == 0) {359 (int_4&)mObjectID = (*objList).size() ? (*((*objList).rbegin())).first+1 : 1;360 (*objList)[mObjectID] = (PShPersist*)this;361 }362 363 s << mObjectID << write ;364 365 // Puis, si necessaire, on s'ecrit366 367 if (write)368 WriteSelf(s);369 }370 371 PShPersist::~PShPersist() // Une securite peut-etre facultative372 {373 DBASSERT(objList);374 if (mObjectID) {375 DBASSERT(!objList->size() && (objList->find(mObjectID) != objList->end()));376 objList->erase(mObjectID);377 }378 }379 380 void381 PPersistMgr::Reset()382 {383 DBASSERT(PShPersist::objList);384 for (PShPersist::ObjList::iterator i = PShPersist::objList->begin();385 i != PShPersist::objList->end(); i++)386 (*i).second->mObjectID = 0; // Une securite peut-etre facultative387 388 PShPersist::objList->erase(PShPersist::objList->begin(), PShPersist::objList->end());389 }390 391 #define PIOP_Delim '\n' // Delimiteur de ligne392 // Les noms ne peuvent depasser 255 caracteres393 #define MAXTAGNAMELEN 255394 199 395 200 //++ … … 407 212 //-- 408 213 { 409 mSbsz = 0; // Pour conserver les noms des tags ( V4 et au dela ) 410 mSbuf = NULL; 411 mTags = NULL; 412 mNTags = 0; // " " " " 413 fName = flnm; 414 #ifdef RFIO 415 s = new erosifstream(flnm.c_str(),"rb"); 214 s = new ifstream(flnm.c_str(),ios::in | IOS_BIN); 215 216 // Read and check header 217 218 char rbuf[36]; 219 GetRawBytes(rbuf, 32); 220 if (strncmp(rbuf,"PlanckDPC-PPersistFile", 22) != 0) { 221 throw FileFormatExc("PInPersist::PInPersist bad header"); 222 } 223 version = atoi(rbuf+24); 224 225 // read endianness 226 GetRawBytes(rbuf, 32); 227 if (strncmp(rbuf,"BIG-ENDIAN",10) == 0) 228 bigEndian = true; 229 else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0) 230 bigEndian = false; 231 else { 232 throw FileFormatExc("PInPersist::PInPersist bad header - endianness"); 233 } 234 235 // read creation date 236 GetRawBytes(rbuf, 32); 237 rbuf[32] = '\0'; 238 struct tm tm; 239 strptime(rbuf,"%d/%m/%Y %T GMT",&tm); 240 creationdate = mktime(&tm); 241 242 if (scan) Scan(); 243 } 244 245 246 247 PInPersist::~PInPersist() 248 { 249 delete s; 250 } 251 252 253 void 254 PInPersist::Scan() 255 { 256 // On cherche la liste des tags, a la fin du fichier 257 258 char ppstype; 259 size_t debut; 260 #ifdef STREAMPOS_IS_CLASS 261 debut = s->tellg().offset(); 416 262 #else 417 // $CHECK$ EA ios::binary (ou ios::bin) doit exister PARTOUT. 418 // Voir note dans defs.h pres de HAS_IOS_BIN 419 #if defined(__MWERKS__) 420 s = new ifstream(flnm.c_str(),ios::in | ios::binary); // ios::binary pas connnu partout - $CHECK$ Reza 13/02/98 421 #else 422 s = new ifstream(flnm.c_str(),ios::in ); 263 debut = s->tellg(); 423 264 #endif 424 #endif 425 PPersistMgr::Reset(); 426 char rbuf[36]; 427 GetBytes(rbuf, 32); 428 if (strncmp(rbuf,"PEIDA-PPersistFile", 18) != 0) { 429 cerr << "PInPersist::PInPersist Error : File " << flnm << " (empty file or bad header)" 430 << endl; 431 THROW(fileErr); } 432 version = atoi(rbuf+20); 433 bigEndian = IS_BIG_ENDIAN; // Les V2 sont relus en mode natif 434 if (version >= 3) { 435 GetBytes(rbuf, 32); 436 if (strncmp(rbuf,"BIG-ENDIAN",10) == 0) 437 bigEndian = true; 438 else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0) 439 bigEndian = false; 440 else { 441 cerr << "PInPersist::PInPersist Error : File" << flnm; 442 cerr << " (V>=3 et ni BIG-ENDIAN, ni LITTLE-ENDIAN" << endl; 443 THROW(fileErr); 444 } 445 } 446 if (version >= 5) { // On lit la date de creation (V5 et au-dela) 447 GetBytes(rbuf, 32); 448 rbuf[32] = '\0'; 449 for(int k=0; k<32; k++) 450 if (rbuf[k] == ' ') { rbuf[k] = '\0'; break; } 451 creationdate = rbuf; 452 } // Fin de lecture de date 453 else creationdate = "??/??/??" ; 454 // Le fichier peut contenir des tag (V4 et au dela) 455 if ( (version >= 4) && scan ) Scan(); 456 } 457 458 459 460 PInPersist::~PInPersist() 461 { 462 list<char*>::iterator it; 463 for(it = mSbuffs.begin(); it != mSbuffs.end(); it++) delete[] (*it); 464 if (mTags) delete[] mTags; 465 delete s; 466 PPersistMgr::Reset(); 467 } 468 469 // A cause de aCC qui ne veut pas char* b = "" 470 static char ChaineVide[2] = {'\0', '\0'}; 471 472 void 473 PInPersist::Scan() 474 { 475 // On parcourt le fichier depuis la fin pour identifier tous les tags 476 int_8 debut, prev, premier; 477 int_4 ntag, ntc, ll; 478 uint_2 ln; 479 char pad[4]; 480 481 #if defined(STREAMPOS_IS_CLASS) && !defined(RFIO) 482 debut = s->tellg().offset(); 483 #else 484 debut = s->tellg(); 485 #endif 486 487 premier = 0; 488 s->seekg(-(sizeof(int_8)+sizeof(int_4)), ios::end); 489 GetI8(prev); 490 GetI4(ntag); 491 if ( (ntag <= 0) || (prev < 0) ) 492 { s->seekg(debut,ios::beg); return; } 493 494 mNTags = ntag; 495 mTags = new PPFTag[ntag]; 496 497 PPFTag* tag; 498 while( (prev > 0) && (ntag > 0) ) { 499 s->seekg(prev); 500 if (ntag == 1) premier = prev; 501 GetI8(prev); 502 GetI4(ntc); 503 ntag--; 504 if (ntc != ntag) { 505 cerr << "PInPersist::Scan() / Error: NTag,TC= " << ntag << "," << ntc << endl; 506 THROW(fileErr); 507 } 508 tag = mTags+ntag; 509 GetI4(tag->key); 510 GetU2(ln); 511 if (ln > 0) { 512 tag->lnom = ln; 513 tag->nom = GetCStr(ln); 514 GetBytes(tag->nom, ln+1); 515 ll = 3-((ln+2)%4); 516 GetBytes(pad, ll); 517 } 518 else { 519 GetU2(ln); 520 tag->lnom = 0; 521 tag->nom = ChaineVide; 522 } 523 // On recupere la position pour l'objet qui suit 524 #ifdef STREAMPOS_IS_CLASS 525 tag->popos = s->tellg().offset(); 526 #else 527 tag->popos = s->tellg(); 528 #endif 529 530 GetI4(tag->cid); 531 } 532 533 if ( (ntag != 0) || (prev >= 0) ) { // En principe , ca ne doit pas arriver 534 cerr << "PInPersist::Scan() / Error: (End: ntag=" << ntag << 535 " prev=" << (int_4)prev << endl; 536 THROW(fileErr); 537 } 538 // on se repositionne au debut du 1er objet du fichier 539 if (premier == debut) GotoTag(0); 540 else s->seekg(debut); 541 return; 542 } 543 544 char* 545 PInPersist::GetCStr(uint_2 sz) 546 { 547 char* rs; 548 sz += 1; // Pour le 0 de la fin 549 if (sz > mSbsz) 550 { mSbsz = (sz > (MAXTAGNAMELEN+1)) ? sz : (MAXTAGNAMELEN+1); 551 mSbuf = new char[mSbsz]; mSbuffs.push_back(mSbuf); } 552 553 rs = mSbuf; 554 mSbuf += sz; 555 mSbsz -= sz; 556 return(rs); 557 } 265 266 // Find tag entries at end of file 267 s->seekg(-(sizeof(int_8)+1), ios::end); 268 GetRawByte(ppstype); 269 if (ppstype != PPS_EOF) 270 throw FileFormatExc("PInPersist::Scan corrupted file, no eof entry at end of file"); 271 272 int_8 pos; 273 GetRawI8(pos); 274 if (pos < 0) { // no tags 275 s->seekg(debut); 276 return; 277 } 278 279 char buffer[MAXTAGLEN+1]; 280 s->seekg(pos); 281 while (true) { 282 GetRawByte(ppstype); 283 if (ppstype == PPS_EOF) break; 284 285 if (ppstype != PPS_TAG) 286 throw FileFormatExc("PInPersist::Scan corrupted file, bad tag entry"); 287 288 GetRawI8(pos); 289 int_4 len; 290 GetRawI4(len); 291 if (len > MAXTAGLEN) 292 throw FileFormatExc("PInPersist::Scan corrupted file, tag name too long"); 293 GetRawBytes(buffer, len); 294 buffer[len] = '\0'; 295 296 tags[buffer] = pos; 297 } 298 s->seekg(debut); 299 } 300 558 301 559 302 bool 560 PInPersist::GotoTag(int_4 num) 561 { 562 if ( (num < 0) || (num >= mNTags) ) return(false); 563 s->seekg(mTags[num].popos); 303 PInPersist::GotoTag(string const& name) 304 { 305 map<string, int_8>::iterator i = tags.find(name); 306 if (i == tags.end()) 307 return false; 308 // throw NotFoundExc("PInPersist::GotoTag tag not found"); 309 s->seekg((*i).second); 564 310 return(true); 565 }566 567 bool568 PInPersist::GotoKey(int_4 key, int rang)569 {570 if (rang < 0) return(false);571 int n = 0, i;572 int fnd = -1;573 for(i=0; i<mNTags; i++)574 if (mTags[i].key == key) {575 if (n == rang) { fnd = i; break; }576 n++; }577 578 if (fnd >= 0) return(GotoTag(fnd));579 else return(false);580 }581 582 int583 PInPersist::NbKey(int_4 key)584 {585 int n = 0, i;586 for(i=0; i<mNTags; i++)587 if (mTags[i].key == key) n++;588 return(n);589 }590 591 void592 PInPersist::ListTags()593 {594 cout << "PInPersist/ File " << fName << "\n";595 cout << "Nb Tags in file : " << mNTags << "\n";596 string str;597 int i;598 for(i=0; i<mNTags; i++) {599 str = mTags[i].nom;600 cout << i << "- CId= " << mTags[i].cid << " Key= "601 << mTags[i].key << " ( " << str << " )\n" ;602 }603 cout << endl;604 return;605 }606 607 int_4608 PInPersist::TagKey(int_4 num, int_4& cid, int_4& ln)609 {610 if ( (num < 0) || (num >= mNTags) ) return(0);611 cid = mTags[num].cid; ln = mTags[num].lnom;612 return(mTags[num].key);613 }614 615 string616 PInPersist::TagName(int_4 num)617 {618 if ( (num < 0) || (num >= mNTags) ) return("");619 return((string)mTags[num].nom);620 311 } 621 312 … … 645 336 //-- 646 337 647 void648 PInPersist::GetByte(char& c)649 {650 GetBytes(&c, 1);651 }652 653 void654 PInPersist::GetBytes(void* ptr, size_t bytes)655 {656 s->read((char*)ptr, bytes);657 }658 338 659 339 static inline void bswap8(void* p) … … 686 366 } 687 367 368 369 void 370 PInPersist::GetRawByte(char& c) 371 { 372 GetRawBytes(&c, 1); 373 } 374 375 void 376 PInPersist::GetRawBytes(void* ptr, size_t bytes) 377 { 378 s->read((char*)ptr, bytes); 379 } 380 381 void 382 PInPersist::GetRawI2 (int_2& result) 383 { 384 GetRawBytes(&result, sizeof(int_2)); 385 if (bigEndian != IS_BIG_ENDIAN) 386 bswap2(&result); 387 } 388 389 void 390 PInPersist::GetRawI4 (int_4& result) 391 { 392 GetRawBytes(&result, sizeof(int_4)); 393 if (bigEndian != IS_BIG_ENDIAN) 394 bswap4(&result); 395 } 396 397 void 398 PInPersist::GetRawI8 (int_8& result) 399 { 400 GetRawBytes(&result, sizeof(int_8)); 401 if (bigEndian != IS_BIG_ENDIAN) 402 bswap8(&result); 403 } 404 405 void 406 PInPersist::GetRawU8 (uint_8& result) 407 { 408 GetRawBytes(&result, sizeof(uint_8)); 409 if (bigEndian != IS_BIG_ENDIAN) 410 bswap8(&result); 411 } 412 413 void 414 PInPersist::CheckTag(short datasz) 415 { 416 char ppstype; 417 GetRawByte(ppstype); 418 if (ppstype != PPS_SIMPLE + datasz) 419 throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file"); 420 } 421 422 void 423 PInPersist::CheckArrayTag(short datasz, size_t sz) 424 { 425 char ppstype; 426 GetRawByte(ppstype); 427 size_t filesz; 428 if (sz <= 0x7fff) { 429 if (ppstype != PPS_SIMPLE_ARRAY + datasz) 430 throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file"); 431 int_2 ff; 432 GetRawI2(ff); filesz=ff; 433 } else if (sz <= 0x7fffffff) { 434 if (ppstype != PPS_SIMPLE_ARRAY4 + datasz) 435 throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file"); 436 int_4 ff; 437 GetRawI4(ff); filesz=ff; 438 } else { 439 if (ppstype != PPS_SIMPLE_ARRAY8 + datasz) 440 throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file"); 441 uint_8 ff; 442 GetRawU8(ff); filesz=ff; 443 } 444 if (filesz != sz) 445 throw TypeMismatchExc("PInPersist::CheckTag bad array size in ppersist file"); 446 } 447 448 void 449 PInPersist::GetByte(char& c) 450 { 451 CheckTag(1); 452 GetRawBytes(&c, 1); 453 } 454 455 void 456 PInPersist::GetBytes(void* ptr, size_t bytes) 457 { 458 CheckArrayTag(1, bytes); 459 GetRawBytes(ptr, bytes); 460 } 688 461 void 689 462 PInPersist::GetR4 (r_4& result) 690 463 { 691 GetBytes(&result, sizeof(r_4)); 464 CheckTag(4); 465 GetRawBytes(&result, sizeof(r_4)); 692 466 if (bigEndian != IS_BIG_ENDIAN) 693 467 bswap4(&result); … … 698 472 PInPersist::GetR4s (r_4* tab, size_t n) 699 473 { 700 GetBytes(tab, n*sizeof(r_4)); 474 CheckArrayTag(4,n); 475 GetRawBytes(tab, n*sizeof(r_4)); 701 476 if (bigEndian == IS_BIG_ENDIAN) return; 702 477 … … 710 485 PInPersist::GetR8 (r_8& result) 711 486 { 712 GetBytes(&result, sizeof(r_8)); 487 CheckTag(8); 488 GetRawBytes(&result, sizeof(r_8)); 713 489 if (bigEndian != IS_BIG_ENDIAN) 714 490 bswap8(&result); … … 718 494 PInPersist::GetR8s (r_8* tab, size_t n) 719 495 { 720 GetBytes(tab, n*sizeof(r_8)); 496 CheckArrayTag(8,n); 497 GetRawBytes(tab, n*sizeof(r_8)); 721 498 if (bigEndian == IS_BIG_ENDIAN) return; 722 499 … … 730 507 PInPersist::GetI2 (int_2& result) 731 508 { 732 GetBytes(&result, sizeof(int_2)); 509 CheckTag(2); 510 GetRawBytes(&result, sizeof(int_2)); 733 511 if (bigEndian != IS_BIG_ENDIAN) 734 512 bswap2(&result); … … 738 516 PInPersist::GetI2s (int_2* tab, size_t n) 739 517 { 740 GetBytes(tab, n*sizeof(int_2)); 518 CheckArrayTag(2,n); 519 GetRawBytes(tab, n*sizeof(int_2)); 741 520 if (bigEndian == IS_BIG_ENDIAN) return; 742 521 … … 750 529 PInPersist::GetU2 (uint_2& result) 751 530 { 752 GetBytes(&result, sizeof(uint_2)); 531 CheckTag(2); 532 GetRawBytes(&result, sizeof(uint_2)); 753 533 if (bigEndian != IS_BIG_ENDIAN) 754 534 bswap2(&result); … … 758 538 PInPersist::GetU2s (uint_2* tab, size_t n) 759 539 { 760 GetBytes(tab, n*sizeof(uint_2)); 540 CheckArrayTag(2,n); 541 GetRawBytes(tab, n*sizeof(uint_2)); 761 542 if (bigEndian == IS_BIG_ENDIAN) return; 762 543 … … 770 551 PInPersist::GetI4 (int_4& result) 771 552 { 772 GetBytes(&result, sizeof(int_4)); 553 CheckTag(4); 554 GetRawBytes(&result, sizeof(int_4)); 773 555 if (bigEndian != IS_BIG_ENDIAN) 774 556 bswap4(&result); … … 778 560 PInPersist::GetI4s (int_4* tab, size_t n) 779 561 { 780 GetBytes(tab, n*sizeof(int_4)); 562 CheckArrayTag(4,n); 563 GetRawBytes(tab, n*sizeof(int_4)); 781 564 if (bigEndian == IS_BIG_ENDIAN) return; 782 565 … … 790 573 PInPersist::GetU4 (uint_4& result) 791 574 { 792 GetBytes(&result, sizeof(uint_4)); 575 CheckTag(4); 576 GetRawBytes(&result, sizeof(uint_4)); 793 577 if (bigEndian != IS_BIG_ENDIAN) 794 578 bswap4(&result); … … 798 582 PInPersist::GetU4s (uint_4* tab, size_t n) 799 583 { 800 GetBytes(tab, n*sizeof(uint_4)); 584 CheckArrayTag(4,n); 585 GetRawBytes(tab, n*sizeof(uint_4)); 801 586 if (bigEndian == IS_BIG_ENDIAN) return; 802 587 … … 811 596 PInPersist::GetI8 (int_8& result) 812 597 { 813 GetBytes(&result, sizeof(int_8)); 598 CheckTag(8); 599 GetRawBytes(&result, sizeof(int_8)); 814 600 if (bigEndian != IS_BIG_ENDIAN) 815 601 bswap8(&result); … … 819 605 PInPersist::GetI8s (int_8* tab, size_t n) 820 606 { 821 GetBytes(tab, n*sizeof(int_8)); 607 CheckArrayTag(8,n); 608 GetRawBytes(tab, n*sizeof(int_8)); 822 609 if (bigEndian == IS_BIG_ENDIAN) return; 823 610 … … 831 618 PInPersist::GetU8 (uint_8& result) 832 619 { 833 GetBytes(&result, sizeof(uint_8)); 620 CheckTag(8); 621 GetRawBytes(&result, sizeof(uint_8)); 834 622 if (bigEndian != IS_BIG_ENDIAN) 835 623 bswap8(&result); … … 839 627 PInPersist::GetU8s (uint_8* tab, size_t n) 840 628 { 841 GetBytes(tab, n*sizeof(uint_8)); 629 CheckArrayTag(8,n); 630 GetRawBytes(tab, n*sizeof(uint_8)); 842 631 if (bigEndian == IS_BIG_ENDIAN) return; 843 632 … … 852 641 PInPersist::GetLine(char* ptr, size_t len) 853 642 { 854 s->getline(ptr, len, PIOP_Delim); 855 } 856 643 char ppstype; 644 GetRawByte(ppstype); 645 if (ppstype != PPS_LINE) 646 throw TypeMismatchExc("PInPersist::GetLine bad type in ppersist file"); 647 s->getline(ptr, len, '\n'); 648 } 649 650 void 651 PInPersist::GetStr(string& str) 652 { 653 char ppstype; 654 GetRawByte(ppstype); 655 if (ppstype != PPS_STRING) 656 throw TypeMismatchExc("PInPersist::GetLine bad type in ppersist file"); 657 int_2 len; 658 GetRawI2(len); 659 char * buff = new char(len+1); 660 GetRawBytes(buff, len); 661 buff[len] = '\0'; 662 str = buff; 663 delete[] buff; 664 } 665 666 PPersist* 667 PInPersist::ReadObject() 668 { 669 // Get tag 670 char ppstype; 671 GetRawByte(ppstype); 672 if (ppstype != PPS_OBJECT && ppstype != PPS_REFERENCE && ppstype != PPS_NULL) { 673 throw TypeMismatchExc("PInPersist::ReadObject : not an object in flow"); 674 } 675 676 if (ppstype == PPS_NULL) { 677 return NULL; 678 } else if (ppstype == PPS_OBJECT) { 679 // Get class id 680 uint_8 classId; 681 GetRawU8(classId); 682 683 // Get factory method 684 ClassCreatorFunc f = FindCreatorFunc(classId); 685 if (!f) { 686 throw NotFoundExc("PInPersist::ReadObject class not registered"); 687 } 688 689 // Create object 690 PPersist* object = f(); 691 object->ReadSelf(*this); 692 assignObjectId(object); 693 return object; 694 } else { 695 // Get object id 696 int_4 id; 697 GetRawI4(id); 698 if (id <0 || id>=objList.size()) { 699 throw FileFormatExc("PInPersist::ReadObject invalid object id for reference"); 700 } 701 return objList[id]; 702 } 703 } 704 705 int_4 706 PInPersist::assignObjectId(PPersist* x) 707 { 708 objList.push_back(x); 709 return objList.size()-1; 710 } 857 711 858 712 //++ … … 880 734 bigEndian = endianness; 881 735 882 #ifdef RFIO 883 s = new erosofstream(flnm.c_str(),"wb"); 736 // Output stream creation 737 s = new ofstream(flnm.c_str(),ios::out | IOS_BIN); 738 739 // Header 740 PutRawBytes("PlanckDPC-PPersistFile V1 ",32); 741 PutRawBytes(bigEndian 742 ? "BIG-ENDIAN " 743 : "LITTLE-ENDIAN ",32); 744 745 // ---- GMT creation date of the file 746 time_t tm = time(NULL); 747 char datestring[33]; 748 int l=strftime(datestring,32,"%d/%m/%Y %T GMT",gmtime(&tm)); 749 for(int i=l; i<32; i++) datestring[i] = ' '; 750 datestring[32] = '\0'; 751 PutRawBytes(datestring, 32); 752 } 753 754 POutPersist::~POutPersist() 755 { 756 if (tags.size() == 0) { 757 PutRawByte(PPS_EOF); 758 PutRawI8(-1); 759 } else { 760 int_8 tagPos; 761 #ifdef STREAMPOS_IS_CLASS 762 tagPos = s->tellp().offset(); 884 763 #else 885 #if defined(__MWERKS__) 886 s = new ofstream(flnm.c_str(),ios::out | ios::binary); // ios::binary pas connnu partout - $CHECK$ Reza 13/02/98 887 #else 888 s = new ofstream(flnm.c_str(),ios::out ); 764 tagPos = s->tellp(); 889 765 #endif 890 #endif 891 PPersistMgr::Reset(); 892 PutBytes("PEIDA-PPersistFile V5 ",32); 893 PutBytes(bigEndian 894 ? "BIG-ENDIAN " 895 : "LITTLE-ENDIAN ",32); 896 // ---- On ecrit la date de creation a partir de V5 897 Date today; 898 string stod = today.DateStr(Date::kLocalTime) + "#" + today.TimeStr(Date::kLocalTime); 899 char buff[36]; 900 int l = stod.length(); 901 if (l < 33) strcpy(buff, stod.c_str()); 902 else strncpy(buff, stod.c_str(), 33); 903 for(int i=l+1; i<32; i++) buff[i] = ' '; 904 buff[32] = '\0'; 905 PutBytes(buff, 32); 906 // Fin d'ecriture de date -------- 907 previous = -1; // Pas de Tag precedant 908 numTag = 0; 909 } 910 911 POutPersist::~POutPersist() 912 { 913 PutI8(previous); 914 PutI4(numTag); 915 delete s; 916 PPersistMgr::Reset(); 917 } 918 919 920 int_4 921 POutPersist::WriteTag(int_4 key, char const * nom) 922 { 923 int_8 nexprev; 924 uint_2 l; 925 int ll; 926 char pad[4] = {'\0', '\0', '\0', '\0'}; 766 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) { 767 string name = (*i).first; 768 int_8 pos = (*i).second; 769 PutRawByte(PPS_TAG); // This is a tag 770 PutRawI8(pos); // position of previous tag 771 PutRawI4(name.length()); // length of the name 772 PutRawBytes(name.c_str(), name.length()); // name, without final "0". 773 } 774 PutRawByte(PPS_EOF); 775 PutRawI8(tagPos); 776 } 777 778 delete s; // Close the stream 779 } 780 781 782 void 783 POutPersist::WriteTag(string const& name) 784 { 785 if (name.length() > MAXTAGLEN) 786 throw ParmError("POutPersist::WriteTag tag name too long"); 787 788 if (tags.find(name) != tags.end()) 789 throw DuplicateIdExc("POutPersist::WriteTag duplicate tag name"); 790 791 // Get current file position 792 int_8 tagPos; 927 793 928 794 #ifdef STREAMPOS_IS_CLASS 929 nexprev= s->tellp().offset();795 tagPos = s->tellp().offset(); 930 796 #else 931 nexprev= s->tellp();797 tagPos = s->tellp(); 932 798 #endif 933 // On ecrit dans l'ordre 934 // - La position du tag precedant (-1 s'il n'y en a pas) 935 // - Le numero de tag ( qui s incremete automatiquement ) 936 // - La cle utilisateur (key) 937 // - La longueur du nom 938 // - le nom lui-meme (Avec un pad afin de faire nom+longueur=multiple de 4) 939 PutI8(previous); 940 PutI4(numTag); 941 PutI4(key); 942 if (nom==NULL) { 943 PutU2(0); PutU2(0); 944 } 945 else { 946 ll = strlen(nom); 947 if (ll <= 0) { 948 PutU2(0); PutU2(0); 949 } 950 l = (ll <= MAXTAGNAMELEN) ? ll : MAXTAGNAMELEN ; 951 PutU2(l); 952 PutBytes(nom,l+1); 953 ll = 3-((ll+2)%4); 954 PutBytes(pad, ll); 955 } 956 previous = nexprev; 957 numTag++; 958 return((numTag-1)); 799 800 tags[name] = tagPos; 959 801 } 960 802 … … 978 820 // void POutPersist::PutU8 (uint_8 result) 979 821 // void POutPersist::PutU8s (uint_8 const* tab, size_t n) 822 // void POutPersist::PutStr (string const&) 980 823 // Ecriture de données portables.. Pour chaque type 981 824 // de données, on peut écrire une valeur, ou un tableau de valeurs. … … 988 831 989 832 void 833 POutPersist::PutRawBytes(void const* ptr, size_t bytes) 834 { 835 s->write((char const*)ptr, bytes); 836 } 837 838 void 839 POutPersist::PutRawByte(char c) 840 { 841 PutRawBytes(&c, 1); 842 } 843 844 void 845 POutPersist::PutRawI2 (int_2 val) 846 { 847 if (bigEndian != IS_BIG_ENDIAN) 848 bswap2(&val); 849 850 PutRawBytes(&val, sizeof(int_2)); 851 } 852 853 void 854 POutPersist::PutRawI4 (int_4 val) 855 { 856 if (bigEndian != IS_BIG_ENDIAN) 857 bswap4(&val); 858 859 PutRawBytes(&val, sizeof(int_4)); 860 } 861 862 void 863 POutPersist::PutRawI8 (int_8 val) 864 { 865 if (bigEndian != IS_BIG_ENDIAN) 866 bswap8(&val); 867 868 PutRawBytes(&val, sizeof(int_8)); 869 } 870 871 void 872 POutPersist::PutRawU8 (uint_8 val) 873 { 874 if (bigEndian != IS_BIG_ENDIAN) 875 bswap8(&val); 876 877 PutRawBytes(&val, sizeof(uint_8)); 878 } 879 880 void 881 POutPersist::PutArrayTag(short datasz, size_t sz) 882 { 883 if (sz <= 0x7fff) { 884 PutRawByte(PPS_SIMPLE_ARRAY + datasz); 885 PutRawI2(sz); 886 } else if (sz <= 0x7fffffff) { 887 PutRawByte(PPS_SIMPLE_ARRAY4 + datasz); 888 PutRawI4(sz); 889 } else { 890 PutRawByte(PPS_SIMPLE_ARRAY8 + datasz); 891 PutRawU8(sz); 892 } 893 } 894 895 void 990 896 POutPersist::PutByte(char c) 991 897 { 992 PutBytes(&c, 1); 993 } 898 PutRawByte(PPS_SIMPLE + 1); 899 PutRawBytes(&c, 1); 900 } 901 902 994 903 995 904 void 996 905 POutPersist::PutBytes(void const* ptr, size_t bytes) 997 906 { 998 s->write((char const*)ptr, bytes); 907 PutArrayTag(1, bytes); 908 PutRawBytes(ptr, bytes); 999 909 } 1000 910 … … 1002 912 POutPersist::PutR4 (r_4 val) 1003 913 { 914 PutRawByte(PPS_SIMPLE + 4); 915 1004 916 if (bigEndian != IS_BIG_ENDIAN) 1005 917 bswap4(&val); 1006 918 1007 Put Bytes(&val, sizeof(r_4));919 PutRawBytes(&val, sizeof(r_4)); 1008 920 } 1009 921 … … 1011 923 POutPersist::PutR4s (r_4 const* tab, size_t n) 1012 924 { 1013 if (bigEndian == IS_BIG_ENDIAN) 1014 PutBytes(tab, n*sizeof(r_4)); 1015 else 1016 for (unsigned int i=0; i<n; i++) 1017 PutR4(tab[i]); 925 PutArrayTag(4, n); 926 927 if (bigEndian == IS_BIG_ENDIAN) { 928 PutRawBytes(tab, n*sizeof(r_4)); 929 } else { 930 for (unsigned int i=0; i<n; i++) { 931 r_4 val = tab[i]; 932 bswap4(&val); 933 PutRawBytes(&val, sizeof(r_4)); 934 } 935 } 1018 936 } 1019 937 … … 1021 939 POutPersist::PutR8 (r_8 val) 1022 940 { 941 PutRawByte(PPS_SIMPLE + 8); 942 1023 943 if (bigEndian != IS_BIG_ENDIAN) 1024 944 bswap8(&val); 1025 945 1026 Put Bytes(&val, sizeof(r_8));946 PutRawBytes(&val, sizeof(r_8)); 1027 947 } 1028 948 … … 1030 950 POutPersist::PutR8s (r_8 const* tab, size_t n) 1031 951 { 1032 if (bigEndian == IS_BIG_ENDIAN) 1033 PutBytes(tab, n*sizeof(r_8)); 1034 else 1035 for (unsigned int i=0; i<n; i++) 1036 PutR8(tab[i]); 952 PutArrayTag(8, n); 953 954 if (bigEndian == IS_BIG_ENDIAN) { 955 PutRawBytes(tab, n*sizeof(r_8)); 956 } else { 957 for (unsigned int i=0; i<n; i++) { 958 r_8 val = tab[i]; 959 bswap8(&val); 960 PutRawBytes(&val, sizeof(r_8)); 961 } 962 } 1037 963 } 1038 964 … … 1040 966 POutPersist::PutI2 (int_2 val) 1041 967 { 968 PutRawByte(PPS_SIMPLE + 2); 969 1042 970 if (bigEndian != IS_BIG_ENDIAN) 1043 971 bswap2(&val); 1044 972 1045 Put Bytes(&val, sizeof(int_2));973 PutRawBytes(&val, sizeof(int_2)); 1046 974 } 1047 975 … … 1049 977 POutPersist::PutI2s (int_2 const* tab, size_t n) 1050 978 { 1051 if (bigEndian == IS_BIG_ENDIAN) 1052 PutBytes(tab, n*sizeof(int_2)); 1053 else 1054 for (unsigned int i=0; i<n; i++) 1055 PutI2(tab[i]); 979 PutArrayTag(2, n); 980 981 if (bigEndian == IS_BIG_ENDIAN) { 982 PutRawBytes(tab, n*sizeof(int_2)); 983 } else { 984 for (unsigned int i=0; i<n; i++) { 985 int_2 val = tab[i]; 986 bswap2(&val); 987 PutRawBytes(&val, sizeof(int_2)); 988 } 989 } 1056 990 } 1057 991 … … 1059 993 POutPersist::PutU2 (uint_2 val) 1060 994 { 995 PutRawByte(PPS_SIMPLE + 2); 996 1061 997 if (bigEndian != IS_BIG_ENDIAN) 1062 998 bswap2(&val); 1063 999 1064 Put Bytes(&val, sizeof(uint_2));1000 PutRawBytes(&val, sizeof(uint_2)); 1065 1001 } 1066 1002 … … 1068 1004 POutPersist::PutU2s (uint_2 const* tab, size_t n) 1069 1005 { 1070 if (bigEndian == IS_BIG_ENDIAN) 1071 PutBytes(tab, n*sizeof(uint_2)); 1072 else 1073 for (unsigned int i=0; i<n; i++) 1074 PutU2(tab[i]); 1006 PutArrayTag(2, n); 1007 1008 if (bigEndian == IS_BIG_ENDIAN) { 1009 PutRawBytes(tab, n*sizeof(uint_2)); 1010 } else { 1011 for (unsigned int i=0; i<n; i++) { 1012 uint_2 val = tab[i]; 1013 bswap2(&val); 1014 PutRawBytes(&val, sizeof(uint_2)); 1015 } 1016 } 1075 1017 } 1076 1018 … … 1078 1020 POutPersist::PutI4 (int_4 val) 1079 1021 { 1022 PutRawByte(PPS_SIMPLE + 4); 1023 1080 1024 if (bigEndian != IS_BIG_ENDIAN) 1081 1025 bswap4(&val); 1082 1026 1083 Put Bytes(&val, sizeof(int_4));1027 PutRawBytes(&val, sizeof(int_4)); 1084 1028 } 1085 1029 … … 1087 1031 POutPersist::PutI4s (int_4 const* tab, size_t n) 1088 1032 { 1089 if (bigEndian == IS_BIG_ENDIAN) 1090 PutBytes(tab, n*sizeof(int_4)); 1091 else 1092 for (unsigned int i=0; i<n; i++) 1093 PutI4(tab[i]); 1033 PutArrayTag(4, n); 1034 1035 if (bigEndian == IS_BIG_ENDIAN) { 1036 PutRawBytes(tab, n*sizeof(int_4)); 1037 } else { 1038 for (unsigned int i=0; i<n; i++) { 1039 int_4 val = tab[i]; 1040 bswap4(&val); 1041 PutRawBytes(&val, sizeof(int_4)); 1042 } 1043 } 1094 1044 } 1095 1045 … … 1097 1047 POutPersist::PutU4 (uint_4 val) 1098 1048 { 1049 PutRawByte(PPS_SIMPLE + 4); 1050 1099 1051 if (bigEndian != IS_BIG_ENDIAN) 1100 1052 bswap4(&val); 1101 1053 1102 Put Bytes(&val, sizeof(uint_4));1054 PutRawBytes(&val, sizeof(uint_4)); 1103 1055 } 1104 1056 … … 1106 1058 POutPersist::PutU4s (uint_4 const* tab, size_t n) 1107 1059 { 1108 if (bigEndian == IS_BIG_ENDIAN) 1109 PutBytes(tab, n*sizeof(uint_4)); 1110 else 1111 for (unsigned int i=0; i<n; i++) 1112 PutU4(tab[i]); 1060 PutArrayTag(4, n); 1061 1062 if (bigEndian == IS_BIG_ENDIAN) { 1063 PutRawBytes(tab, n*sizeof(uint_4)); 1064 } else { 1065 for (unsigned int i=0; i<n; i++) { 1066 uint_4 val = tab[i]; 1067 bswap4(&val); 1068 PutRawBytes(&val, sizeof(uint_4)); 1069 } 1070 } 1113 1071 } 1114 1072 … … 1116 1074 POutPersist::PutI8 (int_8 val) 1117 1075 { 1076 PutRawByte(PPS_SIMPLE + 8); 1077 1118 1078 if (bigEndian != IS_BIG_ENDIAN) 1119 1079 bswap8(&val); 1120 1080 1121 Put Bytes(&val, sizeof(int_8));1081 PutRawBytes(&val, sizeof(int_8)); 1122 1082 } 1123 1083 … … 1125 1085 POutPersist::PutI8s (int_8 const* tab, size_t n) 1126 1086 { 1127 if (bigEndian == IS_BIG_ENDIAN) 1128 PutBytes(tab, n*sizeof(int_8)); 1129 else 1130 for (unsigned int i=0; i<n; i++) 1131 PutI8(tab[i]); 1087 PutArrayTag(8, n); 1088 1089 if (bigEndian == IS_BIG_ENDIAN) { 1090 PutRawBytes(tab, n*sizeof(int_8)); 1091 } else { 1092 for (unsigned int i=0; i<n; i++) { 1093 int_8 val = tab[i]; 1094 bswap8(&val); 1095 PutRawBytes(&val, sizeof(int_8)); 1096 } 1097 } 1132 1098 } 1133 1099 … … 1135 1101 POutPersist::PutU8 (uint_8 val) 1136 1102 { 1103 PutRawByte(PPS_SIMPLE + 8); 1104 1137 1105 if (bigEndian != IS_BIG_ENDIAN) 1138 1106 bswap8(&val); 1139 1107 1140 Put Bytes(&val, sizeof(uint_8));1108 PutRawBytes(&val, sizeof(uint_8)); 1141 1109 } 1142 1110 … … 1144 1112 POutPersist::PutU8s (uint_8 const* tab, size_t n) 1145 1113 { 1146 if (bigEndian == IS_BIG_ENDIAN) 1147 PutBytes(tab, n*sizeof(uint_8)); 1148 else 1149 for (unsigned int i=0; i<n; i++) 1150 PutU8(tab[i]); 1114 PutArrayTag(8, n); 1115 1116 if (bigEndian == IS_BIG_ENDIAN) { 1117 PutRawBytes(tab, n*sizeof(uint_8)); 1118 } else { 1119 for (unsigned int i=0; i<n; i++) { 1120 uint_8 val = tab[i]; 1121 bswap8(&val); 1122 PutRawBytes(&val, sizeof(uint_8)); 1123 } 1124 } 1125 } 1126 1127 void 1128 POutPersist::PutStr(string const& str) 1129 { 1130 PutRawByte(PPS_STRING); 1131 PutRawI2(str.length()); 1132 PutRawBytes(str.c_str(), str.length()); 1151 1133 } 1152 1134 … … 1154 1136 POutPersist::PutLine(char const* ptr, size_t len) 1155 1137 { 1138 PutRawByte(PPS_LINE); 1139 1156 1140 if (len == 0) len = strlen(ptr); 1157 s->write(ptr, len); 1158 #ifdef RFIO 1159 char cd = PIOP_Delim; 1160 s->write(&cd, 1); 1161 #else 1162 s->put(PIOP_Delim); 1163 #endif 1141 PutRawBytes(ptr, len); 1142 PutRawByte('\n'); 1143 } 1144 1145 void 1146 POutPersist::PutObject(PPersist const* obj) 1147 { 1148 if (serializeNullAndRepeat(obj)) return; 1149 1150 PutRawByte(PPS_OBJECT); 1151 PutRawU8(PIOPersist::Hash(typeid(*obj).name())); 1152 assignObjectId(obj); 1153 obj->WriteSelf(*this); 1154 } 1155 1156 bool 1157 POutPersist::serializeNullAndRepeat(PPersist const* x) 1158 { 1159 if (x == NULL) { 1160 PutRawByte(PPS_NULL); 1161 return true; 1162 } 1163 1164 int_4 id = findObjectId(x); 1165 if (id >= 0) { 1166 PutRawByte(PPS_REFERENCE); 1167 PutRawI4(id); 1168 return true; 1169 } 1164 1170 1165 } 1166 1167 1168 // --- Classe d'initialisation de PEIDA++, (PPersistMgr en particulier) 1169 int PeidaInitiator::FgInit = 0; 1170 1171 PeidaInitiator::PeidaInitiator() 1172 { 1173 FgInit++; 1174 if (FgInit > 1) return; 1175 1176 InitFailNewHandler(); 1177 1178 #ifdef xx__mac__ 1179 //InitToolBox(); 1180 //SIOUXSettings.initializeTB = FALSE; 1181 SIOUXSettings.autocloseonquit = FALSE; 1182 SIOUXSettings.asktosaveonclose = FALSE; 1183 SIOUXSettings.showstatusline = TRUE; 1184 #endif 1185 1186 PPersistMgr::classList = new PPersistMgr::ClassList; 1187 PShPersist::objList = new PShPersist::ObjList; 1188 1189 1190 #if (!defined(__GNUG__) && !defined(__MWERKS__) && !defined(HPUX)) 1191 // pas de bufferisation pour printf cmv 18/3/97 selon E.A. 1192 // setvbuf(stdout,NULL,_IOLBF,0); setvbuf(stderr,NULL,_IOLBF,0); 1193 setlinebuf(stdout); 1194 setlinebuf(stderr); 1195 #endif 1196 1197 // si var env PEIDA_NOPRTVER definie pas de print 1198 if(!getenv("PEIDA_NOPRTVER")) PrintPeidaVersion(); 1199 } 1200 1201 PeidaInitiator::~PeidaInitiator() 1202 { 1203 FgInit--; 1204 if (FgInit == 0) 1205 { 1206 delete PPersistMgr::classList; PPersistMgr::classList = NULL; 1207 delete PShPersist::objList; PShPersist::objList = NULL; 1208 } 1209 } 1210 1211 double PeidaInitiator::Version(bool fgprt) 1212 { 1213 if (fgprt) PrintPeidaVersion(); 1214 return(PeidaVersion()); 1215 } 1216 1217 // On met un objet initiator en statique, pour les loaders qui savent 1218 // appeler le constructeur des objets statiques Reza 08/98 1219 static PeidaInitiator ppeidainit; 1220 1171 return false; 1172 } 1173 1174 int_4 1175 POutPersist::assignObjectId(PPersist const* x) 1176 { 1177 int_4 id = objList.size(); 1178 objList[x] = id; 1179 return id; 1180 } 1181 1182 int_4 1183 POutPersist::findObjectId(PPersist const* x) 1184 { 1185 ObjList::iterator i = objList.find(x); 1186 if (i == objList.end()) return -1; 1187 return (*i).second; 1188 } 1189 1190 -
trunk/SophyaLib/BaseTools/ppersist.h
r219 r241 1 // This may look like C code, but it is really -*- C++ -*- 2 1 3 #ifndef PPERSIST_H_SEEN 2 4 #define PPERSIST_H_SEEN 3 5 4 // Classe mixin pour implementer une persistance pas trop bete. 5 6 #include "defs.h" 7 #include "machine.h" 8 #include "perrors.h" 9 #include "pclassids.h" 6 // Flat file persistance, similar to Java serialization 7 // 8 // E. Aubourg CEA DAPNIA/SPP 1999 9 10 11 #include "machdefs.h" 12 #include "pexceptions.h" 13 #include "md5.h" 10 14 11 15 #include <string> 12 #include <list>13 16 #include <map> 14 #include <functional> 17 #include <vector> 18 #include <typeinfo> 15 19 16 20 #if defined(__KCC__) … … 21 25 #endif 22 26 23 #ifdef RFIO 24 #include "erostream.h" 25 #else 26 #include <iostream.h> 27 #endif 28 29 30 class PPersistMgr; 31 class PPersist; 32 class PShPersist; 33 class PInPersist; 34 class POutPersist; 35 36 typedef less<int_4> Int4Compare; 37 38 class PPersistMgr { 39 public: 40 typedef PPersist* (*ClassCreatorFunc)(); 41 42 static void RegisterClass(int_4 classId, ClassCreatorFunc f, 43 bool shared=false); 44 static PPersist* ReadObject(PInPersist&); 45 46 static ClassCreatorFunc FindCreatorFunc(int_4 classId); 47 static ClassCreatorFunc FindCreatorFunc(int_4 classId, bool& isShared); 48 49 static void Reset(); // Nouveau fichier, on oublie les objets partages. 50 51 #ifndef __DECCXX 52 private: 53 #endif 54 struct ClassListEntry { 55 bool shared; 56 ClassCreatorFunc f; 27 namespace PlanckDPC { 28 29 class PIOPersist; 30 class PInPersist; 31 class POutPersist; 32 class PPersist; 33 34 /* Persistant (delegate or mixin) object */ 35 36 class PPersist { 37 public: 38 virtual ~PPersist() {} 39 40 void Write(string const& fn) const; 41 void Read(string const& fn); 42 43 virtual void Write(POutPersist&) const; 44 void Read(PInPersist& s); // Reads the type tag and the object 45 void Write(POutPersist&, string const& tag) const; 46 void ReadAtTag(PInPersist& s, string const& tag); 47 protected: 48 virtual void ReadSelf(PInPersist&)=0; 49 virtual void WriteSelf(POutPersist&) const=0; 50 51 friend class PInPersist; 52 friend class POutPersist; 57 53 }; 58 #ifdef __DECCXX 59 private: 60 #endif 61 62 typedef map<int_4, ClassListEntry, Int4Compare> ClassList; 63 64 65 static ClassList* classList; 66 friend class PeidaInitiator; 67 }; 68 69 70 71 class PPersist EXC_AWARE { 72 public: 73 enum {PPS_NATIVE = -1, PPS_LITTLE_ENDIAN = 0, PPS_BIG_ENDIAN = 1}; 74 virtual ~PPersist() {} 75 virtual int_4 ClassId() const=0; 76 77 void Write(string const& fn) const; 78 void Read(string const& fn); 79 80 virtual void Write(POutPersist&) const; 81 void Read(PInPersist& s); // Se lit, y compris le tag de type. 82 int_4 Write(POutPersist&, int_4 key) const; 83 int_4 Write(POutPersist&, int_4 key, string& nom) const; 84 void ReadAtTag(PInPersist& s, int_4 tagid); 85 void ReadAtKey(PInPersist& s, int_4 key); 86 protected: 87 virtual void ReadSelf(PInPersist&)=0; // A redefinir... 88 virtual void WriteSelf(POutPersist&) const=0; // A redefinir... 89 90 friend class PPersistMgr; 91 }; 92 93 class PShPersist : public PPersist{ 94 public: 95 PShPersist() :mObjectID(0) {} 96 virtual ~PShPersist(); 97 virtual void Write(POutPersist&) const; 98 static PShPersist* FindObject(int_4 objId); 99 100 private: 101 typedef map<int_4, PShPersist*, Int4Compare> ObjList; 102 103 static ObjList* objList; 104 /*mutable*/ int_4 mObjectID; 105 friend class PPersistMgr; 106 friend class PeidaInitiator; 107 }; 108 109 110 111 class PInPersist { 112 public: 113 PInPersist(string const& flnm, bool scan=true); 114 ~PInPersist(); 115 116 bool GotoTag(int_4 num); 117 bool GotoKey(int_4 key, int rang=0); 118 int NbKey(int_4 key); 119 void ListTags(); 120 inline int_4 NbTags() { return(mNTags); } 121 int_4 TagKey(int_4 num, int_4& cid, int_4& ln); 122 string TagName(int_4 num); 123 124 void GetByte (char& c); 125 void GetBytes(void* ptr, size_t bytes); 126 void GetR4 (r_4&); 127 void GetR4s (r_4*, size_t); 128 void GetR8 (r_8&); 129 void GetR8s (r_8*, size_t); 130 void GetI2 (int_2&); 131 void GetI2s (int_2*, size_t); 132 void GetU2 (uint_2&); 133 void GetU2s (uint_2*, size_t); 134 void GetI4 (int_4&); 135 void GetI4s (int_4*, size_t); 136 void GetU4 (uint_4&); 137 void GetU4s (uint_4*, size_t); 138 void GetI8 (int_8&); 139 void GetI8s (int_8*, size_t); 140 void GetU8 (uint_8&); 141 void GetU8s (uint_8*, size_t); 142 void GetLine (char* ptr, size_t len); 143 144 void Get(char& c) {GetByte(c);} 145 void Get(r_4& x) {GetR4(x);} 146 void Get(r_8& x) {GetR8(x);} 147 void Get(uint_2& x) {GetU2(x);} 148 void Get(int_2& x) {GetI2(x);} 149 void Get(uint_4& x) {GetU4(x);} 150 void Get(int_4& x) {GetI4(x);} 151 void Get(uint_8& x) {GetU8(x);} 152 void Get(int_8& x) {GetI8(x);} 153 void Get(r_4* x, size_t n) {GetR4s(x,n);} 154 void Get(r_8* x, size_t n) {GetR8s(x,n);} 155 void Get(uint_2* x, size_t n) {GetU2s(x,n);} 156 void Get(int_2* x, size_t n) {GetI2s(x,n);} 157 void Get(uint_4* x, size_t n) {GetU4s(x,n);} 158 void Get(int_4* x, size_t n) {GetI4s(x,n);} 159 void Get(uint_8* x, size_t n) {GetU8s(x,n);} 160 void Get(int_8* x, size_t n) {GetI8s(x,n);} 161 162 int Version() {return version;} 163 string CreationDate() { return creationdate; } 164 165 protected: 166 void Scan(); 167 char* GetCStr(uint_2 l); 168 #ifdef RFIO 169 erosifstream *s; 170 #else 171 istream* s; 172 #endif 173 int bigEndian; 174 int version; 175 176 string fName; 177 string creationdate; 178 179 struct PPFTag { 180 int_8 popos; 181 int_4 cid; 182 int_4 key; 183 uint_2 lnom; 184 char* nom; 185 }; 186 PPFTag* mTags; 187 int_4 mNTags; 188 list<char*> mSbuffs; 189 char* mSbuf; 190 int mSbsz; 191 }; 192 193 class POutPersist { 194 public: 195 POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE); 196 ~POutPersist(); 197 198 int_4 WriteTag(int_4 key, char const * name=NULL); 199 void PutByte (char c); 200 void PutBytes(void const* ptr, size_t bytes); 201 void PutR4 (r_4); 202 void PutR4s (r_4 const*, size_t); 203 void PutR8 (r_8); 204 void PutR8s (r_8 const*, size_t); 205 void PutI2 (int_2); 206 void PutI2s (int_2 const*, size_t); 207 void PutU2 (uint_2); 208 void PutU2s (uint_2 const*, size_t); 209 void PutI4 (int_4); 210 void PutI4s (int_4 const*, size_t); 211 void PutU4 (uint_4); 212 void PutU4s (uint_4 const*, size_t); 213 void PutI8 (int_8); 214 void PutI8s (int_8 const*, size_t); 215 void PutU8 (uint_8); 216 void PutU8s (uint_8 const*, size_t); 217 void PutLine (char const* ptr, size_t len=0); 218 219 void Put(char c) {PutByte(c);} 220 void Put(r_4 x) {PutR4(x);} 221 void Put(r_8 x) {PutR8(x);} 222 void Put(uint_2 x) {PutU2(x);} 223 void Put(int_2 x) {PutI2(x);} 224 void Put(uint_4 x) {PutU4(x);} 225 void Put(int_4 x) {PutI4(x);} 226 void Put(uint_8 x) {PutU8(x);} 227 void Put(int_8 x) {PutI8(x);} 228 void Put(r_4 const* x, size_t n) {PutR4s(x,n);} 229 void Put(r_8 const* x, size_t n) {PutR8s(x,n);} 230 void Put(uint_2 const* x, size_t n) {PutU2s(x,n);} 231 void Put(int_2 const* x, size_t n) {PutI2s(x,n);} 232 void Put(uint_4 const* x, size_t n) {PutU4s(x,n);} 233 void Put(int_4 const* x, size_t n) {PutI4s(x,n);} 234 void Put(uint_8 const* x, size_t n) {PutU8s(x,n);} 235 void Put(int_8 const* x, size_t n) {PutI8s(x,n);} 236 237 238 protected: 239 #ifdef RFIO 240 erosofstream *s; 241 #else 242 ostream* s; 243 #endif 244 int bigEndian; 245 int_4 numTag; 246 int_8 previous; 247 }; 248 249 /* 250 // Ceci est dangereux car un template a priorite sur un changement de type, meme trivial... 251 252 template <class T> 253 POutPersist& operator << (POutPersist& c, T const& data) 254 { 255 c.PutBytes(&data, sizeof(T)); 256 return c; 257 } 258 259 template <class T> 260 PInPersist& operator >> (PInPersist& c, T& data) 261 { 262 c.GetBytes(&data, sizeof(T)); 263 return c; 264 } 265 */ 266 54 55 56 57 // Ancestor for PInPersist and POutPersist 58 // Handles (statically) the registration of classes. 59 60 class PIOPersist { 61 public: 62 enum {PPS_NATIVE = -1, PPS_LITTLE_ENDIAN = 0, PPS_BIG_ENDIAN = 1}; 63 typedef PPersist* (*ClassCreatorFunc)(); 64 65 static void RegisterClass(uint_8 classId, ClassCreatorFunc f); 66 static ClassCreatorFunc FindCreatorFunc(uint_8 classId); 67 static uint_8 Hash(string const& typname) { 68 MD5Init(&ctx); 69 MD5Update(&ctx, (unsigned char*) typname.c_str(), typname.size()); 70 MD5Final(&ctx); 71 return ( *((uint_8*) ctx.digest) + *((uint_8*) (ctx.digest+8))); 72 } 73 static MD5_CTX ctx; 74 75 private: 76 77 typedef map<uint_8, ClassCreatorFunc, less<uint_8> > ClassList; 78 static ClassList classList; 79 80 protected: 81 enum {PPS_NULL = 0, // this is a null object 82 PPS_STRING = 1, // string, length (2b) + data 83 PPS_OBJECT = 2, // classId, data... 84 PPS_REFERENCE = 3, // objectId 85 PPS_TAG = 4, // tag entries 86 PPS_EOF = 5, // Just before tag infomation, offset to PPS_TAG 87 PPS_LINE = 6, // '\n'-terminated, deprecated ? 88 PPS_SIMPLE = 16, // 16 + number of bytes, up to 8 bytes 89 PPS_SIMPLE_ARRAY = 32, // 32 + number of bytes, up to 8 bytes, then 2 bytes of length 90 PPS_SIMPLE_ARRAY4 = 64, // 64 + number of bytes, up to 8 bytes, then 4 bytes of length 91 PPS_SIMPLE_ARRAY8 = 128 // 64 + number of bytes, up to 8 bytes, then 8 bytes of length 92 }; 93 94 map<string, int_8> tags; 95 }; 96 97 98 // TBD : use hash tables instead of maps. Check hashtbl status in STL. 99 100 class PInPersist : public PIOPersist { 101 public: 102 PInPersist(string const& flnm, bool scan=true); 103 ~PInPersist(); 104 105 bool GotoTag(string const& name); 106 107 void GetByte (char& c); 108 void GetBytes(void* ptr, size_t bytes); 109 void GetR4 (r_4&); 110 void GetR4s (r_4*, size_t); 111 void GetR8 (r_8&); 112 void GetR8s (r_8*, size_t); 113 void GetI2 (int_2&); 114 void GetI2s (int_2*, size_t); 115 void GetU2 (uint_2&); 116 void GetU2s (uint_2*, size_t); 117 void GetI4 (int_4&); 118 void GetI4s (int_4*, size_t); 119 void GetU4 (uint_4&); 120 void GetU4s (uint_4*, size_t); 121 void GetI8 (int_8&); 122 void GetI8s (int_8*, size_t); 123 void GetU8 (uint_8&); 124 void GetU8s (uint_8*, size_t); 125 void GetLine (char* ptr, size_t len); 126 void GetStr (string&); 127 128 void Get(char& c) {GetByte(c);} 129 void Get(r_4& x) {GetR4(x);} 130 void Get(r_8& x) {GetR8(x);} 131 void Get(uint_2& x) {GetU2(x);} 132 void Get(int_2& x) {GetI2(x);} 133 void Get(uint_4& x) {GetU4(x);} 134 void Get(int_4& x) {GetI4(x);} 135 void Get(uint_8& x) {GetU8(x);} 136 void Get(int_8& x) {GetI8(x);} 137 void Get(r_4* x, size_t n) {GetR4s(x,n);} 138 void Get(r_8* x, size_t n) {GetR8s(x,n);} 139 void Get(uint_2* x, size_t n) {GetU2s(x,n);} 140 void Get(int_2* x, size_t n) {GetI2s(x,n);} 141 void Get(uint_4* x, size_t n) {GetU4s(x,n);} 142 void Get(int_4* x, size_t n) {GetI4s(x,n);} 143 void Get(uint_8* x, size_t n) {GetU8s(x,n);} 144 void Get(int_8* x, size_t n) {GetI8s(x,n);} 145 void Get(string& x) {GetStr(x);} 146 147 PPersist* ReadObject(); 148 149 150 int Version() {return version;} 151 time_t CreationDate() { return creationdate; } 152 153 protected: 154 void CheckTag (short datasz); 155 void CheckArrayTag(short datasz, size_t sz); 156 void GetRawByte (char& c); 157 void GetRawBytes(void* ptr, size_t bytes); 158 void GetRawI2 (int_2&); 159 void GetRawI4 (int_4&); 160 void GetRawI8 (int_8&); 161 void GetRawU8 (uint_8&); 162 void GetObject(PPersist*); // Object has been allocated with correct type 163 int_4 assignObjectId(PPersist* x); 164 void Scan(); 165 char* GetCStr(uint_2 l); 166 167 istream* s; 168 169 bool bigEndian; 170 int version; 171 172 time_t creationdate; 173 174 // already read objects, id = order in array 175 typedef vector<PPersist*> ObjList; 176 ObjList objList; 177 178 friend class PPersist; 179 }; 180 181 class POutPersist : public PIOPersist { 182 public: 183 POutPersist(string const& flnm, int endianness = PPS_NATIVE); 184 ~POutPersist(); 185 186 void WriteTag(string const& name); 187 188 void PutByte (char c); 189 void PutBytes(void const* ptr, size_t bytes); 190 void PutR4 (r_4); 191 void PutR4s (r_4 const*, size_t); 192 void PutR8 (r_8); 193 void PutR8s (r_8 const*, size_t); 194 void PutI2 (int_2); 195 void PutI2s (int_2 const*, size_t); 196 void PutU2 (uint_2); 197 void PutU2s (uint_2 const*, size_t); 198 void PutI4 (int_4); 199 void PutI4s (int_4 const*, size_t); 200 void PutU4 (uint_4); 201 void PutU4s (uint_4 const*, size_t); 202 void PutI8 (int_8); 203 void PutI8s (int_8 const*, size_t); 204 void PutU8 (uint_8); 205 void PutU8s (uint_8 const*, size_t); 206 void PutLine (char const* ptr, size_t len=0); // deprecated ? 207 void PutStr (string const&); 208 void PutObject (PPersist const*); // Like doing Write(stream) on object 209 210 void Put(char c) {PutByte(c);} 211 void Put(r_4 x) {PutR4(x);} 212 void Put(r_8 x) {PutR8(x);} 213 void Put(uint_2 x) {PutU2(x);} 214 void Put(int_2 x) {PutI2(x);} 215 void Put(uint_4 x) {PutU4(x);} 216 void Put(int_4 x) {PutI4(x);} 217 void Put(uint_8 x) {PutU8(x);} 218 void Put(int_8 x) {PutI8(x);} 219 void Put(r_4 const* x, size_t n) {PutR4s(x,n);} 220 void Put(r_8 const* x, size_t n) {PutR8s(x,n);} 221 void Put(uint_2 const* x, size_t n) {PutU2s(x,n);} 222 void Put(int_2 const* x, size_t n) {PutI2s(x,n);} 223 void Put(uint_4 const* x, size_t n) {PutU4s(x,n);} 224 void Put(int_4 const* x, size_t n) {PutI4s(x,n);} 225 void Put(uint_8 const* x, size_t n) {PutU8s(x,n);} 226 void Put(int_8 const* x, size_t n) {PutI8s(x,n);} 227 void Put(string const& s) {PutStr(s);} 228 void Put(PPersist const* x) {PutObject(x);} 229 230 231 protected: 232 ostream* s; 233 bool bigEndian; 234 235 void PutArrayTag(short datasz, size_t sz); 236 void PutRawByte (char); 237 void PutRawI2 (int_2); 238 void PutRawI4 (int_4); 239 void PutRawI8 (int_8); 240 void PutRawU8 (uint_8); 241 void PutRawBytes(void const* ptr, size_t bytes); 242 bool serializeNullAndRepeat(PPersist const* x); 243 int_4 findObjectId(PPersist const* x); 244 int_4 assignObjectId(PPersist const* x); 245 246 // already serialized objects 247 typedef map<PPersist const*, int_4, less<PPersist const*> > ObjList; 248 ObjList objList; 249 }; 250 251 267 252 #define RAWPERSISTIO(_Type_,_xtyp_) \ 268 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \269 { \270 c.Put##_xtyp_(data);\271 return c; \272 } \253 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \ 254 { \ 255 c.Put##_xtyp_(data); \ 256 return c; \ 257 } \ 273 258 \ 274 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \275 { \276 c.Get##_xtyp_(data);\277 return c; \278 }279 280 RAWPERSISTIO(int_4,I4) 281 RAWPERSISTIO(uint_4,U4) 282 RAWPERSISTIO(int_2,I2) 283 RAWPERSISTIO(uint_2,U2) 284 RAWPERSISTIO(char,Byte) 285 RAWPERSISTIO(r_4,R4) 286 RAWPERSISTIO(r_8,R8) 287 259 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \ 260 { \ 261 c.Get##_xtyp_(data); \ 262 return c; \ 263 } 264 265 RAWPERSISTIO(int_4,I4); 266 RAWPERSISTIO(uint_4,U4); 267 RAWPERSISTIO(int_2,I2); 268 RAWPERSISTIO(uint_2,U2); 269 RAWPERSISTIO(char,Byte); 270 RAWPERSISTIO(r_4,R4); 271 RAWPERSISTIO(r_8,R8); 272 288 273 #if 0 289 274 #define STRUCTPERSISTIO(_Type_, _field_, _size_) \ 290 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \291 { \292 c.PutBytes(&data._field_, _size_); \293 return c; \294 } \275 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \ 276 { \ 277 c.PutBytes(&data._field_, _size_); \ 278 return c; \ 279 } \ 295 280 \ 296 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \297 { \298 c.GetBytes(&data._field_, _size_); \299 return c; \300 }281 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \ 282 { \ 283 c.GetBytes(&data._field_, _size_); \ 284 return c; \ 285 } 301 286 302 287 #endif 303 304 inline POutPersist& operator << (POutPersist& c, PPersist const& obj) 305 { 306 obj.Write(c); 307 return c; 308 } 309 310 inline PInPersist& operator >> (PInPersist& c, PPersist& obj) 311 { 312 obj.Read(c); 313 return c; 314 } 315 316 // Fabrication de fonction Create automatique, et enregistrement... 317 template <class T> 318 class PPersistRegistrar { 319 public: 320 static PPersist* Create() {return new T();} 321 static void Register() {PPersistMgr::RegisterClass(T::classId,Create);} 322 }; 323 288 289 inline POutPersist& operator << (POutPersist& c, PPersist const& obj) 290 { 291 obj.Write(c); 292 return c; 293 } 294 295 inline PInPersist& operator >> (PInPersist& c, PPersist& obj) 296 { 297 obj.Read(c); 298 return c; 299 } 300 301 // Utility class to 302 // - compute the class ID from a MD5 hash of the class name 303 // - register classes with PIOPersist, through PPRegister macro 304 305 template <class T> 306 class PPersistRegistrar { 307 public: 308 static PPersist* Create() {return new T();} 309 static void Register() {PIOPersist::RegisterClass(Hash(),Create);} 310 static uint_8 Hash() { 311 return PIOPersist::Hash(typeid(T).name()); 312 } 313 }; 314 324 315 #define PPRegister(className) PPersistRegistrar<className>::Register(); 316 317 } // namespace 325 318 326 319 #endif -
trunk/SophyaLib/NTools/dates.cc
r220 r241 1 #include " defs.h"1 #include "machdefs.h" 2 2 #include <stdlib.h> 3 3 #include <string.h> … … 6 6 #include <time.h> 7 7 #include <ctype.h> 8 #include "defs.h"8 #include <math.h> 9 9 #include "perrors.h" 10 10 #include "dates.h" -
trunk/SophyaLib/NTools/dates.h
r220 r241 5 5 #include <string> 6 6 #include <iostream.h> 7 #include " exceptions.h"7 #include "pexceptions.h" 8 8 #if defined(__KCC__) 9 9 using std::string ; -
trunk/SophyaLib/NTools/histos2.cc
r220 r241 2030 2030 SetBandX(min,max); 2031 2031 } 2032 DBASSERT (nban dx==NBandX());2032 DBASSERT (nbanx==NBandX()); 2033 2033 for( j=0; j<NBandX(); j++) { 2034 2034 h = HBandX(j); … … 2043 2043 SetBandY(min,max); 2044 2044 } 2045 DBASSERT (nban dy==NBandY());2045 DBASSERT (nbany==NBandY()); 2046 2046 for( j=0; j<NBandY(); j++) { 2047 2047 h = HBandY(j); -
trunk/SophyaLib/SysTools/ctimer.cc
r219 r241 1 1 // 2 // $Id: ctimer.cc,v 1. 1.1.1 1999-04-09 17:57:04ansari Exp $2 // $Id: ctimer.cc,v 1.2 1999-04-21 13:11:59 ansari Exp $ 3 3 // 4 4 5 #include " defs.h"5 #include "machdefs.h" 6 6 #include "ctimer.h" 7 7 … … 42 42 cpu0 = cpuSplit = clock(); 43 43 elapse0 = elapseSplit = time(0); 44 END_CONSTRUCTOR45 44 } 46 45 -
trunk/SophyaLib/SysTools/ctimer.h
r219 r241 1 1 // This may look like C code, but it is really -*- C++ -*- 2 2 // 3 // $Id: ctimer.h,v 1. 1.1.1 1999-04-09 17:57:04ansari Exp $3 // $Id: ctimer.h,v 1.2 1999-04-21 13:12:00 ansari Exp $ 4 4 // 5 5 … … 12 12 #include <iostream.h> 13 13 #include <stdio.h> 14 #include " peida.h"14 #include "machdefs.h" 15 15 16 16 // <summary> Permet de chronometrer des fonctions. </summary> … … 22 22 23 23 // La macro SPLITTIME lui permet d'afficher des temps partiels. 24 class Timer EXC_AWARE{24 class Timer { 25 25 public: 26 26 // L'objet memorise le temps CPU et l'heure, et le nom donne -
trunk/SophyaLib/SysTools/pdlmgr.h
r230 r241 6 6 #define PDYNLINKMGR_SEEN 7 7 8 #include " defs.h"8 #include "machdefs.h" 9 9 #include <string> 10 10 #if defined(__KCC__) -
trunk/SophyaLib/SysTools/periodic.cc
r219 r241 4 4 /* LAL/IN2P3 (Orsay) DAPNIA/CEA (Saclay) */ 5 5 6 #include " defs.h"6 #include "machdefs.h" 7 7 #include <signal.h> 8 8 #include <unistd.h> -
trunk/SophyaLib/SysTools/periodic.h
r219 r241 4 4 #define PERIODIC_H 5 5 6 #include " defs.h"6 #include "machdefs.h" 7 7 #include <list> 8 8 #if defined(__KCC__) -
trunk/SophyaLib/SysTools/psighand.cc
r219 r241 5 5 #include <signal.h> 6 6 #include "psighand.h" 7 #include "perrors.h" 7 #include "pexceptions.h" 8 9 using namespace PlanckDPC; 8 10 9 11 static bool sigprstate[5] = {false, false, false, false}; … … 96 98 case SIGFPE : 97 99 puts("PeidaProcessSignal: ###Signal SIGFPE catched, throw catchedSIGFPE ###"); 98 THROW(catchedSIGFPE);100 throw CaughtSignalExc("SIGFPE"); 99 101 case SIGSEGV : 100 102 puts("PeidaProcessSignal: ###Signal SIGSEGV catched, throw catchedSIGSEGV ###"); 101 THROW(catchedSIGSEGV);103 throw CaughtSignalExc("SIGSEGV"); 102 104 case SIGINT : 103 105 puts("PeidaProcessSignal: ###Signal SIGINT catched, throw catchedSIGINT ###"); 104 THROW(catchedSIGINT);106 throw CaughtSignalExc("SIGINT"); 105 107 case SIGQUIT : 106 108 puts("PeidaProcessSignal: ###Signal SIGQUIT catched, throw catchedSIGQUIT ###"); 107 THROW(catchedSIGQUIT);109 throw CaughtSignalExc("SIGQUIT"); 108 110 case SIGUSR1 : 109 111 puts("PeidaProcessSignal: ###Signal SIGUSR1 catched, throw catchedSIGUSR1 ###"); 110 THROW(catchedSIGQUIT);112 throw CaughtSignalExc("SIGUSR1"); 111 113 case SIGUSR2 : 112 114 puts("PeidaProcessSignal: ###Signal SIGUSR2 catched, throw catchedSIGUSR2 ###"); 113 THROW(catchedSIGQUIT);115 throw CaughtSignalExc("SIGUSR2"); 114 116 default : 115 117 printf("PeidaProcessSignal: ###Signal %d catched, throw inconsistentErr ### \n", s); 116 THROW(inconsistentErr);118 throw CaughtSignalExc("???"); 117 119 } 118 120 -
trunk/SophyaLib/SysTools/psighand.h
r219 r241 5 5 #define PSIGHANDLE_SEEN 6 6 7 #include " defs.h"7 #include "machdefs.h" 8 8 void PeidaConfigureSignalhandling(bool cfpe=false, bool csegv=false, bool cquit=false, bool cusr1=false, bool cusr2=false); 9 9 #endif
Note:
See TracChangeset
for help on using the changeset viewer.