src/parser.c

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /*
00021   Parser for Parameter files and command line arguments
00022   Copyright (C) 2005 Herve Jegou
00023 */
00024 
00025 #include <it/io.h>
00026 #include <it/parser.h>
00027 #include <stdlib.h>
00028 
00029 /* Where the display the output of the parser */
00030 #define parser_output stderr
00031 
00032 /*----------------------------------------------------------------------*/
00033 parser_t *_parser_add_string (parser_t * p, char *s)
00034 {
00035   int  line_len;
00036   char *next_endl;
00037   char *line;
00038 
00039   if (Vec_length (p)) {
00040     /* continue an existing parser multiline */
00041     line = Vec_head (p);
00042   }
00043   else {
00044     /* initialize the parser by creating a new parser multiline */
00045     Vec_push (p, (char *) malloc (sizeof (char)));
00046     line = Vec_head (p);
00047     line[0] = 0;
00048   }
00049 
00050   while (s != NULL) {
00051 
00052     /* skip spaces */
00053     while ((*s) && (*s == ' ' || *s == '\t' || *s == '\n' || *s == ';'))
00054       s++;
00055     if (*s == '\0')
00056       break;
00057 
00058     /* Search the segment to copy and check for commentaries */
00059     next_endl = strpbrk (s, ";\n#");
00060 
00061     if (next_endl == NULL) {
00062       line_len = strlen (s);
00063       next_endl = s + strlen (s);
00064     }
00065     else {
00066       if (*next_endl == ';')
00067   next_endl++;
00068 
00069       line_len = (int) (next_endl - s) / sizeof (char);
00070       if (*next_endl == '#')
00071   next_endl = strchr (s, '\n'); /* Search the true end of line */
00072     }
00073 
00074     /* skip empty lines */
00075     if (!line_len) {
00076       s = next_endl;
00077       continue;
00078     }
00079 
00080     /* Add the text line to the parser */
00081     if (strchr (s, '=') && strchr (s, '=') < next_endl) {
00082       /* create a new multiline */
00083       Vec_push (p, (char *) malloc ((line_len + 2) * sizeof (char)));
00084       line = Vec_head (p);
00085       strncpy (line, s, line_len);
00086       line[line_len + 1] = 0;
00087       line[line_len] = '\n';
00088     }
00089     else {
00090       /* add to an existing multiline */
00091       line = Vec_head (p);
00092       line = (char *) realloc (line,
00093              (strlen (line) + line_len +
00094         2) * sizeof (char));
00095       strncat (line, s, line_len);
00096       line[strlen (line) + 1] = 0;
00097       line[strlen (line)] = '\n';
00098     }
00099     Vec_head (p) = line;
00100 
00101     /* go to the next text line */
00102     s = next_endl;
00103   }
00104 
00105   return p;
00106 }
00107 
00108 
00109 /*----------------------------------------------------------------------*/
00110 parser_t *_parser_add_params (parser_t * p, int argc, char **argv)
00111 {
00112   int  i;
00113   char *line;
00114   for (i = 1; i < argc; i++) {
00115     line = (char *) malloc (strlen (argv[i]) + 1);
00116     strcpy (line, argv[i]);
00117     Vec_push (p, line);
00118   }
00119   return p;
00120 }
00121 
00122 
00123 /*----------------------------------------------------------------------*/
00124 parser_t *_parser_add_file (parser_t * p, const char *filename)
00125 {
00126   FILE *F;
00127   char *Fstring;
00128   int  Fsize;
00129 
00130   F = fopen (filename, "r+b");
00131   if (F == NULL) {
00132     it_warning ("Unable to open parameter file %s\n", filename);
00133     return p;
00134   }
00135 
00136   fseek (F, 0, SEEK_END);
00137   Fsize = ftell (F);
00138   fseek (F, 0, SEEK_SET);
00139 
00140   Fstring = (char *) malloc (Fsize + 1);
00141   fread (Fstring, Fsize, 1, F);
00142 
00143   /* replace ; with \n to handle multiple variables per line */
00144 
00145   p = _parser_add_string (p, Fstring);
00146   free (Fstring);
00147   return p;
00148 }
00149 
00150 
00151 /*----------------------------------------------------------------------*/
00152 parser_t *parser_init (int argc, char **argv,
00153            const char *filename, char *cmdline)
00154 {
00155   parser_t *p = Vec_new (char *, 0);
00156 
00157   if (argv != NULL && argc > 0)
00158     p = _parser_add_params (p, argc, argv);
00159 
00160   if (filename != NULL)
00161     p = _parser_add_file (p, filename);
00162 
00163   if (cmdline != NULL)
00164     p = _parser_add_string (p, cmdline);
00165 
00166   return p;
00167 }
00168 
00169 
00170 /*----------------------------------------------------------------------*/
00171 void parser_delete (parser_t * p)
00172 {
00173   idx_t i;
00174   assert (p);
00175   for (i = 0; i < Vec_length (p); i++)
00176     free (p[i]);
00177   Vec_delete (p);
00178 }
00179 
00180 /*----------------------------------------------------------------------*/
00181 void parser_print (parser_t * p)
00182 {
00183   idx_t line;
00184   for (line = 0; line < Vec_length (p); line++)
00185     printf ("%s", p[line]);
00186 
00187 }
00188 
00189 
00190 /*----------------------------------------------------------------------*/
00191 /* Return the line of the parser which contains the variable name       */
00192 char *parser_var_line (const parser_t * p, const char *varname)
00193 {
00194   idx_t line;
00195   int  found = -1;
00196   unsigned int n;
00197 
00198   for (line = 0; line < Vec_length (p); line++) {
00199     char const *end = strpbrk (p[line], " \t\n=");
00200     if (end)
00201       n = end - p[line];
00202     else
00203       n = strlen (p[line]);
00204     if (n == strlen (varname) && strncmp (p[line], varname, n) == 0) {
00205       found = line;
00206       break;
00207     }
00208   }
00209 
00210   if (found == -1)
00211     return NULL;
00212   else
00213     return p[line];
00214 }
00215 
00216 
00217 /*----------------------------------------------------------------------*/
00218 int parser_exists (const parser_t * p, const char *varname)
00219 {
00220   if (parser_var_line (p, varname) == NULL)
00221     return 0;
00222   return 1;
00223 }
00224 
00225 
00226 /*----------------------------------------------------------------------*/
00227 int parser_get_int (const parser_t * p, const char *varname)
00228 {
00229   int  return_code;
00230   int  r;
00231   char *line, *s;
00232   line = parser_var_line (p, varname);
00233 
00234   while (1) {
00235     if (line == NULL)
00236       break;
00237 
00238     s = strpbrk (line, "=");
00239     if (s == NULL)
00240       break;
00241     s++;
00242 
00243     return_code = sscanf (s, "%d", &r);
00244     if (return_code != 1)
00245       break;
00246 
00247     /* Search if the value has to be displayed on the standard output */
00248     if (strpbrk (s, ";") == NULL) /* Yes */
00249       fprintf (parser_output, "%s = %d\n", varname, r);
00250 
00251     return r;
00252   }
00253 
00254   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00255   return 0;
00256 }
00257 
00258 
00259 /*----------------------------------------------------------------------*/
00260 int parser_get_int_verbose (const parser_t * p, const char *varname,
00261           int verbose)
00262 {
00263   int  return_code;
00264   int  r;
00265   char *line, *s;
00266   line = parser_var_line (p, varname);
00267 
00268   while (1) {
00269     if (line == NULL)
00270       break;
00271 
00272     s = strpbrk (line, "=");
00273     if (s == NULL)
00274       break;
00275     s++;
00276 
00277     return_code = sscanf (s, "%d", &r);
00278     if (return_code != 1)
00279       break;
00280 
00281     /* Search if the value has to be displayed on the standard output */
00282     if (verbose)    /* Yes */
00283       fprintf (parser_output, "%s = %d\n", varname, r);
00284 
00285     return r;
00286   }
00287 
00288   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00289   return 0;
00290 }
00291 
00292 
00293 /*----------------------------------------------------------------------*/
00294 double parser_get_double (const parser_t * p, const char *varname)
00295 {
00296   double r;
00297   int  return_code;
00298   char *line, *s;
00299   line = parser_var_line (p, varname);
00300 
00301   while (1) {
00302     if (line == NULL)
00303       break;
00304 
00305     s = strpbrk (line, "=");
00306     if (s == NULL)
00307       break;
00308     s++;
00309 
00310     return_code = sscanf (s, "%lg", &r);
00311     if (return_code != 1)
00312       break;
00313 
00314     /* Search if the value has to be displayed on the standard output */
00315     if (strpbrk (s, ";") == NULL) /* Yes */
00316       fprintf (parser_output, "%s = %g\n", varname, r);
00317 
00318     return r;
00319   }
00320 
00321   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00322   return 0.;
00323 }
00324 
00325 
00326 /*----------------------------------------------------------------------*/
00327 double parser_get_double_verbose (const parser_t * p, const char *varname,
00328           int verbose)
00329 {
00330   double r;
00331   int  return_code;
00332   char *line, *s;
00333   line = parser_var_line (p, varname);
00334 
00335   while (1) {
00336     if (line == NULL)
00337       break;
00338 
00339     s = strpbrk (line, "=");
00340     if (s == NULL)
00341       break;
00342     s++;
00343 
00344     return_code = sscanf (s, "%lg", &r);
00345     if (return_code != 1)
00346       break;
00347 
00348     /* Search if the value has to be displayed on the standard output */
00349     if (verbose)    /* Yes */
00350       fprintf (parser_output, "%s = %g\n", varname, r);
00351 
00352     return r;
00353   }
00354 
00355   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00356   return 0.;
00357 }
00358 
00359 
00360 /*----------------------------------------------------------------------*/
00361 byte parser_get_byte (const parser_t * p, const char *varname)
00362 {
00363   int  return_code;
00364   int  r;
00365   char *line, *s;
00366   line = parser_var_line (p, varname);
00367 
00368   while (1) {
00369     if (line == NULL)
00370       break;
00371 
00372     s = strpbrk (line, "=");
00373     if (s == NULL)
00374       break;
00375     s++;
00376 
00377     return_code = sscanf (s, "%d", &r);
00378     if (return_code != 1)
00379       break;
00380 
00381     if (r > 255 || r < 0)
00382       it_warning ("Parameter byte " "%s" " has been rounded\n", varname);
00383 
00384     /* Search if the value has to be displayed on the standard output */
00385     if (strpbrk (s, ";") == NULL) /* Yes */
00386       fprintf (parser_output, "%s = %d\n", varname, (int) (byte) r);
00387 
00388     return (byte) r;
00389   }
00390 
00391   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00392   return 0;
00393 }
00394 
00395 
00396 /*----------------------------------------------------------------------*/
00397 byte parser_get_byte_verbose (const parser_t * p, const char *varname,
00398             int verbose)
00399 {
00400   int  return_code;
00401   int  r;
00402   char *line, *s;
00403   line = parser_var_line (p, varname);
00404 
00405   while (1) {
00406     if (line == NULL)
00407       break;
00408 
00409     s = strpbrk (line, "=");
00410     if (s == NULL)
00411       break;
00412     s++;
00413 
00414     return_code = sscanf (s, "%d", &r);
00415     if (return_code != 1)
00416       break;
00417 
00418     if (r > 255 || r < 0)
00419       it_warning ("Parameter byte " "%s" " has been rounded\n", varname);
00420 
00421     /* Search if the value has to be displayed on the standard output */
00422     if (verbose)    /* Yes */
00423       fprintf (parser_output, "%s = %d\n", varname, (int) (byte) r);
00424 
00425     return (byte) r;
00426   }
00427 
00428   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00429   return 0;
00430 }
00431 
00432 
00433 /*----------------------------------------------------------------------*/
00434 char *parser_get_string (const parser_t * p, const char *varname)
00435 {
00436   int  rs_len;
00437   int  doublequote = 1;
00438   char *rs;
00439   char *line, *s, *s_end, *s_noquote;
00440   line = parser_var_line (p, varname);
00441 
00442   while (1) {
00443     if (line == NULL)
00444       break;
00445 
00446     s = strpbrk (line, "=");
00447     if (s == NULL)
00448       break;
00449 
00450     /* Two cases: string is between quota or not */
00451     s_noquote = s;
00452     s = strpbrk (s, "\"");
00453 
00454     if (s == NULL) {    /* Assume at this point that there is no double-quote */
00455       doublequote = 0;
00456       s = s_noquote + 1;
00457     }
00458     else
00459       s++;
00460 
00461     if (doublequote) {
00462       s_end = strpbrk (s, "\"");
00463       if (s_end == NULL)
00464   break;
00465     }
00466     else {      /* No double quote. Stop on space, tab or \n */
00467       s_end = strpbrk (s, " \t\n");
00468       if (s_end == NULL)
00469   s_end = s + strlen (s);
00470     }
00471 
00472     rs_len = (int) (s_end - s);
00473 
00474     /* Violent malloc */
00475     rs = (char *) malloc (rs_len + 1);
00476     strncpy (rs, s, rs_len);
00477     rs[rs_len] = '\0';
00478 
00479     /* Search if the value has to be displayed on the standard output */
00480     if (strpbrk (s_end, ";") == NULL) /* Yes */
00481       fprintf (parser_output, "%s = %s\n", varname, rs);
00482 
00483     return rs;
00484   }
00485 
00486   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00487   return 0;
00488 }
00489 
00490 
00491 /*----------------------------------------------------------------------*/
00492 char *parser_get_string_verbose (const parser_t * p, const char *varname,
00493          int verbose)
00494 {
00495   int  rs_len;
00496   char *rs;
00497   char *line, *s, *s_end, *s_noquote;
00498   int  doublequote = 1;
00499   line = parser_var_line (p, varname);
00500 
00501   while (1) {
00502     if (line == NULL)
00503       break;
00504 
00505     s = strpbrk (line, "=");
00506     if (s == NULL)
00507       break;
00508 
00509     /* Two cases: string is between quota or not */
00510     s_noquote = s;
00511     s = strpbrk (s, "\"");
00512 
00513     if (s == NULL) {    /* Assume at this point that there is no double-quote */
00514       doublequote = 0;
00515       s = s_noquote + 1;
00516     }
00517     else
00518       s++;
00519 
00520     if (doublequote) {
00521       s_end = strpbrk (s, "\"");
00522       if (s_end == NULL)
00523   break;
00524     }
00525     else {      /* No double quote. Stop on space, tab or \n */
00526       s_end = strpbrk (s, " \t\n");
00527       if (s_end == NULL)
00528   s_end = s + strlen (s);
00529     }
00530 
00531     rs_len = (int) (s_end - s);
00532 
00533     /* Violent malloc */
00534     rs = (char *) malloc (rs_len + 1);
00535     strncpy (rs, s, rs_len);
00536     rs[rs_len] = '\0';
00537 
00538     /* Search if the value has to be displayed on the standard output */
00539     if (verbose)    /* Yes */
00540       fprintf (parser_output, "%s = %s\n", varname, rs);
00541 
00542     return rs;
00543   }
00544 
00545   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00546   return 0;
00547 }
00548 
00549 
00550 /*----------------------------------------------------------------------*/
00551 cplx parser_get_cplx (const parser_t * p, const char *varname)
00552 {
00553   cplx c;
00554   char *line, *s;
00555   line = parser_var_line (p, varname);
00556 
00557   while (1) {
00558     if (line == NULL)
00559       break;
00560 
00561     s = strpbrk (line, "=");
00562     if (s == NULL)
00563       break;
00564 
00565     s = it_read_cplx (s, &c);
00566 
00567     /* Search if the vector has to be displayed on the standard output */
00568     if (strpbrk (s, ";") == NULL) /* Yes */
00569       it_fprintf (parser_output, "%s = %z\n", varname, c);
00570 
00571     return c;
00572   }
00573 
00574   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00575   return cplx_0;
00576 }
00577 
00578 
00579 /*----------------------------------------------------------------------*/
00580 cplx parser_get_cplx_verbose (const parser_t * p, const char *varname,
00581             int verbose)
00582 {
00583   cplx c;
00584   char *line, *s;
00585   line = parser_var_line (p, varname);
00586 
00587   while (1) {
00588     if (line == NULL)
00589       break;
00590 
00591     s = strpbrk (line, "=");
00592     if (s == NULL)
00593       break;
00594 
00595     s = it_read_cplx (s, &c);
00596 
00597     if (verbose)    /* Yes */
00598       it_fprintf (parser_output, "%s = %z\n", varname, c);
00599 
00600     return c;
00601   }
00602 
00603   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00604   return cplx_0;
00605 }
00606 
00607 
00608 /*----------------------------------------------------------------------*/
00609 vec parser_get_vec (const parser_t * p, const char *varname)
00610 {
00611   vec  v = vec_new_alloc (0, 10);
00612   char *line, *s;
00613   line = parser_var_line (p, varname);
00614 
00615   while (1) {
00616     if (line == NULL)
00617       break;
00618 
00619     s = strpbrk (line, "=");
00620     if (s == NULL)
00621       break;
00622 
00623     s = it_read_vec (s, &v);
00624     if (!s)
00625       it_error ("Unable to read a vector of double in string %s\n", s);
00626 
00627     /* Search if the vector has to be displayed on the standard output */
00628     if (strpbrk (s, ";") == NULL) /* Yes */
00629       it_fprintf (parser_output, "%s = $v\n", varname, v);
00630 
00631     return v;
00632   }
00633 
00634   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00635   return 0;
00636 }
00637 
00638 
00639 /*----------------------------------------------------------------------*/
00640 vec parser_get_vec_verbose (const parser_t * p, const char *varname,
00641           int verbose)
00642 {
00643   vec  v = vec_new_alloc (0, 10);
00644   char *line, *s;
00645   line = parser_var_line (p, varname);
00646 
00647   while (1) {
00648     if (line == NULL)
00649       break;
00650 
00651     s = strpbrk (line, "=");
00652     if (s == NULL)
00653       break;
00654 
00655     s = it_read_vec (s, &v);
00656     if (!s)
00657       it_error ("Unable to read a vector of double in string %s\n", s);
00658 
00659     /* Search if the vector has to be displayed on the standard output */
00660     if (verbose)    /* Yes */
00661       it_fprintf (parser_output, "%s = $v\n", varname, v);
00662 
00663     return v;
00664   }
00665 
00666   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00667   return 0;
00668 }
00669 
00670 
00671 /*----------------------------------------------------------------------*/
00672 cvec parser_get_cvec (const parser_t * p, const char *varname)
00673 {
00674   cvec v = cvec_new_alloc (0, 10);
00675   char *line, *s;
00676   line = parser_var_line (p, varname);
00677 
00678   while (1) {
00679     if (line == NULL)
00680       break;
00681 
00682     s = strpbrk (line, "=");
00683     if (s == NULL)
00684       break;
00685 
00686     s = it_read_cvec (s, &v);
00687     if (!s)
00688       it_error ("Unable to read a vector of complexes in string %s\n", s);
00689 
00690     /* Search if the vector has to be displayed on the standard output */
00691     if (strpbrk (s, ";") == NULL) /* Yes */
00692       it_fprintf (parser_output, "%s = $z\n", varname, v);
00693 
00694     return v;
00695   }
00696 
00697   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00698   return v;
00699 }
00700 
00701 
00702 /*----------------------------------------------------------------------*/
00703 cvec parser_get_cvec_verbose (const parser_t * p, const char *varname,
00704             int verbose)
00705 {
00706   cvec v = cvec_new_alloc (0, 10);
00707   char *line, *s;
00708   line = parser_var_line (p, varname);
00709 
00710   while (1) {
00711     if (line == NULL)
00712       break;
00713 
00714     s = strpbrk (line, "=");
00715     if (s == NULL)
00716       break;
00717 
00718     s = it_read_cvec (s, &v);
00719     if (!s)
00720       it_error ("Unable to read a vector of complexes in string %s\n", s);
00721 
00722     /* Search if the vector has to be displayed on the standard output */
00723     if (verbose)    /* Yes */
00724       it_fprintf (parser_output, "%s = $z\n", varname, v);
00725 
00726     return v;
00727   }
00728 
00729   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00730   return v;
00731 }
00732 
00733 
00734 /*----------------------------------------------------------------------*/
00735 ivec parser_get_ivec (const parser_t * p, const char *varname)
00736 {
00737   ivec v = ivec_new_alloc (0, 10);
00738   char *line, *s;
00739   line = parser_var_line (p, varname);
00740 
00741   while (1) {
00742     if (line == NULL)
00743       break;
00744 
00745     s = strpbrk (line, "=");
00746     if (s == NULL)
00747       break;
00748 
00749     s = it_read_ivec (s, &v);
00750     if (!s)
00751       it_error ("Unable to read a vector of integers in string %s\n", s);
00752 
00753     /* Search if the vector has to be displayed on the standard output */
00754     if (strpbrk (s, ";") == NULL) /* Yes */
00755       it_fprintf (parser_output, "%s = $d\n", varname, v);
00756 
00757     return v;
00758   }
00759 
00760   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00761   return 0;
00762 }
00763 
00764 
00765 /*----------------------------------------------------------------------*/
00766 ivec parser_get_ivec_verbose (const parser_t * p, const char *varname,
00767             int verbose)
00768 {
00769   ivec v = ivec_new_alloc (0, 10);
00770   char *line, *s;
00771   line = parser_var_line (p, varname);
00772 
00773   while (1) {
00774     if (line == NULL)
00775       break;
00776 
00777     s = strpbrk (line, "=");
00778     if (s == NULL)
00779       break;
00780 
00781     s = it_read_ivec (s, &v);
00782     if (!s)
00783       it_error ("Unable to read a vector of integers in string %s\n", s);
00784 
00785     /* Search if the vector has to be displayed on the standard output */
00786     if (verbose)    /* Yes */
00787       it_fprintf (parser_output, "%s = $d\n", varname, v);
00788 
00789     return v;
00790   }
00791 
00792   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00793   return 0;
00794 }
00795 
00796 
00797 /*----------------------------------------------------------------------*/
00798 bvec parser_get_bvec (const parser_t * p, const char *varname)
00799 {
00800   bvec v = bvec_new_alloc (0, 10);
00801   char *line, *s;
00802   line = parser_var_line (p, varname);
00803 
00804   while (1) {
00805     if (line == NULL)
00806       break;
00807 
00808     s = strpbrk (line, "=");
00809     if (s == NULL)
00810       break;
00811 
00812     s = it_read_bvec (s, &v);
00813     if (!s)
00814       it_error ("Unable to read a vector of bytes in string %s\n", s);
00815 
00816     /* Search if the vector has to be displayed on the standard output */
00817     if (strpbrk (s, ";") == NULL) /* Yes */
00818       it_fprintf (parser_output, "%s = $b\n", varname, v);
00819 
00820     return v;
00821   }
00822 
00823   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00824   return 0;
00825 }
00826 
00827 
00828 /*----------------------------------------------------------------------*/
00829 bvec parser_get_bvec_verbose (const parser_t * p, const char *varname,
00830             int verbose)
00831 {
00832   bvec v = bvec_new_alloc (0, 10);
00833   char *line, *s;
00834   line = parser_var_line (p, varname);
00835 
00836   while (1) {
00837     if (line == NULL)
00838       break;
00839 
00840     s = strpbrk (line, "=");
00841     if (s == NULL)
00842       break;
00843 
00844     s = it_read_bvec (s, &v);
00845     if (!s)
00846       it_error ("Unable to read a vector of bytes in string %s\n", s);
00847 
00848     /* Search if the vector has to be displayed on the standard output */
00849     if (verbose)    /* Yes */
00850       it_fprintf (parser_output, "%s = $b\n", varname, v);
00851 
00852     return v;
00853   }
00854 
00855   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00856   return 0;
00857 }
00858 
00859 
00860 /*----------------------------------------------------------------------*/
00861 mat parser_get_mat (const parser_t * p, const char *varname)
00862 {
00863   mat  m = mat_new_alloc (0, 0, 10, 10);
00864   char *line, *s;
00865   line = parser_var_line (p, varname);
00866 
00867   while (1) {
00868     if (line == NULL)
00869       break;
00870 
00871     s = strpbrk (line, "=");
00872     if (s == NULL)
00873       break;
00874 
00875 
00876     s = it_read_mat (s, &m);
00877     if (!s)
00878       it_error ("Unable to read a matrix of double in string %s\n", s);
00879 
00880     if (strpbrk (s, ";") == NULL) /* Yes */
00881       it_fprintf (parser_output, "%s = #m\n", varname, m);
00882 
00883     return m;
00884   }
00885 
00886   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00887   return 0;
00888 }
00889 
00890 
00891 /*----------------------------------------------------------------------*/
00892 mat parser_get_mat_verbose (const parser_t * p, const char *varname,
00893           int verbose)
00894 {
00895   mat  m = mat_new_alloc (0, 0, 10, 10);
00896   char *line, *s;
00897   line = parser_var_line (p, varname);
00898 
00899   while (1) {
00900     if (line == NULL)
00901       break;
00902 
00903     s = strpbrk (line, "=");
00904     if (s == NULL)
00905       break;
00906 
00907 
00908     s = it_read_mat (s, &m);
00909     if (!s)
00910       it_error ("Unable to read a matrix of double in string %s\n", s);
00911 
00912     if (verbose)    /* Yes */
00913       it_fprintf (parser_output, "%s = #m\n", varname, m);
00914 
00915     return m;
00916   }
00917 
00918   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00919   return 0;
00920 }
00921 
00922 
00923 /*----------------------------------------------------------------------*/
00924 cmat parser_get_cmat (const parser_t * p, const char *varname)
00925 {
00926   cmat m = cmat_new_alloc (0, 0, 10, 10);
00927   char *line, *s;
00928   line = parser_var_line (p, varname);
00929 
00930   while (1) {
00931     if (line == NULL)
00932       break;
00933 
00934     s = strpbrk (line, "=");
00935     if (s == NULL)
00936       break;
00937 
00938 
00939     s = it_read_cmat (s, &m);
00940     if (!s)
00941       it_error ("Unable to read a matrix of complexes in string %s\n", s);
00942 
00943     /* Search if the vector has to be displayed on the standard output */
00944     if (strpbrk (s, ";") == NULL) /* Yes */
00945       it_fprintf (parser_output, "%s = #.3z\n", varname, m);
00946 
00947     return m;
00948   }
00949 
00950   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00951   return 0;
00952 }
00953 
00954 
00955 /*----------------------------------------------------------------------*/
00956 cmat parser_get_cmat_verbose (const parser_t * p, const char *varname,
00957             int verbose)
00958 {
00959   cmat m = cmat_new_alloc (0, 0, 10, 10);
00960   char *line, *s;
00961   line = parser_var_line (p, varname);
00962 
00963   while (1) {
00964     if (line == NULL)
00965       break;
00966 
00967     s = strpbrk (line, "=");
00968     if (s == NULL)
00969       break;
00970 
00971 
00972     s = it_read_cmat (s, &m);
00973     if (!s)
00974       it_error ("Unable to read a matrix of complexes in string %s\n", s);
00975 
00976     /* Search if the matrix has to be displayed on the standard output */
00977     if (verbose)    /* Yes */
00978       it_fprintf (parser_output, "%s = #.3z\n", varname, m);
00979 
00980     return m;
00981   }
00982 
00983   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
00984   return 0;
00985 }
00986 
00987 
00988 /*----------------------------------------------------------------------*/
00989 imat parser_get_imat (const parser_t * p, const char *varname)
00990 {
00991   imat m = imat_new_alloc (0, 0, 10, 10);
00992   char *line, *s;
00993   line = parser_var_line (p, varname);
00994 
00995   while (1) {
00996     if (line == NULL)
00997       break;
00998 
00999     s = strpbrk (line, "=");
01000     if (s == NULL)
01001       break;
01002 
01003 
01004     s = it_read_imat (s, &m);
01005     if (!s)
01006       it_error ("Unable to read a matrix of double in string %s\n", s);
01007 
01008     /* Search if the vector has to be displayed on the standard output */
01009     if (strpbrk (s, ";") == NULL) /* Yes */
01010       it_fprintf (parser_output, "%s = #d\n", varname, m);
01011 
01012     return m;
01013   }
01014 
01015   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
01016   return 0;
01017 }
01018 
01019 
01020 /*----------------------------------------------------------------------*/
01021 imat parser_get_imat_verbose (const parser_t * p, const char *varname,
01022             int verbose)
01023 {
01024   imat m = imat_new_alloc (0, 0, 10, 10);
01025   char *line, *s;
01026   line = parser_var_line (p, varname);
01027 
01028   while (1) {
01029     if (line == NULL)
01030       break;
01031 
01032     s = strpbrk (line, "=");
01033     if (s == NULL)
01034       break;
01035 
01036 
01037     s = it_read_imat (s, &m);
01038     if (!s)
01039       it_error ("Unable to read a matrix of double in string %s\n", s);
01040 
01041     /* Search if the matrix has to be displayed on the standard output */
01042     if (verbose)    /* Yes */
01043       it_fprintf (parser_output, "%s = #d\n", varname, m);
01044 
01045     return m;
01046   }
01047 
01048   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
01049   return 0;
01050 }
01051 
01052 
01053 /*----------------------------------------------------------------------*/
01054 bmat parser_get_bmat (const parser_t * p, const char *varname)
01055 {
01056   bmat m = bmat_new_alloc (0, 0, 10, 10);
01057   char *line, *s;
01058   line = parser_var_line (p, varname);
01059 
01060   while (1) {
01061     if (line == NULL)
01062       break;
01063 
01064     s = strpbrk (line, "=");
01065     if (s == NULL)
01066       break;
01067 
01068 
01069     s = it_read_bmat (s, &m);
01070     if (!s)
01071       it_error ("Unable to read a matrix of double in string %s\n", s);
01072 
01073     /* Search if the vector has to be displayed on the standard output */
01074     if (strpbrk (s, ";") == NULL) /* Yes */
01075       it_fprintf (parser_output, "%s = #d\n", varname, m);
01076 
01077     return m;
01078   }
01079 
01080   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
01081   return 0;
01082 }
01083 
01084 
01085 /*----------------------------------------------------------------------*/
01086 bmat parser_get_bmat_verbose (const parser_t * p, const char *varname,
01087             int verbose)
01088 {
01089   bmat m = bmat_new_alloc (0, 0, 10, 10);
01090   char *line, *s;
01091   line = parser_var_line (p, varname);
01092 
01093   while (1) {
01094     if (line == NULL)
01095       break;
01096 
01097     s = strpbrk (line, "=");
01098     if (s == NULL)
01099       break;
01100 
01101 
01102     s = it_read_bmat (s, &m);
01103     if (!s)
01104       it_error ("Unable to read a matrix of double in string %s\n", s);
01105 
01106     /* Search if the matrix has to be displayed on the standard output */
01107     if (verbose)    /* Yes */
01108       it_fprintf (parser_output, "%s = #d\n", varname, m);
01109 
01110     return m;
01111   }
01112 
01113   it_warning ("Unable to retrieve variable " "%s" "\n", varname);
01114   return 0;
01115 }

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau