Pagini recente » Cod sursa (job #1607933) | Cod sursa (job #3141108) | Cod sursa (job #1278014) | Cod sursa (job #3031246) | Cod sursa (job #1350862)
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
char *text;
int length;
int at;
FILE *out;
void read() {
FILE *in = fopen("convertor.in", "rb");
fseek(in, 0, SEEK_END);
length = ftell(in);
text = malloc((length + 1) * sizeof(char*));
fseek(in, 0, SEEK_SET);
length = fread(text, sizeof(char), length, in);
text[length] = '\0';
fclose(in);
}
void get_object_values() {
char buffer[2048];
int buffpos = 0;
while (1) {
// skip attribute name
while (text[at] != ':' && text[at] != '}') ++at;
++at;
if (text[at - 1] == '}')
break;
// iterate over spaces and new lines
while (text[at] != '\"' && !('0' <= text[at] && text[at] <= '9')
&& text[at] != '-') ++at;
// value is an integer
// check for sign
if (text[at] == '-') {
buffer[buffpos] = text[at];
++buffpos;
++at;
}
if ('0' <= text[at] && text[at] <= '9') {
while ('0' <= text[at] && text[at] <= '9') {
buffer[buffpos] = text[at];
++at;
++buffpos;
}
buffer[buffpos] = '\0';
}
// value is a 'string'
else {
++at;
while (text[at] != '\"') {
buffer[buffpos] = text[at];
++at;
++buffpos;
}
buffer[buffpos] = '\0';
}
fprintf(out, "%s,", buffer);
buffpos = 0;
}
fprintf(out, "\n");
}
void get_attributes() {
char buffer[2048];
int buffpos = 0;
while (1) {
// get attribute name
while (text[at] != '\"') ++at;
++at;
while (text[at] != '\"') {
buffer[buffpos] = text[at];
++at;
++buffpos;
}
buffer[buffpos] = '\0';
fprintf(out, "%s,", buffer);
buffpos = 0;
// check if there are more atttributes
while (text[at] != ',' && text[at] != '}') ++at;
if (text[at] == '}')
break;
}
fprintf(out, "\n");
}
void solve() {
out = fopen("convertor.out", "w");
// check for at least one object with at leat one attribute
int found = 0;
while (at < length) {
if (text[at] == '{') {
while (at < length) {
if (text[at] == '}') break;
if (text[at] == '\"') {
found = 1;
break;
}
++at;
}
break;
}
++at;
}
if (!found) return;
// get attribus and objects values
at = 0;
get_attributes();
at = 0;
while (at < length) {
if (text[at] == '{')
in_object();
++at;
}
// cleanup
free(text);
fclose(out);
}
int main() {
read();
solve();
return 0;
}