File: scatch.icc

package info (click to toggle)
rheolef 7.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 88,076 kB
  • sloc: cpp: 110,259; sh: 16,733; makefile: 5,438; python: 1,391; yacc: 218; javascript: 203; xml: 191; awk: 61; sed: 5
file content (94 lines) | stat: -rw-r--r-- 3,150 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef _RHEOLEF_SCATCH_ICC
#define _RHEOLEF_SCATCH_ICC
///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// Rheolef is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
///
/// =========================================================================
// utility included by rheostream.cc
// and shared by msh2geo.cc bamg2geo.cc and others utilities
// => avoid code redundancies
#include <iostream>
#include<sys/stat.h> // stat()

namespace rheolef {
using namespace std;

//! @brief file_exists: see the @ref rheostream_7 page for the full documentation
bool
file_exists (const string& filename)
{
    struct stat s;
    if (stat(filename.c_str(), &s) != 0) {
        return false;
    }
    return true;
}
//! @brief scatch: see the @ref rheostream_7 page for the full documentation
bool 
scatch (istream& in, const string& ch, bool full_match)
{
  // null string case
  unsigned int l = ch.length();
  if (l == 0) return true;
  
  // check file
  char c = '\0';
  unsigned int state = 0;
  const char *p = ch.c_str();
  do {
    in.get(c);
    if (*p == '\n') {
	// begining of stream <==> begining of a line, e.g. 
	//   we look at "\nfield" while file starts 
	//   with string "field"; it's ok
	state++;
        p++;
    }
    do {
 
        if (*p == c) {
            // advance in the string
            state++;
            p++;
        } else if (state != 0 && ch[0] == c) {
            // come back to the second position
            state = 1;
            p = ch.c_str() + 1;
        } else if (state != 0) {
            // come back to the begining of the string
            state = 0;
            p = ch.c_str();
        }
    }
    while (state < l && in.get(c) && in.good());
    // here: either state == l or end-of-file is reached
    if (!full_match) return (state == l);
    if (state != l) return false; // reaches end-of-file whithout finding the string
    // 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
    // => otherwise ambiguity with scatch("u") that reaches either "uh" or "u_exact"...!
    c = in.peek();
    if (!c || !in.good()) return true; // end-of-file just after the string: ok...
    if (isspace(c)) return true; // nice! the expected situation
  } while (true);
  // stops when reaching either the string or end-of-file: the next statement is not reached
  return false;
}

}// namespace rheolef
#endif // _RHEOLEF_SCATCH_ICC