Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#define MAX_LINE (128+1) /* Maximum length of a source line plus one */
#define CM_NAME_LENGTH 20
#define CM_CF 1
#define CM_CF_CA 2
#define CM_WF_Shield 3
#define CM_Comma 4
#define CM_Lee_Cld 5
typedef struct
{
char Name[CM_NAME_LENGTH];
int Value;
} NameDefinition_t;
typedef struct
{
int Index;
int X; /* Longitude */
int Y; /* Latitude */
char Direction; /* Direction, East or West */
int CommentLines; /* Number of infolines */
char **Comments;
} NameDescription_t;
NameDefinition_t CM_lijst[] = {
{"CF", CM_CF},
{"CF in CA", CM_CF_CA},
{"WF Shield", CM_WF_Shield},
{"Comma", CM_Comma},
{"Lee Cloud", CM_Lee_Cld},
{0, 0}
};
/* Open Structure to write XML file*/
typedef struct WRITER {
float lat;
float lon;
char CM[50];
char info[2000];
char dir;
int cl;
}WRITE;
WRITE *StrXML;
/* ************* Function ************************* */
int FindNameValue (char *Name) {
int i;
for (i=0; *CM_lijst[i].Name; i++)
if (strcmp(Name, CM_lijst[i].Name)==0)
return(CM_lijst[i].Value);
return(0);
} /* The above function searches the name definitions and returns the associated value */
/* ************************************************ */
NameDescription_t *NameDescriptions = NULL;
/* ************ Start of main ********************* */
main ()
{
NameDescription_t *Name;
char InputBuffer[MAX_LINE];
char StartJunk[5], NameString[CM_NAME_LENGTH];
int X1, X2, X3;
int Y1, Y2, Y3;
int idx, n, k;
char C1, C2, C3;
FILE *STRPfile, *XML;
idx=0;
n=0;
k=0;
/* Allocate memory to later write XML file*/
StrXML=(WRITE*)malloc(256*256*sizeof(WRITE));
if ((STRPfile=fopen("test.txt","r"))==NULL){
fprintf(stderr,"Error opening info-file :%s\n","test.txt");
exit(1);
}
for (idx=0; idx<2; idx++) {
fgets (InputBuffer,MAX_LINE,STRPfile);
}
fgets (InputBuffer,MAX_LINE,STRPfile); /* Loop through all the data */
while (!feof(STRPfile))
{
sscanf (InputBuffer, "%4.4s%s: %2.2d%2.2d%c (%2.2d%2.2d%c - %2.2d%2.2d%c)",
StartJunk, NameString, &X1, &Y1, &C1, &X2, &Y2, &C2, &X3, &Y3, &C3);
Name=(NameDescription_t*)malloc(sizeof(NameDescription_t));
memset (Name,0,sizeof(NameDescription_t));
switch (FindNameValue (NameString)) {
case CM_CF: /* Process Cold Front data */
Name->Index = CM_CF;
Name->X = X1;
Name->Y = Y1;
Name->Direction = C1;
while (fgets(InputBuffer,MAX_LINE,STRPfile))
{
if (memcmp (InputBuffer, "xx",2)==0)
break;
Name->CommentLines++;
Name->Comments=(char**)realloc(Name->Comments,sizeof(char*)*Name->CommentLines);
Name->Comments[Name->CommentLines-1] = strdup(InputBuffer);
}
case CM_CF_CA:
Name->Index = CM_CF_CA;
Name->X = X1;
Name->Y = Y1;
Name->Direction = C1;
while (fgets(InputBuffer,MAX_LINE,STRPfile))
{
if (memcmp (InputBuffer, "xx",2)==0)
break;
Name->CommentLines++;
Name->Comments=(char**)realloc(Name->Comments,sizeof(char*)*Name->CommentLines);
Name->Comments[Name->CommentLines-1] = strdup(InputBuffer);
}
case CM_WF_Shield:
Name->Index = CM_WF_Shield;
Name->X = X1;
Name->Y = Y1;
Name->Direction = C1;
while (fgets(InputBuffer,MAX_LINE,STRPfile))
{
if (memcmp (InputBuffer, "xx",2)==0)
break;
Name->CommentLines++;
Name->Comments=(char**)realloc(Name->Comments,sizeof(char*)*Name->CommentLines);
Name->Comments[Name->CommentLines-1] = strdup(InputBuffer);
}
case CM_Comma:
Name->Index = CM_Comma;
Name->X = X1;
Name->Y = Y1;
Name->Direction = C1;
while (fgets(InputBuffer,MAX_LINE,STRPfile))
{
if (memcmp (InputBuffer, "xx",2)==0)
break;
Name->CommentLines++;
Name->Comments=(char**)realloc(Name->Comments,sizeof(char*)*Name->CommentLines);
Name->Comments[Name->CommentLines-1] = strdup(InputBuffer);
}
case CM_Lee_Cld:
Name->Index = CM_Lee_Cld;
Name->X = X1;
Name->Y = Y1;
Name->Direction = C1;
while (fgets(InputBuffer,MAX_LINE,STRPfile))
{
if (memcmp (InputBuffer, "xx",2)==0)
break;
Name->CommentLines++;
Name->Comments=(char**)realloc(Name->Comments,sizeof(char*)*Name->CommentLines);
Name->Comments[Name->CommentLines-1] = strdup(InputBuffer);
}
} /* End of switch-loop */
} /* End of while-loop */
fclose (STRPfile);
} /* End of program */