Pagini recente » Cod sursa (job #538456) | Cod sursa (job #3032921) | Cod sursa (job #473287) | Cod sursa (job #1872187) | Cod sursa (job #1349258)
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
list<string> get_keys(string input_string){
list<string> keys;
std::size_t found_final = 0;
std::size_t found_start = 0;
std::size_t found_despartitor = 0;
std::size_t found_virgula = 0;
string doua_puncte = ":";
string ghilimele = "\"";
string virgula = ",";
while ( found_despartitor < input_string.size() ){
found_despartitor = input_string.find(doua_puncte);
string object = input_string.substr(0,found_despartitor);
found_virgula = object.find(virgula);
object = object.substr(found_virgula+1);
input_string = input_string.erase(0,found_despartitor+1);
found_start = object.find(ghilimele);
found_final = object.find(ghilimele,found_start+1);
object = object.substr(found_start+1,found_final - found_start -1);
keys.push_back(object);
}
return keys;
}
list<string> from_string_to_objects(string input_string){
list<string> objects;
std::size_t found_start = 0;
std::size_t found_final = 0;
string acolada_deschisa = "{";
string acolada_inchisa = "}";
while(found_final < input_string.size() )
{
//cout<<endl;
//cout<<input_string<<endl;
if( found_final != found_start){
input_string = input_string.erase(0,found_final+1);
}
found_start = input_string.find(acolada_deschisa);
found_final = input_string.find(acolada_inchisa);
string object = input_string.substr(found_start+1,found_final-found_start-1);
//cout<<object<<endl;
if(found_final < input_string.size()){
objects.push_back(object);
}
//cout<<found_start<<" "<<found_final<<endl;
//}
}
//cout<<"aici"<<endl;
return objects;
}
bool test_cifra(char c){
switch(c){
case '0':
return true;
break;
case '1':
return true;
break;
case '2':
return true;
break;
case '3':
return true;
break;
case '4':
return true;
break;
case '5':
return true;
break;
case '6':
return true;
break;
case '7':
return true;
break;
case '8':
return true;
break;
case '9':
return true;
default: return false;
}
}
list<string> get_values(string str, list<string> keys){
list<string> values;
std::size_t found_key1 = 0;
cout<<str<<endl;
int key1_length = 0;
for (std::list<string>::iterator it=keys.begin(); it != keys.end(); ++it){
string key = *it;
cout<<key<<endl;
std::size_t found_key2 = str.find(key);
string value = str.substr(found_key1 + key1_length,found_key2 - found_key1 - key1_length);
std::size_t found1 = value.find("\"");
std::size_t found2 = value.find("\"",found1+1);
if( found2 < value.size() ){
values.push_back(value);
}
found_key1 = found_key2;
key1_length = key.size();
}
string value = str.substr(found_key1 + key1_length, str.size());
values.push_back(value);
for (std::list<string>::iterator it=values.begin(); it != values.end(); ++it){
std::size_t found = (*it).find(":");
(*it).erase(0,found+1);
}
for (std::list<string>::iterator it=values.begin(); it != values.end(); ++it){
cout<<*it<<endl;
}
for (std::list<string>::iterator it=values.begin(); it != values.end(); ++it){
std::size_t found_first = (*it).find("\"");
std::size_t found_second = (*it).find("\"",found_first + 1);
if ( found_second <= (*it).size() ){
*it = (*it).substr(found_first+1,found_second-found_first-1);
}
else{
string aux = *it;
string cifra;
for ( std::string::iterator it_string=aux.begin(); it_string!=aux.end(); ++it_string){
if(test_cifra(*it_string) ){
cifra += *it_string;
}
}
*it = cifra;
}
}
return values;
}
list<list<string> > get_list_of_values(list<string> objects,list<string> keys){
list<list<string> > list_values;
while (!objects.empty())
{
list<string> values = get_values(objects.front(),keys);
objects.pop_front();
list_values.push_back(values);
}
return list_values;
}
string value_tare(string sir,int vigula_pozitie){
std::size_t found_doua_puncte = sir.find(":");
std::size_t found_start;
std::size_t found_final;
sir = sir.substr(found_doua_puncte+1,vigula_pozitie);
string value;
//daca e sir
found_start = sir.find("\"");
found_final = sir.find("\"",found_start+1);
if( found_final < sir.size()){
value = sir.substr(found_start+1,found_final - found_start -1);
}
else{
for ( std::string::iterator it=sir.begin(); it!=sir.end(); ++it){
if(*it != ' ' && *it != ','){
//cout<<"value tare "<<*it;
value += *it;
}
}
}
return value;
}
void cvs_write(list<string> keys,list<list<string> > list_values){
ofstream output_file("convertor.out");
while (!keys.empty()){
output_file<<keys.front()<<",";
keys.pop_front();
}
output_file<<"\n";
int n = list_values.size();
int i = 1;
while (!list_values.empty()){
list<string> values = list_values.front();
while (!values.empty()){
output_file<<values.front()<<",";
values.pop_front();
}
if(i<n){
output_file<<"\n";
}
i++;
list_values.pop_front();
}
output_file.close();
}
string json_read(){
string line,input_string;
ifstream input_file("convertor.in");
while ( getline (input_file,line) ){
input_string += line;
}
input_file.close();
return input_string;
}
int main () {
bool virgula = false;
bool ghilimele = false;
bool avem_key = false;
string input_string = json_read();
string input_fara_spatii;
list<string> values;
string sir;
int vigula_pozitie = 0;
std::size_t found_start;
std::size_t found_final;
/*for ( std::string::iterator it=input_string.begin(); it!=input_string.end(); ++it)
if( *it != ' '){
input_fara_spatii += *it;
}
*/
cout<<input_fara_spatii<<endl;
list<string> objects = from_string_to_objects(input_string);
cout<<"objects"<<endl;
ofstream output_file("convertor.out");
int n = objects.size();
int i = 0;
for ( std::list<string>::iterator it_object=objects.begin(); it_object!=objects.end(); ++it_object){
string object = *it_object;
//cout<< object<<endl;
string aux;
for ( std::string::iterator it=object.begin(); it!=object.end(); ++it){
aux += *it;
if( *it == ','){
virgula = true;
vigula_pozitie = it - object.begin();
}
if( *it == '"'){
ghilimele = true;
}
if( virgula && ghilimele){
virgula = false;
ghilimele = false;
if(! avem_key){
//cout<<"aici"<<aux<<endl;
found_start = aux.find("\"");
found_final = aux.find("\"",found_start+1);
string key = aux.substr(found_start+1,found_final - found_start -1);
output_file<<key<<",";
sir = value_tare(aux,vigula_pozitie);
values.push_back(sir);
aux = "";
}
else{
sir = value_tare(aux,vigula_pozitie);
output_file<<sir<<",";
i += 1;
aux = "";
}
}
}
if( ! avem_key){
found_start = aux.find("\"");
found_final = aux.find("\"",found_start+1);
string key = aux.substr(found_start+1,found_final - found_start -1);
output_file<<key<<",";
sir = value_tare(aux,vigula_pozitie);
values.push_back(sir);
output_file<<'\n';
for ( std::list<string>::iterator it=values.begin(); it!=values.end(); ++it){
output_file<<(*it)<<",";
}
aux = "";
avem_key = true;
}
else{
sir = value_tare(aux,vigula_pozitie);
output_file<<sir<<",";
i += 1;
aux = "";
}
output_file<<"\n";
}
output_file.close();
return 0;
}