head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.38; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.54; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.01; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.57; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# lowlevel.cxx

    00001 // lowlevel.cxx -- routines to handle lowlevel compressed binary IO of
    00002 //                 various datatypes
    00003 //
    00004 // Shamelessly adapted from plib (plib.sourceforge.net) January 2001
    00005 //
    00006 // Original version Copyright (C) 2000  the plib team
    00007 // Local changes Copyright (C) 2000  Curtis L. Olson  - curt@@flightgear.org
    00008 //
    00009 // This program is free software; you can redistribute it and/or modify
    00010 // it under the terms of the GNU General Public License as published by
    00011 // the Free Software Foundation; either version 2 of the License, or
    00012 // (at your option) any later version.
    00013 //
    00014 // This program is distributed in the hope that it will be useful,
    00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017 // GNU General Public License for more details.
    00018 //
    00019 // You should have received a copy of the GNU General Public License
    00020 // along with this program; if not, write to the Free Software
    00021 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00022 //
    00023 // $Id: lowlevel.cxx,v 1.8 2001/07/13 03:36:50 curt Exp $
    00024
    00025
    00026 #include <string.h>             // for memcpy()
    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     if ( gzread ( fd, var, sizeof(float) ) != sizeof(float) ) {
    00059         read_error = true ;
    00060     }
    00061     if ( sgIsBigEndian() ) {
    00062         sgEndianSwap( (unsigned int*)var);
    00063     }
    00064 }
    00065
    00066
    00067 void sgWriteFloat ( gzFile fd, const float var )
    00068 {
    00069     if ( sgIsBigEndian() ) {
    00070         sgEndianSwap( (unsigned int*)&var);
    00071     }
    00072     if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) != sizeof(float) ) {
    00073         write_error = true ;
    00074     }
    00075 }
    00076
    00077
    00078 void sgReadDouble ( gzFile fd, double *var )
    00079 {
    00080     if ( gzread ( fd, var, sizeof(double) ) != sizeof(double) ) {
    00081         read_error = true ;
    00082     }
    00083     if ( sgIsBigEndian() ) {
    00084         sgEndianSwap( (uint64*)var);
    00085     }
    00086 }
    00087
    00088
    00089 void sgWriteDouble ( gzFile fd, const double var )
    00090 {
    00091     if ( sgIsBigEndian() ) {
    00092         sgEndianSwap( (uint64*)&var);
    00093     }
    00094     if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) != sizeof(double) ) {
    00095         write_error = true ;
    00096     }
    00097 }
    00098
    00099
    00100 void sgReadUInt ( gzFile fd, unsigned int *var )
    00101 {
    00102     if ( gzread ( fd, var, sizeof(unsigned int) ) != sizeof(unsigned int) ) {
    00103         read_error = true ;
    00104     }
    00105     if ( sgIsBigEndian() ) {
    00106         sgEndianSwap( (unsigned int*)var);
    00107     }
    00108 }
    00109
    00110
    00111 void sgWriteUInt ( gzFile fd, const unsigned int var )
    00112 {
    00113     if ( sgIsBigEndian() ) {
    00114         sgEndianSwap( (unsigned int*)&var);
    00115     }
    00116     if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) )
    00117          != sizeof(unsigned int) )
    00118     {
    00119         write_error = true ;
    00120     }
    00121 }
    00122
    00123
    00124 void sgReadInt ( gzFile fd, int *var )
    00125 {
    00126     if ( gzread ( fd, var, sizeof(int) ) != sizeof(int) ) {
    00127         read_error = true ;
    00128     }
    00129     if ( sgIsBigEndian() ) {
    00130         sgEndianSwap( (unsigned int*)var);
    00131     }
    00132 }
    00133
    00134
    00135 void sgWriteInt ( gzFile fd, const int var )
    00136 {
    00137     if ( sgIsBigEndian() ) {
    00138         sgEndianSwap( (unsigned int*)&var);
    00139     }
    00140     if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) != sizeof(int) ) {
    00141         write_error = true ;
    00142     }
    00143 }
    00144
    00145
    00146 void sgReadLong ( gzFile fd, long int *var )
    00147 {
    00148     if ( gzread ( fd, var, sizeof(long int) ) != sizeof(long int) ) {
    00149         read_error = true ;
    00150     }
    00151     if ( sgIsBigEndian() ) {
    00152         sgEndianSwap( (unsigned int*)var);
    00153     }
    00154 }
    00155
    00156
    00157 void sgWriteLong ( gzFile fd, const long int var )
    00158 {
    00159     if ( sgIsBigEndian() ) {
    00160         sgEndianSwap( (unsigned int*)&var);
    00161     }
    00162     if ( gzwrite ( fd, (void *)(&var), sizeof(long int) )
    00163          != sizeof(long int) )
    00164     {
    00165         write_error = true ;
    00166     }
    00167 }
    00168
    00169
    00170 void sgReadLongLong ( gzFile fd, int64 *var )
    00171 {
    00172     if ( gzread ( fd, var, sizeof(int64) ) != sizeof(int64) ) {
    00173         read_error = true ;
    00174     }
    00175     if ( sgIsBigEndian() ) {
    00176         sgEndianSwap( (uint64*)var);
    00177     }
    00178 }
    00179
    00180
    00181 void sgWriteLongLong ( gzFile fd, const int64 var )
    00182 {
    00183     if ( sgIsBigEndian() ) {
    00184         sgEndianSwap( (uint64*)&var);
    00185     }
    00186     if ( gzwrite ( fd, (void *)(&var), sizeof(int64) )
    00187          != sizeof(int64) )
    00188     {
    00189         write_error = true ;
    00190     }
    00191 }
    00192
    00193
    00194 void sgReadUShort ( gzFile fd, unsigned short *var )
    00195 {
    00196     if ( gzread ( fd, var, sizeof(unsigned short) ) != sizeof(unsigned short) ){
    00197         read_error = true ;
    00198     }
    00199     if ( sgIsBigEndian() ) {
    00200         sgEndianSwap( (unsigned short int*)var);
    00201     }
    00202 }
    00203
    00204
    00205 void sgWriteUShort ( gzFile fd, const unsigned short var )
    00206 {
    00207     if ( sgIsBigEndian() ) {
    00208         sgEndianSwap( (unsigned short*)&var);
    00209     }
    00210     if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) )
    00211          != sizeof(unsigned short) )
    00212     {
    00213         write_error = true ;
    00214     }
    00215 }
    00216
    00217
    00218 void sgReadShort ( gzFile fd, short *var )
    00219 {
    00220     if ( gzread ( fd, var, sizeof(short) ) != sizeof(short) ) {
    00221         read_error = true ;
    00222     }
    00223     if ( sgIsBigEndian() ) {
    00224         sgEndianSwap( (unsigned short int*)var);
    00225     }
    00226 }
    00227
    00228
    00229 void sgWriteShort ( gzFile fd, const short var )
    00230 {
    00231     if ( sgIsBigEndian() ) {
    00232         sgEndianSwap( (unsigned short*)&var);
    00233     }
    00234     if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) != sizeof(short) ) {
    00235         write_error = true ;
    00236     }
    00237 }
    00238
    00239
    00240 void sgReadFloat ( gzFile fd, const unsigned int n, float *var )
    00241 {
    00242     if ( gzread ( fd, var, sizeof(float) * n ) != (int)(sizeof(float) * n) ) {
    00243         read_error = true ;
    00244     }
    00245     if ( sgIsBigEndian() ) {
    00246         for ( unsigned int i = 0; i < n; ++i ) {
    00247             sgEndianSwap( (unsigned int*)var++);
    00248         }
    00249     }
    00250 }
    00251
    00252
    00253 void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var )
    00254 {
    00255     if ( sgIsBigEndian() ) {
    00256         float *swab = new float[n];
    00257         float *ptr = swab;
    00258         memcpy( swab, var, sizeof(float) * n );
    00259         for ( unsigned int i = 0; i < n; ++i ) {
    00260             sgEndianSwap( (unsigned int*)ptr++);
    00261         }
    00262         var = swab;
    00263     }
    00264     if ( gzwrite ( fd, (void *)var, sizeof(float) * n )
    00265          != (int)(sizeof(float) * n) )
    00266     {
    00267         write_error = true ;
    00268     }
    00269 }
    00270
    00271 void sgReadDouble ( gzFile fd, const unsigned int n, double *var )
    00272 {
    00273     if ( gzread ( fd, var, sizeof(double) * n ) != (int)(sizeof(double) * n) ) {
    00274         read_error = true ;
    00275     }
    00276     if ( sgIsBigEndian() ) {
    00277         for ( unsigned int i = 0; i < n; ++i ) {
    00278             sgEndianSwap( (uint64*)var++);
    00279         }
    00280     }
    00281 }
    00282
    00283
    00284 void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var )
    00285 {
    00286     if ( sgIsBigEndian() ) {
    00287         double *swab = new double[n];
    00288         double *ptr = swab;
    00289         memcpy( swab, var, sizeof(double) * n );
    00290         for ( unsigned int i = 0; i < n; ++i ) {
    00291             sgEndianSwap( (uint64*)ptr++);
    00292         }
    00293         var = swab;
    00294     }
    00295     if ( gzwrite ( fd, (void *)var, sizeof(double) * n )
    00296          != (int)(sizeof(double) * n) )
    00297     {
    00298         write_error = true ;
    00299     }
    00300 }
    00301
    00302 void sgReadBytes ( gzFile fd, const unsigned int n, void *var )
    00303 {
    00304     if ( n == 0) return;
    00305     if ( gzread ( fd, var, n ) != (int)n ) {
    00306         read_error = true ;
    00307     }
    00308 }
    00309
    00310 void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var )
    00311 {
    00312     if ( n == 0) return;
    00313     if ( gzwrite ( fd, (void *)var, n ) != (int)n ) {
    00314         write_error = true ;
    00315     }
    00316 }
    00317
    00318
    00319 void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var )
    00320 {
    00321     if ( gzread ( fd, var, sizeof(unsigned short) * n )
    00322          != (int)(sizeof(unsigned short) * n) )
    00323     {
    00324         read_error = true ;
    00325     }
    00326     if ( sgIsBigEndian() ) {
    00327         for ( unsigned int i = 0; i < n; ++i ) {
    00328             sgEndianSwap( (unsigned short int*)var++);
    00329         }
    00330     }
    00331 }
    00332
    00333
    00334 void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var )
    00335 {
    00336     if ( sgIsBigEndian() ) {
    00337         unsigned short *swab = new unsigned short[n];
    00338         unsigned short *ptr = swab;
    00339         memcpy( swab, var, sizeof(unsigned short) * n );
    00340         for ( unsigned int i = 0; i < n; ++i ) {
    00341             sgEndianSwap( (unsigned short*)ptr++);
    00342         }
    00343         var = swab;
    00344     }
    00345     if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n )
    00346          != (int)(sizeof(unsigned short) * n) )
    00347     {
    00348         write_error = true ;
    00349     }
    00350 }
    00351
    00352
    00353
    00354 void sgReadShort ( gzFile fd, const unsigned int n, short *var )
    00355 {
    00356     if ( gzread ( fd, var, sizeof(short) * n )
    00357          != (int)(sizeof(short) * n) )
    00358     {
    00359         read_error = true ;
    00360     }
    00361     if ( sgIsBigEndian() ) {
    00362         for ( unsigned int i = 0; i < n; ++i ) {
    00363             sgEndianSwap( (unsigned short int*)var++);
    00364         }
    00365     }
    00366 }
    00367
    00368
    00369 void sgWriteShort ( gzFile fd, const unsigned int n, const short *var )
    00370 {
    00371     if ( sgIsBigEndian() ) {
    00372         short *swab = new short[n];
    00373         short *ptr = swab;
    00374         memcpy( swab, var, sizeof(short) * n );
    00375         for ( unsigned int i = 0; i < n; ++i ) {
    00376             sgEndianSwap( (unsigned short*)ptr++);
    00377         }
    00378         var = swab;
    00379     }
    00380     if ( gzwrite ( fd, (void *)var, sizeof(short) * n )
    00381          != (int)(sizeof(short) * n) )
    00382     {
    00383         write_error = true ;
    00384     }
    00385 }
    00386
    00387
    00388 void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var )
    00389 {
    00390     if ( gzread ( fd, var, sizeof(unsigned int) * n )
    00391          != (int)(sizeof(unsigned int) * n) )
    00392     {
    00393         read_error = true ;
    00394     }
    00395     if ( sgIsBigEndian() ) {
    00396         for ( unsigned int i = 0; i < n; ++i ) {
    00397             sgEndianSwap( (unsigned int*)var++);
    00398         }
    00399     }
    00400 }
    00401
    00402
    00403 void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var )
    00404 {
    00405     if ( sgIsBigEndian() ) {
    00406         unsigned int *swab = new unsigned int[n];
    00407         unsigned int *ptr = swab;
    00408         memcpy( swab, var, sizeof(unsigned int) * n );
    00409         for ( unsigned int i = 0; i < n; ++i ) {
    00410             sgEndianSwap( (unsigned int*)ptr++);
    00411         }
    00412         var = swab;
    00413     }
    00414     if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n )
    00415          != (int)(sizeof(unsigned int) * n) )
    00416     {
    00417         write_error = true ;
    00418     }
    00419 }
    00420
    00421
    00422
    00423 void sgReadInt ( gzFile fd, const unsigned int n, int *var )
    00424 {
    00425     if ( gzread ( fd, var, sizeof(int) * n )
    00426          != (int)(sizeof(int) * n) )
    00427     {
    00428         read_error = true ;
    00429     }
    00430     if ( sgIsBigEndian() ) {
    00431         for ( unsigned int i = 0; i < n; ++i ) {
    00432             sgEndianSwap( (unsigned int*)var++);
    00433         }
    00434     }
    00435 }
    00436
    00437
    00438 void sgWriteInt ( gzFile fd, const unsigned int n, const int *var )
    00439 {
    00440     if ( sgIsBigEndian() ) {
    00441         int *swab = new int[n];
    00442         int *ptr = swab;
    00443         memcpy( swab, var, sizeof(int) * n );
    00444         for ( unsigned int i = 0; i < n; ++i ) {
    00445             sgEndianSwap( (unsigned int*)ptr++);
    00446         }
    00447         var = swab;
    00448     }
    00449     if ( gzwrite ( fd, (void *)var, sizeof(int) * n )
    00450          != (int)(sizeof(int) * n) )
    00451     {
    00452         write_error = true ;
    00453     }
    00454 }
    00455
    00456
    00457
    00458 #define MAX_ENTITY_NAME_LENGTH 1024
    00459
    00460 void sgReadString ( gzFile fd, char **var )
    00461 {
    00462     int i ;
    00463     char s [ MAX_ENTITY_NAME_LENGTH ] ;
    00464
    00465     for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ ) {
    00466         int c = gzgetc ( fd ) ;
    00467         s [ i ] = c ;
    00468
    00469         if ( c == '\0' )
    00470             break ;
    00471     }
    00472
    00473     if ( i >= MAX_ENTITY_NAME_LENGTH-1 )
    00474         s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ;
    00475
    00476
    00477     if ( s[0] == '\0' )
    00478         *var = NULL ;
    00479     else {
    00480         *var = new char [ strlen(s)+1 ] ;
    00481         strcpy ( *var, s ) ;
    00482     }
    00483 }
    00484
    00485
    00486 void sgWriteString ( gzFile fd, const char *var )
    00487 {
    00488     if ( var != NULL ) {
    00489         if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) ==
    00490              (int)(strlen(var) + 1) )
    00491             return ;
    00492     } else {
    00493         gzputc( fd, 0 );
    00494     }
    00495 }
    00496
    00497

@ 1.3 log @Oops, try again. @ text @d506 1 a506 1