SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringUtil.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 /// @todo Documentation Core/Utils/Legacy/StringUtil.h
29 
30 #ifndef CORE_UTIL_STRINGUTIL_H
31 #define CORE_UTIL_STRINGUTIL_H 1
32 
33 #include <string>
34 #include <vector>
35 #include <sstream>
36 #include <fstream>
37 #include <iostream>
38 
40 
41 namespace SCIRun {
42 
43 SCISHARE bool from_string(const std::string &str, double &value);
44 SCISHARE bool from_string(const std::string &str, float &value);
45 SCISHARE bool from_string(const std::string &str, int &value);
46 SCISHARE bool from_string(const std::string &str, unsigned int &value);
47 SCISHARE bool from_string(const std::string &str, long &value);
48 SCISHARE bool from_string(const std::string &str, unsigned long &value);
49 SCISHARE bool from_string(const std::string &str, long long &value);
50 SCISHARE bool from_string(const std::string &str, unsigned long long &value);
51 
52 template <class T>
53 bool multiple_from_string(const std::string &str, std::vector<T> &values)
54 {
55  values.clear();
56 
57  std::string data = str;
58  for (size_t j=0; j<data.size(); j++)
59  if ((data[j] == '\t')||(data[j] == '\r')||(data[j] == '\n')||(data[j]=='"')) data[j] = ' ';
60 
61  std::vector<std::string> nums;
62  for (size_t p=0;p<data.size();)
63  {
64  while((data[p] == ' ')&&(p<data.size())) p++;
65  if (p >= data.size()) break;
66 
67  std::string::size_type next_space = data.find(' ',p);
68  if (next_space == std::string::npos) next_space = data.size();
69  T value;
70  if (from_string(data.substr(p,next_space-p), value)) values.push_back(value);
71  p = next_space;
72 
73  if (p >= data.size()) break;
74  }
75  if (values.size() > 0) return (true);
76  return (false);
77 }
78 
79 template <class T>
80 bool from_string(const std::string &str, T &value)
81 {
82  std::istringstream iss(str+" ");
83  iss.exceptions(std::ifstream::eofbit | std::ifstream::failbit | std::ifstream::badbit);
84  try
85  {
86  iss >> value;
87  return (true);
88  }
89  catch (...)
90  {
91  return (false);
92  }
93 }
94 
95 
96 inline bool string_to_bool(const std::string &str, bool &result)
97 { return(from_string(str,result)); }
98 inline bool string_to_int(const std::string &str, int &result)
99 { return(from_string(str,result)); }
100 inline bool string_to_double(const std::string &str, double &result)
101 { return(from_string(str,result)); }
102 inline bool string_to_unsigned_long(const std::string &str, unsigned long &result)
103 { return(from_string(str,result)); }
104 
105 
106 template <class T>
107 std::string to_string(T val)
108 {
109  std::ostringstream oss;
110  oss << val;
111  return (oss.str());
112 }
113 
114 template <class T>
115 std::string to_string(T val,int precision)
116 {
117  std::ostringstream oss;
118  oss.precision(precision);
119  oss << val;
120  return (oss.str());
121 }
122 
123 SCISHARE std::string string_toupper(std::string);
124 SCISHARE std::string string_tolower(std::string);
125 
126 //////////
127 /// Remove directory name
128 inline std::string basename(const std::string &path)
129 { return (path.substr(path.rfind('/')+1)); }
130 
131 //////////
132 /// Return directory name
133 inline std::string pathname(const std::string &path)
134 { return (path.substr(0, path.rfind('/')+1)); }
135 
136 // Split a string into multiple parts, separated by the character sep
137 SCISHARE std::vector<std::string> split_string(const std::string& str, char sep);
138 
139 /////////
140 /// C++ify a string, turn newlines into \n, use \t, \r, \\ \", etc.
141 SCISHARE std::string string_Cify(const std::string &str);
142 
143 //////////
144 /// Unsafe cast from string to char *, used to export strings to C functions.
145 SCISHARE char* ccast_unsafe(const std::string &str);
146 
147 // replaces all occurances of 'substr' in 'str' with 'replacement'
148 SCISHARE std::string replace_substring(std::string str,
149  const std::string &substr,
150  const std::string &replacement);
151 
152 // Returns true if 'str' ends with the string 'substr'
153 SCISHARE bool ends_with(const std::string &str, const std::string &substr);
154 
155 // Remove spaces from start of string
156 SCISHARE void strip_spaces(std::string& str);
157 
158 // Remove spaces from start of string
159 SCISHARE void strip_surrounding_spaces(std::string& str);
160 
161 
162 } // End namespace SCIRun
163 
164 #endif
std::string string_tolower(std::string str)
Definition: StringUtil.cc:154
std::vector< std::string > split_string(const std::string &str, char sep)
Definition: StringUtil.cc:50
std::string to_string(const MatrixHandle &mat)
Definition: Matrix.h:213
bool string_to_int(const std::string &str, int &result)
Definition: StringUtil.h:98
std::string string_toupper(std::string str)
Definition: StringUtil.cc:144
std::string string_Cify(const std::string &str)
C++ify a string, turn newlines into , use , , \ ", etc.
Definition: StringUtil.cc:78
#define SCISHARE
Definition: share.h:39
bool string_to_double(const std::string &str, double &result)
Definition: StringUtil.h:100
list values
Definition: readAllFields.py:22
bool from_string(const std::string &str, double &value)
Definition: StringUtil.cc:163
void strip_surrounding_spaces(std::string &str)
Definition: StringUtil.cc:374
bool string_to_unsigned_long(const std::string &str, unsigned long &result)
Definition: StringUtil.h:102
std::string replace_substring(std::string str, const std::string &substr, const std::string &replacement)
Definition: StringUtil.cc:120
long long size_type
Definition: Types.h:40
dictionary data
Definition: eabLatVolData.py:11
bool string_to_bool(const std::string &str, bool &result)
Definition: StringUtil.h:96
void strip_spaces(std::string &str)
Definition: StringUtil.cc:357
std::string pathname(const std::string &path)
Return directory name.
Definition: StringUtil.h:133
bool multiple_from_string(const std::string &str, std::vector< T > &values)
Definition: StringUtil.h:53
char * ccast_unsafe(const std::string &str)
Unsafe cast from string to char *, used to export strings to C functions.
Definition: StringUtil.cc:41
bool ends_with(const std::string &str, const std::string &substr)
Definition: StringUtil.cc:137
std::string basename(const std::string &path)
Remove directory name.
Definition: StringUtil.h:128