Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1006 - trunk/Model/Readers


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1006 - trunk/Model/Readers
  • Date: Mon, 10 Apr 2006 16:10:41 -0600 (MDT)

Author: bigler
Date: Mon Apr 10 16:10:39 2006
New Revision: 1006

Modified:
   trunk/Model/Readers/IW.cc
Log:

Use c++ version of getline that is more portable.

For what it's worth.  In my experiments in using stl stream stuff to
parse a file, it is considerably faster to use getline then sscanf to
parse rather than using the >> operators.


Modified: trunk/Model/Readers/IW.cc
==============================================================================
--- trunk/Model/Readers/IW.cc   (original)
+++ trunk/Model/Readers/IW.cc   Mon Apr 10 16:10:39 2006
@@ -30,36 +30,40 @@
 #include <MantaTypes.h>
 #include <Core/Color/ColorSpace.h>
 
+#include <string>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+
 using namespace Manta;
+using namespace std;
 
 // Return null if there was a problem
 IWObject* Manta::IWObjectRead(const char* filename) {
 
   // Attempt to open the file
-  FILE* geomf = fopen(filename, "r");
+  ifstream geomf(filename);
   if (!geomf) {
                fprintf(stderr, "Error: cannot open %s for loading\n", 
filename);
     return 0;
   }
 
-  // Start loading in the geometry
-  // Buffer for line.  This will be allocated by getline and
-  // reallocated as needed.  We will need to free it later, though.
-  char* line = NULL;
-  size_t line_len = 0;
   IWObject* iw = new IWObject();
-  
-  while((getline(&line, &line_len, geomf)) != -1) {
-    char token[100];
-    sscanf(line, "%100s %%*\n", token);
-    // Check to see if the token matches newobject
-    if (strcmp(token, "newobject") == 0) {
-      // Don't do anything yet.  We don't know how to handle multiple objects
-    } else if (strcmp(token, "vertices:") == 0) {
+
+  string line;
+  getline(geomf, line);
+  while(geomf.good()) {
+    istringstream line_buf(line);
+    string token;
+    line_buf >> token;
+    if (token == "newobject") {
+      // Don't do anything yet.  We don't know how to handle multiple
+      // objects.
+    } else if (token == "vertices:") {
       /////////////////////////////////////////////////////////////////
       // Load the vertices
       unsigned int num_vertices;
-      sscanf(line, "%*s %u", &num_vertices);
+      line_buf >> num_vertices;
 
       // Allocate space
       iw->vertices.reserve(num_vertices);
@@ -67,23 +71,23 @@
       // Read them in
       for(unsigned int vi = 0; vi < num_vertices; ++vi) {
         float x,y,z;
-        fscanf(geomf, "\t%f %f %f\n", &x, &y, &z);
+        getline(geomf, line);
+        sscanf(line.c_str(), "\t%f %f %f\n", &x, &y, &z);
         iw->vertices.push_back(Vector(x,y,z));
       }
-
+      
       // Check to make sure we read the right number
       if (iw->vertices.size() != num_vertices) {
-        fprintf(stderr, "Read %llu vertices and expected %u vertices\n",
-                (unsigned long long)iw->vertices.size(), num_vertices);
-        fclose(geomf);
+        cerr << "Read "<<iw->vertices.size()<<" vertices and "
+             << "expected "<<num_vertices<<" vertices\n";
         delete iw;
         return 0;
       }
-    } else if (strcmp(token, "vtxnormals:") == 0) {
+    } else if (token == "vtxnormals:") {
       /////////////////////////////////////////////////////////////////
       // Load the vertex normals
       unsigned int num_vtxnormals;
-      sscanf(line, "%*s %u", &num_vtxnormals);
+      line_buf >> num_vtxnormals;
 
       // Allocate space
       iw->vtxnormals.reserve(num_vtxnormals);
@@ -91,23 +95,23 @@
       // Read them in
       for(unsigned int vi = 0; vi < num_vtxnormals; ++vi) {
         float x,y,z;
-        fscanf(geomf, "\t%f %f %f\n", &x, &y, &z);
+        getline(geomf, line);
+        sscanf(line.c_str(), "\t%f %f %f\n", &x, &y, &z);
         iw->vtxnormals.push_back(Vector(x,y,z));
       }
 
       // Check to make sure we read the right number
       if (iw->vtxnormals.size() != num_vtxnormals) {
-        fprintf(stderr, "Read %llu normals and expected %u normals\n",
-                (unsigned long long)iw->vtxnormals.size(), num_vtxnormals);
-        fclose(geomf);
+        cerr << "Read "<<iw->vtxnormals.size()<<" normals and "
+             << "expected "<<num_vtxnormals<<" normals\n";
         delete iw;
         return 0;
       }
-    } else if (strcmp(token, "triangles:") == 0) {
+    } else if (token == "triangles:") {
       /////////////////////////////////////////////////////////////////
       // Load the triangles
       unsigned int num_tris;
-      sscanf(line, "%*s %u", &num_tris);
+      line_buf >> num_tris;
 
       // Allocate space
       iw->triangles.reserve(num_tris);
@@ -115,27 +119,27 @@
       // Read them in
       for(unsigned int ti = 0; ti < num_tris; ++ti) {
         unsigned int data[4];
-        fscanf(geomf, "\t%u %u %u %u\n", data, data+1, data+2, data+3);
+        getline(geomf, line);
+        sscanf(line.c_str(), "\t%u %u %u %u\n", data, data+1, data+2, 
data+3);
         iw->triangles.push_back(VectorT<unsigned int, 4>(data));
       }
 
       // Check to make sure we read the right number
       if (iw->triangles.size() != num_tris) {
-        fprintf(stderr, "Read %llu triangles and expected %u triangles\n",
-                (unsigned long long)iw->triangles.size(), num_tris);
-        fclose(geomf);
+        cerr << "Read "<<iw->triangles.size()<<" triangles and "
+             << "expected "<<num_tris<<" triangles\n";
         delete iw;
         return 0;
       }
-    } else if (strcmp(token, "endobject") == 0) {
+    } else if (token == "endobject") {
       // Do nothing right now
-    } else if (strcmp(token, "camera") == 0) {
+    } else if (token == "camera") {
       // Do nothing right now
-    } else if (strcmp(token, "shaders") == 0) {
+    } else if (token == "shaders") {
       /////////////////////////////////////////////////////////////////
       // Load the triangles
       unsigned int num_shaders;
-      sscanf(line, "%*s %u", &num_shaders);
+      line_buf >> num_shaders;
 
       // Allocate space, need to resize, becase we can't simply just
       // push_back as the shader has the index as part of the file spec.
@@ -145,16 +149,19 @@
       for(unsigned int i = 0; i < num_shaders; ++i) {
         float r, g, b;
         unsigned int index;
-        fscanf(geomf, "shader %u diffuse (%f,%f,%f)", &index, &r, &g, &b);
+        getline(geomf, line);
+        sscanf(line.c_str(), "shader %u diffuse (%f,%f,%f)",
+               &index, &r, &g, &b);
         iw->shaders[index] = Color(RGBColor(r,g,b));
       }
     } else {
-      if (strlen(token))
-        fprintf(stderr, "Unknown token: (%s)\n", token);
+      if (token.length())
+        cerr << "Unknown token: ("<<token<<")\n";
     }
-  } // end while loop
+    getline(geomf, line);
+  }
 
-  fclose(geomf);
+  geomf.close();
 
   // Print some stats
   fprintf(stderr, "vertices       :%10llu\n",
@@ -226,26 +233,23 @@
   unsigned int num_tri_indices = 0;
   unsigned int num_nodes = 0;
   unsigned int num_empty_nodes = 0;
-  
-  FILE* bspf = fopen(filename, "r");
+
+  ifstream bspf(filename);
   if (!bspf) {
                fprintf(stderr, "Error: cannot open %s for loading\n", 
filename);
     return 0;
   }
 
-  // Buffer for line.  This will be allocated by getline and
-  // reallocated as needed.  We will need to free it later, though.
-  char* bline = NULL;
-  size_t bline_len = 0;
-
   IWNode* head = 0;
   IWNode* top = head;
 
-  while((getline(&bline, &bline_len, bspf)) != -1) {
+  string line;
+  getline(bspf, line);
+  while(bspf.good()) {
 //     fprintf (stderr, "line(%u): %s\n", bline_len, bline);
     // Check the first token
     char token[2];
-    int num = sscanf(bline, "%1s", token);
+    int num = sscanf(line.c_str(), "%1s", token);
     //    fprintf(stderr, "num = %d, token = %s\n", num, token);
     if (strcmp(token, "N") == 0) {
       num_nodes ++;
@@ -267,7 +271,7 @@
       top = node;
 
       char axis_c;
-      sscanf(bline, "%*s %c=%f,d=%*d", &axis_c, &(node->split));
+      sscanf(line.c_str(), "%*s %c=%f,d=%*d", &axis_c, &(node->split));
 //       fprintf(stderr, "axis = %c, split = %g\n", axis_c, node->split);
       switch (axis_c) {
       case 'x': node->flags |= 0; break;
@@ -289,7 +293,7 @@
       }
 
       // Grab how many objects we have
-      sscanf(bline, "%*s %u", &(node->num_tris));
+      sscanf(line.c_str(), "%*s %u", &(node->num_tris));
 //       fprintf(stderr, "L %u\n", node->num_tris);
       num_tri_indices += node->num_tris;
       if (node->num_tris) {
@@ -301,9 +305,9 @@
         int pos = 0; // Where to pick up parsing next.  This will give
                      // us an offset to our token string, so that we
                      // can grab the next integer one at a time.
-        sscanf(bline, "%*s %*u %n", &pos);
+        sscanf(line.c_str(), "%*s %*u %n", &pos);
         //        fprintf(stderr, "pos = %d\n", pos);
-        char* bline_p = bline+pos;
+        char const* bline_p = line.c_str()+pos;
         for(unsigned int i = 0; i < node->num_tris; ++i) {
           sscanf(bline_p, "%d %n", tri_ids_p, &pos);
           //          fprintf(stderr, "pos = %d, %d\n", pos, *tri_ids_p);
@@ -318,12 +322,11 @@
     } else {
       fprintf(stderr, "Unrecongnized token (%c)\n", token);
     }
-    
+    getline(bspf, line);
   }
 
-  if (bline) free(bline);
-  fclose(bspf);
-
+  bspf.close();
+  
   IWTree* tree = new IWTree();
   tree->head = head;
   tree->num_nodes = num_nodes;




  • [MANTA] r1006 - trunk/Model/Readers, bigler, 04/10/2006

Archive powered by MHonArc 2.6.16.

Top of page