Structural deformable models
ParseFile.h
Go to the documentation of this file.
1 #ifndef _PARSEFILE_H_
2 #define _PARSEFILE_H_
3 
4 #include <list>
5 #include <string>
6 #include <iostream>
7 #include <fstream>
8 #include <sstream>
9 #include "simpletypes.h"
10 
11 #ifndef COMMENT_CHAR
12 #define COMMENT_CHAR '#'
13 #endif
14 
15 class ParseFile {
16 public:
18 
19 ParseFile(const std::string& filename)
20  : m_IS(m_IF.rdbuf()), m_Error(ERR_OK), c_CommentChar('#') {
21  open(filename);
22  }
23 
24 ParseFile(std::istream& is)
25  : m_IS(is.rdbuf()), m_Error(ERR_OK), c_CommentChar('#') {}
26 
27  bool open(const std::string& filename) {
28  resetStrings();
29  m_IF.open(filename.c_str());
30  if(m_IF) {
31  attach(m_IF);
32  m_Filename = filename;
33  m_Path = getPath(filename);
34  return true;
35  } else { m_Error = ERR_FILE; return false; }
36  }
37  void attach(std::istream& is) {
38  resetStrings();
39  m_IS.rdbuf(is.rdbuf());
40  }
41  void close() {
42  m_Error = ERR_OK;
43  m_LineN = 0;
44  m_Line.clear(); m_Key.clear(); m_Value.clear();
45  m_Filename.clear(); m_Path.clear();
46  m_IF.close();
47  }
48  bool good() const { return m_Error == ERR_OK; }
49  bool eof() const { return m_Error == ERR_EOF; }
50  bool error() const { return m_Error!=ERR_OK && m_Error!=ERR_EOF; }
51 
52  const std::string& getPath() const { return m_Path; }
53  const std::string& getFilename() const { return m_Filename; }
54 
55  const std::string& getLine() const { return m_Line; }
56  const std::string& getKey() const { return m_Key; }
57  const std::string& getValue() const { return m_Value; }
58  const int getLineNumber() const { return m_LineN; }
59  const bool getNextLine() {
60  if(m_Error != ERR_OK) return false;
62  m_Line.clear(); m_Key.clear(); m_Value.clear();
63  while(m_Key.empty() &&
64  (!m_PushedLines.empty() ? true : (bool)std::getline(m_IS, m_Line))) {
65  if(!m_PushedLines.empty()) {
66  m_Line = m_PushedLines.back();
67  m_PushedLines.pop_back();
68  } else m_LineN++;
69  parseLine();
70  }
71  if(m_Key.empty()) { m_Error = ERR_EOF; return false; }
72  return true;
73  }
74  void pushLine(const std::string& line) { m_PushedLines.push_back(line); }
75  void pushLine() {
77  if(!m_LastLine.empty())
78  { m_Line = m_LastLine; m_LastLine.clear(); parseLine(); }
79  }
80  void setParseError(const std::string& msg = "")
81  { if(m_Error == ERR_OK) { m_Error = ERR_PARSE; m_ErrMsg = msg; } }
82  char getCommentChar() const { return c_CommentChar; }
83  void setCommentChar(char cchar) { ((char&)c_CommentChar) = cchar; }
84 
85  std::string getErrorMsg() const {
86  std::ostringstream msg;
87  msg << "[file " << m_Filename << "] ";
88  switch(m_Error) {
89  case ERR_OK: msg << "none"; break;
90  case ERR_FILE: msg << "error opening file"; break;
91  case ERR_PARSE: msg << "parse error in line " << m_LineN;
92  break;
93  case ERR_EOF: msg << "end of file"; break;
94  }
95  if(!m_ErrMsg.empty()) msg << ": " << m_ErrMsg;
96  return msg.str();
97  }
98  void appendErrMsg(const std::string& msg) { m_ErrMsg = msg; }
99 
100  operator bool () const { return good(); }
101  //operator std::ifstream& () { return m_IF; }
102  operator std::istream& () { return m_IS; }
103 
104 // --------- static -----------
105 
106  static std::string getPath(const std::string& sfilename) {
107  dword slashpos = sfilename.rfind('/');
108  if(slashpos == sfilename.npos) slashpos = sfilename.rfind('\\');
109  if(slashpos != sfilename.npos) return sfilename.substr(0,slashpos+1);
110  else return std::string("./");
111  }
112 protected:
113  void resetStrings() {
114  m_Error = ERR_OK;
115  m_LineN = 0;
116  m_Line.clear(); m_Key.clear(); m_Value.clear(); m_LastLine.clear();
117  m_PushedLines.clear();
118  m_ErrMsg.clear();
119  m_Filename.clear(); m_Path.clear();
120  }
121  void parseLine() {
122  m_Line = m_Line.substr(0,m_Line.find(c_CommentChar));
123  std::string::reverse_iterator ch = m_Line.rbegin();
124  while(ch!=m_Line.rend() && *ch<=32) ch++; //moves backwards
125  m_Line = m_Line.substr(0,m_Line.size()-(ch-m_Line.rbegin()));
126  std::istringstream lines(m_Line);
127  std::string token, value;
128  if(lines >> m_Key) {
129  lines >> std::ws;
130  std::getline(lines, m_Value);
131  }
132  }
133 protected:
134  std::ifstream m_IF;
135  std::istream m_IS;
136  std::string m_Filename, m_Path;
138  std::list<std::string> m_PushedLines;
139  std::string m_Line, m_LastLine;
140  int m_LineN;
141  std::string m_Key, m_Value;
142  std::string m_ErrMsg;
143  const char c_CommentChar;
144 };
145 
146 #endif
enum ErrorID m_Error
Definition: ParseFile.h:137
const std::string & getFilename() const
Definition: ParseFile.h:53
const std::string & getLine() const
Definition: ParseFile.h:55
char getCommentChar() const
Definition: ParseFile.h:82
void close()
Definition: ParseFile.h:41
std::string m_ErrMsg
Definition: ParseFile.h:142
static std::string getPath(const std::string &sfilename)
Definition: ParseFile.h:106
const std::string & getValue() const
Definition: ParseFile.h:57
bool error() const
Definition: ParseFile.h:50
const char c_CommentChar
Definition: ParseFile.h:143
void pushLine()
Definition: ParseFile.h:75
std::ifstream m_IF
Definition: ParseFile.h:134
ParseFile(std::istream &is)
Definition: ParseFile.h:24
const std::string & getPath() const
Definition: ParseFile.h:52
bool eof() const
Definition: ParseFile.h:49
std::string m_Value
Definition: ParseFile.h:141
const std::string & getKey() const
Definition: ParseFile.h:56
void appendErrMsg(const std::string &msg)
Definition: ParseFile.h:98
std::istream m_IS
Definition: ParseFile.h:135
std::string m_Filename
Definition: ParseFile.h:136
unsigned long dword
Definition: simpletypes.h:6
std::string m_Line
Definition: ParseFile.h:139
void setParseError(const std::string &msg="")
Definition: ParseFile.h:80
void resetStrings()
Definition: ParseFile.h:113
std::string getErrorMsg() const
Definition: ParseFile.h:85
int m_LineN
Definition: ParseFile.h:140
void parseLine()
Definition: ParseFile.h:121
std::list< std::string > m_PushedLines
Definition: ParseFile.h:138
ParseFile(const std::string &filename)
Definition: ParseFile.h:19
std::string m_Key
Definition: ParseFile.h:141
void pushLine(const std::string &line)
Definition: ParseFile.h:74
void attach(std::istream &is)
Definition: ParseFile.h:37
bool open(const std::string &filename)
Definition: ParseFile.h:27
void setCommentChar(char cchar)
Definition: ParseFile.h:83
const bool getNextLine()
Definition: ParseFile.h:59
bool good() const
Definition: ParseFile.h:48
const int getLineNumber() const
Definition: ParseFile.h:58
std::string m_Path
Definition: ParseFile.h:136
std::string m_LastLine
Definition: ParseFile.h:139