]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_binobj.cxx
Use the new SGPath::create_dir function
[simgear.git] / simgear / io / sg_binobj.cxx
index d2da18ef92ae3a250e256d56842a47ee72472f8d..9a6c3268eb91754998e1b581a667ad1ef527361a 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started January 2000.
 //
-// Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
+// Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 #endif
 
 #include <simgear/compiler.h>
+#include <simgear/debug/logstream.hxx>
 
 #include <stdio.h>
 #include <time.h>
@@ -35,6 +36,7 @@
 #include STL_STRING
 
 #include <simgear/bucket/newbucket.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include "lowlevel.hxx"
 #include "sg_binobj.hxx"
 SG_USING_STD( string );
 SG_USING_STD( vector );
 
-#if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
-SG_USING_STD( cout );
-SG_USING_STD( endl );
-#endif
-
 
-enum {
+static enum {
     SG_BOUNDING_SPHERE = 0,
 
     SG_VERTEX_LIST = 1,
@@ -64,14 +61,14 @@ enum {
     SG_TRIANGLE_FANS = 12
 } sgObjectTypes;
 
-enum {
+static enum {
     SG_IDX_VERTICES =  0x01,
     SG_IDX_NORMALS =   0x02,
     SG_IDX_COLORS =    0x04,
     SG_IDX_TEXCOORDS = 0x08
 } sgIndexTypes;
 
-enum {
+static enum {
     SG_MATERIAL = 0,
     SG_INDEX_TYPES = 1
 } sgPropertyTypes;
@@ -93,7 +90,7 @@ public:
        while ( size < s ) {
            size *= 2;
        }
-       cout << "Creating a new buffer of size = " << size << endl;
+        SG_LOG(SG_EVENT, SG_DEBUG, "Creating a new buffer of size = " << size);
        ptr = new char[size];
     }
 
@@ -111,7 +108,7 @@ public:
            while ( size < s ) {
                size *= 2;
            }
-           cout << "resizing buffer to size = " << size << endl;
+            SG_LOG(SG_EVENT, SG_DEBUG, "resizing buffer to size = " << size);
            ptr = new char[size];
        }
     }
@@ -120,7 +117,7 @@ public:
 
 // calculate the center of a list of points, by taking the halfway
 // point between the min and max points.
-static Point3D calc_center( point_list& wgs84_nodes ) {
+Point3D sgCalcCenter( point_list& wgs84_nodes ) {
     Point3D p, min, max;
 
     if ( wgs84_nodes.size() ) {
@@ -244,8 +241,8 @@ static void read_object( gzFile fp,
        if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
        char *ptr = buf.get_ptr();
        sgReadBytes( fp, nbytes, ptr );
-       int count = nbytes / (idx_size * sizeof(short));
-       short *sptr = (short *)ptr;
+       int count = nbytes / (idx_size * sizeof(unsigned short));
+       unsigned short *sptr = (unsigned short *)ptr;
        int_list vs; vs.clear();
        int_list ns; ns.clear();
        int_list cs; cs.clear();
@@ -253,7 +250,7 @@ static void read_object( gzFile fp,
        for ( k = 0; k < count; ++k ) {
            if ( sgIsBigEndian() ) {
                for ( idx = 0; idx < idx_size; ++idx ) {
-                   sgEndianSwap( (unsigned short *)&(sptr[idx]) );
+                   sgEndianSwap( (uint16_t *)&(sptr[idx]) );
                }
            }
            idx = 0;
@@ -325,8 +322,8 @@ bool SGBinObject::read_bin( const string& file ) {
     if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
        string filegz = file + ".gz";
        if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
-           cout << "ERROR: opening " << file << " or " << filegz
-                << "for reading!" << endl;
+            SG_LOG( SG_EVENT, SG_ALERT,
+               "ERROR: opening " << file << " or " << filegz << "for reading!");
 
            return false;
        }
@@ -364,7 +361,7 @@ bool SGBinObject::read_bin( const string& file ) {
     local_tm = localtime( &calendar_time );
     char time_str[256];
     strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
-    cout << "File created on " << time_str << endl;
+    SG_LOG( SG_EVENT, SG_DEBUG, "File created on " << time_str);
 #endif
 
     // read number of top level objects
@@ -407,9 +404,9 @@ bool SGBinObject::read_bin( const string& file ) {
 
                double *dptr = (double *)ptr;
                if ( sgIsBigEndian() ) {
-                   sgEndianSwap( (uint64 *)&(dptr[0]) );
-                   sgEndianSwap( (uint64 *)&(dptr[1]) );
-                   sgEndianSwap( (uint64 *)&(dptr[2]) );
+                   sgEndianSwap( (uint64_t *)&(dptr[0]) );
+                   sgEndianSwap( (uint64_t *)&(dptr[1]) );
+                   sgEndianSwap( (uint64_t *)&(dptr[2]) );
                }
                gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
                // cout << "Center = " << gbs_center << endl;
@@ -417,7 +414,7 @@ bool SGBinObject::read_bin( const string& file ) {
                
                float *fptr = (float *)ptr;
                if ( sgIsBigEndian() ) {
-                   sgEndianSwap( (unsigned int *)fptr );
+                   sgEndianSwap( (uint32_t *)fptr );
                }
                gbs_radius = fptr[0];
                // cout << "Bounding radius = " << gbs_radius << endl;
@@ -447,9 +444,9 @@ bool SGBinObject::read_bin( const string& file ) {
                wgs84_nodes.reserve( count );
                for ( k = 0; k < count; ++k ) {
                    if ( sgIsBigEndian() ) {
-                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[0]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[1]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[2]) );
                    }
                    wgs84_nodes.push_back( Point3D(fptr[0], fptr[1], fptr[2]) );
                    fptr += 3;
@@ -480,10 +477,10 @@ bool SGBinObject::read_bin( const string& file ) {
                colors.reserve(count);
                for ( k = 0; k < count; ++k ) {
                    if ( sgIsBigEndian() ) {
-                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[3]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[0]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[1]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[2]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[3]) );
                    }
                    colors.push_back( Point3D( fptr[0], fptr[1], fptr[2] ) );
                    fptr += 4;
@@ -548,8 +545,8 @@ bool SGBinObject::read_bin( const string& file ) {
                texcoords.reserve(count);
                for ( k = 0; k < count; ++k ) {
                    if ( sgIsBigEndian() ) {
-                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[0]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[1]) );
                    }
                    texcoords.push_back( Point3D( fptr[0], fptr[1], 0 ) );
                    fptr += 2;
@@ -624,20 +621,13 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     unsigned char idx_mask;
     int idx_size;
 
-    string dir = base + "/" + b.gen_base_path();
-    string command = "mkdir -p " + dir;
-#if defined(_MSC_VER) || defined(__MINGW32__)
-    system( (string("mkdir ") + dir).c_str() );
-#else
-    system(command.c_str());
-#endif
-
-    string file = dir + "/" + name + ".gz";
-    cout << "Output file = " << file << endl;
+    SGPath file = base + "/" + b.gen_base_path() + "/" + name + ".gz";
+    file.create_dir( 0755 );
+    cout << "Output file = " << file.str() << endl;
 
     gzFile fp;
     if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
-       cout << "ERROR: opening " << file << " for writing!" << endl;
+       cout << "ERROR: opening " << file.str() << " for writing!" << endl;
        return false;
     }
 
@@ -660,7 +650,7 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     // write header magic
     sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
     time_t calendar_time = time(NULL);
-    sgWriteLong( fp, (long int)calendar_time );
+    sgWriteLong( fp, (int32_t)calendar_time );
 
     // calculate and write number of top level objects
     string material;
@@ -862,7 +852,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
            // find next group
            material = tri_materials[start];
            while ( (end < (int)tri_materials.size()) && 
-                   (material == tri_materials[end]) )
+                   (material == tri_materials[end]) &&
+                   3*(end-start) < 32760 )
            {
                // cout << "end = " << end << endl;
                end++;
@@ -1055,21 +1046,13 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
     Point3D p;
     int i, j;
 
-    string dir = base + "/" + b.gen_base_path();
-    string command = "mkdir -p " + dir;
-#if defined(_MSC_VER) || defined(__MINGW32__)
-    system( (string("mkdir ") + dir).c_str() );
-#else
-    system(command.c_str());
-#endif
-
-    // string file = dir + "/" + b.gen_index_str();
-    string file = dir + "/" + name;
-    cout << "Output file = " << file << endl;
+    SGPath file = base + "/" + b.gen_base_path() + "/" + name;
+    file.create_dir( 0755 );
+    cout << "Output file = " << file.str() << endl;
 
     FILE *fp;
     if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
-       cout << "ERROR: opening " << file << " for writing!" << endl;
+       cout << "ERROR: opening " << file.str() << " for writing!" << endl;
        return false;
     }
 
@@ -1149,7 +1132,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
            for ( i = start; i < end; ++i ) {
                for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
                    group_nodes.push_back( wgs84_nodes[ tris_v[i][j] ] );
-                   bs_center = calc_center( group_nodes );
+                   bs_center = sgCalcCenter( group_nodes );
                    bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
                }
            }
@@ -1200,7 +1183,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
            for ( i = start; i < end; ++i ) {
                for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
                    group_nodes.push_back( wgs84_nodes[ strips_v[i][j] ] );
-                   bs_center = calc_center( group_nodes );
+                   bs_center = sgCalcCenter( group_nodes );
                    bs_radius = sgCalcBoundingRadius( bs_center, group_nodes );
                }
            }
@@ -1228,7 +1211,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
     // close the file
     fclose(fp);
 
-    command = "gzip --force --best " + file;
+    string command = "gzip --force --best " + file.str();
     system(command.c_str());
 
     return true;