-
Notifications
You must be signed in to change notification settings - Fork 3
/
ubx_cfg.cpp
executable file
·163 lines (154 loc) · 4.29 KB
/
ubx_cfg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
*
* Copyright (C) u-blox AG
* u-blox AG, Thalwil, Switzerland
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR U-BLOX MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*
*******************************************************************************
*
* Project: PE_ANS
*
******************************************************************************/
/*!
\file
\brief time utility library
Module for time functions
*/
/*******************************************************************************
* $Id: ubx_cfg.cpp 63615 2012-11-27 10:12:42Z andrea.foni $
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include "std_types.h"
#include "ubx_cfg.h"
#include "ubx_log.h"
CCfg::CCfg()
{
m_num = 0;
memset(m_itemName, 0, sizeof(m_itemName));
memset(m_itemData, 0, sizeof(m_itemData));
}
CCfg::~CCfg()
{
while (m_num --)
{
delete [] m_itemName[m_num];
delete [] m_itemData[m_num];
}
m_num = 0;
}
void CCfg::load(const char* fileName)
{
FILE* file = fopen(fileName, "r");
LOGD("CCfg::%s : fileName=\"%s\"", __FUNCTION__, fileName);
if (file == NULL)
{
// failed
LOGV("CCfg::%s : Can not open '%s' file : %i", __FUNCTION__, fileName, errno);
return;
}
while(!feof(file))
{
char line[1024];
memset(line, 0, sizeof(line));
// Load each line
if (fgets(line, sizeof(line), file) != NULL)
{
const char* name;
const char* data;
const char* p;
unsigned int nameLen;
unsigned int dataLen = 0;
p = line;
// skip spaces
while (isspace(*p)) p++;
name = p;
// find end of name
while (isgraph(*p) && (*p != '=') && (*p != '#'))
p++;
nameLen = (unsigned int) (p-name);
if (nameLen > 0)
{
// skip spaces
while (isspace(*p) && (*p != '#') && (*p != '\r') && (*p != '\n')) p ++;
// skip equal sign
if (*p == '=') p ++;
// skip spaces
while (isspace(*p) && (*p != '#') && (*p != '\r') && (*p != '\n')) p ++;
data = p;
// take all until we find end of line or comment start
while ((*p != '\0') && (*p != '#') && (*p != '\r') && (*p != '\n'))
{
if (isgraph(*p))
dataLen = (unsigned int) (p - data + 1);
p ++;
}
if (m_num < MAX_ITEM)
{
m_itemName[m_num] = new char[nameLen+1];
m_itemData[m_num] = new char[dataLen+1];
strncpy(m_itemName[m_num], name, nameLen);
strncpy(m_itemData[m_num], data, dataLen);
m_itemName[m_num][nameLen] = '\0';
m_itemData[m_num][dataLen] = '\0';
//LOGD("CCfg::%s index=%d name=\"%s\" data=\"%s\"", __FUNCTION__, m_num, m_itemName[m_num], m_itemData[m_num]);
m_num ++;
}
else
LOGE("CCfg::%s items list full", __FUNCTION__);
}
}
}
fclose(file);
}
int CCfg::get(const char* item, int def) const
{
int val = def;
for (int i = 0; i < m_num; i ++)
{
if (strcmp(m_itemName[i], item) == 0)
{
char* end;
if ((item[0] == '0') && (item[1] == 'x' || item[1] == 'X'))
val = strtol(m_itemData[i]+2, &end, 16);
else if ((item[0] == '0') && (item[1] == 'b' || item[1] == 'B'))
val = strtol(m_itemData[i]+2, &end, 2);
else
val = strtol(m_itemData[i], &end, 10);
if (*end != 0)
val = def;
break;
}
}
LOGD("CCfg::%s item=\"%s\" def=%d => %d", __FUNCTION__, item, def, val);
return val;
}
const char* CCfg::get(const char* item, const char* def) const
{
const char* val = def;
for (int i = 0; i < m_num; i ++)
{
if (strcmp(m_itemName[i], item) == 0)
{
val = m_itemData[i];
break;
}
}
LOGD("CCfg::%s item=\"%s\" def=\"%s\" => \"%s\"", __FUNCTION__, item, def ? def : "NULL", val ? val : "NULL");
return val;
}