1 /*
2 * parse.cc
3 * Copyright 2016 John Lindgren
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions, and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions, and the following disclaimer in the documentation
13 * provided with the distribution.
14 *
15 * This software is provided "as is" and without any warranty, express or
16 * implied. In no event shall the authors be liable for any damages arising from
17 * the use of this software.
18 */
19
20 #include "parse.h"
21 #include <string.h>
22
next()23 void TextParser::next()
24 {
25 m_val = nullptr;
26
27 if (!fgets(m_key, sizeof m_key, m_file))
28 return;
29
30 char * space = strchr(m_key, ' ');
31 if (!space)
32 return;
33
34 *space = 0;
35 m_val = space + 1;
36
37 char * newline = strchr(m_val, '\n');
38 if (newline)
39 *newline = 0;
40 }
41
get_int(const char * key,int & val) const42 bool TextParser::get_int(const char * key, int & val) const
43 {
44 return (m_val && !strcmp(m_key, key) && sscanf(m_val, "%d", &val) == 1);
45 }
46
get_str(const char * key) const47 String TextParser::get_str(const char * key) const
48 {
49 return (m_val && !strcmp(m_key, key)) ? String(m_val) : String();
50 }
51