COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
tab2json.cpp File Reference
Include dependency graph for tab2json.cpp:

Functions

string skip_undesired (string input)
 
int main (int argc, char *argv[])
 

Function Documentation

string skip_undesired ( string  input)
94 {
95  string output;
96  for (uint16_t i = 0; i<input.length()-1; ++i)
97  {
98  if (input[i] == '"' && input[i+1] == '"')
99  {
100  continue;
101  }
102  if (input[i] == '\r' || input[i] == '\n')
103  {
104  continue;
105  }
106  output.push_back(input[i]);
107  }
108  if (input.back() != '\r' && input.back() != '\n')
109  {
110  output.push_back(input.back());
111  }
112  return output;
113 }
int i
Definition: rw_test.cpp:37
string output
Definition: agent-2-0.cpp:56
int main ( int  argc,
char *  argv[] 
)
9 {
10  char buf[1000];
11  FILE *fpi = nullptr;
12  FILE *fpo = nullptr;
13 
14  if (argc != 3)
15  {
16  printf("Usage: tab2json [-|filename.tab] [-|filename.json]\n");
17  exit(GENERAL_ERROR_ARGS);
18  }
19 
20  if (argv[1][0] == '-')
21  {
22  fpi = stdin;
23  }
24  else
25  {
26  fpi = fopen(argv[1], "r");
27  }
28 
29  if (argv[2][0] == '-')
30  {
31  fpo = stdout;
32  }
33  else
34  {
35  fpo = fopen(argv[2], "w");
36  }
37 
38  if (fpi == nullptr || fpo == nullptr)
39  {
40  printf("Usage: tab2json [-|filename.tab] [-|filename.json]\n");
41  exit(GENERAL_ERROR_ARGS);
42  }
43 
44  if (fgets(buf, 1000, fpi) == nullptr)
45  {
46  exit(-errno);
47  }
48  vector <string> fields = string_split(skip_undesired(buf), "\t");
49 
50  while (fgets(buf, 1000, fpi) != nullptr)
51  {
52  vector <string> values = string_split(skip_undesired(buf), "\t");
53  JSONObject jobject;
54  for (uint16_t i=0; i<fields.size(); ++i)
55  {
56  if (fields[i].find("event_utc") == 0)
57  {
58  jobject.addElement(fields[i], stof(values[i]));
59  }
60  else if (fields[i].find("event_name") == 0)
61  {
62  jobject.addElement(fields[i], values[i]);
63  }
64  else if (fields[i].find("event_type") == 0)
65  {
66  jobject.addElement(fields[i], stoi(values[i]));
67  }
68  else if (fields[i].find("event_flag") == 0)
69  {
70  jobject.addElement(fields[i], stoi(values[i]));
71  }
72  else if (fields[i].find("event_data") == 0)
73  {
74  jobject.addElement(fields[i], values[i]);
75  }
76  else if (fields[i].find("event_condition") == 0)
77  {
78  if (values[i][0] == '"')
79  {
80  jobject.addElement(fields[i], values[i].substr(1,values[i].size()-2));
81  }
82  else
83  {
84  jobject.addElement(fields[i], values[i]);
85  }
86  }
87  }
88  fprintf(fpo, "%s\n", jobject.to_json_string().c_str());
89  }
90 
91 }
int i
Definition: rw_test.cpp:37
string skip_undesired(string input)
Definition: tab2json.cpp:93
#define GENERAL_ERROR_ARGS
Definition: cosmos-errno.h:305
Definition: jsonobject.h:5
void addElement(string key, JSONValue value)
Definition: jsonobject.cpp:10
char buf[128]
Definition: rw_test.cpp:40
string to_json_string()
Definition: jsonobject.cpp:91
vector< string > string_split(string in, string delimeters)
Parse a string.
Definition: stringlib.cpp:47