Test-string.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 | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Test some string functionality
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "string.H"
30 #include "stringOps.H"
31 #include "dictionary.H"
32 #include "IOstreams.H"
33 
34 using namespace Foam;
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // Main program:
38 
39 int main(int argc, char *argv[])
40 {
41  string test
42  (
43  " $HOME kjhkjhkjh \" \\$HOME/tyetyery $; ${FOAM_RUN} \n $; hkjh;"
44  " $(DONOTSUBST) some other <${USER}> with '${__UNKNOWN:-some default}'"
45  " value "
46  " or with '${HOME:+Home was set}' via :+ alternative"
47  " or with '${__UNKNOWN:+unknown}' empty"
48  );
49 
51  dict.add("HOME", "myHome");
52 
53  dictionary subDict;
54  subDict.add("value1", "test1");
55  subDict.add("value2", "test2");
56  dict.add("FOAM_RUN", subDict);
57 
58 
59  Info<< "string:" << test << nl << "hash:"
60  << unsigned(string::hash()(test)) << endl;
61 
62  Info<<"trimLeft: " << stringOps::trimLeft(test) << endl;
63  Info<<"trimRight: " << stringOps::trimRight(test) << endl;
64  Info<<"trim: " << stringOps::trim(test) << endl;
65 
66 
67  // test sub-strings via iterators
68  string::const_iterator iter = test.end();
69  string::const_iterator iter2 = test.end();
70  string::size_type fnd = test.find('\\');
71 
72  if (fnd != string::npos)
73  {
74  iter = test.begin() + fnd;
75  iter2 = iter + 6;
76  }
77 
78  Info<< "sub-string via iterators : >";
79  while (iter != iter2)
80  {
81  Info<< *iter;
82  ++iter;
83  }
84  Info<< "<\n";
85 
86  Info<< string(test).replace("kj", "zzz") << endl;
87  Info<< string(test).replace("kj", "") << endl;
88  Info<< string(test).replaceAll("kj", "zzz") << endl;
89  Info<< string(test).replaceAll("kj", "z") << endl;
90 
91  Info<< "expanded: " << string(test).expand() << endl;
92 
93  Info<<"dictionary-based substitution: " << dict << endl;
94  Info<< "expand dict: " << stringOps::expand(test, dict) << endl;
95 
96  string test2("~OpenFOAM/controlDict");
97  Info<< test2 << " => " << test2.expand() << endl;
98 
99  // replace controlDict with "newName"
100  {
101  string::size_type i = test2.rfind('/');
102 
103  if (i == string::npos)
104  {
105  test2 = "newName";
106  }
107  else
108  {
109  // this uses the std::string::replace
110  test2.replace(i+1, string::npos, "newName");
111  }
112  Info<< "after replace: " << test2 << endl;
113 
114  // do another replace
115  // this uses the Foam::string::replace
116  test2.replace("OpenFOAM", "openfoam");
117 
118  Info<< "after replace: " << test2 << endl;
119  }
120 
121  string s;
122  Sin.getLine(s);
123 
124  string s2(s.expand());
125 
126  cout<< "output string with " << s2.length() << " characters\n";
127  cout<< "ostream<< >" << s2 << "<\n";
128  Info<< "Ostream<< >" << s2 << "<\n";
129  Info<< "hash:" << hex << string::hash()(s2) << endl;
130 
131  Info<< "\nEnd\n" << endl;
132  return 0;
133 }
134 
135 
136 // ************************************************************************* //
test
double test(hashFn hash)
Definition: Test-HashingSpeed.C:1036
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::string::replaceAll
string & replaceAll(const string &oldStr, const string &newStr, size_type start=0)
Replace all occurences of sub-string oldStr with newStr.
Definition: string.C:77
Foam::stringOps::trimLeft
string trimLeft(const string &)
Return string trimmed of leading whitespace.
Definition: stringOps.C:845
string.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::ISstream::getLine
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().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))
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
Foam::stringOps::trimRight
string trimRight(const string &)
Return string trimmed of trailing whitespace.
Definition: stringOps.C:885
Foam::string::hash
Hashing function class, shared by all the derived classes.
Definition: string.H:90
Foam::Sin
ISstream Sin(cin, "Sin")
Definition: IOstreams.H:50
Foam::string::replace
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
Definition: string.C:58
Foam::string::expand
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurences of environment variables.
Definition: string.C:98
dictionary.H
main
int main(int argc, char *argv[])
Definition: Test-string.C:39
stringOps.H
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729
Foam::stringOps::expand
string expand(const string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Expand occurences of variables according to the mapping.
Definition: stringOps.C:74
Foam::stringOps::trim
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:922