1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2 McGill University
3
4 Routine:
5 int STkeyXpar (const char Line[], const char Delims[], const char Quotes[],
6 const char *KeyTable[], char Par[])
7
8 Purpose:
9 Decode keywords and return parameter values
10
11 Description:
12 This routine decodes a keyword and returns the associated parameter value
13 from a line of text. Each parameter specification line contains information
14 of the following form.
15 <keyword> <sep> <value>
16 The value string is separated from the keyword by a character from a set of
17 delimiter characters. The allowable keywords are specified in a keyword
18 table. This routine finds the index of the keyword match and returns the
19 value associated with the keyword as a string.
20
21 Whitespace before and after the keyword and the value is ignored. The entire
22 value may be optionally enclosed in quotes. The quotes are removed before
23 the value is returned.
24
25 The format of the keyword table entries is described in the documentation for
26 routine STkeyMatch. This routine prints a warning message if the keyword
27 does not match an entry in the keyword table.
28
29 Parameters:
30 <- int STkeyXpar
31 Index of the matched keyword. This value is set to -1 if no match is
32 found.
33 -1 - No match
34 0 - Match to the first keyword
35 1 - Match to the second keyword
36 ...
37 -> const char Line[]
38 Input text line
39 -> const char Delims[]
40 Character string specifying delimiter characters
41 -> const char Quotes[]
42 Character string specifying pairs of quote characters (the left and
43 right quote characters). In the part of the input string between a
44 matched pair of quote characters, any other characters, including quote
45 characters other than from the active pair, are treated as ordinary
46 characters. Up to 5 pairs of quote characters can be specified. A zero
47 length string indicates that quote characters should not to be
48 recognized.
49 -> const char *KeyTable[]
50 Pointer array with pointers to the keyword strings. The end of the
51 keyword table is signalled with a NULL pointer. Note that with ANSI C,
52 if the actual parameter is not declared to have the const attribute, an
53 explicit cast to (const char **) is required.
54 <- char Par[]
55 Parameter value associated with the decoded keyword. This array should
56 allow for room for as many characters as are in the input line. This
57 string can be the same string as Line.
58
59 Author / revision:
60 P. Kabal Copyright (C) 2003
61 $Revision: 1.16 $ $Date: 2003/05/09 03:02:44 $
62
63 -------------------------------------------------------------------------*/
64
65 #include <string.h>
66
67 #include <libtsp.h>
68 #include <libtsp/STmsg.h>
69
70 #define NCBUF 512
71 #define WS_STRIP 1
72
73
74 int
STkeyXpar(const char Line[],const char Delims[],const char Quotes[],const char * Keytable[],char Par[])75 STkeyXpar (const char Line[], const char Delims[], const char Quotes[],
76 const char *Keytable[], char Par[])
77
78 {
79 char cbuf[NCBUF+1];
80 char *p;
81 char *token;
82 int nc;
83 int ind;
84
85 /* Set up the token buffer */
86 nc = strlen (Line);
87 if (nc <= NCBUF)
88 token = cbuf;
89 else
90 token = (char *) UTmalloc (nc + 1);
91
92 /* Find the keyword string */
93 p = STfindToken (Line, Delims, "", token, WS_STRIP, nc);
94 ind = STkeyMatch (token, Keytable);
95 if (ind < 0)
96 UTwarn ("STkeyXpar - %s: \"%s\"", STM_BadIdent, token);
97
98 /* Find the value string */
99 p = STfindToken (p, "", Quotes, token, WS_STRIP, nc);
100 if (p != NULL)
101 UTwarn ("STkeyXpar - %s: \"%s\"", STM_ExtraChars, token);
102
103 /* Remove outer quote characters */
104 STunQuote (token, Quotes, Par);
105
106 /* Deallocate the buffer */
107 if (nc > NCBUF)
108 UTfree ((void *) token);
109
110 /* Return the index */
111 return ind;
112 }
113