build: use LT_INIT(pic-only) instead of forcing -fPIC.
[osmocom-bb.git] / src / msgfile.c
blobd2b180d78bbef6ccfffdb693d1f398eb67c9b0be
1 /*
2 * Parse a simple file with messages, e.g used for USSD messages
4 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
6 * All Rights Reserved
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <osmocom/core/msgfile.h>
25 #include <osmocom/core/talloc.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
32 static struct osmo_config_entry *
33 alloc_entry(struct osmo_config_list *entries,
34 const char *mcc, const char *mnc,
35 const char *option, const char *text)
37 struct osmo_config_entry *entry =
38 talloc_zero(entries, struct osmo_config_entry);
39 if (!entry)
40 return NULL;
42 entry->mcc = talloc_strdup(entry, mcc);
43 entry->mnc = talloc_strdup(entry, mnc);
44 entry->option = talloc_strdup(entry, option);
45 entry->text = talloc_strdup(entry, text);
47 llist_add_tail(&entry->list, &entries->entry);
48 return entry;
51 static struct osmo_config_list *alloc_entries(void *ctx)
53 struct osmo_config_list *entries;
55 entries = talloc_zero(ctx, struct osmo_config_list);
56 if (!entries)
57 return NULL;
59 INIT_LLIST_HEAD(&entries->entry);
60 return entries;
64 * split a line like 'foo:Text'.
66 static void handle_line(struct osmo_config_list *entries, char *line)
68 int i;
69 const int len = strlen(line);
71 char *items[3];
72 int last_item = 0;
74 /* Skip comments from the file */
75 if (line[0] == '#')
76 return;
78 for (i = 0; i < len; ++i) {
79 if (line[i] == '\n' || line[i] == '\r')
80 line[i] = '\0';
81 else if (line[i] == ':' && last_item < 3) {
82 line[i] = '\0';
84 items[last_item++] = &line[i + 1];
88 if (last_item == 3) {
89 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
90 return;
93 /* nothing found */
96 struct osmo_config_list *osmo_config_list_parse(void *ctx, const char *filename)
98 struct osmo_config_list *entries;
99 size_t n;
100 char *line;
101 FILE *file;
103 file = fopen(filename, "r");
104 if (!file)
105 return NULL;
107 entries = alloc_entries(ctx);
108 if (!entries) {
109 fclose(file);
110 return NULL;
113 n = 2342;
114 line = NULL;
115 while (getline(&line, &n, file) != -1) {
116 handle_line(entries, line);
117 free(line);
118 line = NULL;
121 fclose(file);
122 return entries;