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 #include <string.h>
00027
00028 #include "lowlevel.hxx"
00029
00030
00031 static int read_error = false ;
00032 static int write_error = false ;
00033
00034 void sgClearReadError() { read_error = false; }
00035 void sgClearWriteError() { write_error = false; }
00036 int sgReadError() { return read_error ; }
00037 int sgWriteError() { return write_error ; }
00038
00039
00040 void sgReadChar ( gzFile fd, char *var )
00041 {
00042 if ( gzread ( fd, var, sizeof(char) ) != sizeof(char) ) {
00043 read_error = true ;
00044 }
00045 }
00046
00047
00048 void sgWriteChar ( gzFile fd, const char var )
00049 {
00050 if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) != sizeof(char) ) {
00051 write_error = true ;
00052 }
00053 }
00054
00055
00056 void sgReadFloat ( gzFile fd, float *var )
00057 {
00058 union { float v; uint32_t u; } buf;
00059 if ( gzread ( fd, &buf.u, sizeof(float) ) != sizeof(float) ) {
00060 read_error = true ;
00061 }
00062 if ( sgIsBigEndian() ) {
00063 sgEndianSwap( &buf.u );
00064 }
00065 *var = buf.v;
00066 }
00067
00068
00069 void sgWriteFloat ( gzFile fd, const float var )
00070 {
00071 union { float v; uint32_t u; } buf;
00072 buf.v = var;
00073 if ( sgIsBigEndian() ) {
00074 sgEndianSwap( &buf.u );
00075 }
00076 if ( gzwrite ( fd, (void *)(&buf.u), sizeof(float) ) != sizeof(float) ) {
00077 write_error = true ;
00078 }
00079 }
00080
00081
00082 void sgReadDouble ( gzFile fd, double *var )
00083 {
00084 union { double v; uint64_t u; } buf;
00085 if ( gzread ( fd, &buf.u, sizeof(double) ) != sizeof(double) ) {
00086 read_error = true ;
00087 }
00088 if ( sgIsBigEndian() ) {
00089 sgEndianSwap( &buf.u );
00090 }
00091 *var = buf.v;
00092 }
00093
00094
00095 void sgWriteDouble ( gzFile fd, const double var )
00096 {
00097 union { double v; uint64_t u; } buf;
00098 buf.v = var;
00099 if ( sgIsBigEndian() ) {
00100 sgEndianSwap( &buf.u );
00101 }
00102 if ( gzwrite ( fd, (void *)(&buf.u), sizeof(double) ) != sizeof(double) ) {
00103 write_error = true ;
00104 }
00105 }
00106
00107
00108 void sgReadUInt ( gzFile fd, unsigned int *var )
00109 {
00110 if ( gzread ( fd, var, sizeof(unsigned int) ) != sizeof(unsigned int) ) {
00111 read_error = true ;
00112 }
00113 if ( sgIsBigEndian() ) {
00114 sgEndianSwap( (uint32_t *)var);
00115 }
00116 }
00117
00118
00119 void sgWriteUInt ( gzFile fd, const unsigned int var )
00120 {
00121 if ( sgIsBigEndian() ) {
00122 sgEndianSwap( (uint32_t *)&var);
00123 }
00124 if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) )
00125 != sizeof(unsigned int) )
00126 {
00127 write_error = true ;
00128 }
00129 }
00130
00131
00132 void sgReadInt ( gzFile fd, int *var )
00133 {
00134 if ( gzread ( fd, var, sizeof(int) ) != sizeof(int) ) {
00135 read_error = true ;
00136 }
00137 if ( sgIsBigEndian() ) {
00138 sgEndianSwap( (uint32_t *)var);
00139 }
00140 }
00141
00142
00143 void sgWriteInt ( gzFile fd, const int var )
00144 {
00145 if ( sgIsBigEndian() ) {
00146 sgEndianSwap( (uint32_t *)&var);
00147 }
00148 if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) != sizeof(int) ) {
00149 write_error = true ;
00150 }
00151 }
00152
00153
00154 void sgReadLong ( gzFile fd, int32_t *var )
00155 {
00156 if ( gzread ( fd, var, sizeof(int32_t) ) != sizeof(int32_t) ) {
00157 read_error = true ;
00158 }
00159 if ( sgIsBigEndian() ) {
00160 sgEndianSwap( (uint32_t *)var);
00161 }
00162 }
00163
00164
00165 void sgWriteLong ( gzFile fd, const int32_t var )
00166 {
00167 if ( sgIsBigEndian() ) {
00168 sgEndianSwap( (uint32_t *)&var);
00169 }
00170 if ( gzwrite ( fd, (void *)(&var), sizeof(int32_t) )
00171 != sizeof(int32_t) )
00172 {
00173 write_error = true ;
00174 }
00175 }
00176
00177
00178 void sgReadLongLong ( gzFile fd, int64_t *var )
00179 {
00180 if ( gzread ( fd, var, sizeof(int64_t) ) != sizeof(int64_t) ) {
00181 read_error = true ;
00182 }
00183 if ( sgIsBigEndian() ) {
00184 sgEndianSwap( (uint64_t *)var);
00185 }
00186 }
00187
00188
00189 void sgWriteLongLong ( gzFile fd, const int64_t var )
00190 {
00191 if ( sgIsBigEndian() ) {
00192 sgEndianSwap( (uint64_t *)&var);
00193 }
00194 if ( gzwrite ( fd, (void *)(&var), sizeof(int64_t) )
00195 != sizeof(int64_t) )
00196 {
00197 write_error = true ;
00198 }
00199 }
00200
00201
00202 void sgReadUShort ( gzFile fd, unsigned short *var )
00203 {
00204 if ( gzread ( fd, var, sizeof(unsigned short) ) != sizeof(unsigned short) ){
00205 read_error = true ;
00206 }
00207 if ( sgIsBigEndian() ) {
00208 sgEndianSwap( (uint16_t *)var);
00209 }
00210 }
00211
00212
00213 void sgWriteUShort ( gzFile fd, const unsigned short var )
00214 {
00215 if ( sgIsBigEndian() ) {
00216 sgEndianSwap( (uint16_t *)&var);
00217 }
00218 if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) )
00219 != sizeof(unsigned short) )
00220 {
00221 write_error = true ;
00222 }
00223 }
00224
00225
00226 void sgReadShort ( gzFile fd, short *var )
00227 {
00228 if ( gzread ( fd, var, sizeof(short) ) != sizeof(short) ) {
00229 read_error = true ;
00230 }
00231 if ( sgIsBigEndian() ) {
00232 sgEndianSwap( (uint16_t *)var);
00233 }
00234 }
00235
00236
00237 void sgWriteShort ( gzFile fd, const short var )
00238 {
00239 if ( sgIsBigEndian() ) {
00240 sgEndianSwap( (uint16_t *)&var);
00241 }
00242 if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) != sizeof(short) ) {
00243 write_error = true ;
00244 }
00245 }
00246
00247
00248 void sgReadFloat ( gzFile fd, const unsigned int n, float *var )
00249 {
00250 if ( gzread ( fd, var, sizeof(float) * n ) != (int)(sizeof(float) * n) ) {
00251 read_error = true ;
00252 }
00253 if ( sgIsBigEndian() ) {
00254 for ( unsigned int i = 0; i < n; ++i ) {
00255 sgEndianSwap( (uint32_t *)var++);
00256 }
00257 }
00258 }
00259
00260
00261 void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var )
00262 {
00263 if ( sgIsBigEndian() ) {
00264 float *swab = new float[n];
00265 float *ptr = swab;
00266 memcpy( swab, var, sizeof(float) * n );
00267 for ( unsigned int i = 0; i < n; ++i ) {
00268 sgEndianSwap( (uint32_t *)ptr++);
00269 }
00270 var = swab;
00271 }
00272 if ( gzwrite ( fd, (void *)var, sizeof(float) * n )
00273 != (int)(sizeof(float) * n) )
00274 {
00275 write_error = true ;
00276 }
00277 }
00278
00279 void sgReadDouble ( gzFile fd, const unsigned int n, double *var )
00280 {
00281 if ( gzread ( fd, var, sizeof(double) * n ) != (int)(sizeof(double) * n) ) {
00282 read_error = true ;
00283 }
00284 if ( sgIsBigEndian() ) {
00285 for ( unsigned int i = 0; i < n; ++i ) {
00286 sgEndianSwap( (uint64_t *)var++);
00287 }
00288 }
00289 }
00290
00291
00292 void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var )
00293 {
00294 if ( sgIsBigEndian() ) {
00295 double *swab = new double[n];
00296 double *ptr = swab;
00297 memcpy( swab, var, sizeof(double) * n );
00298 for ( unsigned int i = 0; i < n; ++i ) {
00299 sgEndianSwap( (uint64_t *)ptr++);
00300 }
00301 var = swab;
00302 }
00303 if ( gzwrite ( fd, (void *)var, sizeof(double) * n )
00304 != (int)(sizeof(double) * n) )
00305 {
00306 write_error = true ;
00307 }
00308 }
00309
00310 void sgReadBytes ( gzFile fd, const unsigned int n, void *var )
00311 {
00312 if ( n == 0) return;
00313 if ( gzread ( fd, var, n ) != (int)n ) {
00314 read_error = true ;
00315 }
00316 }
00317
00318 void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var )
00319 {
00320 if ( n == 0) return;
00321 if ( gzwrite ( fd, (void *)var, n ) != (int)n ) {
00322 write_error = true ;
00323 }
00324 }
00325
00326
00327 void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var )
00328 {
00329 if ( gzread ( fd, var, sizeof(unsigned short) * n )
00330 != (int)(sizeof(unsigned short) * n) )
00331 {
00332 read_error = true ;
00333 }
00334 if ( sgIsBigEndian() ) {
00335 for ( unsigned int i = 0; i < n; ++i ) {
00336 sgEndianSwap( (uint16_t *)var++);
00337 }
00338 }
00339 }
00340
00341
00342 void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var )
00343 {
00344 if ( sgIsBigEndian() ) {
00345 unsigned short *swab = new unsigned short[n];
00346 unsigned short *ptr = swab;
00347 memcpy( swab, var, sizeof(unsigned short) * n );
00348 for ( unsigned int i = 0; i < n; ++i ) {
00349 sgEndianSwap( (uint16_t *)ptr++);
00350 }
00351 var = swab;
00352 }
00353 if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n )
00354 != (int)(sizeof(unsigned short) * n) )
00355 {
00356 write_error = true ;
00357 }
00358 }
00359
00360
00361
00362 void sgReadShort ( gzFile fd, const unsigned int n, short *var )
00363 {
00364 if ( gzread ( fd, var, sizeof(short) * n )
00365 != (int)(sizeof(short) * n) )
00366 {
00367 read_error = true ;
00368 }
00369 if ( sgIsBigEndian() ) {
00370 for ( unsigned int i = 0; i < n; ++i ) {
00371 sgEndianSwap( (uint16_t *)var++);
00372 }
00373 }
00374 }
00375
00376
00377 void sgWriteShort ( gzFile fd, const unsigned int n, const short *var )
00378 {
00379 if ( sgIsBigEndian() ) {
00380 short *swab = new short[n];
00381 short *ptr = swab;
00382 memcpy( swab, var, sizeof(short) * n );
00383 for ( unsigned int i = 0; i < n; ++i ) {
00384 sgEndianSwap( (uint16_t *)ptr++);
00385 }
00386 var = swab;
00387 }
00388 if ( gzwrite ( fd, (void *)var, sizeof(short) * n )
00389 != (int)(sizeof(short) * n) )
00390 {
00391 write_error = true ;
00392 }
00393 }
00394
00395
00396 void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var )
00397 {
00398 if ( gzread ( fd, var, sizeof(unsigned int) * n )
00399 != (int)(sizeof(unsigned int) * n) )
00400 {
00401 read_error = true ;
00402 }
00403 if ( sgIsBigEndian() ) {
00404 for ( unsigned int i = 0; i < n; ++i ) {
00405 sgEndianSwap( (uint32_t *)var++);
00406 }
00407 }
00408 }
00409
00410
00411 void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var )
00412 {
00413 if ( sgIsBigEndian() ) {
00414 unsigned int *swab = new unsigned int[n];
00415 unsigned int *ptr = swab;
00416 memcpy( swab, var, sizeof(unsigned int) * n );
00417 for ( unsigned int i = 0; i < n; ++i ) {
00418 sgEndianSwap( (uint32_t *)ptr++);
00419 }
00420 var = swab;
00421 }
00422 if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n )
00423 != (int)(sizeof(unsigned int) * n) )
00424 {
00425 write_error = true ;
00426 }
00427 }
00428
00429
00430
00431 void sgReadInt ( gzFile fd, const unsigned int n, int *var )
00432 {
00433 if ( gzread ( fd, var, sizeof(int) * n )
00434 != (int)(sizeof(int) * n) )
00435 {
00436 read_error = true ;
00437 }
00438 if ( sgIsBigEndian() ) {
00439 for ( unsigned int i = 0; i < n; ++i ) {
00440 sgEndianSwap( (uint32_t *)var++);
00441 }
00442 }
00443 }
00444
00445
00446 void sgWriteInt ( gzFile fd, const unsigned int n, const int *var )
00447 {
00448 if ( sgIsBigEndian() ) {
00449 int *swab = new int[n];
00450 int *ptr = swab;
00451 memcpy( swab, var, sizeof(int) * n );
00452 for ( unsigned int i = 0; i < n; ++i ) {
00453 sgEndianSwap( (uint32_t *)ptr++);
00454 }
00455 var = swab;
00456 }
00457 if ( gzwrite ( fd, (void *)var, sizeof(int) * n )
00458 != (int)(sizeof(int) * n) )
00459 {
00460 write_error = true ;
00461 }
00462 }
00463
00464
00465
00466 #define MAX_ENTITY_NAME_LENGTH 1024
00467
00468 void sgReadString ( gzFile fd, char **var )
00469 {
00470 int i ;
00471 char s [ MAX_ENTITY_NAME_LENGTH ] ;
00472
00473 for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ ) {
00474 int c = gzgetc ( fd ) ;
00475 s [ i ] = c ;
00476
00477 if ( c == '\0' )
00478 break ;
00479 }
00480
00481 if ( i >= MAX_ENTITY_NAME_LENGTH-1 )
00482 s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ;
00483
00484
00485 if ( s[0] == '\0' )
00486 *var = NULL ;
00487 else {
00488 *var = new char [ strlen(s)+1 ] ;
00489 strcpy ( *var, s ) ;
00490 }
00491 }
00492
00493
00494 void sgWriteString ( gzFile fd, const char *var )
00495 {
00496 if ( var != NULL ) {
00497 if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) ==
00498 (int)(strlen(var) + 1) )
00499 return ;
00500 } else {
00501 gzputc( fd, 0 );
00502 }
00503 }
00504
00505