-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.c
145 lines (132 loc) · 3.56 KB
/
settings.c
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
// Settings INI file reader
//
// Copyright(C) 2016. Nebojsa Sumrak and Jelena (Petra) Markovic
//
// This program is free software; you can redistribute it and / or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA.
#include <stdio.h>
#include <ctype.h>
#include "platform.h"
#include "common.h"
#include "settings.h"
#include "debug.h"
BOOL settings_get(const char *iname, char *value, int valsize)
{
fs_file fh;
int offset, readed, inname;
int state=0, navodnik=0;
char buffek[256], name[32];
// char section[32];
if((fh=fs_open(SETTINGS_FILE, FS_OPEN_MODE_READ)) == -1) return FALSE;
// section[0]=0;
valsize--; // trailing zero
for(offset = 0; state!=6 && (readed = fs_read(fh,offset,buffek,sizeof(buffek)-1)) > 0; offset += readed) {
char *t = buffek, *e = buffek+readed;
while(state!=6 && t<e) {
switch(state) {
case 0: // new line beginning
if(isspace(*t)) break;
state = 1; // drop through
case 1: // beginning of string
// if(*t=='[') { // section
// state = ;
// break;
// } else
if(*t==';') { // comment - skip till EOL
navodnik = 0;
state = 5;
break;
}
inname = 0;
state = 2; // drop through
case 2: // in name of name/value
if(inname == sizeof(name)-1) break;
if(isalnum(*t)) {
name[inname++] = *t;
break;
} else {
name[inname] = 0;
state = 3; // drop through
}
case 3: // between name and value
if(*t == '\n' || *t == '\r') {
if(!strncmp(name, iname, sizeof(name)-1)) {
*(value++) = '1'; // boolean
valsize--;
state = 6;
continue;
} else {
state = 0;
break;
}
}
if(*t != ' ' && *t != '\t' && *t != '=' && *t != ':') {
state = (!strncmp(name, iname, sizeof(name)-1)) ? 4 : 5;
if((navodnik = (*t=='"'))) break;
continue;
}
break;
case 4: // in value of name/value - copying
if(navodnik) {
if(*t == '"') {
state = 6;
continue;
}
} else if(*t == '\n' || *t == '\r') {
state = 6;
continue;
}
if(!valsize) break;
*(value++) = *t;
valsize--;
break;
case 5: // in value of name/value - skipping
if(navodnik) {
if(*t == '"') {
navodnik = 0; // after closed quote skip till EOL
break;
}
} else if(*t == '\n' || *t == '\r') {
state = 0;
continue;
}
break;
case 6: // found name/value
continue;
}
t++;
}
}
fs_close(fh);
if(state==4 || state==6) {
*value=0;
return TRUE;
}
return FALSE;
}
void settings_write(char *buf, int contentlength)
{
_i32 fh;
UART_PRINT("writing settings len: %d '%s'\n\r", contentlength, buf);
sl_FsDel(SETTINGS_FILE, 0);
short err = sl_FsOpen(SETTINGS_FILE, FS_MODE_OPEN_CREATE(contentlength, _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST), 0, &fh);
if(err)
{
UART_PRINT("error (%d) writing settings.ini file\n\r", err);
return;
}
sl_FsWrite(fh, 0, (_u8*)buf, contentlength);
sl_FsClose(fh, 0, 0, 0);
UART_PRINT("new settings written\n\r");
}