VistaIOIdentifyFiles(3) | Library Functions Manual | VistaIOIdentifyFiles(3) |
VistaIOIdentifyFiles - identify files specified by command line arguments
#include <vistaio.h>
VistaIOBoolean VistaIOIdentifyFiles (noptions, options, keyword, argc, argv, fd)
int noptions; VistaIOOptionDescRec options[noptions]; VistaIOStringConst keyword; int *argc; char *argv; int fd;
By convention, a Vista program that reads one or more files can be told of those files in any of three ways:
In parsing a command's arguments, these alternatives are considered in order. That is, first the program looks for an appropriate command line option (e.g., -in). If no such option has been specified, it checks for command line arguments not associated with any option. If those are also absent, it tries to read its file from the standard input stream.
Similarly, an output file may be specified using a command line option (e.g., vconvolve -out file1), or, by omitting the option and directing the program's standard output stream (e.g., vconvolve > file1).
VistaIOIdentifyFiles identifies a program's input files or output files according to this convention. It is called after VistaIOParseCommand(3) has parsed any command line arguments that can be identified as options, leaving the remaining arguments in *argc and argv. VistaIOIdentifyFiles is then called once to identify any input files, and perhaps again to identify an output file.
The noptions and options parameters specify the table used earlier by VistaIOParseCommand to parse command line options. (See VistaIOoption(3) for details.) The argc and argv parameters specify a list of command line arguments that VistaIOParseCommand has returned as unparsed (including the program's name, which should be first in this list). The keyword parameter specifies the command line option that may be used to specify filenames (e.g., ``in'' or ``out''), absent any ``-'' prefix. The entry in the option table for the option specified by keyword must (a) refer to a ``found'' flag that VistaIOParseCommand can use to record whether or not the option is present on the command line, and (b) refer to a location where the option's arguments can be stored.
VistaIOIdentifyFiles consults the ``found'' flag to determine whether the option was present on the command line. If it was, VistaIOIdentifyFiles returns immediately. Otherwise, it then examines *argc to determine whether an appropriate number of unparsed command line arguments exist. If there are a sufficient number of arguments present, they are interpreted as filenames and stored at the location indicated by the option table entry
Finally, if no filenames are found either as arguments to a -keyword option or as additional command line arguments, VistaIOIdentifyFiles considers the possibility that a file has been attached to a file descriptor such as the standard input or output stream. You may wish to ensure that a file is allowed to default to one of these streams only if the stream has been associated with a file or pipe, and not a terminal. In that case, pass as fd the file descriptor that will serve as the default source or destination of the file, and VistaIOIdentifyFiles will check that it is indeed associated with a file or pipe. Otherwise, pass -1 for fd and it will not perform this check.
VistaIOIdentifyFiles returns TRUE if it finds one or more files specified on the command line, and if the number of files specified is compatible with the number field of the option table entry. It returns FALSE if no files were specified, if the wrong number of files were specified, or if the file would default to a file descriptor but the file descriptor fd is not associated with a file or pipe.
On successful return VistaIOIdentifyFiles will have eliminated, from the list represented by *argc and argv, any command line arguments that it identified as filenames. Also, the filenames found will be stored at the location indicated by the option table entry for keyword. A default to the standard input or output stream will be identified by a filename of ``-''.
The following fragment is drawn from a program that reads one or more files and writes a single file. The input files may be specified with a -in option, as extra command line arguments, or by being directed to the standard input stream. The output file may be specified with a -out option, and, if that option is not present, the file will be written to the standard output stream regardless of whether it is associated with a file or a terminal.
VistaIOArgVector in_filenames; VistaIOStringConst out_filename; VistaIOBoolean in_found, out_found; VistaIOOptionDescRec options[] = { { "in", VistaIOStringRepn, 0, & in_filenames, & in_found, NULL, "Input file(s)" }, { "out", VistaIOStringRepn, 1, & out_filename, & out_found, NULL, "Output file" } }; main (int argc, char *argv) { /* Parse command line options: */ if (! VistaIOParseCommand (VistaIONumber (options), options, & argc, argv)) { Usage: VistaIOReportUsage (argv[0], VistaIONumber (options), options, "file1 file2..."); exit (1); } /* Identify input file(s): */ if (! VistaIOIdentifyFiles (VistaIONumber (options), options, "in", & argc, argv, 0)) goto Usage; /* Any remaining unparsed arguments are erroneous: */ if (argc > 1) { VistaIOReportBadArgs (argc, argv); goto Usage; } /* Identify output file: */ if (! VistaIOIdentifyFiles (VistaIONumber (options), options, "out", & argc, argv, -1)) goto Usage; /* Open and process each input file: */ for (i = 0; i < in_filenames.number; i++) { filename = ((VistaIOStringConst *) in_filename.vector)[i]; if (strcmp (filename, "-") != 0) { f = fopen (filename, "r"); if (f == NULL) VistaIOError ("Unable to open file \"%s\"", filename); } else f = stdin; ... } }
VistaIOOpenFile(3), VistaIOParseCommand(3),
VistaIOReportBadArgs(3), VistaIOReportUsage(3),
VistaIOoption(3),
VistaIOIdentifyFiles reports errors in command line options by printing directly to the standard error stream. Error reports include the program name obtained from argv[0]. The following messages may be produced:
In addition, VistaIOIdentifyFiles may invoke VistaIOError with the following messages:
Art Pope <pope@cs.ubc.ca>
Adaption to vistaio: Gert Wollny <gw.fossdev@gmail.com>
24 April 1993 | VistaIO Version 1.2.14 |