Newer
Older
#include "MantidKernel/Strings.h"
#include "MantidKernel/Exception.h"
#include <Poco/StringTokenizer.h>
#include <Poco/Path.h>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
Janik Zikovsky
committed
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <sstream>
#include <cstring>
#include <vector>
Janik Zikovsky
committed
using std::size_t;
namespace Mantid
{
namespace Kernel
namespace Strings
//------------------------------------------------------------------------------------------------
/** Loads the entire contents of a text file into a string
*
* @param filename :: full path to file
* @return string contents of text file
*/
std::string loadFile(const std::string & filename)
{
std::string retVal;
std::string str;
std::ifstream in;
in.open(filename.c_str());
getline(in,str);
while ( in ) {
retVal += str + "\n";
getline(in,str);
}
in.close();
return retVal;
}
Janik Zikovsky
committed
//------------------------------------------------------------------------------------------------
/** Return a string with all matching occurence-strings
*
* @param input :: input string
* @param find_what :: will search for all occurences of this string
* @param replace_with :: ... and replace them with this.
* @return the modified string.
*/
std::string replace(const std::string &input, const std::string &find_what, const std::string &replace_with)
{
std::string output = input;
std::string::size_type pos=0;
while((pos=output.find(find_what, pos))!=std::string::npos)
Janik Zikovsky
committed
{
output.erase(pos, find_what.length());
output.insert(pos, replace_with);
pos+=replace_with.length();
}
return output;
}
/**
* Return a string with all occurrences of the characters in the input replaced by the replace string
* @param input :: The input string to perform the replacement on
* @param charStr :: Each occurrence of ANY character in this string within the input string will be replaced by substitute
* @param substitute :: A substitute string
* @return A new string with the characters replaced
*/
MANTID_KERNEL_DLL std::string replaceAll(const std::string & input, const std::string & charStr, const std::string & substitute)
{
std::string replaced;
replaced.reserve(input.size());
std::string::const_iterator iend = input.end();
for( std::string::const_iterator itr = input.begin(); itr != iend; ++itr )
Janik Zikovsky
committed
{
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
131
132
133
char inputChar = (*itr);
if( charStr.find_first_of(inputChar) == std::string::npos ) // Input string char is not one of those to be replaced
{
replaced.push_back(inputChar);
}
else
{
replaced.append(substitute);
}
}
return replaced;
}
//------------------------------------------------------------------------------------------------
/**
* Function to convert a number into hex
* output (and leave the stream un-changed)
* @param OFS :: Output stream
* @param n :: Integer to convert
* \todo Change this to a stream operator
*/
void printHex(std::ostream& OFS,const int n)
{
std::ios_base::fmtflags PrevFlags=OFS.flags();
OFS<<"Ox";
OFS.width(8);
OFS.fill('0');
hex(OFS);
OFS << n;
OFS.flags(PrevFlags);
return;
}
//------------------------------------------------------------------------------------------------
/**
* Removes the multiple spaces in the line
* @param Line :: Line to process
* @return String with single space components
*/
std::string stripMultSpc(const std::string& Line)
{
std::string Out;
int spc(1);
int lastReal(-1);
for(unsigned int i=0;i<Line.length();i++)
Janik Zikovsky
committed
{
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
if (Line[i]!=' ' && Line[i]!='\t' &&
Line[i]!='\r' && Line[i]!='\n')
{
lastReal=i;
spc=0;
Out+=Line[i];
}
else if (!spc)
{
spc=1;
Out+=' ';
}
}
lastReal++;
if (lastReal<static_cast<int>(Out.length()))
Out.erase(lastReal);
return Out;
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
/**
* Checks that as least cnt letters of
* works is part of the string. It is currently
* case sensitive. It removes the Word if found
* @param Line :: Line to process
* @param Word :: Word to use
* @param cnt :: Length of Word for significants [default =4]
* @retval 1 on success (and changed Line)
* @retval 0 on failure
*/
int extractWord(std::string& Line,const std::string& Word,const int cnt)
{
if (Word.empty())
return 0;
size_t minSize(cnt>static_cast<int>(Word.size()) ? Word.size() : cnt);
std::string::size_type pos=Line.find(Word.substr(0,minSize));
if (pos==std::string::npos)
return 0;
// Pos == Start of find
size_t LinePt=minSize+pos;
for(;minSize<Word.size() && LinePt<Line.size()
&& Word[minSize]==Line[LinePt];LinePt++,minSize++)
Janik Zikovsky
committed
{
}
Line.erase(pos,LinePt-(pos-1));
return 1;
}
//------------------------------------------------------------------------------------------------
/** If a word ends with a number representing a positive integer, return
* the value of that int.
*
* @param word :: string possibly ending in a number
* @return the number, or -1 if it does not end in a number
*/
int endsWithInt(const std::string & word)
{
if (word.empty())
return -1;
int out = -1;
// Find the index of the first number in the string (if any)
int firstNumber = int(word.size());
for (int i=int(word.size())-1; i>=0; i--)
Loading
Loading full blame...