NameAttributeValue.cc

00001 // --8<--8<--8<--8<--
00002 //
00003 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00004 //
00005 // This file is part of paramxx
00006 //
00007 // paramxx is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // paramxx is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the 
00019 //       Free Software Foundation, Inc. 
00020 //       51 Franklin Street, Fifth Floor
00021 //       Boston, MA  02110-1301, USA
00022 //
00023 // -->8-->8-->8-->8--
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   // cout << "NameAttributeValue::check_boolean( " << str << " )\n";
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   // os << "NameAttributeValue::init_NameAttributeValue( " << str << " ) " << endl;
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   // Must call unquote before str_prune since str coud be ` " foo=bar " '
00086   (void) ::unquote( str );
00087   (void) str_prune( str );
00088 
00089   // str could be of the form: foo+, foo- or --foo
00090   if ( check_boolean( str ) )
00091     return;
00092 
00093   // Get the value.
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 // The current state of the txt_argv is:
00117 //
00118 //    txt_argv[ NameAttributeValue::name ] = name.attribute
00119 //    txt_argv[ NameAttributeValue::val ] = value
00120 //
00121 // where .attribute may or may not be present
00122 //
00123 void NameAttributeValue::seperate_keyword_attribute( char* str ) {
00124 
00125   // There is no attribute in the original string. So return
00126   if ( NULL == strstr( str, "." ) )
00127     return;
00128 
00129   // the attribute starts immediately after the period: name.attribute = value
00130   char* period = strchr( str, '.' );
00131   // increment to get to the start of the attribute.
00132   period++;
00133 
00134   attribute = period;
00135 
00136   // decrememnt to back to where the period is
00137   period--;
00138 
00139   // set it to zero
00140   *period = '\0';
00141   name = str;
00142 
00143   return;
00144 
00145 }
00146 
00147 void NameAttributeValue::seperate_keyword_value( char* txt ) {
00148 
00149   //
00150   // Tokenize on '=' to seperate parameter name, attribute and value.
00151   // Do not tokenize on '.' yet since a '.' can appear multiple times.
00152   // That is, do not make the call as str_tokenize( txt, ".=" )
00153   // since it will fail on foo.min=12.3
00154   //
00155   // strstr is used instead of parse or tokenize since the following
00156   // example is a legitimate entry (the optimizer program):
00157   //    optimizer override='a0.fitme=1 a1.value=3'
00158   // The parse or tokenize command produces too many tokens to be
00159   // meaningful.
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 }

Generated on Thu Oct 2 17:54:19 2008 for paramxx by  doxygen 1.5.6