NameAttributeValue.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <memory>
00026 #include <cstring>
00027
00028 using namespace std;
00029
00030 #include <suplib/str.h>
00031
00032 #include "NameAttributeValue.h"
00033
00034 bool NameAttributeValue::check_boolean( char* str ) {
00035
00036
00037
00038 if ( 0 == str || 0 == *str )
00039 return false;
00040
00041 size_t last_char_pos = strlen( str ) - 1;
00042 char last_char = str[ last_char_pos ];
00043
00044 if ( '+' == last_char ) {
00045
00046 str[ last_char_pos ] = '\0';
00047 name = str;
00048 value = "1";
00049 return true;
00050
00051 } else if ( '-' == last_char ) {
00052
00053 str[ last_char_pos ] = '\0';
00054 name = str;
00055 value = "0";
00056 return true;
00057
00058 } else if ( 0 == strncmp( str, "--", 2 ) ) {
00059
00060 name = str + 2;
00061 value = "1";
00062 return true;
00063
00064 }
00065
00066 return false;
00067
00068 }
00069
00070 void NameAttributeValue::init_NameAttributeValue( const char* txt ) {
00071
00072
00073
00074 name ="";
00075 attribute="";
00076 value="";
00077
00078 if ( 0 == txt || 0 == *txt )
00079 return;
00080
00081 auto_ptr< char > tmp( new char[ strlen( txt ) + 1 ] );
00082 char* str = tmp.get( );
00083 strcpy( str, txt );
00084
00085
00086 (void) ::unquote( str );
00087 (void) str_prune( str );
00088
00089
00090 if ( check_boolean( str ) )
00091 return;
00092
00093
00094 seperate_keyword_value( str );
00095
00096 strcpy( str, name.c_str( ) );
00097 seperate_keyword_attribute( str );
00098
00099 return;
00100
00101 }
00102
00103 void NameAttributeValue::print( ostream& os ) {
00104
00105 os << name;
00106
00107 if ( "" != attribute )
00108 os << "." << attribute;
00109
00110 os << " = " << value;
00111
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 void NameAttributeValue::seperate_keyword_attribute( char* str ) {
00124
00125
00126 if ( NULL == strstr( str, "." ) )
00127 return;
00128
00129
00130 char* period = strchr( str, '.' );
00131
00132 period++;
00133
00134 attribute = period;
00135
00136
00137 period--;
00138
00139
00140 *period = '\0';
00141 name = str;
00142
00143 return;
00144
00145 }
00146
00147 void NameAttributeValue::seperate_keyword_value( char* txt ) {
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 char* ptr = strstr( txt, "=" );
00163 if ( NULL == ptr ) {
00164 name = txt;
00165 return;
00166 }
00167
00168 str_prune( ptr + 1 );
00169 value = ptr + 1;
00170
00171 char tmp[ 256 ];
00172 strncpy( tmp, txt, ptr - txt );
00173 tmp[ ptr - txt ] = '\0';
00174
00175 name = str_prune( tmp );
00176
00177 return;
00178
00179 }