rllib  1
rljson.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rljson.cpp - description
3  -------------------
4  begin : Sat Jul 29 2023
5  copyright : (C) Lehrig Software Enigineering
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rljson.h"
17 
19 {
20 }
21 
23 {
24 }
25 
27 {
28  record->startsWithText = 0; // "text"
29  record->startsWithOpenBracket = 0; // {
30  record->startsWithCloseBracket = 0; // }
31  record->startsWithEndArray = 0; // ]
32  record->text.setText(""); // white space in front of the tag name is ignored
33  record->optColon = 0; // a colon must follow when text is a tag
34  record->optOpenBracket = 0; // a openBracket { might follow
35  record->optBeginArray = 0; // a beginArray [ might follow
36  record->optParamType = 0; // 0=default=text 1=integer 2=float
37  record->param.setText(""); // the parameter in text form
38  record->intParam = 0; // 0 if there is no integer number
39  record->floatParam = 0.0f; // 0.0 if there is no floating point number
40  record->optComma = 0; // tells us that more elements follow
41  return 0;
42 }
43 
44 int rlJSON::interpret(const char *line, rlJSONrecord *record)
45 {
46  if(line == NULL) return -1;
47  if(record == NULL) return -1;
48  initRecord(record);
49 
50  int i=0;
51  while(line[i] == ' ') i++;
52  if (line[i] == '"') record->startsWithText = 1;
53  else if(line[i] == '{') record->startsWithOpenBracket = 1;
54  else if(line[i] == '}') record->startsWithCloseBracket = 1;
55  else if(line[i] == ']') record->startsWithEndArray = 1;
56 
57  if(record->startsWithText)
58  {
59  i++;
60  rlString text(&line[i]);
61  char *start = text.text();
62  char *end = strchr(start,'"');
63  if(end != NULL)
64  {
65  *end = '\0';
66  record->text = start;
67  end++;
68  start = end;
69  start = strchr(start,'"');
70  if(start != NULL)
71  {
72  start++;
73  end = strchr(start,'"');
74  if(end != NULL)
75  {
76  *end = '\0';
77  record->param = start;
78  }
79  }
80  }
81  }
82  else if(record->startsWithOpenBracket == 1)
83  {
84  }
85  else if(record->startsWithCloseBracket == 1)
86  {
87  }
88  else if(record->startsWithEndArray == 1)
89  {
90  }
91  else // this must be either an empty line or an integer or float parameter
92  {
93  if(strchr(line,'.') != NULL)
94  {
95  record->optParamType = 2; // float
96  record->param = &line[i];
97  char *cptr = record->param.text();
98  cptr = strchr(cptr,',');
99  if(cptr != NULL) *cptr = '\0';
100  }
101  else
102  {
103  if(line[i] >= '0' && line[i] <= '9') record->optParamType = 1; // integer
104  else if(line[i] == '-') record->optParamType = 1; // integer
105  else if(line[i] == '+') record->optParamType = 1; // integer
106  if(record->optParamType == 1) record->param = &line[i];
107  char *cptr = record->param.text();
108  cptr = strchr(cptr,',');
109  if(cptr != NULL) *cptr = '\0';
110  }
111  }
112 
113  if(strchr(line,':') != NULL) record->optColon = 1; // a colon must follow when text is a tag
114  if(strchr(line,'{') != NULL) record->optOpenBracket = 1; // a openBracket { might follow
115  if(strchr(line,'[') != NULL) record->optBeginArray = 1; // a beginArray [ might follow
116  if(strchr(line,',') != NULL) record->optComma = 1; // tells us that more elements follow
117 
118  return 0;
119 }
120 
121 int rlJSON::print(FILE *fout, rlJSONrecord *record, const char *indent)
122 {
123  if(record == NULL) return -1;
124  if(indent != NULL) printf("%s",indent);
125  if(record->startsWithText)
126  {
127  fprintf(fout,"\"%s\"",record->text.text());
128  if(record->optOpenBracket == 0 && record->optBeginArray == 0)
129  {
130  if(record->optColon == 1)
131  {
132  fprintf(fout,": ");
133  if(record->optParamType == 0) fprintf(fout,"\"%s\"",record->param.text());
134  else fprintf(fout,"%s",record->param.text());
135  if(record->optComma == 1) fprintf(fout,",");
136  fprintf(fout,"\n");
137  }
138  if(record->optColon == 0)
139  {
140  if(record->optComma == 1) fprintf(fout,",");
141  fprintf(fout,"\n");
142  }
143  }
144  else if(record->optOpenBracket == 1)
145  {
146  if(record->optColon == 1) fprintf(fout,":");
147  fprintf(fout," ");
148  if(record->optParamType == 0) fprintf(fout,"\"%s\"",record->param.text());
149  else fprintf(fout,"%s",record->param.text());
150  fprintf(fout," {");
151  fprintf(fout,"\n");
152  }
153  else if(record->optBeginArray == 1)
154  {
155  if(record->optColon == 1) fprintf(fout,":");
156  fprintf(fout," [\n");
157  }
158  }
159  else if(record->startsWithOpenBracket == 1)
160  {
161  fprintf(fout,"{");
162  fprintf(fout,"\n");
163  }
164  else if(record->startsWithCloseBracket == 1)
165  {
166  fprintf(fout,"}");
167  if(record->optComma == 1) fprintf(fout,",");
168  fprintf(fout,"\n");
169  }
170  else if(record->startsWithEndArray == 1)
171  {
172  fprintf(fout,"]");
173  if(record->optComma == 1) fprintf(fout,",");
174  fprintf(fout,"\n");
175  }
176  return 0;
177 }
178 
179 #define TESTING
180 #ifdef TESTING
181 int main()
182 {
183  printf("Test rljson begin:\n");
184  rlJSON rljson;
185  rlJSONrecord record;
186  int ret = rljson.initRecord(&record);
187  printf("rljson.initRecord ret=%d\n", ret);
188 
189  record.startsWithText = 1;
190  record.text.setText("id");
191  record.optColon = 1;
192  record.optOpenBracket = 1;
193  record.optParamType = 1; // 0=default=text 1=integer 2=float
194  record.param.setText("overture:places:place:1");
195  record.optComma = 0;
196 
197  printf("startsWithText=%d\n", record.startsWithText);
198  printf("startsWithOpenBracket=%d\n", record.startsWithOpenBracket);
199  printf("startsWithCloseBracket=%d\n", record.startsWithCloseBracket);
200  printf("startsWithEndArray=%d\n", record.startsWithEndArray);
201  printf("optColon=%d\n", record.optColon);
202  printf("optOpenBracket=%d\n", record.optOpenBracket);
203  printf("optBeginArray=%d\n", record.optBeginArray);
204  printf("optComma=%d\n", record.optComma);
205  printf("optParamType=%d (0=default=text 1=integer 2=float)\n", record.optParamType);
206 
207  rljson.print(stdout,&record);
208  printf("rljson.print ret=%d\n", ret);
209 
210  FILE *fin = fopen("/home/lehrig/temp/json1.txt","r");
211  if(fin == NULL)
212  {
213  printf("could not open /home/lehrig/temp/json1.txt\n");
214  }
215  else
216  {
217  char line[1024];
218  while(fgets(line,1023,fin) != NULL)
219  {
220  printf("json1.txt: %s", line);
221  rljson.interpret(line, &record);
222  printf("DEBUG: text=%s param=%s withText=%d\n"
223  "startsWith: openBracket=%d closeBracket=%d endArray=%d\n"
224  "opt: colon=%d openBracket=%d beginArray=%d paramType=%d comma=%d\n",
225  record.text.text(), record.param.text(), record.startsWithText,
227  record.optColon, record.optOpenBracket, record.optBeginArray, record.optParamType, record.optComma
228  );
229  /*
230  int startsWithText; // "text"
231  int startsWithOpenBracket; // {
232  int startsWithCloseBracket; // }
233  int startsWithEndArray; // ]
234  rlString text; // white space in front of the tag name is ignored
235  int optColon; // a colon must follow when text is a tag
236  int optOpenBracket; // a openBracket { might follow
237  int optBeginArray; // a beginArray [ might follow
238  int optParamType; // 0=default=text 1=integer 2=float
239  rlString param; // the parameter in text form
240  int intParam; // 0 if there is no integer number
241  float floatParam; // 0.0 if there is no floating point number
242  int optComma; // tells us that more elements follow
243  */
244  }
245  }
246  fclose(fin);
247  return 0;
248 }
249 #endif
250 
virtual ~rlJSON()
Definition: rljson.cpp:22
float floatParam
Definition: rljson.h:40
int print(FILE *fout, rlJSONrecord *record, const char *indent="")
Definition: rljson.cpp:121
rlString text
Definition: rljson.h:33
int startsWithText
Definition: rljson.h:29
int interpret(const char *line, rlJSONrecord *record)
Definition: rljson.cpp:44
int main()
Definition: rljson.cpp:181
int startsWithOpenBracket
Definition: rljson.h:30
int optOpenBracket
Definition: rljson.h:35
char * text()
Definition: rlstring.cpp:126
rlString param
Definition: rljson.h:38
int optComma
Definition: rljson.h:41
int intParam
Definition: rljson.h:39
int setText(const char *text)
Definition: rlstring.cpp:136
rlJSON()
Definition: rljson.cpp:18
int optBeginArray
Definition: rljson.h:36
int startsWithEndArray
Definition: rljson.h:32
int startsWithCloseBracket
Definition: rljson.h:31
int optParamType
Definition: rljson.h:37
int initRecord(rlJSONrecord *record)
Definition: rljson.cpp:26
int optColon
Definition: rljson.h:34
Definition: rljson.h:47