Pagini recente » Cod sursa (job #2065878) | Cod sursa (job #1946549) | Cod sursa (job #1462296) | Cod sursa (job #1716131) | Cod sursa (job #1340518)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINE_LENGTH 1024
#define INPUT_FILE "convertor.in"
#define OUTPUT_FILE "convertor.out"
typedef struct pair {
char *key;
char *value;
} pair;
int main() {
// Declaration part
FILE *fdin = NULL;
FILE *fdout = NULL;
char line[LINE_LENGTH + 2];
int mark_for_first_entry = 0, mark_for_colon = 0, mark_for_paranthesis = 0;
int position, pair_index = 0, i;
int pair_max = 512;
char *indicator;
pair *e;
// Open files
fdin = fopen( INPUT_FILE, "r" );
fdout = fopen( OUTPUT_FILE, "w" );
// Initial malloc for the number of pairs (key, value) in a single entry
e = (pair*)malloc( pair_max * sizeof( pair ) );
// Read a single line of text
indicator = fgets( line, LINE_LENGTH + 1, fdin );
while ( indicator != NULL ) {
position = 0;
int length = strlen(line);
while ( position < length ) {
if ( line[ position ] == '}' ) {
// Print the table header if necessary
if ( !mark_for_first_entry ) {
for ( i = 0; i < pair_index; i++ ) {
fprintf( fdout, "%s,", e[i].key );
}
fprintf( fdout, "\n" );
mark_for_first_entry = 1;
}
// Print the entry
for ( i = 0; i < pair_index; i++ ) {
fprintf(fdout, "%s,", e[i].value);
}
fprintf(fdout, "\n");
// Reinit parameters
mark_for_colon = 0;
mark_for_paranthesis = 0;
pair_index = 0;
}
// Read the key
if ( line[ position ] == '"' && !mark_for_colon ) {
int prev = position + 1;
position = position + 2;
while ( line[ position ] != '"' && position < length ) {
++position;
}
// Check if we need to realloc
if ( pair_index + 1 == pair_max ) {
pair *new_p = (pair*)realloc( e, 2 * pair_max * sizeof(pair) );
if ( new_p != NULL ) e = new_p;
pair_max = 2 * pair_max;
}
e[pair_index++].key = strndup( line + prev, position - prev );
}
if ( line[ position ] >= '0' && line[ position ] <= '9' && !mark_for_colon ) {
int prev = position;
position = position + 1;
while ( line[ position ] >= '0' && line[ position ] <= '9' && position < length ) {
++position;
}
// Check if we need to realloc
if ( pair_index + 1 == pair_max ) {
pair *new_p = (pair*)realloc( e, 2 * pair_max * sizeof(pair) );
if ( new_p != NULL ) e = new_p;
pair_max = 2 * pair_max;
}
e[pair_index++].key = strndup( line + prev, position - prev );
}
// Read the colon
if ( line[ position ] == ':' ) {
++position;
// Clear multiple spaces
while ( line[ position ] == ' ' && position < length ) {
++position;
}
mark_for_colon = 1;
}
// Read the value
if ( line[ position ] == '"' && mark_for_colon ) {
int prev = position + 1;
position = position + 2;
while ( line[ position ] != '"' && position < length ) {
++position;
}
e[ pair_index - 1 ].value = strndup( line + prev, position - prev );
}
if ( line[ position ] >= '0' && line[ position ] <= '9' && mark_for_colon ) {
int prev = position;
++position;
while ( line[ position ] >= '0' && line[ position ] <= '9' && position < length ) {
++position;
}
e[ pair_index - 1 ].value = strndup( line + prev, position - prev );
}
if ( line[ position ] == ',' ) mark_for_colon = 0;
position++;
}
// read the next line
indicator = fgets( line, LINE_LENGTH + 1, fdin );
}
return 0;
}