genericRagelLemonDriver.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
29 #include "string.H"
30 #include "error.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  content_(std::cref<std::string>(Foam::string::null)),
37  start_(0),
38  length_(0),
39  position_(0)
40 {}
41 
42 
43 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 
46 {
47  content_ = std::cref<std::string>(Foam::string::null);
48  start_ = 0;
49  length_ = 0;
50  position_ = 0;
51 }
52 
53 
55 (
56  const std::string& s,
57  size_t pos,
58  size_t len
59 )
60 {
61  content_ = std::cref<std::string>(s);
62  start_ = pos;
63  length_ = len;
64  position_ = 0;
65 }
66 
67 
68 std::string::const_iterator
70 {
71  const std::string& s = content_.get();
72 
73  if (start_ >= s.length())
74  {
75  return s.cend();
76  }
77 
78  return s.cbegin() + start_;
79 }
80 
81 
82 std::string::const_iterator
84 {
85  const std::string& s = content_.get();
86 
87  if (length_ == std::string::npos || start_ >= s.length())
88  {
89  return s.cend();
90  }
91 
92  const size_t strEnd = start_ + length_;
93 
94  if (strEnd >= s.length())
95  {
96  return s.cend();
97  }
98 
99  return s.cbegin() + strEnd;
100 }
101 
102 
104 (
105  Ostream& os
106 ) const
107 {
108  const auto endIter = cend();
109 
110  for (auto iter = cbegin(); iter != endIter; ++iter)
111  {
112  char c(*iter);
113 
114  // if (!c) break;
115 
116  if (c == '\t')
117  {
118  // Flatten tab to single space for better alignment
119  os << ' ';
120  }
121  else
122  {
123  os << c;
124  }
125  }
126 
127  return os;
128 }
129 
130 
132 (
133  const std::string& msg
134 ) const
135 {
136  if (position_)
137  {
138  reportFatal(msg, position_);
139  }
140  else
141  {
142  auto& os = FatalIOError
143  (
145  __FILE__,
146  __LINE__,
147  ""
148  );
149 
150  os << nl << msg.c_str() << " in expression\n"
151  << "<<<<\n";
152 
153  printBuffer(os)
154  << "\n>>>>\n"
156  }
157 }
158 
159 
161 (
162  const std::string& msg,
163  size_t pos
164 ) const
165 {
166  auto& os = Foam::FatalIOError
167  (
169  __FILE__,
170  __LINE__,
171  ""
172  );
173 
174  os << nl << msg.c_str()
175  << " in expression at position:" << long(pos) << nl
176  << "<<<<\n";
177 
178  const auto begIter = cbegin();
179  const auto endIter = cend();
180 
181  // Position of newline(s)
182  size_t newline0 = 0, newline1 = 0;
183 
184  auto iter = begIter;
185 
186  for (/*nil*/; iter != endIter; ++iter)
187  {
188  char c(*iter);
189 
190  if ('\t' == c)
191  {
192  // Flatten tab to single space for better alignment
193  os << ' ';
194  }
195  else if ('\n' == c)
196  {
197  os << c;
198 
199  newline1 = (iter-begIter);
200 
201  if (newline1 < pos)
202  {
203  newline0 = newline1;
204  }
205  else
206  {
207  ++iter;
208  break;
209  }
210  }
211  else
212  {
213  os << c;
214  }
215  }
216 
217  if (newline0 == newline1 || newline1 == pos)
218  {
219  os << '\n';
220  }
221 
222  size_t col = std::min(newline0, newline1);
223  if (col < pos)
224  {
225  // This still isn't quite right
226  col = pos - col;
227  if (col) --col;
228 
229  for (/*nil*/; col; --col)
230  {
231  os << ' ';
232  }
233  }
234 
235  os << "^^^^ near here\n";
236 
237  // Finish output
238  for (/*nil*/; iter != endIter; ++iter)
239  {
240  char c(*iter);
241 
242  if ('\t' == c)
243  {
244  // Flatten tab to single space for better alignment
245  os << ' ';
246  }
247  else
248  {
249  os << c;
250  }
251  }
252 
253  os << "\n>>>>\n"
255 }
256 
257 
258 // ************************************************************************* //
Foam::parsing::genericRagelLemonDriver::genericRagelLemonDriver
genericRagelLemonDriver()
Construct null.
Definition: genericRagelLemonDriver.C:34
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::parsing::genericRagelLemonDriver::content
const std::string & content() const
Get reference to the input buffer content.
Definition: genericRagelLemonDriver.H:109
Foam::FatalIOError
IOerror FatalIOError
string.H
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
genericRagelLemonDriver.H
error.H
Foam::string::null
static const string null
An empty string.
Definition: string.H:169
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
Foam::parsing::genericRagelLemonDriver::cend
std::string::const_iterator cend() const
Iterator to end of content (sub)string.
Definition: genericRagelLemonDriver.C:83
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::parsing::genericRagelLemonDriver::cbegin
std::string::const_iterator cbegin() const
Iterator to begin of content (sub)string.
Definition: genericRagelLemonDriver.C:69
Foam::parsing::genericRagelLemonDriver::printBuffer
Ostream & printBuffer(Ostream &os) const
Output the input buffer string content.
Definition: genericRagelLemonDriver.C:104
Foam::parsing::genericRagelLemonDriver::clear
void clear()
Reset references.
Definition: genericRagelLemonDriver.C:45
Foam::nl
constexpr char nl
Definition: Ostream.H:404
stdFoam::cbegin
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:113
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::parsing::genericRagelLemonDriver::reportFatal
void reportFatal(const std::string &msg) const
Report FatalError.
Definition: genericRagelLemonDriver.C:132
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177