xorp

xrl_parser_input.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxipc/xrl_parser_input.hh,v 1.12 2008/10/02 21:57:24 bms Exp $
00023 
00024 #ifndef __LIBXIPC_XRL_PARSER_INPUT_HH__
00025 #define __LIBXIPC_XRL_PARSER_INPUT_HH__
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 #include "libxorp/xorp.h"
00034 #include "libxorp/exceptions.hh"
00035 
00045 class XrlParserInput {
00046 public:
00052     virtual bool getline(string& lineout) = 0;
00053 
00057     virtual bool eof() const = 0;
00058 
00063     virtual string stack_trace() const = 0;
00064 
00065     virtual ~XrlParserInput() {};
00066 };
00067 
00072 struct XrlParserInputException : public XorpReasonedException {
00073     XrlParserInputException(const char* file, int line, const string& reason)
00074     : XorpReasonedException("XrlParserInputException", file, line, reason) {}
00075 };
00076 
00083 class XrlParserFileInput : public XrlParserInput {
00084 public:
00091     XrlParserFileInput(istream* input, const char* fname = "")
00092     throw (XrlParserInputException);
00093 
00094     XrlParserFileInput(const char* filename)
00095     throw (XrlParserInputException);
00096 
00097     ~XrlParserFileInput();
00098 
00099     bool eof() const;
00100     bool getline(string& line) throw (XrlParserInputException);
00101     string stack_trace() const;
00102 
00104     list<string>& path() { return _path; }
00105 
00106 protected:
00107     bool slurp_line(string& line) throw (XrlParserInputException);
00108 
00109     struct FileState {
00110     FileState(istream* input, const char* fname) :
00111         _input(input), _fname(fname), _line(0) {}
00112     FileState() { _input = NULL; _fname = NULL; } // for uSTL
00113 
00114     // Accessors
00115     istream*    input() const { return _input; }
00116     const char*     filename() const { return _fname; }
00117     int         line() const { return _line; }
00118     void        incr_line() { _line++; }
00119     private:
00120     istream*   _input;
00121     const char* _fname;
00122     int     _line;
00123     };
00127     void push_stack(const FileState& fs)
00128     throw (XrlParserInputException);
00129 
00130     void pop_stack();
00131     FileState& stack_top();
00132     size_t stack_depth() const;
00133 
00134     ifstream* path_open_input(const char* filename)
00135     throw (XrlParserInputException);
00136     void close_input(istream* pif);
00137 
00138     string try_include(string::const_iterator& begin,
00139                const string::const_iterator& end)
00140     throw (XrlParserInputException);
00141     void initialize_path();
00142 
00143     vector<FileState>   _stack;
00144     list<string>    _path;
00145     bool        _own_bottom;     // We alloced stack bottom istream
00146     list<string>    _inserted_lines;
00147 
00148     bool filter_line(string& output, const string& input);
00149     enum Mode {
00150     NORMAL      = 0x00,
00151     IN_SQUOTE   = 0x01,
00152     IN_DQUOTE   = 0x02,
00153     IN_C_COMMENT    = 0x04
00154     } _current_mode;
00155 };
00156 
00157 #endif // __LIBXIPC_XRL_PARSER_INPUT_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations