GetFEM  5.4.2
bgeot_ftool.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2000-2020 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file bgeot_ftool.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date March 09, 2000.
35  @brief "File Tools"
36 */
37 
38 #ifndef BGEOT_FTOOL_H
39 #define BGEOT_FTOOL_H
40 
41 #include <iostream>
42 #include <map>
43 #include <vector>
44 
45 namespace bgeot
46 {
47  /* ********************************************************************* */
48  /* Read a char string. */
49  /* ********************************************************************* */
50 
51  bool read_until(std::istream &ist, const char *st);
52 
53  /** Very simple lexical analysis of general interest for reading small
54  * languages with a "MATLAB like" syntax :
55  * spaces are ignored, '%' indicates a commentary until the end of the line,
56  * '...' indicates that the instruction continue on the next line
57  * (or separate two sub part of the same character string).
58  *
59  * The function returns
60  * 0 if there is nothing else to read in the file
61  * 1 if an end line has been found (st is empty in this case)
62  * 2 if a number as been read
63  * 3 if a string has been read
64  * 4 if a alphanumeric name has been read
65  * 5 for a one character symbol
66  * 6 for a two characters symbol (<=, >=, ==, !=, &&, ||)
67  *
68  * Note that when the end of line is not significant the option ignore_cr
69  * allows to consider the carriage return as a space character.
70  */
71  int get_token(std::istream &ist, std::string &st,
72  bool ignore_cr = true, bool to_up = true,
73  bool read_un_pm = true, int *linenb = 0);
74 
75  struct skip {
76  const char *s;
77  skip(const char *s_) : s(s_) {}
78  };
79  std::istream& operator>>(std::istream& is, const skip& t);
80 
81  /* ********************************************************************* */
82  /* Case-insensitive string operations */
83  /* ********************************************************************* */
84  int casecmp(const char *a, const char *b, unsigned n=unsigned(-1));
85 
86  inline int casecmp(const std::string& a, const char *b,
87  unsigned n=unsigned(-1))
88  { return casecmp(a.c_str(),b,n); }
89 
90  inline int casecmp(const std::string& a, const std::string& b,
91  unsigned n=unsigned(-1))
92  { return casecmp(a.c_str(), b.c_str(),n); }
93 
94  inline int casecmp(char a, char b)
95  { return toupper(a)<toupper(b) ? -1 : (toupper(a) == toupper(b) ? 0 : +1); }
96 
97  /* ********************************************************************* */
98  /* Read a parameter file. */
99  /* ********************************************************************* */
100 
101  // The associated language has approximatively the following grammar:
102  //
103  // 'instruction' := 'parameter_name' '=' 'expression';
104  // or 'if' 'expression'
105  // 'instruction_list'
106  // [ 'else' 'instruction_list' ]
107  // 'end'
108  // 'expression' := '[' 'expression' ',' ... ']'
109  // or 'parameter_name'
110  // or 'numeric_value'
111  // or 'character_string'
112  // or '(' 'expression' ')'
113  // or '+' 'expression'
114  // or '-' 'expression'
115  // or '!' 'expression'
116  // or 'expression' '+' 'expression'
117  // or 'expression' '-' 'expression'
118  // or 'expression' '*' 'expression'
119  // or 'expression' '/' 'expression'
120  // or 'expression' '||' 'expression'
121  // or 'expression' '&&' 'expression'
122  // or 'expression' '==' 'expression'
123  // or 'expression' '!=' 'expression'
124  // or 'expression' '<' 'expression'
125  // or 'expression' '>' 'expression'
126  // or 'expression' '<=' 'expression'
127  // or 'expression' '>=' 'expression'
128 
129  class md_param {
130  public :
131 
132  typedef enum { REAL_VALUE, STRING_VALUE, ARRAY_VALUE } param_type;
133 
134  class param_value {
135  param_type pt;
136  double real_value;
137  std::string string_value;
138  std::vector<param_value> array_value;
139 
140  public :
141  param_type type_of_param() const { return pt; }
142  double &real() { return real_value; }
143  double real() const { return real_value; }
144  std::string &string() { return string_value; }
145  const std::string &string() const { return string_value; }
146  std::vector<param_value> &array() { return array_value; }
147  const std::vector<param_value> &array() const { return array_value; }
148  param_value(double e = 0.0) : pt(REAL_VALUE), real_value(e) {}
149  param_value(std::string s) : pt(STRING_VALUE), real_value(0.0),
150  string_value(s) {}
151  param_value(char *s) : pt(STRING_VALUE), real_value(0.0),
152  string_value(s) {}
153  param_value(param_type p): pt(p), real_value(0.0) {}
154  };
155 
156  protected :
157 
158  std::map<std::string, param_value> parameters;
159  bool token_is_valid;
160  int current_line;
161  std::string current_file;
162 
163  int get_next_token(std::istream &f);
164  void valid_token();
165  std::string temp_string;
166  param_value read_expression_list(std::istream &f, bool skipped);
167  param_value read_expression(std::istream &f, bool skipped);
168  int read_instruction_list(std::istream &f, bool sk = false);
169  int read_instruction(std::istream &f, bool sk = false);
170  void parse_error(const std::string &t);
171  void syntax_error(const std::string &t);
172  void do_bin_op(std::vector<md_param::param_value> &value_list,
173  std::vector<int> &op_list, std::vector<int> &prior_list);
174 
175  public :
176 
177  double real_value(const std::string &name, const char *comment = 0,
178  double default_val=0.);
179  long int_value(const std::string &name, const char *comment = 0,
180  long default_val=0);
181  const std::string &string_value(const std::string &name,
182  const char *comment = 0,
183  const std::string &default_val="");
184  const std::vector<param_value> &array_value(const std::string &name,
185  const char *comment = 0);
186  void add_int_param(const std::string &name, long e)
187  { parameters[name] = param_value(double(e)); }
188  void add_real_param(const std::string &name, double e)
189  { parameters[name] = param_value(e); }
190  void add_string_param(const std::string &name, const std::string &s)
191  { parameters[name] = param_value(s); }
192 
193  // todo : add the access to the arrays
194 
195  void read_param_file(std::istream &f);
196 
197  /** Read the parameters on the command line. If a name is found the
198  * corresponding .param file is searched. If a -dNOM=VALUE is found
199  * (or -d NOM=VALUE), it is evaluated.
200  */
201  void read_command_line(int argc, char *argv[]);
202  };
203 
204 }
205 
206 
207 namespace ftool {
208 
209  // For compatibility with Getfem 2.0
210 
211  using bgeot::md_param;
212 
213 }
214 
215 #endif
bgeot::get_token
int get_token(std::istream &ist, std::string &st, bool ignore_cr, bool to_up, bool read_un_pm, int *linenb)
Very simple lexical analysis of general interest for reading small languages with a "MATLAB like" syn...
Definition: bgeot_ftool.cc:50
bgeot
Basic Geometric Tools.
Definition: bgeot_convex_ref.cc:27