contrib/oxl/osl/osl_load_topology.cxx
Go to the documentation of this file.
00001 // This is oxl/osl/osl_load_topology.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "osl_load_topology.h"
00010 #include <vcl_cassert.h>
00011 #include <vcl_cstring.h>
00012 #include <vcl_cstdio.h> // sscanf()
00013 #include <vcl_fstream.h>
00014 #include <vcl_vector.h>
00015 
00016 void osl_load_topology(char const *f, vcl_list<osl_edge*> &e, vcl_list<osl_vertex*> &v)
00017 {
00018   vcl_ifstream file(f);
00019   osl_load_topology(file, e, v);
00020 }
00021 
00022 #define streamok \
00023 { if (f.bad()) { vcl_cerr << __FILE__ ":" << __LINE__ << " stream bad at this point\n"; return; } }
00024 
00025 void osl_load_topology(vcl_istream &f, vcl_list<osl_edge*> &es, vcl_list<osl_vertex*> &vs)
00026 {
00027   es.clear();
00028   vs.clear();
00029 
00030   char buf[1024]; // line buffer
00031   char tmp[1024];
00032 
00033   // check version string
00034   f >> vcl_ws;
00035   f.getline(buf, sizeof(buf));
00036   if (vcl_strcmp("osl_save_topology 1.0", buf) != 0) {
00037     vcl_cerr << __FILE__ ": version string mismatch\n";
00038     return;
00039   }
00040   streamok;
00041 
00042   // read number of vertices :
00043   f >> vcl_ws;
00044   f.getline(buf, sizeof(buf));
00045   int numverts = -1;
00046   if (vcl_sscanf(buf, "%d%[ ]vertices", &numverts, tmp) != 2) {
00047     vcl_cerr << __FILE__ ": error reading number of vertices\n";
00048     return;
00049   }
00050   assert(numverts >= 0);
00051   streamok;
00052   // read vertices :
00053   vcl_cerr << "reading " << numverts << " vertices...\n";
00054   vcl_vector<osl_vertex*> vert(numverts+1, (osl_vertex*)0);
00055   for (int i=0; i<numverts; ++i) {
00056     unsigned int stashid;
00057     int id;
00058     float x, y;
00059     f >> vcl_ws >> vcl_hex >> stashid >> vcl_dec >> id >> x >> y;
00060     assert(stashid<vert.size() && !vert[stashid]);
00061     vert[stashid] = new osl_vertex(x, y, id);
00062 
00063     vs.push_front(vert[stashid]);
00064   }
00065   streamok;
00066 
00067   // read number of edges :
00068   f >> vcl_ws;
00069   f.getline(buf, sizeof(buf));
00070   int numedges = -1;
00071   if (vcl_sscanf(buf, "%d%[ ]edges", &numedges, tmp) != 2) {
00072     vcl_cerr << __FILE__ ": error reading number of edges\n";
00073     return;
00074   }
00075   assert(numedges >= 0);
00076   streamok;
00077   // read edges :
00078   vcl_cerr << "reading " << numedges << " edges...\n";
00079   for (int i=0; i<numedges; ++i) {
00080     unsigned int stashid1 = vert.size(), stashid2 = vert.size();
00081     f >> vcl_ws >> vcl_hex >> stashid1 >> stashid2 >> vcl_dec;
00082     assert(stashid1<vert.size() && vert[stashid1]);
00083     assert(stashid2<vert.size() && vert[stashid2]);
00084 
00085     int id; // edge id
00086     f >> vcl_ws >> id;
00087 
00088     osl_edge *e = new osl_edge(2/*dummy*/, vert[stashid1], vert[stashid2]);
00089     e->SetId(id);
00090 
00091     e->read_ascii(f);
00092 
00093     es.push_front(e);
00094   }
00095   streamok;
00096 
00097   // done
00098 }