From: curt Date: Fri, 5 Jan 2001 00:04:20 +0000 (+0000) Subject: Additional bug fixes after testing. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=260dbeb3d2a8e711ae7b9d386f09e33ccaa992db;p=simgear.git Additional bug fixes after testing. --- diff --git a/simgear/io/lowlevel.cxx b/simgear/io/lowlevel.cxx index 1439c070..dc1c389f 100644 --- a/simgear/io/lowlevel.cxx +++ b/simgear/io/lowlevel.cxx @@ -28,222 +28,224 @@ static int read_error = false ; static int write_error = false ; -int sgReadError (void) { return read_error ; } -int sgWriteError (void) { return write_error ; } +void sgClearReadError() { read_error = false; } +void sgClearWriteError() { write_error = false; } +int sgReadError() { return read_error ; } +int sgWriteError() { return write_error ; } void sgReadChar ( gzFile fd, char *var ) { - if ( gzread ( fd, var, sizeof(char) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(char) ) > 0 ) return ; + read_error = true ; } void sgWriteChar ( gzFile fd, const char var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(char) ) > 0 ) return ; + write_error = true ; } void sgReadFloat ( gzFile fd, float *var ) { - if ( gzread ( fd, var, sizeof(float) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(float) ) > 0 ) return ; + read_error = true ; } void sgWriteFloat ( gzFile fd, const float var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) > 0 ) return ; + write_error = true ; } void sgReadDouble ( gzFile fd, double *var ) { - if ( gzread ( fd, var, sizeof(double) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(double) ) > 0 ) return ; + read_error = true ; } void sgWriteDouble ( gzFile fd, const double var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) > 0 ) return ; + write_error = true ; } void sgReadUInt ( gzFile fd, unsigned int *var ) { - if ( gzread ( fd, var, sizeof(unsigned int) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(unsigned int) ) > 0 ) return ; + read_error = true ; } void sgWriteUInt ( gzFile fd, const unsigned int var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) ) > 0 ) return ; + write_error = true ; } void sgReadInt ( gzFile fd, int *var ) { - if ( gzread ( fd, var, sizeof(int) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(int) ) > 0 ) return ; + read_error = true ; } void sgWriteInt ( gzFile fd, const int var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) > 0 ) return ; + write_error = true ; } void sgReadLong ( gzFile fd, long int *var ) { - if ( gzread ( fd, var, sizeof(long int) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(long int) ) > 0 ) return ; + read_error = true ; } void sgWriteLong ( gzFile fd, const long int var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(long int) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(long int) ) > 0 ) return ; + write_error = true ; } void sgReadUShort ( gzFile fd, unsigned short *var ) { - if ( gzread ( fd, var, sizeof(unsigned short) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(unsigned short) ) > 0 ) return ; + read_error = true ; } void sgWriteUShort ( gzFile fd, const unsigned short var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) ) > 0 ) return ; + write_error = true ; } void sgReadShort ( gzFile fd, short *var ) { - if ( gzread ( fd, var, sizeof(short) ) == 1 ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(short) ) > 0 ) return ; + read_error = true ; } void sgWriteShort ( gzFile fd, const short var ) { - if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) == 1 ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) > 0 ) return ; + write_error = true ; } void sgReadFloat ( gzFile fd, const unsigned int n, float *var ) { - if ( gzread ( fd, var, sizeof(float) * n ) == (int)n ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(float) * n ) == (int)n ) return ; + read_error = true ; } void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var ) { - if ( gzwrite ( fd, (void *)var, sizeof(float) * n ) == (int)n ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)var, sizeof(float) * n ) == (int)n ) return ; + write_error = true ; } void sgReadDouble ( gzFile fd, const unsigned int n, double *var ) { - if ( gzread ( fd, var, sizeof(double) * n ) == (int)n ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(double) * n ) == (int)n ) return ; + read_error = true ; } void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var ) { - if ( gzwrite ( fd, (void *)var, sizeof(double) * n ) == (int)n ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)var, sizeof(double) * n ) == (int)n ) return ; + write_error = true ; } void sgReadBytes ( gzFile fd, const unsigned int n, void *var ) { - if ( n == 0) - return; - if ( gzread ( fd, var, n ) == 1 ) - return ; - read_error = true ; + if ( n == 0) + return; + if ( gzread ( fd, var, n ) > 0 ) + return ; + read_error = true ; } void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var ) { - if ( n == 0) - return; - if ( gzwrite ( fd, (void *)var, n ) == 1 ) - return ; - write_error = true ; + if ( n == 0) + return; + if ( gzwrite ( fd, (void *)var, n ) > 0 ) + return ; + write_error = true ; } void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var ) { - if ( gzread ( fd, var, sizeof(unsigned short) * n ) == (int)n ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(unsigned short) * n ) == (int)n ) return ; + read_error = true ; } void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var ) { - if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n ) == (int)n ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)var, sizeof(unsigned short) * n ) == (int)n ) return ; + write_error = true ; } void sgReadShort ( gzFile fd, const unsigned int n, short *var ) { - if ( gzread ( fd, var, sizeof(short) * n ) == (int)n ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(short) * n ) == (int)n ) return ; + read_error = true ; } void sgWriteShort ( gzFile fd, const unsigned int n, const short *var ) { - if ( gzwrite ( fd, (void *)var, sizeof(short) * n ) == (int)n ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)var, sizeof(short) * n ) == (int)n ) return ; + write_error = true ; } void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var ) { - if ( gzread ( fd, var, sizeof(unsigned int)* n ) == (int)n ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(unsigned int)* n ) == (int)n ) return ; + read_error = true ; } void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var ) { - if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n ) == (int)n ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)var, sizeof(unsigned int) * n ) == (int)n ) return ; + write_error = true ; } void sgReadInt ( gzFile fd, const unsigned int n, int *var ) { - if ( gzread ( fd, var, sizeof(int) * n ) == (int)n ) return ; - read_error = true ; + if ( gzread ( fd, var, sizeof(int) * n ) == (int)n ) return ; + read_error = true ; } void sgWriteInt ( gzFile fd, const unsigned int n, const int *var ) { - if ( gzwrite ( fd, (void *)var, sizeof(int) * n ) == (int)n ) return ; - write_error = true ; + if ( gzwrite ( fd, (void *)var, sizeof(int) * n ) == (int)n ) return ; + write_error = true ; } @@ -252,41 +254,41 @@ void sgWriteInt ( gzFile fd, const unsigned int n, const int *var ) void sgReadString ( gzFile fd, char **var ) { - int i ; - char s [ MAX_ENTITY_NAME_LENGTH ] ; + int i ; + char s [ MAX_ENTITY_NAME_LENGTH ] ; - for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ ) - { - int c = gzgetc ( fd ) ; - s [ i ] = c ; + for ( i = 0 ; i < MAX_ENTITY_NAME_LENGTH ; i++ ) + { + int c = gzgetc ( fd ) ; + s [ i ] = c ; - if ( c == '\0' ) - break ; - } + if ( c == '\0' ) + break ; + } - if ( i >= MAX_ENTITY_NAME_LENGTH-1 ) - s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ; + if ( i >= MAX_ENTITY_NAME_LENGTH-1 ) + s [ MAX_ENTITY_NAME_LENGTH-1 ] = '\0' ; - if ( s[0] == '\0' ) - *var = NULL ; - else - { - *var = new char [ strlen(s)+1 ] ; - strcpy ( *var, s ) ; - } + if ( s[0] == '\0' ) + *var = NULL ; + else + { + *var = new char [ strlen(s)+1 ] ; + strcpy ( *var, s ) ; + } } void sgWriteString ( gzFile fd, const char *var ) { - if ( var != NULL ) { - if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) == - (int)(strlen(var) + 1) ) - return ; - } else { - gzputc( fd, 0 ); - } + if ( var != NULL ) { + if ( gzwrite ( fd, (void *)var, strlen(var) + 1 ) == + (int)(strlen(var) + 1) ) + return ; + } else { + gzputc( fd, 0 ); + } } diff --git a/simgear/io/lowlevel.hxx b/simgear/io/lowlevel.hxx index 460d6e26..b01b9f1a 100644 --- a/simgear/io/lowlevel.hxx +++ b/simgear/io/lowlevel.hxx @@ -81,8 +81,10 @@ void sgWriteVec4 ( gzFile fd, const sgVec4 var ) ; void sgReadMat4 ( gzFile fd, sgMat4 var ) ; void sgWriteMat4 ( gzFile fd, const sgMat4 var ) ; -int sgReadError ( void ) ; -int sgWriteError ( void ) ; +void sgClearReadError(); +void sgClearWriteError(); +int sgReadError(); +int sgWriteError(); #endif // _SG_LOWLEVEL_HXX diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index 0a779970..41c47072 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -110,7 +110,7 @@ double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) { // write out the structures to an ASCII file. We assume that the // groups come to us sorted by material property. If not, things // don't break, but the result won't be as optimal. -void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, +bool sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, Point3D gbs_center, float gbs_radius, const point_list& wgs84_nodes, const point_list& normals, const point_list& texcoords, @@ -139,7 +139,7 @@ void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, FILE *fp; if ( (fp = fopen( file.c_str(), "w" )) == NULL ) { cout << "ERROR: opening " << file << " for writing!" << endl; - exit(-1); + return false; } cout << "triangles size = " << tris_v.size() << " tri_materials = " @@ -299,11 +299,13 @@ void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, command = "gzip --force --best " + file; system(command.c_str()); + + return true; } // read a binary file and populate the provided structures. -void sgReadBinObj( const string& file, +bool sgReadBinObj( const string& file, Point3D &gbs_center, float *gbs_radius, point_list& wgs84_nodes, point_list& normals, point_list& texcoords, @@ -315,7 +317,8 @@ void sgReadBinObj( const string& file, string_list& fan_materials ) { Point3D p; - int i, j, k, m; + int i, j, k; + char material[256]; // zero out structures wgs84_nodes.clear(); @@ -342,19 +345,23 @@ void sgReadBinObj( const string& file, if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) { cout << "ERROR: opening " << file << " or " << filegz << "for reading!" << endl; - exit(-1); + return false; } } + sgClearReadError(); + // read headers int header, version; sgReadInt( fp, &header ); - if ( ((header & 0xFF000000) >> 24) == 'T' && + if ( ((header & 0xFF000000) >> 24) == 'S' && ((header & 0x00FF0000) >> 16) == 'G' ) { cout << "Good header" << endl; // read file version version = (header & 0x0000FFFF); cout << "File version = " << version << endl; + } else { + return false; } // read creation time @@ -470,8 +477,9 @@ void sgReadBinObj( const string& file, sgReadBytes( fp, nbytes, ptr ); int count = nbytes / 3; for ( k = 0; k < count; ++k ) { - p = Point3D( ptr[0] / 256.0, ptr[1] / 256.0, - ptr[2] / 256.0 ); + p = Point3D( ptr[0] / 128.0 - 1.0, + ptr[1] / 128.0 - 1.0, + ptr[2] / 128.0 - 1.0 ); cout << "normal = " << p << endl; normals.push_back( p ); ptr += 3; @@ -521,11 +529,9 @@ void sgReadBinObj( const string& file, char *ptr = buf; sgReadBytes( fp, nbytes, ptr ); if ( prop_type == SG_MATERIAL ) { - char material[256]; strncpy( material, ptr, nbytes ); material[nbytes] = '\0'; cout << "material type = " << material << endl; - tri_materials.push_back( material ); } } @@ -537,21 +543,20 @@ void sgReadBinObj( const string& file, char buf[nbytes]; char *ptr = buf; sgReadBytes( fp, nbytes, ptr ); - int count = nbytes / (sizeof(short) * 2 * 3); + int count = nbytes / (sizeof(short) * 2); short *sptr = (short *)ptr; int_list vs, tcs; + vs.clear(); tcs.clear(); for ( k = 0; k < count; ++k ) { - vs.clear(); tcs.clear(); - for ( m = 0; m < 3; ++m ) { - vs.push_back( sptr[0] ); - tcs.push_back( sptr[1] ); - cout << sptr[0] << "/" << sptr[1] << " "; - sptr += 2; - } - cout << endl; - tris_v.push_back( vs ); - tris_tc.push_back( tcs ); + vs.push_back( sptr[0] ); + tcs.push_back( sptr[1] ); + cout << sptr[0] << "/" << sptr[1] << " "; + sptr += 2; } + cout << endl; + tris_v.push_back( vs ); + tris_tc.push_back( tcs ); + tri_materials.push_back( material ); } } else if ( obj_type == SG_TRIANGLE_STRIPS ) { // read triangle strip properties @@ -566,11 +571,9 @@ void sgReadBinObj( const string& file, char *ptr = buf; sgReadBytes( fp, nbytes, ptr ); if ( prop_type == SG_MATERIAL ) { - char material[256]; strncpy( material, ptr, nbytes ); material[nbytes] = '\0'; cout << "material type = " << material << endl; - strip_materials.push_back( material ); } } @@ -582,7 +585,7 @@ void sgReadBinObj( const string& file, char buf[nbytes]; char *ptr = buf; sgReadBytes( fp, nbytes, ptr ); - int count = nbytes / (sizeof(short) * 2 * 3); + int count = nbytes / (sizeof(short) * 2); short *sptr = (short *)ptr; int_list vs, tcs; vs.clear(); tcs.clear(); @@ -595,6 +598,7 @@ void sgReadBinObj( const string& file, cout << endl; strips_v.push_back( vs ); strips_tc.push_back( tcs ); + strip_materials.push_back( material ); } } else if ( obj_type == SG_TRIANGLE_FANS ) { // read triangle fan properties @@ -609,11 +613,9 @@ void sgReadBinObj( const string& file, char *ptr = buf; sgReadBytes( fp, nbytes, ptr ); if ( prop_type == SG_MATERIAL ) { - char material[256]; strncpy( material, ptr, nbytes ); material[nbytes] = '\0'; cout << "material type = " << material << endl; - fan_materials.push_back( material ); } } @@ -625,7 +627,7 @@ void sgReadBinObj( const string& file, char buf[nbytes]; char *ptr = buf; sgReadBytes( fp, nbytes, ptr ); - int count = nbytes / (sizeof(short) * 2 * 3); + int count = nbytes / (sizeof(short) * 2); short *sptr = (short *)ptr; int_list vs, tcs; vs.clear(); tcs.clear(); @@ -638,6 +640,7 @@ void sgReadBinObj( const string& file, cout << endl; fans_v.push_back( vs ); fans_tc.push_back( tcs ); + fan_materials.push_back( material ); } } else { // unknown object type, just skip @@ -669,13 +672,20 @@ void sgReadBinObj( const string& file, // close the file gzclose(fp); + + if ( sgReadError() ) { + cout << "We detected an error while reading the file." << endl; + return false; + } + + return true; } // write out the structures to a binary file. We assume that the // groups come to us sorted by material property. If not, things // don't break, but the result won't be as optimal. -void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, +bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b, Point3D gbs_center, float gbs_radius, const point_list& wgs84_nodes, const point_list& normals, const point_list& texcoords, @@ -705,9 +715,11 @@ void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, gzFile fp; if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) { cout << "ERROR: opening " << file << " for writing!" << endl; - exit(-1); + return false; } + sgClearWriteError(); + cout << "triangles size = " << tris_v.size() << " tri_materials = " << tri_materials.size() << endl; cout << "strips size = " << strips_v.size() << " strip_materials = " @@ -808,9 +820,9 @@ void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, char normal[3]; for ( i = 0; i < (int)normals.size(); ++i ) { p = normals[i]; - normal[0] = (char)(p.x() * 256); - normal[1] = (char)(p.y() * 256); - normal[2] = (char)(p.z() * 256); + normal[0] = (char)((p.x() + 1.0) * 128); + normal[1] = (char)((p.y() + 1.0) * 128); + normal[2] = (char)((p.z() + 1.0) * 128); sgWriteBytes( fp, 3, normal ); } @@ -947,4 +959,11 @@ void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, // close the file gzclose(fp); + + if ( sgWriteError() ) { + cout << "We detected an error while writing the file." << endl; + return false; + } + + return true; } diff --git a/simgear/io/sg_binobj.hxx b/simgear/io/sg_binobj.hxx index fc9a3de9..f50614c4 100644 --- a/simgear/io/sg_binobj.hxx +++ b/simgear/io/sg_binobj.hxx @@ -34,6 +34,8 @@ #include #include +#include +#include #include #include @@ -41,7 +43,6 @@ #include #include STL_STRING -#include typedef vector < int_list > group_list; @@ -79,7 +80,7 @@ double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ); // write out the structures to an ASCII file. We assume that the // groups come to us sorted by material property. If not, things // don't break, but the result won't be as optimal. -void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, +bool sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, Point3D gbs_center, float gbs_radius, const point_list& wgs84_nodes, const point_list& normals, const point_list& texcoords, @@ -92,7 +93,7 @@ void sgWriteAsciiObj( const string& base, const string& name, const SGBucket& b, // read a binary file object and populate the provided structures. -void sgReadBinObj( const string& file, +bool sgReadBinObj( const string& file, Point3D &gbs_center, float *gbs_radius, point_list& wgs84_nodes, point_list& normals, point_list& texcoords, @@ -106,7 +107,7 @@ void sgReadBinObj( const string& file, // write out the structures to a binary file. We assume that the // groups come to us sorted by material property. If not, things // don't break, but the result won't be as optimal. -void sgWriteBinObj( const string& base, const string& name, const SGBucket& b, +bool sgWriteBinObj( const string& base, const string& name, const SGBucket& b, Point3D gbs_center, float gbs_radius, const point_list& wgs84_nodes, const point_list& normals, const point_list& texcoords,