Pagini recente » Cod sursa (job #49799) | Cod sursa (job #3149570) | Cod sursa (job #489581) | Cod sursa (job #74615) | Cod sursa (job #1346741)
#include<stdio.h>
void keys(FILE* f, FILE* g)
{
int endFlag = 0, keyFlag = 1, stringFlag = 0;
char x;
while(!feof(f) && endFlag == 0){
x = getc(f);
//if the first object closes
if(x == '}'){
endFlag = 1;
}
else if(!feof(f)){
//if we read the whole key
if(x == ':'){
keyFlag = 0;
}
//true means that a key will follow after ','
else if(x == ','){
keyFlag = 1;
fprintf(g, ",");
}
//if we are at the beginning of the file
else if(x == '[' || x == '{'){
;//do nothing
}
//keep track whether i am in a string or not
else if(x == '"' && stringFlag == 0){
stringFlag = 1;
}
else if(x == '"' && stringFlag == 1){
stringFlag = 0;
}
//if the current element is not a newline or space
else if(x != '\n' && x != ' '){
//print the char if it could be part of a key
if(x != '"' && keyFlag == 1){
fprintf(g, "%c", x);
}
}
//if the key contains spaces, print them
else if(x == ' ' && stringFlag == 1 && keyFlag == 1){
fprintf(g, " ");
}
}
}
//the comma at the end of the line
fprintf(g, ",");
}
void CSV(FILE* f, FILE* g)
{
int valueFlag = 0, endFlag = 0, objectFlag = 0, stringFlag = 0;
char x;
while(endFlag == 0){
x = getc(f);
//if i reached the end of the file
if(x == ']'){
endFlag = 1;
}
//if i am at the beginning of the file
else if(x == '['){
;//do nothing
}
//if i am at the beginning of an object, start new line
else if(x == '{'){
objectFlag = 1;
fprintf(g, "\n");
}
//if i am at the end of an object
else if(x == '}'){
;//do nothing
}
//if i find a comma outside an object
else if(x == ',' && objectFlag == 0){
;//do nothing
}
//IMPORTANT: some chars were negative (-30)
else if(x > 0){
//if true, a value will follow
if(x == ':'){
valueFlag = 1;
}
//keep track whether or not i am in a string
else if(x == '"' && stringFlag == 1){
stringFlag = 0;
}
else if(x == '"' && stringFlag == 0){
stringFlag = 1;
}
//print the comma outside of a string
else if(x == ',' && stringFlag == 0){
valueFlag = 0;
fprintf(g, ",");
}
//if not newline
else if(x != '\n'){
//if i find a space inside a string, print it
if(x == ' ' && valueFlag == 1 && stringFlag == 1){
fprintf(g, " ");
}
//if i find a character inside a string, print it
else if(x != '"' && valueFlag == 1 && x != ' '){
fprintf(g, "%c", x);
}
}
}
}
fprintf(g, ",");
}
int main()
{
//open the files
FILE* f = fopen("convertor.in", "r");
FILE* g = fopen("convertor.out", "w");
//get the keys
keys(f, g);
//restart the position of the pointer in the file
fseek(f, 0, SEEK_SET);
//start getting the objects and teir values
CSV(f, g);
//close the files
fclose(f);
fclose(g);
return 0;
}