1 /*******************************************************************
2 Components for embedded applications builded for
3 laboratory and medical instruments firmware
5 utils.c - utilities for reading numbers and parsing of text line
7 Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
8 (C) 2002 by PiKRON Ltd. http://www.pikron.com
10 *******************************************************************/
20 /* string parse and input routines */
23 * si_skspace - Skip space and blank characters
24 * @ps: Pointer to character poiter into parsed string
26 * Functions checks character **@ps by isspace() category and
27 * moves *@ps pointer after last blank character. If there is at least one
28 * blank character, returns 1, else returns 0.
30 int si_skspace(char **ps)
34 if (!isspace((uint8_t) * (p++)))
36 while (isspace((uint8_t) * (p++))) ;
43 * si_fndsep - Finds field separator character
44 * @ps: Pointer to character pointer into parsed string
45 * @sepchrs: String of possible separator characters.
47 * Function skips blanks, then checks, if next character belongs
48 * to @sepchrs list. If character belongs, function returns found
49 * character and *@ps points after separator else returns -1.
51 int si_fndsep(char **ps, char *sepchrs)
59 if (!strchr(sepchrs, c))
67 * si_alnumn - Reads alphanumeric value
68 * @ps: Pointer to character pointer into parsed string
69 * @pout: Pointer to read value buffer
70 * @n: Maximal number of read characters.
72 * Function reads alphanumeric characters and stores them in @pout,
73 * if there is no more alphanumeric characters or @n is reached,
74 * it returns count of stored characters.
76 int si_alnumn(char **ps, char *pout, int n)
82 while (isalnum((uint8_t) (c = *p)) || (*p == '_')) {
95 * si_alphan - Reads alphabetic value
96 * @ps: Pointer to character pointer into parsed string
97 * @pout: Pointer to read value buffer
98 * @n: Maximal number of read characters.
100 * Function reads alphabetic characters and stores them in @pout,
101 * if there is no more alphabetic characters or @n is reached,
102 * it returns count of stored characters.
104 int si_alphan(char **ps, char *pout, int n)
110 while (isalpha((uint8_t) (c = *p))) {
123 * si_long - Reads numeric value
124 * @ps: Pointer to character pointer into parsed string
125 * @val: Pointer to long integer value buffer
126 * @base: Base of converted number, 0 means select by C rules
128 * Function reads digits and converts them to longint value.
129 * It stops at end of the string or if non digit character is reached.
130 * Returns 1 for successful conversion or -1 if no character can be
131 * converted. Pointer *@ps paints after last character belonging to
134 int si_long(char **ps, long *val, int base)
138 *val = strtol(*ps, &p, base);
148 * Read argument from commands in format (XX,XX,XX...)
149 * @param[in] ps Pointer to source string
150 * @param[out] buf Pointer to array, where numbers from argument will be stored
151 * @param[in] base Base of numbers in argument
152 * @return number of values
154 int read_arg(char **ps, uint32_t *buf, int n, int base)
160 if (si_fndsep(ps, "({") < 0)
161 return -CMDERR_BADSEP;
164 if ((**ps != ')') && (**ps != '}'))
167 return -CMDERR_BADPAR;
168 if (si_long(ps, (long*)&val, base) < 0)
169 return -CMDERR_BADPAR;
172 if ((c = si_fndsep(ps, ",)}")) < 0)
173 return -CMDERR_BADSEP;