OpenImageIO
 All Classes Files Friends Macros Pages
optparser.h
Go to the documentation of this file.
1 /*
2  Copyright 2011 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 #ifndef OPENIMAGEIO_OPTPARSER_H
40 #define OPENIMAGEIO_OPTPARSER_H
41 
42 #include <string>
43 
44 OIIO_NAMESPACE_ENTER
45 {
46 
47 
50 template<class C>
51 inline bool
52 optparse1 (C &system, const std::string &opt)
53 {
54  std::string::size_type eq_pos = opt.find_first_of ("=");
55  if (eq_pos == std::string::npos) {
56  // malformed option
57  return false;
58  }
59  std::string name (opt, 0, eq_pos);
60  // trim the name
61  while (name.size() && name[0] == ' ')
62  name.erase (0);
63  while (name.size() && name[name.size()-1] == ' ')
64  name.erase (name.size()-1);
65  std::string value (opt, eq_pos+1, std::string::npos);
66  if (name.empty())
67  return false;
68  char v = value.size() ? value[0] : ' ';
69  if ((v >= '0' && v <= '9') || v == '+' || v == '-') { // numeric
70  if (strchr (value.c_str(), '.')) // float
71  return system.attribute (name.c_str(), (float)atof(value.c_str()));
72  else // int
73  return system.attribute (name.c_str(), (int)atoi(value.c_str()));
74  }
75  // otherwise treat it as a string
76 
77  // trim surrounding double quotes
78  if (value.size() >= 2 &&
79  value[0] == '\"' && value[value.size()-1] == '\"')
80  value = std::string (value, 1, value.size()-2);
81 
82  return system.attribute (name.c_str(), value.c_str());
83 }
84 
85 
86 
93 template<class C>
94 inline bool
95 optparser (C &system, const std::string &optstring)
96 {
97  bool ok = true;
98  size_t len = optstring.length();
99  size_t pos = 0;
100  while (pos < len) {
101  std::string opt;
102  bool inquote = false;
103  while (pos < len) {
104  unsigned char c = optstring[pos];
105  if (c == '\"') {
106  // Hit a double quote -- toggle "inquote" and add the quote
107  inquote = !inquote;
108  opt += c;
109  ++pos;
110  } else if (c == ',' && !inquote) {
111  // Hit a comma and not inside a quote -- we have an option
112  ++pos; // skip the comma
113  break; // done with option
114  } else {
115  // Anything else: add to the option
116  opt += c;
117  ++pos;
118  }
119  }
120  // At this point, opt holds an option
121  ok &= optparse1 (system, opt);
122  }
123  return ok;
124 }
125 
126 
127 }
128 OIIO_NAMESPACE_EXIT
129 
130 #endif // OPENIMAGEIO_OPTPARSER_H