2 // C++ Implementation: gm_utils
7 // Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
20 #include "my_sstream.h"
21 #include "ggo_options.h"
25 extern groups_collection_t gengetopt_groups;
28 canonize_names(const char *name) {
34 for (p = pvar; *p; ++p)
35 if (*p == '.' || *p == '-')
41 // remove the path from the file name
42 const string strip_path(const string &s) {
43 string::size_type pos_of_sep;
45 pos_of_sep = s.rfind("/");
46 if (pos_of_sep == string::npos)
47 pos_of_sep = s.rfind("\\"); // try also with DOS path sep
49 if (pos_of_sep == string::npos)
52 return s.substr(pos_of_sep + 1);
55 const string to_upper(const string &old) {
58 for (string::iterator s = upper.begin(); s != upper.end(); ++s)
64 const string canonize_name(const string &old) {
65 string canonized = old;
67 for (string::iterator s = canonized.begin(); s != canonized.end(); ++s)
68 if (*s == '.' || *s == '-' || *s == ' ')
74 const string canonize_enum(const string &old) {
77 for (string::const_iterator s = old.begin(); s != old.end(); ++s)
79 canonized += "MINUS_";
88 bool has_multiple_options_all_string() {
89 if (!has_multiple_options())
92 struct gengetopt_option * opt = 0;
95 if (opt->multiple && (opt->type && opt->type != ARG_STRING))
102 bool has_multiple_options_string() {
103 if (!has_multiple_options())
106 struct gengetopt_option * opt = 0;
109 if (opt->multiple && opt->type == ARG_STRING)
116 bool has_multiple_options() {
117 struct gengetopt_option * opt = 0;
127 bool has_multiple_options_with_type() {
128 gengetopt_option * opt = 0;
130 for (gengetopt_option_list::iterator it = gengetopt_options.begin(); it
131 != gengetopt_options.end() && (opt = *it); ++it)
132 if (opt->multiple && opt->type)
138 bool has_multiple_options_with_default() {
139 gengetopt_option * opt = 0;
141 for (gengetopt_option_list::iterator it = gengetopt_options.begin(); it
142 != gengetopt_options.end() && (opt = *it); ++it)
143 if (opt->multiple && opt->default_given)
149 bool has_options_with_details() {
150 gengetopt_option * opt = 0;
152 for (gengetopt_option_list::iterator it = gengetopt_options.begin(); it
153 != gengetopt_options.end() && (opt = *it); ++it)
160 bool has_options_with_type() {
161 gengetopt_option * opt = 0;
163 for (gengetopt_option_list::iterator it = gengetopt_options.begin(); it
164 != gengetopt_options.end() && (opt = *it); ++it)
165 if (opt->type && opt->type != ARG_FLAG)
171 bool has_options_with_mode() {
172 gengetopt_option * opt = 0;
174 for (gengetopt_option_list::iterator it = gengetopt_options.begin(); it
175 != gengetopt_options.end() && (opt = *it); ++it)
182 bool has_required() {
183 struct gengetopt_option * opt = 0;
190 groups_collection_t::const_iterator end = gengetopt_groups.end();
191 for (groups_collection_t::const_iterator idx = gengetopt_groups.begin(); idx
193 if (idx->second.required) {
201 bool has_dependencies() {
202 struct gengetopt_option * opt = 0;
213 struct gengetopt_option * opt = 0;
216 if (opt->short_opt) {
217 if (opt->short_opt != 'h' && opt->short_opt != 'V')
225 bool has_hidden_options() {
226 struct gengetopt_option * opt = 0;
238 struct gengetopt_option * opt = 0;
240 for (gengetopt_option_list::iterator it = gengetopt_options.begin(); it
241 != gengetopt_options.end(); ++it) {
243 if (opt->acceptedvalues) {
251 int not_newlines(const string &buf, int &num_of_newlines) {
253 // searches for the first non newline char
254 string::size_type notnewline = buf.find_first_not_of("\r\n");
256 if (notnewline == string::npos) {
257 // a string made only of newlines
258 num_of_newlines = buf.size();
259 return num_of_newlines;
263 // everything before the non newline char is a newline
264 num_of_newlines = notnewline;
271 void wrap_cstr(string& wrapped, unsigned int from_column,
272 unsigned int second_indent, const string &orig) {
273 int next_space = from_column;
275 const char * out_buf = orig.c_str();
276 ostringstream stream;
277 const unsigned int second_line_column = from_column + second_indent;
278 string indent(second_line_column, ' ');
279 int newline_chars = 0;
280 int num_of_newlines = 0;
283 // check for a new line
285 if ((newline_chars = not_newlines(out_buf, num_of_newlines))) {
286 for (int i = 1; i <= num_of_newlines; ++i)
289 out_buf += newline_chars;
293 next_space = second_line_column;
298 stream << *out_buf++;
302 // search next whitespace, i.e., next word
303 while ((*out_buf) && (*out_buf != ' ') &&
304 !not_newlines(out_buf, num_of_newlines)) {
305 next_word += *out_buf++;
309 // wrap line if it's too long
310 if (next_space > 79) {
311 stream << "\\n" << indent << next_word;
312 next_space = second_line_column + next_word.size();
314 stream << next_word; // simply write word
319 wrapped += stream.str();
322 bool is_numeric(const gengetopt_option *opt) {