HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
optparser.h
Go to the documentation of this file.
1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio
4 
5 
6 /////////////////////////////////////////////////////////////////////////
7 /// @file optparser.h
8 ///
9 /// @brief Option parser template
10 /////////////////////////////////////////////////////////////////////////
11 
12 
13 #pragma once
14 
15 #include <OpenImageIO/strutil.h>
16 #include <string>
17 
19 
20 
21 /// Parse a string of the form "name=value" and then call
22 /// system.attribute (name, value), with appropriate type conversions.
23 template<class C>
24 inline bool
25 optparse1(C& system, const std::string& opt)
26 {
27  std::string::size_type eq_pos = opt.find_first_of("=");
28  if (eq_pos == std::string::npos) {
29  // malformed option
30  return false;
31  }
32  std::string name(opt, 0, eq_pos);
33  // trim the name
34  while (name.size() && name[0] == ' ')
35  name.erase(0);
36  while (name.size() && name[name.size() - 1] == ' ')
37  name.erase(name.size() - 1);
38  std::string value(opt, eq_pos + 1, std::string::npos);
39  if (name.empty())
40  return false;
41  char v = value.size() ? value[0] : ' ';
42  if ((v >= '0' && v <= '9') || v == '+' || v == '-') { // numeric
43  if (strchr(value.c_str(), '.'))
44  return system.attribute(name, Strutil::stof(value)); // float
45  else
46  return system.attribute(name, Strutil::stoi(value)); // int
47  }
48  // otherwise treat it as a string
49 
50  // trim surrounding double quotes
51  if (value.size() >= 2 && (value[0] == '\"' || value[0] == '\'')
52  && value[value.size() - 1] == value[0])
53  value = std::string(value, 1, value.size() - 2);
54 
55  return system.attribute(name, value);
56 }
57 
58 
59 
60 /// Parse a string with comma-separated name=value directives, calling
61 /// system.attribute(name,value) for each one, with appropriate type
62 /// conversions. Examples:
63 /// optparser(texturesystem, "verbose=1");
64 /// optparser(texturesystem, "max_memory_MB=32.0");
65 /// optparser(texturesystem, "a=1,b=2,c=3.14,d=\"a string\"");
66 template<class C>
67 inline bool
68 optparser(C& system, const std::string& optstring)
69 {
70  bool ok = true;
71  size_t len = optstring.length();
72  size_t pos = 0;
73  while (pos < len) {
74  std::string opt;
75  char inquote = 0;
76  while (pos < len) {
77  unsigned char c = optstring[pos];
78  if (c == inquote) {
79  // Ending a quote
80  inquote = 0;
81  opt += c;
82  ++pos;
83  } else if (c == '\"' || c == '\'') {
84  // Found a quote
85  inquote = c;
86  opt += c;
87  ++pos;
88  } else if (c == ',' && !inquote) {
89  // Hit a comma and not inside a quote -- we have an option
90  ++pos; // skip the comma
91  break; // done with option
92  } else {
93  // Anything else: add to the option
94  opt += c;
95  ++pos;
96  }
97  }
98  // At this point, opt holds an option
99  ok &= optparse1(system, opt);
100  }
101  return ok;
102 }
103 
104 
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
String-related utilities, all in namespace Strutil.
OIIO_UTIL_API float stof(string_view s, size_t *pos=0)
OIIO_NAMESPACE_BEGIN bool optparse1(C &system, const std::string &opt)
Definition: optparser.h:25
OIIO_UTIL_API int stoi(string_view s, size_t *pos=0, int base=10)
GLuint const GLchar * name
Definition: glcorearb.h:786
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
bool optparser(C &system, const std::string &optstring)
Definition: optparser.h:68
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93