00001 #ifndef tag_h
00002 #define tag_h
00003
00004 #include <string>
00005 #include <map>
00006 #include <list>
00007 #include "strings.h"
00008
00009 typedef struct Line
00010 {
00011 std::string directory;
00012 std::string file_name;
00013 std::string tag;
00014 std::string orig_tag;
00015
00017
00026 std::string tag_type;
00027
00028 int line_number;
00029 int cursor_pos;
00030
00032 std::string line;
00033
00034 Line()
00035 { line_number = -1; cursor_pos = -1; }
00036
00037 Line( std::string directory,
00038 std::string file_name,
00039 std::string tag,
00040 std::string tag_type,
00041 int line_number=-1,
00042 int cursor_pos=-1 ) :
00043 directory( directory ),
00044 file_name( file_name ),
00045 tag( tag ),
00046 tag_type( tag_type ),
00047 line_number( line_number ),
00048 cursor_pos( cursor_pos )
00049 {}
00050
00051 Line( std::string& directory,
00052 std::string& file_name,
00053 std::string& tag,
00054 std::string& tag_type,
00055 int line_number=-1,
00056 int cursor_pos=-1 ) :
00057 directory( directory ),
00058 file_name( file_name ),
00059 tag( tag ),
00060 tag_type( tag_type ),
00061 line_number( line_number ),
00062 cursor_pos( cursor_pos )
00063 {}
00064
00065 };
00066
00067
00069
00070 class Tag
00071 {
00072 private:
00073 std::string tag_type;
00074
00075 Line line;
00076 bool got_line;
00077
00078 bool searched_level;
00079 int level;
00080
00081 protected:
00082
00084
00094 struct Option
00095 {
00096 std::string first;
00097 std::string second;
00098
00099 Option( std::string name, std::string value )
00100 : first( name ), second( value )
00101 {}
00102 };
00103
00104 typedef std::list<Option> options_list;
00105 typedef options_list::iterator options_list_it;
00106
00107 mutable options_list options;
00108 bool is_valid_tag;
00109
00110 bool options_extracted;
00111
00113
00114 bool no_warning_check;
00115 bool nowarning;
00116
00117 public:
00119 Tag( std::string tag, bool no_warning_check = false );
00120 Tag( Line& line, bool no_warning_check = false );
00121 Tag() { is_valid_tag = false; }
00122
00124 bool operator!() const
00125 { return !is_valid_tag; }
00126
00128
00136 bool get_option( const std::string which, std::string &result );
00137
00138 template<class T> bool get_option( const std::string which, T &result )
00139 {
00140 std::string dummy;
00141 if( get_option( which, dummy ) )
00142 {
00143 result = Strings::s2x<T>( dummy );
00144 return true;
00145 }
00146 return false;
00147 }
00148
00150 bool get_option( const std::string which )
00151 {
00152 std::string dummy;
00153 return get_option( which, dummy );
00154 }
00155
00156 template <class T> bool set_option( const std::string o, const T f, bool ff=false )
00157 { return set_option( o, Strings::x2s( f ), ff ); }
00158
00163 bool set_option( const std::string option, const std::string flag="", bool fail_if_set=false );
00164
00166 std::string get_tag() const
00167 { return line.tag; }
00168
00170 std::string get_tag_type() const
00171 { return tag_type; }
00172
00174
00175 void set_tag_type( std::string type )
00176 { tag_type = type; }
00177
00178 Line get_line() const
00179 { return line; }
00180
00182
00183
00184
00185
00186
00187
00188 void rebuild_tag();
00189
00191 void delete_option( const std::string option );
00192
00193 void delete_default_options()
00194 { delete_option( "nowarning" ); }
00195
00197 std::string get_options();
00198
00199 int get_number_of_options()
00200 {
00201 if( !options_extracted )
00202 {
00203 extract_options();
00204 options_extracted = true;
00205 }
00206
00207 if( options.empty() )
00208 return 0;
00209
00210 int i=0;
00211 for( options_list_it it = options.begin(); it != options.end(); ++it )
00212 i++;
00213
00214 return i;
00215 }
00216
00218 bool get_option( int number, std::string &option, std::string &value )
00219 {
00220 int n = 0;
00221 for( options_list_it it = options.begin(); it != options.end(); ++it )
00222 {
00223 if( n == number )
00224 {
00225 value = it->second;
00226 option = it->first;
00227 return true;
00228 }
00229
00230 if( n > number )
00231 return false;
00232
00233 ++n;
00234 }
00235
00236 return false;
00237 }
00238
00240 bool get_option( int number, std::string &value )
00241 {
00242 std::string dummy;
00243 return get_option( number, dummy, value );
00244 }
00245
00253 int get_level( bool guess_it = false );
00254
00255 private:
00256
00258 bool is_tag();
00259
00261 void extract_options();
00262
00264 void extract_type();
00265
00267 void split_option( std::string option, std::string &name, std::string &value );
00268
00269 inline bool check_if_valid();
00270 };
00271
00272
00273 #endif