OpenImageIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
strutil.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
37 
38 
39 
40 #ifndef OPENIMAGEIO_STRUTIL_H
41 #define OPENIMAGEIO_STRUTIL_H
42 
43 #include <cstdarg>
44 #include <string>
45 #include <cstring>
46 #include <cstdlib>
47 #include <vector>
48 #include <map>
49 
50 #include "export.h"
51 #include "version.h"
52 
53 #include "tinyformat.h"
54 
55 #ifndef OPENIMAGEIO_PRINTF_ARGS
56 # ifndef __GNUC__
57 # define __attribute__(x)
58 # endif
59  // Enable printf-like warnings with gcc by attaching
60  // OPENIMAGEIO_PRINTF_ARGS to printf-like functions. Eg:
61  //
62  // void foo (const char* fmt, ...) OPENIMAGEIO_PRINTF_ARGS(1,2);
63  //
64  // The arguments specify the positions of the format string and the the
65  // beginning of the varargs parameter list respectively.
66  //
67  // For member functions with arguments like the example above, you need
68  // OPENIMAGEIO_PRINTF_ARGS(2,3) instead. (gcc includes the implicit this
69  // pointer when it counts member function arguments.)
70 # define OPENIMAGEIO_PRINTF_ARGS(fmtarg_pos, vararg_pos) \
71  __attribute__ ((format (printf, fmtarg_pos, vararg_pos) ))
72 #endif
73 
74 
75 OIIO_NAMESPACE_ENTER
76 {
80 namespace Strutil {
81 
90 TINYFORMAT_WRAP_FORMAT (std::string, format, ,
91  std::ostringstream msg;, msg, return msg.str();)
92 
98 std::string OIIO_API format_raw (const char *fmt, ...)
99  OPENIMAGEIO_PRINTF_ARGS(1,2);
100 
104 std::string OIIO_API vformat (const char *fmt, va_list ap)
105  OPENIMAGEIO_PRINTF_ARGS(1,0);
106 
112 std::string OIIO_API memformat (long long bytes, int digits=1);
113 
116 std::string OIIO_API timeintervalformat (double secs, int digits=1);
117 
118 
127 bool OIIO_API get_rest_arguments (const std::string &str, std::string &base,
128  std::map<std::string, std::string> &result);
129 
133 std::string OIIO_API escape_chars (const std::string &unescaped);
134 
137 std::string OIIO_API unescape_chars (const std::string &escaped);
138 
145 std::string OIIO_API wordwrap (std::string src, int columns=80, int prefix=0);
146 
150 inline size_t
151 strhash (const char *s)
152 {
153  if (! s) return 0;
154  unsigned int h = 0;
155  while (*s) {
156  h += (unsigned char)(*s);
157  h += h << 10;
158  h ^= h >> 6;
159  ++s;
160  }
161  h += h << 3;
162  h ^= h >> 11;
163  h += h << 15;
164  return h;
165 }
166 
167 
168 
171 bool OIIO_API iequals (const std::string &a, const std::string &b);
172 bool OIIO_API iequals (const char *a, const char *b);
173 
176 bool OIIO_API istarts_with (const std::string &a, const std::string &b);
177 bool OIIO_API istarts_with (const char *a, const char *b);
178 
181 bool OIIO_API iends_with (const std::string &a, const std::string &b);
182 bool OIIO_API iends_with (const char *a, const char *b);
183 
186 bool OIIO_API iends_with (const std::string &a, const std::string &b);
187 bool OIIO_API iends_with (const char *a, const char *b);
188 
190 bool OIIO_API contains (const std::string &a, const std::string &b);
191 bool OIIO_API contains (const char *a, const char *b);
192 
195 bool OIIO_API icontains (const std::string &a, const std::string &b);
196 bool OIIO_API icontains (const char *a, const char *b);
197 
200 void OIIO_API to_lower (std::string &a);
201 
204 void OIIO_API to_upper (std::string &a);
205 
209 std::string OIIO_API strip (const std::string &str,
210  const std::string &chars=std::string());
211 
215 void OIIO_API split (const std::string &str, std::vector<std::string> &result,
216  const std::string &sep = "", int maxsplit = -1);
217 
220 std::string OIIO_API join (const std::vector<std::string> &seq,
221  const std::string &sep="");
222 
223 
224 
225 // Helper template to convert from generic type to string
226 template<typename T>
227 inline T from_string (const std::string &s) {
228  return T(s); // Generic: assume there is an explicit converter
229 }
230 // Special case for int
231 template<> inline int from_string<int> (const std::string &s) {
232  return strtol (s.c_str(), NULL, 10);
233 }
234 // Special case for float
235 template<> inline float from_string<float> (const std::string &s) {
236  return (float)strtod (s.c_str(), NULL);
237 }
238 
239 
240 
254 template<class T>
255 void extract_from_list_string (std::vector<T> &vals,
256  const std::string &list,
257  const std::string &sep = ",")
258 {
259  size_t nvals = vals.size();
260  std::vector<std::string> valuestrings;
261  Strutil::split (list, valuestrings, sep);
262  for (size_t i = 0, e = valuestrings.size(); i < e; ++i) {
263  if (valuestrings[i].size())
264  vals[i] = from_string<T> (valuestrings[i]);
265  }
266  if (valuestrings.size() == 1) {
267  vals.resize (1);
268  vals.resize (nvals, vals[0]);
269  }
270 }
271 
272 
273 
274 
282 class StringHash {
283 public:
284  size_t operator() (const char *s) const {
285  return (size_t)Strutil::strhash(s);
286  }
287  size_t operator() (const std::string &s) const {
288  return (size_t)Strutil::strhash(s.c_str());
289  }
290 };
291 
292 
293 
296 class StringEqual {
297 public:
298  bool operator() (const char *a, const char *b) const {
299  return strcmp (a, b) == 0;
300  }
301 };
302 
303 #ifdef _WIN32
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
334 
335 // Conversion to wide char
336 //
337 std::wstring OIIO_API utf8_to_utf16(const std::string& utf8str);
338 
339 // Conversion from wide char
340 //
341 std::string OIIO_API utf16_to_utf8(const std::wstring& utf16str);
342 #endif
343 
344 } // namespace Strutil
345 
346 }
347 OIIO_NAMESPACE_EXIT
348 
349 
350 #endif // OPENIMAGEIO_STRUTIL_H