Pagini recente » Cod sursa (job #1269108) | Cod sursa (job #782132) | Cod sursa (job #1216900) | Cod sursa (job #1961165) | Cod sursa (job #1337775)
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <vector>
const char IN[] = "convertor.in", OUT[] = "convertor.out";
std::ifstream fin(IN);
std::ofstream fout (OUT);
std::string line;
std::string text;
std::set<std::string> header;
std::vector<std::map<std::string, std::string> > values;
std::map<std::string, std::string> m;
void get( std::string const & s, std::string & t, int & poz ) {
t = "";
for ( ++ poz; s[poz] != '\"'; ++ poz )
t += s[poz];
}
void get2( std::string const & s, std::string & t, int & poz ) {
t = "";
for ( ++ poz; s[poz] == ' '; ++ poz );
for ( ; s[poz] != ',' && s[poz] != '}'; ++ poz )
t += s[poz];
}
int main() {
while (std::getline(fin, line))
text += line;
for ( int i = 1; i < text.length(); ++ i ) {
if ( text[i] == '{' ) {
m.clear();
for ( ; text[i] != '}' ; ++ i ) {
if ( text[i] == '\"') {
std::string name, value;
get( text, name, i );
for ( ++ i; text[i] != ':'; ++ i );
get2( text, value, i );
header.insert(name);
while ( value[value.length()] == ' ') value = value.substr(0, value.length() - 1);
while ( value[0] == ' ') value = value.substr(1);
if ( value[0] == '\"') {
value = value.substr(1, value.length());
while ( value[value.length() - 1] != '\"') value = value.substr(0,value.length() - 1);
value = value.substr(0, value.length() - 1);
}
m[name] = value;
}
if ( text[i] == '}' ) -- i;
}
values.push_back(m);
}
}
for ( auto it = header.begin(); it != header.end(); ++ it )
fout << * it << ",";
fout << std::endl;
for ( int i = 0; i < values.size(); ++ i ) {
for ( auto it = header.begin(); it != header.end(); ++ it ) {
fout << values[i][*it] << ",";
}
fout << std::endl;
}
return 0;
}