Pagini recente » Cod sursa (job #2185956) | Cod sursa (job #455424) | Cod sursa (job #2274989) | Cod sursa (job #34979) | Cod sursa (job #1357592)
//
// main.cpp
// convertor2
//
// Created by Alex Petrache on 23/02/15.
// Copyright (c) 2015 Alex Petrache. All rights reserved.
//
#include <iostream>
#include <fstream>
using namespace std;
string linie;
string keys, values;
int main(int argc, const char * argv[]) {
ifstream f("convertor.in");
ofstream g("convertor.out");
// ifstream f("/Users/alexpetrache/Documents/Programare/Xcode/convertor2/convertor2/convertor.in");
// ofstream g("/Users/alexpetrache/Documents/Programare/Xcode/convertor2/convertor2/convertor.out");
int sz,i;
bool obj=false,firstTime=true;
bool key=false,expectKey=false,numberKey=false;
bool value=false,expectValue=false,numberValue=false;
while(getline(f,linie)){
sz=linie.size();
for(i=0;i<sz;i++){
//in cazul in care stim ca suntem intr-un key
if(key){
numberValue=false;
//cout<<"[BAG "<<linie[i]<<" ]";
if(linie[i]=='"'){
//finalul key
key=false;
keys.push_back(',');
expectValue=true;
continue;
}
else{
keys.push_back(linie[i]);
continue;
}
}
//suntem in value
if(value){
//cout<<"[[VALUE: "<<linie[i]<<", "<<numberValue<<" ]]";
if(linie[i]=='}'){
value=false;
values.push_back(',');
}
else{
if(linie[i]=='"' || linie[i]==','){
//finalul value
value=false;
values.push_back(',');
if(linie[i]==',' || (linie[i]=='"' && linie[i+1]==','))
expectKey=true;
continue;
}
if(linie[i]!=' ' || !numberValue)
values.push_back(linie[i]);
continue;
}
}
//stim ca urmeaza o valoare
if(expectValue){
numberKey=false;
if(linie[i]==':' || linie[i]==' '){
continue;
}
if(linie[i]=='"'){
expectValue=false;
value=true;
continue;
}
numberValue=true;
expectValue=false;
value=true;
values.push_back(linie[i]);
continue;
}
//stim ca urmeaza un key
if(expectKey){
if(linie[i]=='"'){
key=true;
expectKey=false;
continue;
}
}
if(linie[i]=='{'){
obj=true;
expectKey=true;
}
if(linie[i]==':')
expectValue=true;
if(linie[i]=='}'){
// cout<<'\n';
// cout<<"KEYS: "<<keys<<'\n';
// cout<<"VALEUS: "<<values<<'\n';
// cout<<"END OBJECT\n";
if(firstTime)
g<<keys<<'\n';
g<<values<<'\n';
firstTime=false;
keys="";
values="";
key=false;
expectKey=false;
value=false;
expectValue=false;
}
}
// cout<<keys;
}
return 0;
}