Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

util_io.C

Go to the documentation of this file.
00001 
00002        //=======================================================//    _\|/_
00003       //  __  _____           ___                    ___       //      /|\ ~
00004      //  /      |      ^     |   \  |         ^     |   \     //          _\|/_
00005     //   \__    |     / \    |___/  |        / \    |___/    //            /|\ ~
00006    //       \   |    /___\   |  \   |       /___\   |   \   // _\|/_
00007   //     ___/   |   /     \  |   \  |____  /     \  |___/  //   /|\ ~
00008  //                                                       //            _\|/_
00009 //=======================================================//              /|\ ~
00010 
00011 // util_io.C:  functions for formatted I/O.
00012 
00013 #include "starlab_vector.h"
00014 #include "util_io.h"
00015 #include "story.h"
00016 #include <ctype.h>
00017 
00018 #undef isalnum          /* Hacks for Irix 6.5 <ctype.h> backward compatibility */
00019 #undef isspace
00020 
00021 int get_line(istream & s, char * line)
00022 {
00023     s.get(line,MAX_INPUT_LINE_LENGTH,'\n');
00024     char c;
00025     if(s.get(c) && c!='\n'){
00026         cerr << "get_line : input line too long :'"<<line<<"'\n";
00027         exit(1);
00028     }
00029     return strlen(line);
00030 }
00031 
00032 int check_input_line(istream &s, char* reference_string)
00033 {
00034     char input_line[MAX_INPUT_LINE_LENGTH];
00035     get_line(s,input_line);
00036     if(s.eof()){
00037         cerr << "check_input_line : unexpected EOF, expected '";
00038         cerr << reference_string <<"'\n";
00039         exit(1);
00040     }
00041     return matchbracket(reference_string, input_line);
00042 }    
00043 
00044 int check_and_skip_input_line(istream &s, char* reference_string)
00045 {
00046 //  char dummy;
00047     char input_line[MAX_INPUT_LINE_LENGTH];
00048     while (! get_line(s,input_line) && !s.eof())
00049         {
00050 //      if(s.eof())
00051 //          return 0;
00052 //      cin >> dummy;           // to get the g++ compiler to the EOF
00053 // cerr << "  dummy = " << dummy << endl;
00054 //      if(s.eof())
00055 //          return 0;
00056 //      if (dummy == '\0')
00057 
00058         int shhh = 1;           // to make the SG compiler shut up
00059         if (shhh)               // to make the SG compiler shut up
00060             return 0;
00061         }
00062     if(!matchbracket(reference_string, input_line)){
00063         if(s.eof()){
00064             return 0;
00065         }else{
00066             cerr << "Input line must be '"<<reference_string;
00067             cerr <<"', I got '" << input_line << "'\n";
00068             exit(1);
00069         }
00070     }
00071     return 1;
00072 }    
00073 
00074 int get_data_line(istream & s,char * input_line)
00075 {
00076     get_line(s,input_line);
00077     return strcmp(input_line,")");
00078 }
00079 
00080 /* Returns 1 if either token matches line, or first two chars of token
00081  * matches line.  So matchbracket("(Particle", line) matches "(Particle" or "(P".
00082  */
00083 int matchbracket(const char *token, const char *line) {
00084   while(*line == ' ' || *line == '\t')
00085       line++;
00086   if(token[0] != line[0] || token[1] != line[1])
00087     return 0;
00088   return (line[2] == '\0') || (0 == strcmp(token+2, line+2));
00089 }
00090 
00091 const char *getequals(const char *input_line, char *keyword)
00092 {
00093     const char *cp = input_line;
00094 
00095     /* Grab first token from line, like sscanf %s */
00096     while(isspace(*cp)) cp++;
00097     int i;
00098     for(i = 0; isalnum(*cp) || *cp == '_'; )
00099         keyword[i++] = *cp++;
00100     keyword[i] = '\0';
00101 
00102     cp = strchr(cp, '=');
00103     if(cp == NULL) {
00104         cerr << "Expected keyword = value, but got '"<< input_line <<"'\n";
00105         exit(1);
00106     }
00107     cp++;
00108     while(isspace(*cp)) cp++;
00109     return cp;
00110 }
00111 
00112 void set_vector_from_input_line(vector & v, char * input_line)
00113 {
00114     char *eq = strchr(input_line, '=');
00115     if(eq)
00116         set_vector_from_string( v, eq+1 );
00117 }
00118 
00119 void set_vector_from_string(vector & v, char *val)
00120 {
00121     real component[3];
00122     char *cp, *ep;
00123     component[0] = strtod(val, &cp);
00124     component[1] = strtod(cp, &cp);
00125     component[2] = strtod(cp, &ep);
00126     if(cp == ep) {
00127         cerr << "Expected three reals, got: " << val << endl;
00128         exit(1);
00129     }
00130     v = vector(component[0],component[1],component[2]);
00131 }
00132     
00133 
00134 static bool print = true;
00135 
00136 xreal get_xreal_from_input_line(char * input_line)
00137 {
00138     char *val = strchr(input_line, '=');
00139     if(val == NULL) return (xreal)0;
00140     val++;
00141 
00142 #if defined USE_XREAL
00143 
00144     // "True" xreal:
00145 
00146     char *sp, *ep;
00147     long long i = STRTOL(val, &sp, 10);           // signed integer part
00148     unsigned long long f = STRTOUL(sp, &ep, 10);  // unsigned fractional part
00149 
00150     if (sp == ep) {                               // if we didn't get both
00151                                                   // of above,
00152 
00153         // Hmmm... most likely we have real input data.  Try just reading
00154         // a real number.  (Steve, 6/00)
00155 
00156         if (print) {
00157             cerr << "get_xreal_from_input_line: error reading xreal input "
00158                  << "from line" << endl
00159                  << "    " << input_line << endl
00160                  << "Assuming real data." << endl << endl;
00161             print = false;
00162         }
00163 
00164         return (xreal)strtod( val, NULL );
00165     }
00166 
00167     return xreal(i, f);
00168 
00169 #else
00170 
00171     // xreal is really just real:
00172 
00173     return (xreal)strtod( val, NULL );
00174 
00175 #endif
00176 }
00177 
00178 //
00179 //---------------------------------------------------------------------
00180 //
00181 // Horrible kludges:
00182 // ----------------
00183 //
00184 
00185 static bool short_story_keywords = false;
00186 
00187 bool use_short_story_keywords( bool useshort ) {
00188   bool was = short_story_keywords;
00189   short_story_keywords = useshort;
00190   return was;
00191 }
00192 
00193 
00194 void put_story_header(ostream & s, char * id)
00195 {
00196 #ifdef BAD_GNU_IO
00197     if (s == cout) {
00198         if(short_story_keywords) {
00199             fprintf(stdout, "(%c\n", id[0]);
00200         } else {
00201             fprintf(stdout, "(%s\n", id);
00202         }
00203     }
00204     else
00205 #endif
00206     if(short_story_keywords) {
00207         s << '(' << id[0] << endl;
00208     } else {
00209         s << '(' << id << endl;
00210     }
00211 }
00212 
00213 void put_story_footer(ostream & s, char * id)
00214 {
00215 #ifdef BAD_GNU_IO
00216     if (s == cout) {
00217         if(short_story_keywords) {
00218             fprintf(stdout, ")%c\n", id[0]);
00219         } else {
00220             fprintf(stdout, ")%s\n", id);
00221         }
00222     }
00223     else
00224 #endif
00225     if(short_story_keywords) {
00226         s << ')' << id[0] << endl;
00227     } else {
00228         s << ')' << id << endl;
00229     }
00230 }
00231 
00232 
00233 #ifdef USE_XREAL
00234 void put_real_number(ostream & s, char * label, xreal x)
00235 {
00236     s << label << x.get_i() << " " << x.get_f() << endl;
00237 }
00238 #endif
00239 
00240 // NOTE use of precision here.  If the STARLAB_PRECISION environment
00241 // variable is set, the first call to set_starlab_precision will use
00242 // its value (whatever it may be).  Subsequent calls will return the
00243 // same value originally read from the environment.  If no environment
00244 // variable is set, a default value (currently 18) is used.
00245 
00246 void put_real_number(ostream & s, char * label, real x)
00247 {
00248     int old_precision = set_starlab_precision(s);
00249 
00250 #ifndef BAD_GNU_IO
00251     s << label << x << endl;
00252 #else
00253     if (s == cout) {
00254         static char format[40];
00255         static int local_precision = -1;
00256         int precision = get_starlab_precision();
00257         if (local_precision != precision) {
00258             local_precision = precision;
00259             int p = precision;
00260             if (p < 0) p = 5;
00261             sprintf(format, "%%s%%.%dg\n", p);
00262         }
00263         fprintf(stdout, format, label, x);
00264     } else
00265         s << label << x << endl;
00266 #endif
00267 
00268     // Restore the current precision.
00269 
00270     if (get_starlab_precision() != old_precision)
00271         s.precision(old_precision);
00272 }
00273 
00274 void put_real_vector(ostream & s, char * label, vector v)
00275 {
00276     int old_precision = set_starlab_precision(s);
00277 
00278 #ifndef BAD_GNU_IO
00279     s << label << v << endl;
00280 #else
00281     if (s == cout){
00282         static char format[40];
00283         static int local_precision = -1;
00284         int precision = get_starlab_precision();
00285         if (local_precision != precision) {
00286             local_precision = precision;
00287             int p = precision;
00288             if (p < 0) p = 5;
00289             sprintf(format,"%%s%%.%dg %%.%dg %%.%dg\n", p, p, p);
00290         }
00291         fprintf(stdout, format, label, v[0], v[1], v[2]);
00292     } else
00293         s << label << v << endl;
00294 #endif
00295 
00296     // Restore the current precision.
00297 
00298     if (get_starlab_precision() != old_precision)
00299         s.precision(old_precision);
00300 }
00301 
00302 void put_integer(ostream & s, char * label, int i)
00303 {
00304 #ifndef BAD_GNU_IO
00305     s << label << i << endl;
00306 #else
00307     if (s == cout)
00308         fprintf(stdout, "%s%d\n", label, i);
00309     else
00310         s << label << i << endl;
00311 #endif
00312 }
00313 
00314 void put_string(ostream & s, char * label, char * str)
00315 {
00316 #ifndef BAD_GNU_IO
00317     s << label << str << endl;
00318 #else
00319     if (s == cout) 
00320         fprintf(stdout, "%s%s\n", label, str);
00321     else
00322         s << label << str << endl;
00323 #endif
00324 }

Generated at Sun Feb 24 09:57:20 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001