X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Fcommon%2Fceph_argparse.cc;fp=src%2Fceph%2Fsrc%2Fcommon%2Fceph_argparse.cc;h=355224d0a84f0b975d10a722ed1d036145031207;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/common/ceph_argparse.cc b/src/ceph/src/common/ceph_argparse.cc new file mode 100644 index 0000000..355224d --- /dev/null +++ b/src/ceph/src/common/ceph_argparse.cc @@ -0,0 +1,535 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "auth/Auth.h" +#include "common/ceph_argparse.h" +#include "common/config.h" +#include "common/version.h" +#include "include/str_list.h" + +/* + * Ceph argument parsing library + * + * We probably should eventually replace this with something standard like popt. + * Until we do that, though, this file is the place for argv parsing + * stuff to live. + */ + +#undef dout +#undef pdout +#undef derr +#undef generic_dout +#undef dendl + +struct strict_str_convert { + const char *str; + std::string *err; + strict_str_convert(const char *str, std::string *err) + : str(str), err(err) {} + + inline operator float() const + { + return strict_strtof(str, err); + } + inline operator int() const + { + return strict_strtol(str, 10, err); + } + inline operator long long() const + { + return strict_strtoll(str, 10, err); + } +}; + +void string_to_vec(std::vector& args, std::string argstr) +{ + istringstream iss(argstr); + while(iss) { + string sub; + iss >> sub; + if (sub == "") break; + args.push_back(sub); + } +} + +bool split_dashdash(const std::vector& args, + std::vector& options, + std::vector& arguments) { + bool dashdash = false; + for (std::vector::const_iterator i = args.begin(); + i != args.end(); + ++i) { + if (dashdash) { + arguments.push_back(*i); + } else { + if (strcmp(*i, "--") == 0) + dashdash = true; + else + options.push_back(*i); + } + } + return dashdash; +} + +void env_to_vec(std::vector& args, const char *name) +{ + if (!name) + name = "CEPH_ARGS"; + char *p = getenv(name); + if (!p) + return; + + bool dashdash = false; + std::vector options; + std::vector arguments; + if (split_dashdash(args, options, arguments)) + dashdash = true; + + std::vector env_options; + std::vector env_arguments; + static vector str_vec; + std::vector env; + str_vec.clear(); + get_str_vec(p, " ", str_vec); + for (vector::iterator i = str_vec.begin(); + i != str_vec.end(); + ++i) + env.push_back(i->c_str()); + if (split_dashdash(env, env_options, env_arguments)) + dashdash = true; + + args.clear(); + args.insert(args.end(), options.begin(), options.end()); + args.insert(args.end(), env_options.begin(), env_options.end()); + if (dashdash) + args.push_back("--"); + args.insert(args.end(), arguments.begin(), arguments.end()); + args.insert(args.end(), env_arguments.begin(), env_arguments.end()); +} + +void argv_to_vec(int argc, const char **argv, + std::vector& args) +{ + args.insert(args.end(), argv + 1, argv + argc); +} + +void vec_to_argv(const char *argv0, std::vector& args, + int *argc, const char ***argv) +{ + *argv = (const char**)malloc(sizeof(char*) * (args.size() + 1)); + if (!*argv) + throw bad_alloc(); + *argc = 1; + (*argv)[0] = argv0; + + for (unsigned i=0; i= '0' && nextargstr[i] <= '9')) { + // May be negative numeral value + if ((i == 0) && (strlen(nextargstr) >= 2)) { + if (nextargstr[0] == '-') + continue; + } + if ( (nextargstr[i] == '.') && (is_float == false) ) { + is_float = true; + continue; + } + + is_numeric = false; + break; + } + } + + // -