1 /* dxfReader for OpenSceneGraph  Copyright (C) 2005 by GraphArchitecture ( grapharchitecture.com )
2  * Programmed by Paul de Repentigny <pdr@grapharchitecture.com>
3  *
4  * OpenSceneGraph is (C) 2004 Robert Osfield
5  *
6  * This library is provided as-is, without support of any kind.
7  *
8  * Read DXF docs or OSG docs for any related questions.
9  *
10  * You may contact the author if you have suggestions/corrections/enhancements.
11  */
12 
13 /*	File handling.
14 	dxfFile creates a dxfReader, which in turn create a readerText or a readerBinary
15 	depending on file type.
16 */
17 #ifndef DXF_READER
18 #define DXF_READER 1
19 
20 #include <string>
21 #include <sstream>
22 
23 #include <osg/Referenced>
24 #include <osg/ref_ptr>
25 
26 #include <osgDB/fstream>
27 
28 class codeValue;
29 
30 /// readerBase. abstract base class for reading a dxf file
31 /// and interpreting data types
32 class readerBase :public osg::Referenced
33 {
34 public:
readerBase()35     readerBase() {}
~readerBase()36     virtual ~readerBase() {}
37     bool readGroup(std::ifstream& f, codeValue& cv);
38 
39 protected:
40     virtual bool readGroupCode(std::ifstream& f, int &groupcode)  = 0;
41     virtual bool readValue(std::ifstream& f, std::string &s)  = 0;
42     virtual bool readValue(std::ifstream& f, bool &b)  = 0;
43     virtual bool readValue(std::ifstream& f, short &s)  = 0;
44     virtual bool readValue(std::ifstream& f, int &i)  = 0;
45     virtual bool readValue(std::ifstream& f, long &l)  = 0;
46     virtual bool readValue(std::ifstream& f, double &d)  = 0;
47 };
48 
49 /// readerText. convert data using stringstream.
50 class readerText : public readerBase
51 {
52 public:
readerBase()53     readerText(char delim = '\n') : readerBase(), _lineCount(0), _delim(delim) {}
~readerText()54     virtual ~readerText() {}
55 
56 protected:
57     bool success(bool inSuccess, std::string type);
58     bool getTrimmedLine(std::ifstream& f);
59 
60     virtual bool readGroupCode(std::ifstream& f, int &groupcode);
61     virtual bool readValue(std::ifstream& f, std::string &s);
62     virtual bool readValue(std::ifstream& f, bool &b);
63     virtual bool readValue(std::ifstream& f, short &s);
64     virtual bool readValue(std::ifstream& f, int &i);
65     virtual bool readValue(std::ifstream& f, long &l);
66     virtual bool readValue(std::ifstream& f, double &d);
67     std::stringstream _str;
68     unsigned long _lineCount;
69     char _delim;
70 };
71 
72 
73 /// readerBinary. to be implemented
74 class readerBinary : public readerBase
75 {
76 public:
readerBinary()77     readerBinary() : readerBase() {}
~readerBinary()78     virtual ~readerBinary() {}
79 protected:
readGroupCode(std::ifstream &,int &)80     virtual bool readGroupCode(std::ifstream& /*f*/, int& /*groupcode*/) { return false; }
readValue(std::ifstream &,std::string &)81     virtual bool readValue(std::ifstream& /*f*/, std::string& /*s*/) { return false; }
readValue(std::ifstream &,bool &)82     virtual bool readValue(std::ifstream& /*f*/, bool& /*b*/) { return false; }
readValue(std::ifstream &,short &)83     virtual bool readValue(std::ifstream& /*f*/, short& /*s*/) { return false; }
readValue(std::ifstream &,int &)84     virtual bool readValue(std::ifstream& /*f*/, int& /*i*/) { return false; }
readValue(std::ifstream &,long &)85     virtual bool readValue(std::ifstream& /*f*/, long& /*l*/) { return false; }
readValue(std::ifstream &,double &)86     virtual bool readValue(std::ifstream& /*f*/, double& /*d*/) { return false; }
87 };
88 
89 /// dxfReader. gets you through the dxf file, one group code/value pair at a time.
90 /// just instantiate, openFile(), then loop while(nextGroupCode())
91 class dxfReader : public osg::Referenced
92 {
93 public:
dxfReader()94     dxfReader() {}
~dxfReader()95     virtual ~dxfReader() {}
96     bool    openFile(std::string fileName);
97     bool    nextGroupCode(codeValue& cv);
98 protected:
99     osgDB::ifstream                _ifs;
100     osg::ref_ptr<readerBase>    _reader;
101 };
102 
103 #endif
104