#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char *vocab_1;
char *vocab_2;
} vocabs;
int struct_array(char *word_1, int lines, int cntWords)
{
static int x = 15;
static int y = 0;
printf("%d\n",lines);
vocabs *arr_vocab = malloc(10*sizeof(char));
if(arr_vocab == NULL)
{ //out of memory
return 4;
}
if(lines > (9 + y))
{
y += 5;
x += 5;
arr_vocab = realloc(arr_vocab, x*sizeof(char));
if(arr_vocab == NULL)
{
return 4;
}
}
if(cntWords == 1)
{
arr_vocab[lines].vocab_1 = word_1;
}
if(cntWords == 2)
{
arr_vocab[lines].vocab_2 = word_1;
}
return 0;
}
char cntelements(char* element_str)
{
int elements;
for(elements = 0; element_str[elements] != '\n'; ++elements)
{
continue;
}
return elements;
}
int counter(char *test_str)
{
int i;
int elements = cntelements(test_str);
int cntWords = 0;
for(i = 0; i < elements; i++)
{
if(((test_str[i+1] == ' ') || (test_str[i+1] == '\n')) && (test_str[i] != ' '))
{
cntWords++;
}
if(test_str[i] == '\0')
return -1;
}
return cntWords;
}
int make_str(char *buffer_str, int lines)
{
vocabs vocabs;
const char *delimiter = " ";
char *word_str;
int i;
char *tmp;
int X;
int cntWords = counter(buffer_str);
word_str = strtok(buffer_str, delimiter);
while(word_str != NULL)
{
if(cntWords == 1)
{
X = struct_array(word_str, lines, cntWords);
}
if(cntWords == 2)
{
X = struct_array(word_str, lines, cntWords);
}
word_str = strtok(NULL, delimiter);
}
if(cntWords != 2 && cntWords != -1)
{
return 3;
}
return 0;
}
int read_file(char *filename)
{
char buffer_str[255];
int new_strs;
int cnt_lines = 0;
FILE *ptr;
ptr = fopen(filename, "r");
if(ptr == NULL)
{
return 2; //kann nicht geöffnet werden
}
while(!feof(ptr))
{
cnt_lines++;
fgets(buffer_str, 255, ptr);
new_strs = make_str(buffer_str,cnt_lines);
if(new_strs == 3)
{
return 3; //falsche Formatierung einer Zeile im Dokument
}
}
return 0;
}
int main(int argc, char *argv[])
{
vocabs vocabs;
int file_data;
char *argx = argv[1];
if(argc < 2)
{
printf("usage: [executable] filename\n");
return 1;
}
file_data = read_file(argx);
if(file_data == 2)
{
printf("ERROR: cannot open file %s\n", argx);
}
if(file_data == 3)
{
printf("ERROR: file %s invalid\n", argx);
}
printf("%d",file_data);
}