Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
scatch.icc
Go to the documentation of this file.
1#ifndef _RHEOLEF_SCATCH_ICC
2#define _RHEOLEF_SCATCH_ICC
23// utility included by rheostream.cc
24// and shared by msh2geo.cc bamg2geo.cc and others utilities
25// => avoid code redundancies
26#include <iostream>
27#include<sys/stat.h> // stat()
28
29namespace rheolef {
30using namespace std;
31
33bool
34file_exists (const string& filename)
35{
36 struct stat s;
37 if (stat(filename.c_str(), &s) != 0) {
38 return false;
39 }
40 return true;
41}
43bool
44scatch (istream& in, const string& ch, bool full_match)
45{
46 // null string case
47 unsigned int l = ch.length();
48 if (l == 0) return true;
49
50 // check file
51 char c = '\0';
52 unsigned int state = 0;
53 const char *p = ch.c_str();
54 do {
55 in.get(c);
56 if (*p == '\n') {
57 // begining of stream <==> begining of a line, e.g.
58 // we look at "\nfield" while file starts
59 // with string "field"; it's ok
60 state++;
61 p++;
62 }
63 do {
64
65 if (*p == c) {
66 // advance in the string
67 state++;
68 p++;
69 } else if (state != 0 && ch[0] == c) {
70 // come back to the second position
71 state = 1;
72 p = ch.c_str() + 1;
73 } else if (state != 0) {
74 // come back to the begining of the string
75 state = 0;
76 p = ch.c_str();
77 }
78 }
79 while (state < l && in.get(c) && in.good());
80 // here: either state == l or end-of-file is reached
81 if (!full_match) return (state == l);
82 if (state != l) return false; // reaches end-of-file whithout finding the string
83 // here: state == l and we want a full match: check also that next char is a space, tab, end-of-line, or end-of-file
84 // => otherwise ambiguity with scatch("u") that reaches either "uh" or "u_exact"...!
85 c = in.peek();
86 if (!c || !in.good()) return true; // end-of-file just after the string: ok...
87 if (isspace(c)) return true; // nice! the expected situation
88 } while (true);
89 // stops when reaching either the string or end-of-file: the next statement is not reached
90 return false;
91}
92
93}// namespace rheolef
94#endif // _RHEOLEF_SCATCH_ICC
This file is part of Rheolef.
bool scatch(std::istream &in, const std::string &ch, bool full_match=true)
scatch: see the rheostream page for the full documentation
Definition scatch.icc:44
bool file_exists(const std::string &filename)
file_exists: see the rheostream page for the full documentation
Definition scatch.icc:34
STL namespace.
Definition sphere.icc:25