Structural deformable models
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ParseFile Class Reference

#include <ParseFile.h>

Collaboration diagram for ParseFile:
Collaboration graph
[legend]

Public Types

enum  ErrorID { ERR_OK =0, ERR_FILE, ERR_PARSE, ERR_EOF }
 

Public Member Functions

 ParseFile (const std::string &filename)
 
 ParseFile (std::istream &is)
 
bool open (const std::string &filename)
 
void attach (std::istream &is)
 
void close ()
 
bool good () const
 
bool eof () const
 
bool error () const
 
const std::string & getPath () const
 
const std::string & getFilename () const
 
const std::string & getLine () const
 
const std::string & getKey () const
 
const std::string & getValue () const
 
const int getLineNumber () const
 
const bool getNextLine ()
 
void pushLine (const std::string &line)
 
void pushLine ()
 
void setParseError (const std::string &msg="")
 
char getCommentChar () const
 
void setCommentChar (char cchar)
 
std::string getErrorMsg () const
 
void appendErrMsg (const std::string &msg)
 
 operator bool () const
 
 operator std::istream & ()
 

Static Public Member Functions

static std::string getPath (const std::string &sfilename)
 

Protected Member Functions

void resetStrings ()
 
void parseLine ()
 

Protected Attributes

std::ifstream m_IF
 
std::istream m_IS
 
std::string m_Filename
 
std::string m_Path
 
enum ErrorID m_Error
 
std::list< std::string > m_PushedLines
 
std::string m_Line
 
std::string m_LastLine
 
int m_LineN
 
std::string m_Key
 
std::string m_Value
 
std::string m_ErrMsg
 
const char c_CommentChar
 

Detailed Description

Definition at line 15 of file ParseFile.h.

Member Enumeration Documentation

Enumerator
ERR_OK 
ERR_FILE 
ERR_PARSE 
ERR_EOF 

Definition at line 17 of file ParseFile.h.

Constructor & Destructor Documentation

ParseFile::ParseFile ( const std::string &  filename)
inline

Definition at line 19 of file ParseFile.h.

References open().

20  : m_IS(m_IF.rdbuf()), m_Error(ERR_OK), c_CommentChar('#') {
21  open(filename);
22  }
enum ErrorID m_Error
Definition: ParseFile.h:137
const char c_CommentChar
Definition: ParseFile.h:143
std::ifstream m_IF
Definition: ParseFile.h:134
std::istream m_IS
Definition: ParseFile.h:135
bool open(const std::string &filename)
Definition: ParseFile.h:27
ParseFile::ParseFile ( std::istream &  is)
inline

Definition at line 24 of file ParseFile.h.

25  : m_IS(is.rdbuf()), m_Error(ERR_OK), c_CommentChar('#') {}
enum ErrorID m_Error
Definition: ParseFile.h:137
const char c_CommentChar
Definition: ParseFile.h:143
std::istream m_IS
Definition: ParseFile.h:135

Member Function Documentation

void ParseFile::appendErrMsg ( const std::string &  msg)
inline

Definition at line 98 of file ParseFile.h.

References m_ErrMsg.

98 { m_ErrMsg = msg; }
std::string m_ErrMsg
Definition: ParseFile.h:142
void ParseFile::attach ( std::istream &  is)
inline

Definition at line 37 of file ParseFile.h.

References m_IS, and resetStrings().

Referenced by open().

37  {
38  resetStrings();
39  m_IS.rdbuf(is.rdbuf());
40  }
std::istream m_IS
Definition: ParseFile.h:135
void resetStrings()
Definition: ParseFile.h:113
void ParseFile::close ( )
inline

Definition at line 41 of file ParseFile.h.

References ERR_OK, m_Error, m_Filename, m_IF, m_Key, m_Line, m_LineN, m_Path, and m_Value.

41  {
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  }
enum ErrorID m_Error
Definition: ParseFile.h:137
std::ifstream m_IF
Definition: ParseFile.h:134
std::string m_Value
Definition: ParseFile.h:141
std::string m_Filename
Definition: ParseFile.h:136
std::string m_Line
Definition: ParseFile.h:139
int m_LineN
Definition: ParseFile.h:140
std::string m_Key
Definition: ParseFile.h:141
std::string m_Path
Definition: ParseFile.h:136
bool ParseFile::eof ( ) const
inline

Definition at line 49 of file ParseFile.h.

References ERR_EOF, and m_Error.

Referenced by operator>>().

49 { return m_Error == ERR_EOF; }
enum ErrorID m_Error
Definition: ParseFile.h:137
bool ParseFile::error ( ) const
inline

Definition at line 50 of file ParseFile.h.

References ERR_EOF, ERR_OK, and m_Error.

Referenced by SubStructure::read(), StructTable::read(), and MStructure::read().

50 { return m_Error!=ERR_OK && m_Error!=ERR_EOF; }
enum ErrorID m_Error
Definition: ParseFile.h:137
char ParseFile::getCommentChar ( ) const
inline

Definition at line 82 of file ParseFile.h.

References c_CommentChar.

82 { return c_CommentChar; }
const char c_CommentChar
Definition: ParseFile.h:143
std::string ParseFile::getErrorMsg ( ) const
inline

Definition at line 85 of file ParseFile.h.

References ERR_FILE, ERR_OK, ERR_PARSE, m_ErrMsg, m_Error, m_Filename, and m_LineN.

Referenced by Brain::load(), and StructTable::read().

85  {
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  }
enum ErrorID m_Error
Definition: ParseFile.h:137
std::string m_ErrMsg
Definition: ParseFile.h:142
std::string m_Filename
Definition: ParseFile.h:136
int m_LineN
Definition: ParseFile.h:140
const std::string& ParseFile::getFilename ( ) const
inline

Definition at line 53 of file ParseFile.h.

References m_Filename.

Referenced by SpeciesDB::load(), and operator>>().

53 { return m_Filename; }
std::string m_Filename
Definition: ParseFile.h:136
const std::string& ParseFile::getKey ( ) const
inline
const std::string& ParseFile::getLine ( ) const
inline

Definition at line 55 of file ParseFile.h.

References m_Line.

55 { return m_Line; }
std::string m_Line
Definition: ParseFile.h:139
const int ParseFile::getLineNumber ( ) const
inline

Definition at line 58 of file ParseFile.h.

References m_LineN.

Referenced by operator>>().

58 { return m_LineN; }
int m_LineN
Definition: ParseFile.h:140
const bool ParseFile::getNextLine ( )
inline

Definition at line 59 of file ParseFile.h.

References ERR_EOF, ERR_OK, m_Error, m_IS, m_Key, m_LastLine, m_Line, m_LineN, m_PushedLines, m_Value, and parseLine().

Referenced by Brain::load(), MStructure::loadRefProp(), operator>>(), SubStructure::read(), MStructure::read(), SearcherParams::read(), Model::readEdge(), StructTable::readInterpreations(), Model::readNode(), Model::readParameter(), SensorCollection::readSensor(), and Model::readSensor().

59  {
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  }
enum ErrorID m_Error
Definition: ParseFile.h:137
std::string m_Value
Definition: ParseFile.h:141
std::istream m_IS
Definition: ParseFile.h:135
std::string m_Line
Definition: ParseFile.h:139
int m_LineN
Definition: ParseFile.h:140
void parseLine()
Definition: ParseFile.h:121
std::list< std::string > m_PushedLines
Definition: ParseFile.h:138
std::string m_Key
Definition: ParseFile.h:141
std::string m_LastLine
Definition: ParseFile.h:139
const std::string& ParseFile::getPath ( ) const
inline

Definition at line 52 of file ParseFile.h.

References m_Path.

Referenced by Brain::load(), SpeciesDB::load(), open(), operator<<(), StructTable::read(), MStructure::read(), and Model::readSensor().

52 { return m_Path; }
std::string m_Path
Definition: ParseFile.h:136
static std::string ParseFile::getPath ( const std::string &  sfilename)
inlinestatic

Definition at line 106 of file ParseFile.h.

106  {
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  }
unsigned long dword
Definition: simpletypes.h:6
const std::string& ParseFile::getValue ( ) const
inline
bool ParseFile::good ( ) const
inline

Definition at line 48 of file ParseFile.h.

References ERR_OK, and m_Error.

Referenced by operator bool().

48 { return m_Error == ERR_OK; }
enum ErrorID m_Error
Definition: ParseFile.h:137
bool ParseFile::open ( const std::string &  filename)
inline

Definition at line 27 of file ParseFile.h.

References attach(), ERR_FILE, getPath(), m_Error, m_Filename, m_IF, m_Path, and resetStrings().

Referenced by ParseFile().

27  {
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  }
enum ErrorID m_Error
Definition: ParseFile.h:137
std::ifstream m_IF
Definition: ParseFile.h:134
const std::string & getPath() const
Definition: ParseFile.h:52
std::string m_Filename
Definition: ParseFile.h:136
void resetStrings()
Definition: ParseFile.h:113
void attach(std::istream &is)
Definition: ParseFile.h:37
std::string m_Path
Definition: ParseFile.h:136
ParseFile::operator bool ( ) const
inline

Definition at line 100 of file ParseFile.h.

References good().

100 { return good(); }
bool good() const
Definition: ParseFile.h:48
ParseFile::operator std::istream & ( )
inline

Definition at line 102 of file ParseFile.h.

