#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TTL 2048 //Total
#define HLF TTL/2 //Half
int blk_read(char *, FILE *);
int main()
{
FILE *f = fopen("convertor.in", "r"), *g = fopen("convertor.out", "w");
char *line1, *c, a[100], b[100], *p;
int i, ok = 0, nr = 0, k, test = 0;
line1 = calloc(TTL, 1);
memset(a, 0, 100);
memset(b, 0, 100);
while (fread(line1 + HLF, 1, HLF, f)) {
c = strchr(line1 + HLF, ':');
while (c) {
for (i = 0; c[i] != '"'; i--) ;
p = c + i - 1;
for (i = 0; p[i] != '"'; i--) ;
p = p + i + 1;
if (!nr) {
strncpy(a, p, -i);
nr++;
fputs(a, g);
fputc(',', g);
} else {
strncpy(b, p, -i);
if (strcmp(a, b) == 0) {
ok = -1;
break;
}
nr++;
fputs(b, g);
fputc(',', g);
memset(b, 0, 100);
}
c = strchr(c + 1, ':');
}
memcpy(line1, line1 + HLF, HLF);
memset(line1 + HLF, 0, HLF);
if (ok == -1)
break;
}
fputc('\n', g);
fseek(f, 0, 0);
k = 0;
ok = fread(line1, 1, TTL, f);
while (ok || test) {
c = strchr(line1, ':');
while (c && c < line1 + HLF) {
for (c = c + 1; *c < 33; c++) {
if (c - line1 >= TTL) {
ok = blk_read(line1, f);
c = line1 + HLF - 1;
}
}
if (*c == '"') {
c++;
while (*c != '"') {
fputc(*c, g);
c++;
if (c - line1 >= TTL) {
ok = blk_read(line1, f);
c = line1 + HLF;
}
}
} else
while (strchr("0123456789-", *c)) {
fputc(*c, g);
c++;
if (c - line1 >= TTL) {
ok = blk_read(line1, f);
c = line1 + HLF;
}
}
k++;
fputc(',', g);
if (k == nr) {
k = 0;
fputc('\n', g);
}
c = strchr(c, ':');
}
ok = blk_read(line1, f);
if (test)
break;
if (!ok) {
test = 1;
memcpy(line1 + HLF, line1, HLF);
}
}
free(line1);
fclose(f);
fclose(g);
return 0;
}
int blk_read(char *line, FILE * f)
{
memcpy(line, line + HLF, HLF);
memset(line + HLF, 0, HLF);
return fread(line + HLF, 1, HLF, f);
}