[Tuxcpprogramming] Inheritance: creating a configuration stream

Mark Phillips mark at austrics.com.au
Thu Jul 12 12:35:31 UTC 2001


Hi,

I am wanting to create a generalized stream class
which makes it easy to read-in information from
a text configuration file.

It should have the following functionality:
* remove comments --- everything on a line
  after a '#' character
* support "carry-over" lines --- ie if a line ends
  with a '\', the next line should be treated as an
  extension of the current line.  The getline()
  message should automatically perform this
  "carry-over" operation (as well as getting rid
  of comments).
* provide ability to extract "tokens" --- a token
  is either an integer, eg 255, a double, eg 6.3217,
  a string with no white space, eg BANANA, or
  a string within quotes which may contain white
  space, eg "The cat".  A line should comprise a
  list of tokens, separated by white space.  Eg
      "The cat" 6.3217 BANANA 255
  The class should be able to "get the next token"
  and automatically do the right conversion so
  the result is returned as an integer, double or
  string, depending on what it is.

So far, my idea for this class is as follows:

class configStreamTy: public ifstream {
private:
  string& curLine;
public:
  bool getline();
  bool getline(string& line) {
    bool success=ifstream::getline();
    line=curLine;
    return success;
  }
  bool getToken(string& token);
  bool getToken(int& token);
  bool getToken(double& token);
};

But I'm still new to c++ programming and I'm
not sure if this is the best way of setting things
up.  Also, I'm a bit unsure of exactly how
inheritance works.  That is, do I need to provide
new definitions for constructors and destructors,
or do the ones provided by ifstream work?  What
if I wanted to initialize curLine to "" as part of
the constructor --- how would I do this without
affecting the ifstream constructors?  Does my
redefining of getline() cause problems?

Oh the other thing is --- am I reinventing the
wheel?  Is there a C++ library out there that does
all this?

Thanks,

Mark.





More information about the tuxCPProgramming mailing list