Utils: String join utility
[senf.git] / Console / Parse.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Parse public header */
25
26 #ifndef HH_Parse_
27 #define HH_Parse_ 1
28
29 // Custom includes
30 #include <string>
31 #include <vector>
32 #include <boost/utility.hpp>
33 #include <boost/scoped_ptr.hpp>
34 #include <boost/range/iterator_range.hpp>
35
36 //#include "Parse.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41
42     namespace detail { struct ParserAccess; }
43
44     /** \brief
45       */
46     class ArgumentToken
47     {
48     public:
49         std::string const & value() const;
50
51     protected:
52
53     private:
54         explicit ArgumentToken(std::string token);
55
56         std::string token_;
57
58         friend class detail::ParserAccess;
59     };
60
61
62     /** \brief
63       */
64     class ParseCommandInfo
65     {
66         typedef std::vector<ArgumentToken> Tokens;
67         
68     public:
69         typedef Tokens::const_iterator token_iterator;
70         typedef boost::iterator_range<token_iterator> argument_value_type;
71
72     private:
73         typedef std::vector<argument_value_type> Arguments;
74
75     public:
76         typedef Arguments::const_iterator argument_iterator;
77         typedef Arguments::size_type size_type;
78
79         std::string const & commandPath() const;
80         
81         size_type arguments() const;
82         argument_iterator begin_arguments() const;
83         argument_iterator end_arguments() const;
84         
85         size_type tokens() const;
86         token_iterator begin_tokens() const;
87         token_iterator end_tokens() const;
88         
89     protected:
90
91     private:
92         void init();
93         void setCommand(std::string const & commandPath);
94         void startArgument();
95         void endArgument();
96         void addToken(ArgumentToken const & token);
97         void finalize();
98
99         struct MakeRange;
100
101         std::string commandPath_;
102
103         typedef std::pair<Tokens::size_type, Tokens::size_type> TempArgumentRange;
104         typedef std::vector<TempArgumentRange> TempArguments;
105
106         Tokens tokens_;
107         Arguments arguments_;
108         TempArguments tempArguments_;
109
110         friend class detail::ParserAccess;
111     };
112
113     /** \brief
114       */
115     class SingleCommandParser
116         : boost::noncopyable
117     {
118     public:
119         ///////////////////////////////////////////////////////////////////////////
120         // Types
121
122         ///////////////////////////////////////////////////////////////////////////
123         ///\name Structors and default members
124         ///@{
125
126         SingleCommandParser();
127         ~SingleCommandParser();
128
129         ///@}
130         ///////////////////////////////////////////////////////////////////////////
131
132         bool parseCommand(std::string command, ParseCommandInfo & info);
133
134     private:
135         struct Impl;
136
137         Impl & impl();
138
139         boost::scoped_ptr<Impl> impl_;
140     };
141
142 }}
143
144 ///////////////////////////////hh.e////////////////////////////////////////
145 #include "Parse.cci"
146 //#include "Parse.ct"
147 //#include "Parse.cti"
148 #endif
149
150 \f
151 // Local Variables:
152 // mode: c++
153 // fill-column: 100
154 // comment-column: 40
155 // c-file-style: "senf"
156 // indent-tabs-mode: nil
157 // ispell-local-dictionary: "american"
158 // compile-command: "scons -u test"
159 // End: