7 #include "parse_string.h"
9 struct token_ops token_string_ops = {
10 .parse = parse_string,
11 .complete_get_nb = complete_get_nb_string,
12 .complete_get_elt = complete_get_elt_string,
13 .get_help = get_help_string,
16 #define MULTISTRING_HELP PSTR("Mul-choice STRING")
17 #define ANYSTRING_HELP PSTR("Any STRING")
18 #define FIXEDSTRING_HELP PSTR("Fixed STRING")
21 get_token_len(const prog_char * s)
26 c = pgm_read_byte(s+i);
27 while (c!='#' && c!='\0') {
29 c = pgm_read_byte(s+i);
34 static const prog_char *
35 get_next_token(const prog_char * s)
39 if (pgm_read_byte(s+i) == '#')
45 parse_string(parse_pgm_token_hdr_t * tk, const char * buf, void * res)
47 struct token_string_data sd;
49 const prog_char * str;
54 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
60 token_len = get_token_len(str);
62 /* if token is too big... */
63 if (token_len >= STR_TOKEN_SIZE - 1) {
67 if ( strncmp_P(buf, str, token_len) ) {
71 if ( !isendoftoken(*(buf+token_len)) ) {
76 } while ( (str = get_next_token(str)) != NULL );
81 /* unspecified string */
84 while(!isendoftoken(buf[token_len]) &&
85 token_len < (STR_TOKEN_SIZE-1))
88 /* return if token too long */
89 if (token_len >= STR_TOKEN_SIZE - 1) {
95 /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
96 strncpy(res, buf, token_len);
97 *((char *)res + token_len) = 0;
103 int8_t complete_get_nb_string(parse_pgm_token_hdr_t * tk)
105 struct token_string_data sd;
108 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
113 while( (sd.str = get_next_token(sd.str)) != NULL ) {
119 int8_t complete_get_elt_string(parse_pgm_token_hdr_t * tk, int8_t idx,
120 char * dstbuf, uint8_t size)
122 struct token_string_data sd;
126 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
130 s = get_next_token(s);
135 len = get_token_len(s);
139 memcpy_P(dstbuf, s, len);
146 int8_t get_help_string(parse_pgm_token_hdr_t * tk, char * dstbuf, uint8_t size)
148 struct token_string_data sd;
151 memcpy_P(&sd, &((struct token_string *)tk)->string_data, sizeof(sd));
155 if (get_next_token(s)) {
156 strncpy_P(dstbuf, MULTISTRING_HELP, size);
159 strncpy_P(dstbuf, FIXEDSTRING_HELP, size);
163 strncpy_P(dstbuf, ANYSTRING_HELP, size);
166 dstbuf[size-1] = '\0';