ParTxt.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 <suplib/str.h>
00026 
00027 #include "ParTxt.h"
00028 #include "Par.h"
00029 #include "ParFile.h"
00030 
00031 int ParTxt::restore=0;
00032 
00033 ParTxt::~ParTxt( ) {
00034 
00035   buffer = NULL;
00036 
00037   if ( buffer_argv )
00038     str_tokenize_free( buffer_argv, restore );
00039   
00040 }
00041 
00042 ParTxt::ParTxt( char* str ) throw( ParFileException ) {
00043 
00044   buffer_argv = NULL;
00045   num_tokens = 0;
00046 
00047   if ( 0 == str )
00048     return;
00049 
00050   buffer = str;
00051 
00052   try {
00053     init_ParTxt( str );
00054   } catch( ParFileException& pfe ) {
00055     throw;
00056   }
00057 
00058 }
00059 
00060 char* ParTxt::get_token( size_t num ) throw( ParFileException ) {
00061 
00062   if ( NULL == buffer_argv ) {
00063     char msg[ 128 ];
00064     const char* format = "ParTxt::get_token( %d )\n";
00065     sprintf( msg, format, num );
00066     throw ParFileException( msg );
00067   }
00068 
00069   return buffer_argv[ num ];
00070 
00071 }
00072 
00073 void ParTxt::init_ParTxt( char* str ) throw( ParFileException ) {
00074 
00075   buffer_argv = NULL;
00076   num_tokens = 0;
00077 
00078   if ( ParFile::is_comment( str ) )
00079     return;
00080 
00081   buffer = str;
00082 
00083   char delimit[ 2 ] = { Par::DELIMIT, '\0' };
00084   
00085   num_tokens = str_tokenize( str, delimit, &buffer_argv, 0, restore );
00086 
00087   if ( Par::NUMTOKENS != num_tokens ) {
00088     char msg[ 256 ];
00089     const char* format = 
00090       "ParTxt::init_ParTxt( %s ) : Expected %d tokens, but parsed %d "
00091       "number of tokens";
00092     sprintf( msg, format, str, Par::NUMTOKENS, num_tokens );
00093     throw ParFileException( msg );
00094   }
00095 
00096   for ( int ii = 0; ii < num_tokens; ii++ ) {
00097     if ( ii != num_tokens - 1 )
00098       unquote( buffer_argv[ ii ] );
00099     str_trim( buffer_argv[ ii ] );
00100     str_prune( buffer_argv[ ii ] );
00101   }
00102 
00103   try {
00104 
00105     verify_mode( buffer_argv[ Par::PARMODE ] );
00106 
00107   } catch( ParFileException& pfe ) {
00108     throw;
00109   }
00110 
00111 }
00112 
00113 void ParTxt::print( ostream& os ) {
00114 
00115   os << buffer;
00116   if ( 0 == num_tokens )
00117     return;
00118 
00119   for ( int i = 0; i < num_tokens; i++ )
00120     os << "buffer_argv[" << i << "] = `" << buffer_argv[ i ] << "'\n";
00121 
00122 }
00123 
00127 void ParTxt::verify_mode( const string& str ) throw( ParFileException ) {
00128 
00129   const char* ptr = str.c_str( );
00130 
00131   int c;
00132   while( (c=*ptr++) )
00133     switch( c ) {
00134     case Par::AUTOS:
00135     case Par::BATCH:
00136     case Par::hIDDEN:
00137     case Par::HIDDEN:
00138     case Par::QUERRY:
00139     case Par::LEARN:
00140       break;
00141     default:
00142       throw ParFileException( "ParTxt::verify_mode( " + str +
00143                               " ) : Incorrect mode\n" );
00144     }
00145 
00146   return;
00147 
00148 }

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