Logo Search packages:      
Sourcecode: t1utils version File versions  Download package

Clp_Parser* Clp_NewParser ( int  argc,
const char *const *  argv,
int  nopt,
const Clp_Option opt 
)

Create a new Clp_Parser.

Parameters:
argc number of arguments
argv argument array
nopt number of option definitions
opt option definition array
Returns:
the parser
The new Clp_Parser that will parse the arguments in argv according to the option definitions in opt.

The Clp_Parser is created with the following characteristics:

  • The "-" character introduces short options (Clp_SetOptionChar(clp, '-', Clp_Short)).
  • Clp_ProgramName is set from the first argument in argv, if any. The first argument returned by Clp_Next() will be the second argument in argv. Note that this behavior differs from Clp_SetArguments.
  • UTF-8 support is on iff the LANG environment variable contains one of the substrings "UTF-8", "UTF8", or "utf8". Override this with Clp_SetUTF8().
  • The Clp_ValString, Clp_ValStringNotOption, Clp_ValInt, Clp_ValUnsigned, Clp_ValBool, and Clp_ValDouble types are installed.
  • Errors are reported to standard error.

You may also create a Clp_Parser with no arguments or options (Clp_NewParser(0, 0, 0, 0)) and set the arguments and options later.

Returns NULL if there isn't enough memory to construct the parser.

Note:
The CLP library will not modify the contents of argv or opt. The calling program must not modify opt. It may modify argv in limited cases.

Definition at line 469 of file clp.c.

References Clp_DisallowOptions, Clp_Short, Clp_ValBool, Clp_ValDouble, Clp_ValInt, Clp_ValString, Clp_ValStringNotOption, Clp_ValUnsigned, Clp_Parser::have_val, Clp_Parser::internal, Clp_Parser::negated, Clp_Parser::user_data, and Clp_Parser::vstr.

{
    Clp_Parser *clp = (Clp_Parser *)malloc(sizeof(Clp_Parser));
    Clp_Internal *cli = (Clp_Internal *)malloc(sizeof(Clp_Internal));
    Clp_InternOption *iopt = (Clp_InternOption *)malloc(sizeof(Clp_InternOption) * nopt);
    if (cli)
      cli->valtype = (Clp_ValType *)malloc(sizeof(Clp_ValType) * Clp_InitialValType);
    if (!clp || !cli || !iopt || !cli->valtype)
      goto failed;

    clp->negated = 0;
    clp->have_val = 0;
    clp->vstr = 0;
    clp->user_data = 0;
    clp->internal = cli;

    cli->opt = opt;
    cli->nopt = nopt;
    cli->iopt = iopt;
    cli->opt_generation = 0;
    cli->error_handler = 0;

    /* Assign program name (now so we can call Clp_OptionError) */
    if (argc > 0) {
      const char *slash = strrchr(argv[0], '/');
      cli->program_name = slash ? slash + 1 : argv[0];
    } else
      cli->program_name = 0;

    /* Assign arguments, skipping program name */
    Clp_SetArguments(clp, argc - 1, argv + 1);

    /* Initialize UTF-8 status and option classes */
    {
      char *s = getenv("LANG");
      cli->utf8 = (s && (strstr(s, "UTF-8") != 0 || strstr(s, "UTF8") != 0
                     || strstr(s, "utf8") != 0));
    }
    cli->oclass[0].c = '-';
    cli->oclass[0].type = Clp_Short;
    cli->noclass = 1;
    cli->long1pos = cli->long1neg = 0;

    /* Add default type parsers */
    cli->nvaltype = 0;
    Clp_AddType(clp, Clp_ValString, 0, parse_string, 0);
    Clp_AddType(clp, Clp_ValStringNotOption, Clp_DisallowOptions, parse_string, 0);
    Clp_AddType(clp, Clp_ValInt, 0, parse_int, 0);
    Clp_AddType(clp, Clp_ValUnsigned, 0, parse_int, (void *)cli);
    Clp_AddType(clp, Clp_ValBool, 0, parse_bool, 0);
    Clp_AddType(clp, Clp_ValDouble, 0, parse_double, 0);

    /* Set options */
    Clp_SetOptions(clp, nopt, opt);

    return clp;

  failed:
    if (cli && cli->valtype)
      free(cli->valtype);
    if (cli)
      free(cli);
    if (clp)
      free(clp);
    if (iopt)
      free(iopt);
    return 0;
}


Generated by  Doxygen 1.6.0   Back to index