Pagini recente » Cod sursa (job #451776) | Cod sursa (job #2146869) | Cod sursa (job #1633757) | Cod sursa (job #1790866) | Cod sursa (job #1360946)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *in = fopen("convertor.in","r");
FILE *out = fopen("convertor.out","w");
char read[1024], *ch, key[1024];
int ok = 0, current = 0, i, length, item = 0, number, check = 0;
while (ok == 0 && fgets(read,1024,in) != NULL) {
ch = read;
while (*ch != '\0') {
if (ch[0] == '}') {
ok = 1;
break;
}
if (ch[0] == '"') {
if (item % 2 == 0) {
char *pass = ch + 1;
char *pass2 = strchr(pass,'"');
length = strlen(pass) - strlen(pass2);
strncpy(key,pass,length);
key[length] = '\0';
ch = ch + strlen(pass) - strlen(pass2) + 2;
fprintf(out,"%s,",key);
}
if (item % 2 == 1) {
char *pass = ch + 1;
char *pass2 = strchr(pass,'"');
ch = ch + strlen(pass) - strlen(pass2) + 2;
}
}
else {
if (ch[0] == ':' || ch[0] == ',')
item++;
ch++;
}
}
}
fclose(in);
in = fopen("convertor.in","r");
fprintf(out,"\n");
number = (item + 1) / 2;
item = 0;
while (fgets(read,1024,in) != NULL) {
ch = read;
while (*ch != '\0') {
if (item % (number * 2) == 0 && check == 1)
fprintf(out,"\n");
if (ch[0] == '"') {
if (item % 2 == 0) {
char *pass = ch + 1;
char *pass2 = strchr(pass,'"');
ch = ch + strlen(pass) - strlen(pass2) + 2;
}
if (item % 2 == 1) {
char *pass = ch + 1;
char *pass2 = strchr(pass,'"');
length = strlen(pass) - strlen(pass2);
strncpy(key,pass,length);
key[length] = '\0';
ch = ch + strlen(pass) - strlen(pass2) + 2;
// printf("2 %s %d\n",key,item);
fprintf(out,"%s,",key);
}
check = 0;
}
else {
if (ch[0] >= '0' && ch[0] <= '9' && item % 2 == 1) {
char *pass = ch;
while (pass[0] >= '0' && pass[0] <= '9')
pass++;
length = strlen(ch) - strlen(pass);
strncpy(key,ch,length);
key[length] = '\0';
ch = ch + strlen(ch) - strlen(pass);
// printf("1 %s %d\n",key,item);
fprintf(out,"%s,",key);
check = 0;
}
else {
if (ch[0] == ':' || ch[0] == ',') {
item++;
check = 1;
}
else
check = 0;
ch++;
}
}
}
}
fclose(in);
fclose(out);
return 0;
}