Reuse, not rework
Home

License Awareness



Highly Reusable Software

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...


NAME

librock_avscan - Recognize option prefixes and values in an argv[]-like list.
#License - #Source code - #Example Use -

SYNOPSIS

#include <librock/target/types.c>
#include <librock/exec.h>

int librock_avscan(int argc,
              const char **argv, /* argument array */
              const char *optKeys, /* blank separated list of accepted options */
              const char **ppszOpt,      /* Store pointer into optKeys of match*/
              const char **ppszValue)  /* Store pointer to option value, if any */

DESCRIPTION

Recognize and pre-process items in an argv[]-like array, recognizing option prefixes/keywords. Stores a pointer to the recognized option, and if the specified option takes a value, a pointer to the value, which may be located in the immediately following argv[] space. Returns the number of argv[] items to advance, or 0 if nothing was recognized.

This replaces optarg(), with a better behaved function that does not use globals or static storage, or write error messages. librock_avscan is designed to be used in contexts other than command line argv[] processing.

optKeys defines the accepted options. It is a list of blank separated strings of one or more of the following forms:

   -a
   -a=

   --long-name
   --long-name=

where 'a' may be any single alphabetic character.  "long-name"
may contain any alpha-numeric character, and '_', and '-'.   The
suffix of '=' means that a value is always expected to be
present.

When the key is the short form '-a=', the value is taken to start
at the character immediately following the 'a'.  If there is no
character following the 'a', the entire next argument in the argv
input array is the value.  If the character following 'a' is '=',
the value is everything after the '='.

When the key is the long form '--long-option', the value is
taken to start after '=', if '=' is not present, the
entire next argument in the argv array is the value.

Typical use is

#ifdef librock_TYPICAL_USE_avscan
    int testargc = 6;
    char *testargv[7] = {
        "testargv0",
        "-b",
        "-t=value",
        "--long-t",
        "-b",  /* Argument to previous */
        "-b=20", /* Not recognized. -b does not take a value */
        0
    };
    int iArg = 1;
    char *pszOpt;
    char *pszOptVal;

    while(iArg < testargc) {
        iArg += librock_avscan(testargc - iArg,&testargv[iArg],"-b -t= --long-t=",&pszOpt,&pszOptVal);
        if (!pszOpt) {
            /* Nothing matched on this one.  Stop processing*/
            break;
        }
        if (!librock_wordcmp(pszOpt,"-b")) {
            printf("Option: -b\n");
        }
        if (!librock_wordcmp(pszOpt,"-t=")||!librock_wordcmp(pszOpt,"--long-t=")) {
            /* the testargv had one of the following

                   -t=Optval     --long-t=Optval
                   -t Optval     --long-t Optval
                   -tOptval
            */
            printf("Option: -t=%s\n",pszOptVal);

        }
    }
#endif

USES

  strncmp()
  librock_countbl() librock_counttowh()
  librock_wordcmp()



LICENSE

  Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com
  Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block.
  License text in <librock/license/librock.txt> librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45

Source Code

./exec/process/vararg/avscan.c (implementation, plus source of this manual page)

Tests and Supported Platform Types

This is a representative sample. Librock code is highly portable. For a particular platform not reported here, request paid support

librock_avscan passed tests in tavscan (Unix/Linux/BSD: 2002/08/08 sys=FreeBSD using gcc)
librock_avscan passed tests in tavscan (WIN32: 2002/08/08 sys=NT 4.0 using MSVC)


This software is part of Librock

Rapid reuse, without rework. Details
This page copyright (C) 2002-2003 Forrest J. Cavalier III, d-b-a Mib Software, Saylorsburg PA 18353, USA

Verbatim copying and distribution of this generated page is permitted in any medium provided that no changes are made.
(The source of this manual page may be covered by a more permissive license which allows modifications.)

Want to help? We welcome comments, patches. -- Need help? Request paid support.