DeSR Dependency Parser |
00001 /* 00002 ** IXE C++ Library 00003 ** ixe/OptionStream.h 00004 ** ---------------------------------------------------------------------- 00005 ** Copyright (c) 2000 Ideare SpA. 00006 ** Copyright (c) 2000 Giuseppe Attardi (attardi@di.unipi.it). 00007 ** ---------------------------------------------------------------------- 00008 ** 00009 ** This file is part of DeSR. 00010 ** 00011 ** DeSR is free software; you can redistribute it and/or modify it 00012 ** under the terms of the GNU General Public License, version 3, 00013 ** as published by the Free Software Foundation. 00014 ** 00015 ** DeSR is distributed in the hope that it will be useful, 00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 ** GNU General Public License for more details. 00019 ** 00020 ** You should have received a copy of the GNU General Public License 00021 ** along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 ** ---------------------------------------------------------------------- 00023 */ 00024 00025 #ifndef IXE_OptionStream_H 00026 #define IXE_OptionStream_H 00027 00028 // Settings 00029 #include "platform.h" 00030 00031 // standard 00032 #include <iostream> 00033 00034 namespace IXE { 00035 00036 using namespace std; 00037 00042 struct Options 00043 { 00044 enum { 00045 no_arg = 0, 00046 req_arg = 1, 00047 opt_arg = 2 00048 }; 00049 00050 struct spec { 00051 // 00052 // A spec gives a specification for an option: its long name, 00053 // its argument type, and it's short option equivalent. A 00054 // null-terminated array of specs is given to the OptionStream 00055 // constructor. 00056 // 00057 // The arg_type takes on one of the above enum values, but is 00058 // not declared as an enum to allow the integer values to be 00059 // used as a shorthand. 00060 // 00061 // Regardless of whether the user enters a long or a short 00062 // option, it is the short option character that is returned to 00063 // the caller in order to be able to use a simple switch 00064 // statement to do different things depending on the option. 00065 // 00066 // If, for a given long option there is to be no short option 00067 // equivalent, then the caller has to make up bogus ones that 00068 // the user will never type. Either the ASCII control or high 00069 // characters can be user for this. The null character may not 00070 // be used. 00071 // 00072 00073 char const* long_name; 00074 short arg_type; 00075 unsigned char c; 00076 char const* use_msg; 00077 }; 00078 00079 static ostream& usage(spec const options[], ostream& err); 00080 }; 00081 00092 class OptionStream 00093 { 00094 public: 00095 OptionStream(int argc, char* argv[], Options::spec const[], 00096 std::ostream& = std::cerr); 00097 00098 int shift() const { return index; } 00099 operator bool() const { return !end; } 00100 00101 class Option { 00102 // 00103 // An option is what is extracted from an OptionStream. Its 00104 // operator char() gives which option it is and arg() gives its 00105 // argument, if any. For options that do not have an argument, 00106 // arg() returns the null pointer. 00107 // 00108 // An option may be copied in which case it has a private copy 00109 // of its argument. 00110 // 00111 public: 00112 Option(char c = '\0', char* a = 0) : 00113 c(c), _arg(a), am_copy(false) { } 00114 Option(Option const& o) { 00115 if (&o != this) copy(o); 00116 } 00117 ~Option() { 00118 if (am_copy) delete[] _arg; 00119 } 00120 Option& operator =(Option const &o) { 00121 if (&o != this) { 00122 if (am_copy) delete[] _arg; 00123 copy(o); 00124 } 00125 return *this; 00126 } 00127 00128 char* arg() const { return _arg; } 00129 operator char() const { return c; } 00130 00131 friend OptionStream& operator >>(OptionStream&, Option&); 00132 private: 00133 char c; 00134 char* _arg; 00135 bool am_copy; 00136 void copy(Option const&); 00137 }; 00138 00139 friend OptionStream& operator >>(OptionStream&, Option&); 00140 private: 00141 int argc; 00142 char** argv; 00143 Options::spec const* specs; 00144 std::ostream& err; 00145 int index; 00146 char* next_c; 00147 bool end; 00148 }; 00149 00150 } // namespace IXE 00151 00152 #endif /* IXE_OptionStream_H */