Pagini recente » Cod sursa (job #1276298) | Cod sursa (job #3252235) | Cod sursa (job #357099) | Cod sursa (job #362565) | Cod sursa (job #1345800)
#include <iostream>
#include <fstream>
#include<string>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
std::ifstream fin("convertor.in");
std::ofstream fout("convertor.out");
int numberOfKeys = 0;
int index=0;
int findKey(std::string line, int i) {
int j;
for (j = i + 1; j < line.length(); ++j) {
if (line.at(j) == '"') {
fout << line.substr(i + 1, j - i - 1) << ",";
numberOfKeys++;
return j;
}
}
return 0;
}
void start(std::string line) {
int position = 0, i;
int found = 0;
while (1) {
for (i = 0; i < line.length(); ++i) {
if (line.at(i) == '"') {
position = findKey(line, i);
found = 1;
break;
}
}
if (!found) {
std::getline(fin, line);
}
else {
found = 0;
line = line.substr(position + 1);
while (1) {
for (i = 0; i < line.length(); ++i) {
if (line.at(i) == ',') {
found = 1;
line = line.substr(i + 1);
break;
}
if (line.at(i) == '}') {
return;
}
}
if (!found) {
std::getline(fin, line);
}
else {
found = 0;
break;
}
}
}
}
}
std::string getValue(std::string line) {
std::string value;
int i;
while (1){
if (std::string::npos != line.find_first_of("0123456789") || std::string::npos != line.find('"')) {
line.erase(line.begin(), std::find_if(line.begin(), line.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
if (line.at(0) == '"') {
value = line.substr(1);
value = value.substr(0, value.find('"') );
}
else {
for (i = 0; i < line.length(); i++) {
if ( std::isspace((line.at(i))) || line.at(i) == ',') {
break;
}
}
value = line.substr(0, i);
}
fout << value << ",";
index++;
if (index % numberOfKeys == 0) {
fout << "\n";
}
line = line.substr(value.length());
return line;
}
else {
std::getline(fin, line);
}
}
}
int main()
{
std::string line;
std::getline(fin, line);
if (line.find('{') == std::string::npos) {
std::getline(fin, line);
}
start(line);
fout << "\n";
fin.close();
std::ifstream fin("convertor.in");
while (1) {
if (line.length() != 0) {
if (line.find(':') != std::string::npos) {
line = line.substr(line.find(':') + 1);
line = getValue(line);
} else {
if (!std::getline(fin, line)) {
break;
}
}
}
else {
if (!std::getline(fin, line)) {
break;
}
}
}
fout.close();
return 0;
}