Pagini recente » Cod sursa (job #1353710) | Cod sursa (job #2316955) | Cod sursa (job #2936017) | Cod sursa (job #425272) | Cod sursa (job #1354862)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING 2
#define INT 1
int main()
{
FILE* IN = fopen("convertor.in", "r");
FILE* OUT = fopen("convertor.out", "w");
char line[1024], *readBuffer, ***table;
int length, crtKey = 0, crtVal = 0, crtObj = 0, bufferCrt = 0, bufferLength = 10, bufferStep = 10;
int i, j;
short isReading = 0, isValue = 0, type = 0;
readBuffer = (char *)malloc(bufferLength * sizeof(char));
table = (char***)malloc( sizeof(char**) );//table header / keys' row
table[0] = (char**)malloc(sizeof(char*));
//start of JSON
while ( fgetc(IN) != '[' );
while( fgets(line, 1024, IN) != NULL )
{
length = strlen(line);
for( i = 0 ; i < length ; i++ )
{
if( isReading )
{
//get value type, if not done so already
if(!type && isValue)
{
if(line[i] >= '0' && line [i] <= '9')
type = INT; //int
else if(line[i] == '"')
{
type = STRING; //string
continue; //string-ul incepe de la urmatorul caracter
}
else
continue;//salt peste caracterele dintre : si value
}
//sfarsitul citirii unei chei sau valori
if( (type == STRING && line[i] == '"') ||
(type == INT && !(line[i] >= '0' && line [i] <= '9') ) )
{
readBuffer[bufferCrt++] = '\0';//end of string
//write the keys in table header, only for first object
if(!isValue && crtObj == 1)
{
table[0] = (char**)realloc( table[0], (crtKey+1) * sizeof(char*));
table[0][crtKey] = (char*)malloc( bufferCrt * sizeof(char) );
//copy the key from buffer to table
for( j=0 ; j<bufferCrt ; j++ )
table[0][crtKey][j] = readBuffer[j];
crtKey++;
}
else if(isValue)
{
table[crtObj] = (char**)realloc( table[ crtObj ], (crtVal+1) * sizeof(char*));
table[crtObj][crtVal] = (char*)malloc( bufferCrt * sizeof(char) );
//copy the value from buffer to table
for( j=0 ; j<bufferCrt ; j++)
table[crtObj][crtVal][j] = readBuffer[j];
crtVal++;
}
//reset the reading buffer
free(readBuffer);
bufferCrt = isReading = isValue = type = 0;
bufferLength = 10;
readBuffer = (char *) malloc (bufferLength * sizeof(char));
continue;
}
//insert current char in buffer
if(bufferCrt <= bufferLength)
{
bufferLength += bufferStep;
readBuffer = (char *)realloc( readBuffer, bufferLength * sizeof(char) );
}
readBuffer[bufferCrt++] = line[i];
}
else
{
//daca nu citeste o cheie sau o valoare
switch(line[i])
{
case '"': //cheie
isReading = 1;
isValue = 0;
type = STRING; //cheia e de tip string
break;
case ':': //valoare
isReading = 1;
isValue = 1;
break;
case '{': //next object
crtObj++;
crtVal = 0;
table = (char***)realloc(table, (crtObj+1) * sizeof(char**));
table[crtObj] = (char**)malloc(sizeof(char*));
break;
default: //pentru spatii, tab-uri...
continue;
}
}
}
}
//print
for(i = 0 ; i <= crtObj ; i++)
{
for(j = 0 ; j < crtKey ; j++)
fprintf(OUT, "%s,", table[i][j]);
fprintf(OUT, "\n");
}
//free memory
for( i = 0 ; i <= crtObj ; i++)
{
for( j = 0 ; j < crtKey ; j++)
free( table[i][j] );
free( table[i] );
}
free( table );
//close files
fclose( IN );
fclose( OUT );
return 0;
}