1 // This is oxl/osl/osl_load_topology.cxx
2 //:
3 // \file
4 // \author fsm
5
6 #include <cstring>
7 #include <iostream>
8 #include <cstdio>
9 #include <fstream>
10 #include <vector>
11 #include "osl_load_topology.h"
12 #include <cassert>
13 #ifdef _MSC_VER
14 # include "vcl_msvc_warnings.h"
15 #endif
16
osl_load_topology(char const * f,std::list<osl_edge * > & e,std::list<osl_vertex * > & v)17 void osl_load_topology(char const *f, std::list<osl_edge*> &e, std::list<osl_vertex*> &v)
18 {
19 std::ifstream file(f);
20 osl_load_topology(file, e, v);
21 }
22
23 #define streamok \
24 { if (f.bad()) { std::cerr << __FILE__ ":" << __LINE__ << " stream bad at this point\n"; return; } }
25
osl_load_topology(std::istream & f,std::list<osl_edge * > & es,std::list<osl_vertex * > & vs)26 void osl_load_topology(std::istream &f, std::list<osl_edge*> &es, std::list<osl_vertex*> &vs)
27 {
28 es.clear();
29 vs.clear();
30
31 char buf[1024]; // line buffer
32 char tmp[1024];
33
34 // check version string
35 f >> std::ws;
36 f.getline(buf, sizeof(buf));
37 if (std::strcmp("osl_save_topology 1.0", buf) != 0) {
38 std::cerr << __FILE__ ": version string mismatch\n";
39 return;
40 }
41 streamok;
42
43 // read number of vertices :
44 f >> std::ws;
45 f.getline(buf, sizeof(buf));
46 int numverts = -1;
47 if (std::sscanf(buf, "%d%[ ]vertices", &numverts, tmp) != 2) {
48 std::cerr << __FILE__ ": error reading number of vertices\n";
49 return;
50 }
51 assert(numverts >= 0);
52 streamok;
53 // read vertices :
54 std::cerr << "reading " << numverts << " vertices...\n";
55 std::vector<osl_vertex*> vert(numverts+1, (osl_vertex*)nullptr);
56 for (int i=0; i<numverts; ++i) {
57 unsigned int stashid;
58 int id;
59 float x, y;
60 f >> std::ws >> std::hex >> stashid >> std::dec >> id >> x >> y;
61 assert(stashid<vert.size() && !vert[stashid]);
62 vert[stashid] = new osl_vertex(x, y, id);
63
64 vs.push_front(vert[stashid]);
65 }
66 streamok;
67
68 // read number of edges :
69 f >> std::ws;
70 f.getline(buf, sizeof(buf));
71 int numedges = -1;
72 if (std::sscanf(buf, "%d%[ ]edges", &numedges, tmp) != 2) {
73 std::cerr << __FILE__ ": error reading number of edges\n";
74 return;
75 }
76 assert(numedges >= 0);
77 streamok;
78 // read edges :
79 std::cerr << "reading " << numedges << " edges...\n";
80 for (int i=0; i<numedges; ++i) {
81 unsigned int stashid1 = vert.size(), stashid2 = vert.size();
82 f >> std::ws >> std::hex >> stashid1 >> stashid2 >> std::dec;
83 assert(stashid1<vert.size() && vert[stashid1]);
84 assert(stashid2<vert.size() && vert[stashid2]);
85
86 int id; // edge id
87 f >> std::ws >> id;
88
89 auto *e = new osl_edge(2/*dummy*/, vert[stashid1], vert[stashid2]);
90 e->SetId(id);
91
92 e->read_ascii(f);
93
94 es.push_front(e);
95 }
96 streamok;
97
98 // done
99 }
100