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

int Clp_AddStringListTypeVec ( Clp_Parser clp,
int  val_type,
int  flags,
int  nstrs,
const char *const *  strs,
const int *  vals 
)

Define a new string list value type for clp.

Parameters:
clp the parser
val_type value type ID
flags string list flags
nstrs number of strings in list
strs array of strings
vals array of values
Returns:
0 on success, -1 on failure
Defines argument type val_type in parser clp. The parsing function sets clp->val.i to an integer. The value string is matched against the strs. If there's a unique match, the corresponding entry from vals is returned. Unique prefix matches also work. Finally, if flags contains the Clp_AllowNumbers flag, then integers are also accepted.

For example:

 const char * const strs[] = { "cat", "cattle", "dog" };
 const int vals[]          = { 1,     2,        3     };
 Clp_AddStringListTypeVec(clp, 100, Clp_AllowNumbers, 3, strs, vals);

Say that option --animal takes value type 100. Then:

  • --animal=cat will succeed and set clp->val.i = 1.
  • --animal=cattle will succeed and set clp->val.i = 2.
  • --animal=dog will succeed and set clp->val.i = 3.
  • --animal=d will succeed and set clp->val.i = 3.
  • --animal=c will fail, since c is ambiguous.
  • --animal=4 will succeed and set clp->val.i = 4.

Note:
The CLP library will not modify the contents of strs or vals. The calling program can modify the strs array, but the actual strings (for instance, strs[0] and strs[1]) must not be modified or freed until the parser is destroyed.

Definition at line 1341 of file clp.c.

References Clp_Option::flags, Clp_Option::long_name, and Clp_Option::option_id.

{
    int i;
    int itemscap = (nstrs < 5 ? 5 : nstrs);
    Clp_Option *items = (Clp_Option *)malloc(sizeof(Clp_Option) * itemscap);
    if (!items)
      return -1;

    /* copy over items */
    for (i = 0; i < nstrs; i++) {
      items[i].long_name = strs[i];
      items[i].option_id = vals[i];
      items[i].flags = 0;
    }

    if (finish_string_list(clp, val_type, flags, items, nstrs, itemscap) >= 0)
      return 0;
    else {
      free(items);
      return -1;
    }
}


Generated by  Doxygen 1.6.0   Back to index