00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "starlab_vector.h"
00014 #include "util_io.h"
00015 #include "story.h"
00016 #include <ctype.h>
00017
00018 #undef isalnum
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
00047 char input_line[MAX_INPUT_LINE_LENGTH];
00048 while (! get_line(s,input_line) && !s.eof())
00049 {
00050
00051
00052
00053
00054
00055
00056
00057
00058 int shhh = 1;
00059 if (shhh)
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
00081
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
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
00145
00146 char *sp, *ep;
00147 long long i = STRTOL(val, &sp, 10);
00148 unsigned long long f = STRTOUL(sp, &ep, 10);
00149
00150 if (sp == ep) {
00151
00152
00153
00154
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
00172
00173 return (xreal)strtod( val, NULL );
00174
00175 #endif
00176 }
00177
00178
00179
00180
00181
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
00241
00242
00243
00244
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
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
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 }