#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <ctype.h>
typedef struct
{
int count, max;
char **strings;
} stStringList;
#ifndef __cplusplus
typedef enum { false, true } bool;
#endif
#define GROW 5
#define NewStringList(list) \
(list) = (stStringList *)malloc(sizeof(stStringList)); \
(list)->strings = 0; \
(list)->count = (list)->max = 0;
bool AddString(stStringList *list, char *str)
{
if(list->count >= list->max)
{
char **strings = (char **)realloc(list->strings, sizeof(char *) * (list->max + GROW));
if(strings)
{
list->strings = strings;
list->max += GROW;
}
else
return false;
}
list->strings[list->count] = strdup(str);
return list->strings[list->count] ? list->count++, true : false;
}
void ParseBuffer(stStringList *list, char *buffer)
{
char *tok = strchr(buffer, '\n');
tok ? *tok = '\0' : 0;
tok = strtok(buffer, ",");
while(tok)
{
while(isspace(*tok))
tok++;
AddString(list, tok);
tok = strtok(0, ",");
}
}
stStringList *ReadFile(const char *FileName)
{
stStringList *list;
char *str;
FILE *fpSource = fopen(FileName, "rt");
if(!fpSource)
return 0;
NewStringList(list);
str = (char *)malloc(1024);
while(fgets(str, sizeof(char) * 1024, fpSource))
ParseBuffer(list, str);
free(str);
fclose(fpSource);
return list;
}
void main(int ArgC, char *ArgV[])
{
if(ArgC == 2)
{
stStringList *list = ReadFile(ArgV[1]);
if(list)
{
int c;
printf("Palavras Lidas: %d\n\n", list->count);
for(c = 0; c < list->count; c++)
{
printf("%s\n", list->strings[c]);
free(list->strings[c]);
}
free(list);
}
}
}