Pagini recente » Cod sursa (job #1371074) | Cod sursa (job #1357628)
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream f("convertor.in");
ofstream g("convertor.out");
class JParser {
private:
string *json;
vector <vector <string> > values;
vector <string> keys;
public:
JParser ();
void JGetKeys();
void JPrint ();
void JDelExtra();
void JGetValues();
void JCSV();
};
JParser::JParser()
{
f.seekg (0, f.end);
int length = f.tellg();
char *buff = new char [length];
f.seekg (0, f.beg);
f.read(buff, length);
json = new string(buff);
// g << length << '\n';
delete[] buff;
}
void JParser::JCSV()
{
for(int i = 0; i < keys.size(); i++)
g << keys[i] << ',';
g << '\n';
int x = values[0].size();
for(int c = 0; c < x; c++)
{
for(int j = 0; j < values.size(); j++)
g << values[j][c] << ',';
g << '\n';
}
}
void JParser::JGetKeys()
{
string *temp = new string;
int i = 0, j = 0;
int g = 0, v = 0, s = 0;
while(json->at(i) != '}')
{
switch(json->at(i))
{
case '\n':
break;
case '"':
g++;
break;
case ':':
s++;
keys.push_back(*temp);
temp->clear();
j++;
break;
case ',':
v++;
break;
default:
if(g % 2 != 0 && s == v)
temp->push_back(json->at(i));
break;
}
i++;
}
delete temp;
}
void JParser::JGetValues()
{
values.resize(keys.size());
string *temp = new string;
int i = 0, j = 0;
int g = 0, v = 0, s = 0;
while(json->at(i) != ']')
{
switch(json->at(i))
{
case '\n':
case '[':
case '{':
break;
case '}':
values[j].push_back(*temp);
j = 0;
temp->clear();
break;
case ' ':
if(g % 2 != 0)
temp->push_back(json->at(i));
break;
case '"':
if(s > v)
g++;
break;
case ':':
s++;
break;
case ',':
v++;
if(!temp->empty())
{
values[j].push_back(*temp);
j++;
temp->clear();
}
break;
default:
if(s > v)
temp->push_back(json->at(i));
break;
}
i++;
}
delete temp;
}
void JParser::JDelExtra()
{
int s = 0;
for(size_t i = 0; i < json->length(); i++)
{
if(json->at(i) == '"')
s++;
g<< i<< ' ' << json->at(i) << ' ' << s << '\n';
if(s % 2 == 0)
if(json->at(i) == ' ' || json->at(i) == '\n')
{
json->erase(json->begin()+i);
i--;
}
}
}
int main()
{
JParser j;
j.JGetKeys();
j.JGetValues();
j.JCSV();
f.close();
g.close();
return 0;
}