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