forked from antirez/smaz
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsubforms.c
More file actions
168 lines (148 loc) · 5.02 KB
/
subforms.c
File metadata and controls
168 lines (148 loc) · 5.02 KB
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
163
164
165
166
167
168
/*
Copyright (C) 2016 Paul Gardner-Stephen, Flinders University.
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 <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include<sys/types.h>
#include<sys/time.h>
#include "charset.h"
#include "visualise.h"
#include "arithmetic.h"
#include "packed_stats.h"
#include "smac.h"
#include "recipe.h"
#include "subforms.h"
#ifdef ANDROID
#include <jni.h>
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "libsmac", __VA_ARGS__))
#define C LOGI("%s:%d: checkpoint",__FILE__,__LINE__);
#else
#define LOGI(...)
#define C
#endif
int record_free(struct record *r)
{
if (!r) return -1;
for(int i=0;i<r->field_count;i++)
{
if (r->fields[i].key) free(r->fields[i].key);
r->fields[i].key=NULL;
if (r->fields[i].value) free(r->fields[i].value);
r->fields[i].value=NULL;
if (r->fields[i].subrecord)
record_free(r->fields[i].subrecord);
r->fields[i].subrecord=NULL;
}
free(r);
return 0;
}
struct record *parse_stripped_with_subforms(char *in,int in_len)
{
struct record *record=calloc(sizeof(record),1);
struct record *current_record=record;
int i;
char line[1024];
char key[1024],value[1024];
int line_number=1;
int l=0;
for(i=0;i<=in_len;i++) {
if ((i==in_len)||(in[i]=='\n')||(in[i]=='\r')) {
// Process key=value line
if (l>=1000) l=0; // ignore long lines
line[l]=0;
if (line[0]=='{') {
// Start of sub-form
// Move current record down into a new sub-record at this point
if (current_record->field_count>=MAX_FIELDS) {
snprintf(recipe_error,2048,"line:%d:Too many data lines (must be <=%d, or increase MAX_FIELDS).\n",
line_number,MAX_FIELDS);
record_free(record);
return NULL;
}
current_record->fields[record->field_count].subrecord=calloc(sizeof(struct record),1);
current_record->fields[record->field_count].subrecord->parent=current_record;
current_record->field_count++;
current_record=current_record->fields[record->field_count-1].subrecord;
} else if (line[0]=='}') {
// End of sub-form
if (!current_record->fields[record->field_count].subrecord->parent) {
snprintf(recipe_error,2048,"line:%d:} without matching {.\n",
line_number);
record_free(record);
return NULL;
}
// Find the question field name, so that we can promote it to our caller
char *question=NULL;
for(i=0;i<current_record->field_count;i++) {
if (!strcmp("question",current_record->fields[i].key)) {
// Found it
question=current_record->fields[i].value;
}
}
if (!question) {
snprintf(recipe_error,2048,"line:%d:No 'question' value in sub-form.\n",
line_number);
record_free(record);
return NULL;
}
// Step back out to parent
current_record->fields[record->field_count].subrecord
=current_record->fields[record->field_count].subrecord->parent;
// Update key name for sub-form we have exited to match the question name
current_record->fields[record->field_count-1].key=strdup(question);
} else if ((l>0)&&(line[0]!='#')) {
if (sscanf(line,"%[^=]=%[^\n]",key,value)==2) {
if (current_record->field_count>=MAX_FIELDS) {
snprintf(recipe_error,2048,"line:%d:Too many data lines (must be <=%d, or increase MAX_FIELDS).\n",
line_number,MAX_FIELDS);
record_free(record);
return NULL;
}
current_record->fields[current_record->field_count].key=strdup(key);
current_record->fields[current_record->field_count].key=strdup(value);
current_record->field_count++;
} else {
snprintf(recipe_error,2048,"line:%d:Malformed data line (%s:%d): '%s'\n",
line_number,__FILE__,__LINE__,line);
record_free(record);
return NULL;
}
}
line_number++;
l=0;
} else {
if (l<1000) { line[l++]=in[i]; }
else {
if (l==1000) {
fprintf(stderr,"line:%d:Line too long -- ignoring (must be < 1000 characters).\n",line_number);
LOGI("line:%d:Line too long -- ignoring (must be < 1000 characters).\n",line_number);
}
l++;
}
}
}
if (current_record->fields[record->field_count].subrecord->parent) {
snprintf(recipe_error,2048,"line:%d:End of input, but } expected.\n",
line_number);
record_free(record);
return NULL;
}
printf("Read %d data lines, %d values.\n",line_number,record->field_count);
LOGI("Read %d data lines, %d values.\n",line_number,record->field_count);
return record;
}