1 /*
2 * dvbcfg - support for linuxtv configuration files
3 * common functions
4 *
5 * Copyright (C) 2006 Christoph Pfister <christophpfister@gmail.com>
6 * Copyright (C) 2005 Andrew de Quincey <adq_dvb@lidskialf.net>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include "dvbcfg_common.h"
27
dvbcfg_parse_int(char ** text,char * tokens)28 int dvbcfg_parse_int(char **text, char *tokens)
29 {
30 char *start = *text;
31 char *stop = *text;
32 int value;
33
34 while (*stop != '\0') {
35 if (strchr(tokens, *stop) != NULL) {
36 *stop = '\0';
37 stop++;
38 break;
39 }
40 stop++;
41 }
42
43 if (sscanf(start, "%i", &value) == 1) {
44 *text = stop;
45 return value;
46 }
47
48 *text = NULL;
49 return -1;
50 }
51
dvbcfg_parse_char(char ** text,char * tokens)52 int dvbcfg_parse_char(char **text, char *tokens)
53 {
54 char *start = *text;
55 char *stop = *text;
56 char value;
57
58 while (*stop != '\0') {
59 if (strchr(tokens, *stop) != NULL) {
60 *stop = '\0';
61 stop++;
62 break;
63 }
64 stop++;
65 }
66
67 if (sscanf(start, "%c", &value) == 1) {
68 *text = stop;
69 return value;
70 }
71
72 *text = NULL;
73 return -1;
74 }
75
dvbcfg_parse_setting(char ** text,char * tokens,const struct dvbcfg_setting * settings)76 int dvbcfg_parse_setting(char **text, char *tokens, const struct dvbcfg_setting *settings)
77 {
78 char *start = *text;
79 char *stop = *text;
80
81 while (*stop != '\0') {
82 if (strchr(tokens, *stop) != NULL) {
83 *stop = '\0';
84 stop++;
85 break;
86 }
87 stop++;
88 }
89
90 while (settings->name) {
91 if (strcmp(start, settings->name) == 0) {
92 *text = stop;
93 return settings->value;
94 }
95 settings++;
96 }
97
98 *text = NULL;
99 return -1;
100 }
101
dvbcfg_parse_string(char ** text,char * tokens,char * dest,unsigned long size)102 void dvbcfg_parse_string(char **text, char *tokens, char *dest, unsigned long size)
103 {
104 char *start = *text;
105 char *stop = *text;
106 unsigned long length;
107
108 while ((*stop != '\0') && (strchr(tokens, *stop) == NULL))
109 stop++;
110
111 length = (stop - start) + 1;
112
113 if (length <= size) {
114 if (strchr(tokens, *stop) != NULL) {
115 *stop = '\0';
116 *text = stop + 1;
117 } else
118 *text = stop;
119 memcpy(dest, start, length);
120 return;
121 }
122
123 *text = NULL;
124 return;
125 }
126
dvbcfg_lookup_setting(unsigned int setting,const struct dvbcfg_setting * settings)127 const char *dvbcfg_lookup_setting(unsigned int setting, const struct dvbcfg_setting *settings)
128 {
129 while (settings->name) {
130 if (setting == settings->value)
131 return settings->name;
132 settings++;
133 }
134
135 return NULL;
136 }
137