#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char **argv)
{
if (3 != argc)
{
fprintf(stderr, "%s <inputfile> <outputfile>.\n", argv[0]);
return EXIT_FAILURE;
}
// Try to open input file
FILE *fInput = fopen(argv[1], "r");
if (NULL == fInput)
{
fprintf(stderr, "Could not open input file.\n");
return EXIT_FAILURE;
}
// Try to open output file
FILE *fOutput = fopen(argv[2], "w");
if (NULL == fInput)
{
fprintf(stderr, "Could not open output file.\n");
return EXIT_FAILURE;
}
// Determine the data length
fseek(fInput, 0L, SEEK_END);
long fInput_size = ftell(fInput);
fseek(fInput, 0L, SEEK_SET);
// Allocate memory
char *data = (char *)malloc(fInput_size);
if (NULL == data)
{
fprintf(stderr, "Could not allocate enough memory.\n");
return EXIT_FAILURE;
}
// Read the file content
if (0 == fread(data, fInput_size, 1, fInput))
{
fprintf(stderr, "Could not read entire file.\n");
return EXIT_FAILURE;
}
fclose(fInput);
// Remove the first line
char *p;
char *end = data + fInput_size;
for(p = strstr(data, "\n") + 1; p < (data + fInput_size); )
{
// Determine the next row
char *next_row = strstr(p, "\n");
if (NULL == next_row)
{
next_row = end;
}
// Skip the first 3 ";"
unsigned int i;
for(i = 0; i < 3; i++)
{
p = strstr(p, ";") + 1;
}
// Work remaining fields
for(i = 0; p < next_row; i++)
{
// Determine the next separator
char *next_semi = strstr(p, ";");
if (NULL == next_semi || next_semi > next_row)
{
next_semi = next_row;
}
if (0 == strncmp("unclassified", p, 12))
{
fprintf(fOutput, "0 ");
}
else if (0 == strncmp("0", p, 1))
{
}
else if (0 == strncmp(";", p, 1))
{
}
else
{
fprintf(fOutput, "%u:", i);
fwrite(p, (next_semi - p), 1, fOutput);
fprintf(fOutput, " ");
}
p = next_semi + 1;
}
fprintf(fOutput, "\n");
p = next_row + 1;
}
// Clean up
fclose(fOutput);
free(data);
return EXIT_SUCCESS;
}