Command-line and other configurationparsing and handling. More...
#include <stdio.h>
#include <stdarg.h>
#include <sphinxbase_export.h>
#include <prim_type.h>
#include <hash_table.h>
Go to the source code of this file.
Classes | |
struct | arg_s |
Defines | |
#define | ARG_STRINGIFY(s) ARG_STRINGIFY1(s) |
#define | ARG_STRINGIFY1(s) #s |
#define | cmd_ln_boolean_r(c, n) (cmd_ln_int_r(c,n) != 0) |
#define | cmd_ln_set_boolean_r(c, n, b) (cmd_ln_set_int_r(c,n,(b)!=0)) |
#define | cmd_ln_int32_r(c, n) cmd_ln_int_r(c,n) |
#define | cmd_ln_float32_r(c, n) (float32)cmd_ln_float_r(c,n) |
#define | cmd_ln_float64_r(c, n) (float64)cmd_ln_float_r(c,n) |
#define | cmd_ln_set_int32_r(c, n, i) cmd_ln_set_int_r(c,n,i) |
#define | cmd_ln_set_float32_r(c, n, f) cmd_ln_set_float_r(c,n,(double)f) |
#define | cmd_ln_set_float64_r(c, n, f) cmd_ln_set_float_r(c,n,(double)f) |
#define | cmd_ln_exists(name) cmd_ln_exists_r(cmd_ln_get(), name) |
#define | cmd_ln_access(name) cmd_ln_access_r(cmd_ln_get(), name) |
#define | cmd_ln_str(name) cmd_ln_str_r(cmd_ln_get(), name) |
#define | cmd_ln_int32(name) (int32)cmd_ln_int_r(cmd_ln_get(), name) |
#define | cmd_ln_float32(name) (float32)cmd_ln_float_r(cmd_ln_get(), name) |
#define | cmd_ln_float64(name) (float64)cmd_ln_float_r(cmd_ln_get(), name) |
#define | cmd_ln_boolean(name) cmd_ln_boolean_r(cmd_ln_get(), name) |
#define | cmd_ln_set_str(n, s) cmd_ln_set_str_r(cmd_ln_get(),n,s) |
#define | cmd_ln_set_int32(n, i) cmd_ln_set_int_r(cmd_ln_get(),n,i) |
#define | cmd_ln_set_float32(n, f) cmd_ln_set_float_r(cmd_ln_get(),n,f) |
#define | cmd_ln_set_float64(n, f) cmd_ln_set_float_r(cmd_ln_get(),n,f) |
#define | cmd_ln_set_boolean(n, b) cmd_ln_set_boolean_r(cmd_ln_get(),n,b) |
#define | cmd_ln_print_help(f, d) cmd_ln_print_help_r(cmd_ln_get(),f,d) |
Values for arg_t::type | |
#define | ARG_REQUIRED (1<<0) |
#define | ARG_INTEGER (1<<1) |
#define | ARG_FLOATING (1<<2) |
#define | ARG_STRING (1<<3) |
#define | ARG_BOOLEAN (1<<4) |
#define | REQARG_INTEGER (ARG_INTEGER | ARG_REQUIRED) |
#define | REQARG_FLOATING (ARG_FLOATING | ARG_REQUIRED) |
#define | REQARG_STRING (ARG_STRING | ARG_REQUIRED) |
#define | REQARG_BOOLEAN (ARG_BOOLEAN | ARG_REQUIRED) |
#define | ARG_INT32 ARG_INTEGER |
#define | ARG_FLOAT32 ARG_FLOATING |
#define | ARG_FLOAT64 ARG_FLOATING |
#define | REQARG_INT32 (ARG_INT32 | ARG_REQUIRED) |
#define | REQARG_FLOAT32 (ARG_FLOAT32 | ARG_REQUIRED) |
#define | REQARG_FLOAT64 (ARG_FLOAT64 | ARG_REQUIRED) |
Typedefs | |
typedef struct arg_s | arg_t |
typedef struct cmd_ln_s | cmd_ln_t |
Functions | |
SPHINXBASE_EXPORT cmd_ln_t * | cmd_ln_init (cmd_ln_t *inout_cmdln, arg_t const *defn, int32 strict,...) |
SPHINXBASE_EXPORT cmd_ln_t * | cmd_ln_retain (cmd_ln_t *cmdln) |
SPHINXBASE_EXPORT int | cmd_ln_free_r (cmd_ln_t *cmdln) |
SPHINXBASE_EXPORT cmd_ln_t * | cmd_ln_parse_r (cmd_ln_t *inout_cmdln, arg_t const *defn, int32 argc, char *argv[], int32 strict) |
SPHINXBASE_EXPORT cmd_ln_t * | cmd_ln_parse_file_r (cmd_ln_t *inout_cmdln, arg_t const *defn, char const *filename, int32 strict) |
SPHINXBASE_EXPORT anytype_t * | cmd_ln_access_r (cmd_ln_t *cmdln, char const *name) |
SPHINXBASE_EXPORT char const * | cmd_ln_str_r (cmd_ln_t *cmdln, char const *name) |
SPHINXBASE_EXPORT long | cmd_ln_int_r (cmd_ln_t *cmdln, char const *name) |
SPHINXBASE_EXPORT double | cmd_ln_float_r (cmd_ln_t *cmdln, char const *name) |
SPHINXBASE_EXPORT void | cmd_ln_set_str_r (cmd_ln_t *cmdln, char const *name, char const *str) |
SPHINXBASE_EXPORT void | cmd_ln_set_int_r (cmd_ln_t *cmdln, char const *name, long iv) |
SPHINXBASE_EXPORT void | cmd_ln_set_float_r (cmd_ln_t *cmdln, char const *name, double fv) |
SPHINXBASE_EXPORT int | cmd_ln_exists_r (cmd_ln_t *cmdln, char const *name) |
SPHINXBASE_EXPORT void | cmd_ln_print_help_r (cmd_ln_t *cmdln, FILE *fp, const arg_t *defn) |
SPHINXBASE_EXPORT int32 | cmd_ln_parse (const arg_t *defn, int32 argc, char *argv[], int32 strict) |
SPHINXBASE_EXPORT int32 | cmd_ln_parse_file (const arg_t *defn, char const *filename, int32 strict) |
SPHINXBASE_EXPORT void | cmd_ln_appl_enter (int argc, char *argv[], char const *default_argfn, const arg_t *defn) |
SPHINXBASE_EXPORT void | cmd_ln_appl_exit (void) |
SPHINXBASE_EXPORT cmd_ln_t * | cmd_ln_get (void) |
SPHINXBASE_EXPORT void | cmd_ln_free (void) |
Command-line and other configurationparsing and handling.
Configuration parameters, optionally parsed from the command line.
#define ARG_BOOLEAN (1<<4) |
Boolean (true/false) argument (optional).
#define ARG_FLOAT32 ARG_FLOATING |
#define ARG_FLOAT64 ARG_FLOATING |
#define ARG_FLOATING (1<<2) |
Floating point argument (optional).
#define ARG_INT32 ARG_INTEGER |
#define ARG_INTEGER (1<<1) |
Integer argument (optional).
#define ARG_REQUIRED (1<<0) |
Bit indicating a required argument.
#define ARG_STRING (1<<3) |
String argument (optional).
#define ARG_STRINGIFY | ( | s | ) | ARG_STRINGIFY1(s) |
Helper macro to stringify enums and other non-string values for default arguments.
#define ARG_STRINGIFY1 | ( | s | ) | #s |
#define cmd_ln_access | ( | name | ) | cmd_ln_access_r(cmd_ln_get(), name) |
Return a pointer to the previously parsed value for the given argument name.
#define cmd_ln_boolean | ( | name | ) | cmd_ln_boolean_r(cmd_ln_get(), name) |
Retrieve a boolean from the global command line.
#define cmd_ln_boolean_r | ( | c, | |||
n | ) | (cmd_ln_int_r(c,n) != 0) |
Retrieve a boolean value from a command-line object.
#define cmd_ln_exists | ( | name | ) | cmd_ln_exists_r(cmd_ln_get(), name) |
Test the existence of a command-line argument in the global set of definitions.
#define cmd_ln_float32 | ( | name | ) | (float32)cmd_ln_float_r(cmd_ln_get(), name) |
Retrieve a 32-bit float from the global command line.
#define cmd_ln_float32_r | ( | c, | |||
n | ) | (float32)cmd_ln_float_r(c,n) |
#define cmd_ln_float64 | ( | name | ) | (float64)cmd_ln_float_r(cmd_ln_get(), name) |
Retrieve a 64-bit float from the global command line.
#define cmd_ln_float64_r | ( | c, | |||
n | ) | (float64)cmd_ln_float_r(c,n) |
#define cmd_ln_int32 | ( | name | ) | (int32)cmd_ln_int_r(cmd_ln_get(), name) |
Retrieve a 32-bit integer from the global command line.
#define cmd_ln_int32_r | ( | c, | |||
n | ) | cmd_ln_int_r(c,n) |
#define cmd_ln_print_help | ( | f, | |||
d | ) | cmd_ln_print_help_r(cmd_ln_get(),f,d) |
Print a help message listing the valid argument names, and the associated attributes as given in defn.
#define cmd_ln_set_boolean | ( | n, | |||
b | ) | cmd_ln_set_boolean_r(cmd_ln_get(),n,b) |
Set a boolean value in the global command line.
#define cmd_ln_set_boolean_r | ( | c, | |||
n, | |||||
b | ) | (cmd_ln_set_int_r(c,n,(b)!=0)) |
Set a boolean value in a command-line object.
#define cmd_ln_set_float32 | ( | n, | |||
f | ) | cmd_ln_set_float_r(cmd_ln_get(),n,f) |
Set a 32-bit float in the global command line.
#define cmd_ln_set_float32_r | ( | c, | |||
n, | |||||
f | ) | cmd_ln_set_float_r(c,n,(double)f) |
#define cmd_ln_set_float64 | ( | n, | |||
f | ) | cmd_ln_set_float_r(cmd_ln_get(),n,f) |
Set a 64-bit float in the global command line.
#define cmd_ln_set_float64_r | ( | c, | |||
n, | |||||
f | ) | cmd_ln_set_float_r(c,n,(double)f) |
#define cmd_ln_set_int32 | ( | n, | |||
i | ) | cmd_ln_set_int_r(cmd_ln_get(),n,i) |
Set a 32-bit integer value in the global command line.
#define cmd_ln_set_int32_r | ( | c, | |||
n, | |||||
i | ) | cmd_ln_set_int_r(c,n,i) |
#define cmd_ln_set_str | ( | n, | |||
s | ) | cmd_ln_set_str_r(cmd_ln_get(),n,s) |
Set a string in the global command line.
#define cmd_ln_str | ( | name | ) | cmd_ln_str_r(cmd_ln_get(), name) |
Retrieve a string from the global command line.
#define REQARG_BOOLEAN (ARG_BOOLEAN | ARG_REQUIRED) |
Required boolean argument.
#define REQARG_FLOAT32 (ARG_FLOAT32 | ARG_REQUIRED) |
#define REQARG_FLOAT64 (ARG_FLOAT64 | ARG_REQUIRED) |
#define REQARG_FLOATING (ARG_FLOATING | ARG_REQUIRED) |
Required floating point argument.
#define REQARG_INT32 (ARG_INT32 | ARG_REQUIRED) |
#define REQARG_INTEGER (ARG_INTEGER | ARG_REQUIRED) |
Required integer argument.
#define REQARG_STRING (ARG_STRING | ARG_REQUIRED) |
Required string argument.
Access the generic type union for a command line argument.
SPHINXBASE_EXPORT void cmd_ln_appl_enter | ( | int | argc, | |
char * | argv[], | |||
char const * | default_argfn, | |||
const arg_t * | defn | |||
) |
Old application initialization routine for Sphinx3 code.
argc | In: Number of actual arguments | |
argv | In: Number of actual arguments | |
default_argfn | In: default argument file name | |
defn | Command-line argument definition |
SPHINXBASE_EXPORT void cmd_ln_appl_exit | ( | void | ) |
Finalization routine corresponding to cmd_ln_appl_enter().
SPHINXBASE_EXPORT int cmd_ln_exists_r | ( | cmd_ln_t * | cmdln, | |
char const * | name | |||
) |
Re-entrant version of cmd_ln_exists().
SPHINXBASE_EXPORT double cmd_ln_float_r | ( | cmd_ln_t * | cmdln, | |
char const * | name | |||
) |
Retrieve a floating-point number from a command-line object.
cmdln | Command-line object. | |
name | the command-line flag to retrieve. |
name
, or 0.0 if name
does not exist. You must use cmd_ln_exists_r() to distinguish between cases where a value is legitimately zero and where the corresponding flag is unknown. SPHINXBASE_EXPORT void cmd_ln_free | ( | void | ) |
Free the global command line, if any exists.
SPHINXBASE_EXPORT int cmd_ln_free_r | ( | cmd_ln_t * | cmdln | ) |
Release a command-line argument set and all associated strings.
SPHINXBASE_EXPORT cmd_ln_t* cmd_ln_get | ( | void | ) |
Retrieve the global cmd_ln_t object used by non-re-entrant functions.
SPHINXBASE_EXPORT cmd_ln_t* cmd_ln_init | ( | cmd_ln_t * | inout_cmdln, | |
arg_t const * | defn, | |||
int32 | strict, | |||
... | ||||
) |
Create a cmd_ln_t from NULL-terminated list of arguments.
This function creates a cmd_ln_t from a NULL-terminated list of argument strings. For example, to create the equivalent of passing "-hmm foodir -dsratio 2 -lm bar.lm" on the command-line:
config = cmd_ln_init(NULL, defs, TRUE, "-hmm", "foodir", "-dsratio", "2", "-lm", "bar.lm", NULL);
Note that for simplicity, all arguments are passed as strings, regardless of the actual underlying type.
inout_cmdln | Previous command-line to update, or NULL to create a new one. | |
defn | Array of argument name definitions, or NULL to allow any arguments. | |
strict | Whether to fail on duplicate or unknown arguments. |
SPHINXBASE_EXPORT long cmd_ln_int_r | ( | cmd_ln_t * | cmdln, | |
char const * | name | |||
) |
Retrieve an integer from a command-line object.
cmdln | Command-line object. | |
name | the command-line flag to retrieve. |
name
, or 0 if name
does not exist. You must use cmd_ln_exists_r() to distinguish between cases where a value is legitimately zero and where the corresponding flag is unknown. SPHINXBASE_EXPORT int32 cmd_ln_parse | ( | const arg_t * | defn, | |
int32 | argc, | |||
char * | argv[], | |||
int32 | strict | |||
) |
Non-reentrant version of cmd_ln_parse().
defn | In: Array of argument name definitions | |
argc | In: Number of actual arguments | |
argv | In: Actual arguments | |
strict | In: Fail on duplicate or unknown arguments, or no arguments? |
SPHINXBASE_EXPORT int32 cmd_ln_parse_file | ( | const arg_t * | defn, | |
char const * | filename, | |||
int32 | strict | |||
) |
Parse an arguments file by deliminating on " \r\t\n" and putting each tokens into an argv[] for cmd_ln_parse().
defn | In: Array of argument name definitions | |
filename | In: A file that contains all the arguments | |
strict | In: Fail on duplicate or unknown arguments, or no arguments? |
SPHINXBASE_EXPORT cmd_ln_t* cmd_ln_parse_file_r | ( | cmd_ln_t * | inout_cmdln, | |
arg_t const * | defn, | |||
char const * | filename, | |||
int32 | strict | |||
) |
Parse an arguments file by deliminating on " \r\t\n" and putting each tokens into an argv[] for cmd_ln_parse().
inout_cmdln | In/Out: Previous command-line to update, or NULL to create a new one. | |
defn | In: Array of argument name definitions | |
filename | In: A file that contains all the arguments | |
strict | In: Fail on duplicate or unknown arguments, or no arguments? |
SPHINXBASE_EXPORT cmd_ln_t* cmd_ln_parse_r | ( | cmd_ln_t * | inout_cmdln, | |
arg_t const * | defn, | |||
int32 | argc, | |||
char * | argv[], | |||
int32 | strict | |||
) |
Parse a list of strings into argumetns.
Parse the given list of arguments (name-value pairs) according to the given definitions. Argument values can be retrieved in future using cmd_ln_access(). argv[0] is assumed to be the program name and skipped. Any unknown argument name causes a fatal error. The routine also prints the prevailing argument values (to stderr) after parsing.
inout_cmdln | In/Out: Previous command-line to update, or NULL to create a new one. | |
defn | In: Array of argument name definitions | |
argc | In: Number of actual arguments | |
argv | In: Actual arguments | |
strict | In: Fail on duplicate or unknown arguments, or no arguments? |
Print a help message listing the valid argument names, and the associated attributes as given in defn.
fp | In: File to which to print | |
defn | In: Array of argument name definitions |
Retain ownership of a command-line argument set.
SPHINXBASE_EXPORT void cmd_ln_set_float_r | ( | cmd_ln_t * | cmdln, | |
char const * | name, | |||
double | fv | |||
) |
Set a floating-point number in a command-line object.
cmdln | Command-line object. | |
name | The command-line flag to set. | |
fv | Integer value to set. |
SPHINXBASE_EXPORT void cmd_ln_set_int_r | ( | cmd_ln_t * | cmdln, | |
char const * | name, | |||
long | iv | |||
) |
Set an integer in a command-line object.
cmdln | Command-line object. | |
name | The command-line flag to set. | |
iv | Integer value to set. |
SPHINXBASE_EXPORT void cmd_ln_set_str_r | ( | cmd_ln_t * | cmdln, | |
char const * | name, | |||
char const * | str | |||
) |
Set a string in a command-line object.
cmdln | Command-line object. | |
name | The command-line flag to set. | |
str | String value to set. The command-line object does not retain ownership of this pointer. |
SPHINXBASE_EXPORT char const* cmd_ln_str_r | ( | cmd_ln_t * | cmdln, | |
char const * | name | |||
) |
Retrieve a string from a command-line object.
The command-line object retains ownership of this string, so you should not attempt to free it manually.
cmdln | Command-line object. | |
name | the command-line flag to retrieve. |
name
, or NULL if name
does not exist. You must use cmd_ln_exists_r() to distinguish between cases where a value is legitimately NULL and where the corresponding flag is unknown.