2 * Parse a simple file with messages, e.g used for USSD messages
4 * (C) 2010 by Holger Hans Peter Freyther
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>
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
);
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
);
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
);
59 INIT_LLIST_HEAD(&entries
->entry
);
64 * split a line like 'foo:Text'.
66 static void handle_line(struct osmo_config_list
*entries
, char *line
)
69 const int len
= strlen(line
);
74 /* Skip comments from the file */
78 for (i
= 0; i
< len
; ++i
) {
79 if (line
[i
] == '\n' || line
[i
] == '\r')
81 else if (line
[i
] == ':' && last_item
< 3) {
84 items
[last_item
++] = &line
[i
+ 1];
89 alloc_entry(entries
, &line
[0] , items
[0], items
[1], items
[2]);
96 struct osmo_config_list
*osmo_config_list_parse(void *ctx
, const char *filename
)
98 struct osmo_config_list
*entries
;
103 file
= fopen(filename
, "r");
107 entries
= alloc_entries(ctx
);
115 while (getline(&line
, &n
, file
) != -1) {
116 handle_line(entries
, line
);