]> granicus.if.org Git - xz/commitdiff
Refactored option parsing.
authorLasse Collin <lasse.collin@tukaani.org>
Tue, 1 Sep 2009 17:40:01 +0000 (20:40 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Tue, 1 Sep 2009 17:40:01 +0000 (20:40 +0300)
src/xz/options.c

index c52f61e06483d17e1d90d099b8f95af3ffdbda8b..6fdf3a2612dd42662007751491330eead0cbe328 100644 (file)
@@ -87,47 +87,47 @@ parse_options(const char *str, const option_map *opts,
                                        "pairs separated with commas"), str);
 
                // Look for the option name from the option map.
-               bool found = false;
-               for (size_t i = 0; opts[i].name != NULL; ++i) {
-                       if (strcmp(name, opts[i].name) != 0)
-                               continue;
-
-                       if (opts[i].map != NULL) {
-                               // value is a string which we should map
-                               // to an integer.
-                               size_t j;
-                               for (j = 0; opts[i].map[j].name != NULL; ++j) {
-                                       if (strcmp(opts[i].map[j].name, value)
-                                                       == 0)
-                                               break;
-                               }
-
-                               if (opts[i].map[j].name == NULL)
-                                       message_fatal(_("%s: Invalid option "
-                                                       "value"), value);
-
-                               set(filter_options, i, opts[i].map[j].id,
-                                               value);
+               size_t i = 0;
+               while (true) {
+                       if (opts[i].name == NULL)
+                               message_fatal(_("%s: Invalid option name"),
+                                               name);
+
+                       if (strcmp(name, opts[i].name) == 0)
+                               break;
 
-                       } else if (opts[i].min == UINT64_MAX) {
-                               // value is a special string that will be
-                               // parsed by set().
-                               set(filter_options, i, 0, value);
+                       ++i;
+               }
 
-                       } else {
-                               // value is an integer.
-                               const uint64_t v = str_to_uint64(name, value,
-                                               opts[i].min, opts[i].max);
-                               set(filter_options, i, v, value);
+               // Option was found from the map. See how we should handle it.
+               if (opts[i].map != NULL) {
+                       // value is a string which we should map
+                       // to an integer.
+                       size_t j;
+                       for (j = 0; opts[i].map[j].name != NULL; ++j) {
+                               if (strcmp(opts[i].map[j].name, value) == 0)
+                                       break;
                        }
 
-                       found = true;
-                       break;
-               }
+                       if (opts[i].map[j].name == NULL)
+                               message_fatal(_("%s: Invalid option value"),
+                                               value);
+
+                       set(filter_options, i, opts[i].map[j].id, value);
 
-               if (!found)
-                       message_fatal(_("%s: Invalid option name"), name);
+               } else if (opts[i].min == UINT64_MAX) {
+                       // value is a special string that will be
+                       // parsed by set().
+                       set(filter_options, i, 0, value);
+
+               } else {
+                       // value is an integer.
+                       const uint64_t v = str_to_uint64(name, value,
+                                       opts[i].min, opts[i].max);
+                       set(filter_options, i, v, value);
+               }
 
+               // Check if it was the last option.
                if (split == NULL)
                        break;