forked from ekapadi/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_util.cpp
130 lines (111 loc) · 4.06 KB
/
scanner_util.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// $Source: /usr/data0/leipzig_work/tmat_cvs/src/scanner_util.cpp,v $
/* *********************************************************************** */
/* */
/* Copyright (C) 2010 Kort Travis */
/* */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU Lesser General Public License as */
/* published by the Free Software Foundation; version 2.1 of the License. */
/* */
/* This program 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 Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, */
/* USA. */
/* */
/* *********************************************************************** */
#include <portability.h>
#if defined(__USE_PTHREAD)
#include <pthread.h>
#endif
#if defined(__USE_MPI)
#include <mpi.h>
#endif
#include <typeinfo>
#include <iostream>
#include <iomanip>
#include <complex>
#include <string>
#include <sstream>
#include <list>
// #include <functional>
// #include <algorithm>
// #include <numeric>
#if defined(__PGI)
// TACC port: see discussion in "portability.h" at definition of _STL_EXT_NAMESPACE_ macro:
// #include <slist>
#include <hash_map>
#else
// #include <ext/algorithm>
// #include <ext/numeric>
// #include <ext/slist>
#include <ext/hash_map>
#endif
#include <scanner_util.h>
namespace scanner_util{
/*
* command-line arguments as single string (excluding argv[0]):
*/
void argument_string(std::string& s, int argc, const char* argv[])
{
s.clear();
for(int n = 1; n < argc; ++n){
if (n != 1)
s += ' ';
s += argv[n];
}
}
// load all available contents of a std::istream to a std::string:
// helper method (based on a utility from boost::regex examples)
void load_from_stream(std::string& s, std::istream& is)
{
s.erase();
s.reserve(is.rdbuf()->in_avail()); // if this doesn't work, gradually increase capacity (below)
char c;
while(is.get(c))
{
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}
// load contents from a stream to a string:
// contents are delimited by start-delim, end-delim
// leading whitespace is ignored
void load_group_from_stream(std::string& s, std::istream& is, char ldelim, char rdelim)
{
using std::isspace;
// allow a failed read without destroying stream contents
std::streampos savePos(is.tellg());
s.erase();
#if 0 // ------- assume that a group is relatively small in comparison to the entire input file: ------------
s.reserve(is.rdbuf()->in_avail()); // if this doesn't work, gradually increase capacity (below)
#endif // -------
char c;
while(is.get(c) && isspace(c));
int nesting_level(0);
if (c == ldelim){
do{
if (c == ldelim)
++nesting_level;
else
if (c == rdelim)
--nesting_level;
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
if (nesting_level == 0)
break;
} while(is.get(c));
}
else{
is.setstate(std::ios_base::failbit); // return status in the stream
is.seekg(savePos, std::ios::beg); // restore entry position
}
}
} // namespace scanner_util