Пример #1
0
/// Clears any bindings and releases their memory.
void
sqlite::statement::clear_bindings(void)
{
    const int error = ::sqlite3_clear_bindings(_pimpl->stmt);
    PRE_MSG(error == SQLITE_OK, "SQLite3 contract has changed; it should "
            "only return SQLITE_OK");
}
Пример #2
0
/// Returns the name of a parameter by index.
///
/// \param index The index to query; must be valid.
///
/// \return The name of the parameter.
std::string
sqlite::statement::bind_parameter_name(const int index)
{
    const char* name = ::sqlite3_bind_parameter_name(_pimpl->stmt, index);
    PRE_MSG(name != NULL, "Index value out of range or nameless parameter");
    return std::string(name);
}
Пример #3
0
/// Returns the index of a named parameter.
///
/// \param name The name of the parameter to be queried; must exist.
///
/// \return A parameter index.
int
sqlite::statement::bind_parameter_index(const std::string& name)
{
    const int index = ::sqlite3_bind_parameter_index(_pimpl->stmt,
                                                     name.c_str());
    PRE_MSG(index > 0, "Parameter name not in statement");
    return index;
}
Пример #4
0
/// Initializes the global state of the CLI.
///
/// This function can only be called once during the execution of a program,
/// unless override_for_testing is set to true.
///
/// \param argv0 The value of argv[0]; i.e. the program name.
/// \param override_for_testing Should always be set to false unless for tests
///     of this functionality, which may set this to true to redefine internal
///     state.
void
cmdline::init(const char* argv0, const bool override_for_testing)
{
    if (!override_for_testing)
        PRE_MSG(Progname.empty(), "cmdline::init called more than once");
    Progname = utils::fs::path(argv0).leaf_name();
    LD(F("Program name: %s") % Progname);
    POST(!Progname.empty());
}
Пример #5
0
/// Converts a path argument to a utils::fs::path.
///
/// \param raw_value The argument representing a path as provided by the user.
///
/// \return The path.
///
/// \pre validate(raw_value) must be true.
utils::fs::path
cmdline::path_option::convert(const std::string& raw_value)
{
    try {
        return utils::fs::path(raw_value);
    } catch (const std::runtime_error& e) {
        PRE_MSG(false, F("Raw value '%s' for path option not properly "
                         "validated: %s") % raw_value % e.what());
    }
}
Пример #6
0
/// Converts a string argument to a vector.
///
/// \param raw_value The argument representing a list as provided by the user.
///
/// \return The list.
///
/// \pre validate(raw_value) must be true.
cmdline::list_option::option_type
cmdline::list_option::convert(const std::string& raw_value)
{
    try {
        return text::split(raw_value, ',');
    } catch (const std::runtime_error& e) {
        PRE_MSG(false, F("Raw value '%s' for list option not properly "
                         "validated: %s") % raw_value % e.what());
    }
}
Пример #7
0
/// Converts an integer argument to a native integer.
///
/// \param raw_value The argument representing an integer as provided by the
///     user.
///
/// \return The integer.
///
/// \pre validate(raw_value) must be true.
int
cmdline::int_option::convert(const std::string& raw_value)
{
    try {
        return text::to_type< int >(raw_value);
    } catch (const std::runtime_error& e) {
        PRE_MSG(false, F("Raw value '%s' for int option not properly "
                         "validated: %s") % raw_value % e.what());
    }
}
Пример #8
0
/// Parses a command line.
///
/// \param args The command line to parse, broken down by words.
/// \param options The description of the supported options.
///
/// \return The parsed command line.
///
/// \pre args[0] must be the program or command name.
///
/// \throw cmdline::error See the description of parse(argc, argv, options) for
///     more details on the raised errors.
cmdline::parsed_cmdline
cmdline::parse(const cmdline::args_vector& args,
               const cmdline::options_vector& options)
{
    PRE_MSG(args.size() >= 1, "No progname or command name found");

    utils::auto_array< const char* > argv(new const char*[args.size() + 1]);
    for (args_vector::size_type i = 0; i < args.size(); i++)
        argv[i] = args[i].c_str();
    argv[args.size()] = NULL;
    return parse(static_cast< int >(args.size()), argv.get(), options);
}
Пример #9
0
/// Opens a named on-disk SQLite database.
///
/// \param file The path to the database file to be opened.  This does not
///     accept the values "" and ":memory:"; use temporary() and in_memory()
///     instead.
/// \param open_flags The flags to be passed to the open routine.
///
/// \return A file-backed database instance.
///
/// \throw std::bad_alloc If there is not enough memory to open the database.
/// \throw api_error If there is any problem opening the database.
sqlite::database
sqlite::database::open(const fs::path& file, int open_flags)
{
    PRE_MSG(!file.str().empty(), "Use database::temporary() instead");
    PRE_MSG(file.str() != ":memory:", "Use database::in_memory() instead");

    int flags = 0;
    if (open_flags & open_readonly) {
        flags |= SQLITE_OPEN_READONLY;
        open_flags &= ~open_readonly;
    }
    if (open_flags & open_readwrite) {
        flags |= SQLITE_OPEN_READWRITE;
        open_flags &= ~open_readwrite;
    }
    if (open_flags & open_create) {
        flags |= SQLITE_OPEN_CREATE;
        open_flags &= ~open_create;
    }
    PRE(open_flags == 0);

    return database(impl::safe_open(file.c_str(), flags), true);
}
Пример #10
0
/// Clears any bindings and releases their memory.
void
sqlite::statement::clear_bindings(void)
{
    const int error = ::sqlite3_clear_bindings(_pimpl->stmt);
#if defined(__minix) && defined(NDEBUG)
#undef PRE_MSG
#define PRE_MSG(expr, msg) \
    do { \
        if (!(expr)) \
            utils::sanity_failure(utils::precondition, __FILE__, __LINE__, msg); \
    } while (0)
#endif /* defined(__minix) && defined(NDEBUG) */
    PRE_MSG(error == SQLITE_OK, "SQLite3 contract has changed; it should "
            "only return SQLITE_OK");
}
Пример #11
0
/// Gets the program name.
///
/// \pre init() must have been called in advance.
///
/// \return The program name.
const std::string&
cmdline::progname(void)
{
    PRE_MSG(!Progname.empty(), "cmdline::init not called yet");
    return Progname;
}
Пример #12
0
/// Parses a command line.
///
/// \param argc The number of arguments in argv, without counting the
///     terminating NULL.
/// \param argv The arguments to parse.  The array is NULL-terminated.
/// \param options The description of the supported options.
///
/// \return The parsed command line.
///
/// \pre args[0] must be the program or command name.
///
/// \throw cmdline::missing_option_argument_error If the user specified an
///     option that requires an argument, but no argument was provided.
/// \throw cmdline::unknown_option_error If the user specified an unknown
///     option (i.e. an option not defined in options).
/// \throw cmdline::option_argument_value_error If the user passed an invalid
///     argument to a supported option.
cmdline::parsed_cmdline
cmdline::parse(const int argc, const char* const* argv,
               const cmdline::options_vector& options)
{
    PRE_MSG(argc >= 1, "No progname or command name found");

    getopt_data data;
    options_to_getopt_data(options, data);

    std::map< std::string, std::vector< std::string > > option_values;

    for (cmdline::options_vector::const_iterator iter = options.begin();
         iter != options.end(); iter++) {
        const cmdline::base_option* option = *iter;
        if (option->needs_arg() && option->has_default_value())
            option_values[option->long_name()].push_back(
                option->default_value());
    }

    args_vector args;

    int mutable_argc = argc;
    char** mutable_argv = make_mutable_argv(argc, argv);
    const int old_opterr = ::opterr;
    try {
        int ch;

        ::opterr = 0;

        while ((ch = ::getopt_long(mutable_argc, mutable_argv,
                                   ("+:" + data.short_options).c_str(),
                                   data.long_options.get(), NULL)) != -1) {
            if (ch == ':' ) {
                const std::string name = find_option_name(
                    data, ::optopt, mutable_argv, ::optind);
                throw cmdline::missing_option_argument_error(name);
            } else if (ch == '?') {
                const std::string name = find_option_name(
                    data, ::optopt, mutable_argv, ::optind);
                throw cmdline::unknown_option_error(name);
            }

            const std::map< int, const cmdline::base_option* >::const_iterator
                id = data.ids.find(ch);
            INV(id != data.ids.end());
            const cmdline::base_option* option = (*id).second;

            if (option->needs_arg()) {
                if (::optarg != NULL) {
                    option->validate(::optarg);
                    option_values[option->long_name()].push_back(::optarg);
                } else
                    INV(option->has_default_value());
            } else {
                option_values[option->long_name()].push_back("");
            }
        }
        args = argv_to_vector(mutable_argc - optind, mutable_argv + optind);

        ::opterr = old_opterr;
        ::optind = GETOPT_OPTIND_RESET_VALUE;
#if defined(HAVE_GETOPT_WITH_OPTRESET)
        ::optreset = 1;
#endif
    } catch (...) {
        free_mutable_argv(mutable_argv);
        ::opterr = old_opterr;
        ::optind = GETOPT_OPTIND_RESET_VALUE;
#if defined(HAVE_GETOPT_WITH_OPTRESET)
        ::optreset = 1;
#endif
        throw;
    }
    free_mutable_argv(mutable_argv);

    return parsed_cmdline(option_values, args);
}