References m_IS.

102 { return m_IS; }
std::istream m_IS
Definition: ParseFile.h:135
void ParseFile::parseLine ( )
inlineprotected

Definition at line 121 of file ParseFile.h.

References c_CommentChar, m_Key, m_Line, and m_Value.

Referenced by getNextLine(), and pushLine().

121  {
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  }
const char c_CommentChar
Definition: ParseFile.h:143
std::string m_Value
Definition: ParseFile.h:141
std::string m_Line
Definition: ParseFile.h:139
std::string m_Key
Definition: ParseFile.h:141
void ParseFile::pushLine ( const std::string &  line)
inline
void ParseFile::pushLine ( )
inline

Definition at line 75 of file ParseFile.h.

References m_LastLine, m_Line, and parseLine().

75  {
77  if(!m_LastLine.empty())
78  { m_Line = m_LastLine; m_LastLine.clear(); parseLine(); }
79  }
void pushLine()
Definition: ParseFile.h:75
std::string m_Line
Definition: ParseFile.h:139
void parseLine()
Definition: ParseFile.h:121
std::string m_LastLine
Definition: ParseFile.h:139
void ParseFile::resetStrings ( )
inlineprotected

Definition at line 113 of file ParseFile.h.

References ERR_OK, m_ErrMsg, m_Error, m_Filename, m_Key, m_LastLine, m_Line, m_LineN, m_Path, m_PushedLines, and m_Value.

Referenced by attach(), and open().

113  {
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  }
enum ErrorID m_Error
Definition: ParseFile.h:137
std::string m_ErrMsg
Definition: ParseFile.h:142
std::string m_Value
Definition: ParseFile.h:141
std::string m_Filename
Definition: ParseFile.h:136
std::string m_Line
Definition: ParseFile.h:139
int m_LineN
Definition: ParseFile.h:140
std::list< std::string > m_PushedLines
Definition: ParseFile.h:138
std::string m_Key
Definition: ParseFile.h:141
std::string m_Path
Definition: ParseFile.h:136
std::string m_LastLine
Definition: ParseFile.h:139
void ParseFile::setCommentChar ( char  cchar)
inline

Definition at line 83 of file ParseFile.h.

References c_CommentChar.

83 { ((char&)c_CommentChar) = cchar; }
const char c_CommentChar
Definition: ParseFile.h:143
void ParseFile::setParseError ( const std::string &  msg = "")
inline

Member Data Documentation

const char ParseFile::c_CommentChar
protected

Definition at line 143 of file ParseFile.h.

Referenced by getCommentChar(), parseLine(), and setCommentChar().

std::string ParseFile::m_ErrMsg
protected

Definition at line 142 of file ParseFile.h.

Referenced by appendErrMsg(), getErrorMsg(), resetStrings(), and setParseError().

enum ErrorID ParseFile::m_Error
protected

Definition at line 137 of file ParseFile.h.

Referenced by close(), eof(), error(), getErrorMsg(), getNextLine(), good(), open(), resetStrings(), and setParseError().

std::string ParseFile::m_Filename
protected

Definition at line 136 of file ParseFile.h.

Referenced by close(), getErrorMsg(), getFilename(), open(), and resetStrings().

std::ifstream ParseFile::m_IF
protected

Definition at line 134 of file ParseFile.h.

Referenced by close(), and open().

std::istream ParseFile::m_IS
protected

Definition at line 135 of file ParseFile.h.

Referenced by attach(), getNextLine(), and operator std::istream &().

std::string ParseFile::m_Key
protected

Definition at line 141 of file ParseFile.h.

Referenced by close(), getKey(), getNextLine(), parseLine(), and resetStrings().

std::string ParseFile::m_LastLine
protected

Definition at line 139 of file ParseFile.h.

Referenced by getNextLine(), pushLine(), and resetStrings().

std::string ParseFile::m_Line
protected

Definition at line 139 of file ParseFile.h.

Referenced by close(), getLine(), getNextLine(), parseLine(), pushLine(), and resetStrings().

int ParseFile::m_LineN
protected

Definition at line 140 of file ParseFile.h.

Referenced by close(), getErrorMsg(), getLineNumber(), getNextLine(), and resetStrings().

std::string ParseFile::m_Path
protected

Definition at line 136 of file ParseFile.h.

Referenced by close(), getPath(), open(), and resetStrings().

std::list<std::string> ParseFile::m_PushedLines
protected

Definition at line 138 of file ParseFile.h.

Referenced by getNextLine(), pushLine(), and resetStrings().

std::string ParseFile::m_Value
protected

Definition at line 141 of file ParseFile.h.

Referenced by close(), getNextLine(), getValue(), parseLine(), and resetStrings().


The documentation for this class was generated from the following file: