#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>


int main(int argc, char *argv[])
{
    char *sourceName      = NULL,
         *destinationName = NULL,
         *word            = (char *) malloc(1025),
         lastCharacter,
         valid;
    FILE *sourceFile      = stdin,
         *destinationFile = stdout;
    int  result           = 0;
    long position;

    if (3 < argc)
    {
        fprintf(stderr, "Utility to generate a list of words from a ASCII text\n");
        fprintf(stderr, "Usage    : %s [<sourceName> [<destinationName>]]\n", argv[0]);
        fprintf(stderr, "Parameter: <sourceName>     : file with the ASCII-text\n");
        fprintf(stderr, "                              If no filename is given, 'stdin'\n");
        fprintf(stderr, "                              will be read\n");
        fprintf(stderr, "           <destinationName>: Name of the list of words.\n");
        fprintf(stderr, "                              Is no name given, the list will\n");
        fprintf(stderr, "                              written to 'stdout'\n");
        result = 1;
    }
    else
    {
        if (word == 0)
        {
            fprintf(stderr, "%s: not enoug memory available\n", argv[0]);
            result = 1;
        }
        else
        {
            if (argc != 1)
                sourceName = argv[1];
            if (argc == 3)
                destinationName = argv[2];

            if (sourceName != NULL)
            {
                sourceFile = fopen(sourceName, "r");
            }
            if (sourceFile == 0)
            {
                fprintf(stderr, "%s: Can't open %s to read => error %d\n", argv[0], sourceName, errno);
                result = 1;
            }
            else
            {
                if (destinationName != NULL)
                {
                    destinationFile = fopen(destinationName, "w");
                }
                
                if (destinationFile == 0)
                {
                    fprintf(stderr, "%s: Can't open %s to write => error %d\n", argv[0], destinationName, errno);
                    result = 1;
                }
                else
                {
                    position = 0;
                    valid  = 0;
                    while (!feof(sourceFile))
                    {
                        word[position] = fgetc(sourceFile);
                        if (position == 0)
                        { /* the first letter must be an upper or lower case 
                           * letter
                           */
                            if (isalpha(word[position]) ||
                                word[position] == 'Ä'   ||
                                word[position] == 'Ö'   ||
                                word[position] == 'Ü'   ||
                                word[position] == 'ä'   ||
                                word[position] == 'ö'   ||
                                word[position] == 'ü'   ||
                                word[position] == 'ß'      )
                            {
                                valid = 1;
                                lastCharacter = word[position++];
                            }
                        }
                        else
                        { /*
                           * all other characters must be a lower case letter
			   * sein
			   */
			    if (!islower(word[position]) &&
                                 word[position] != 'ä'   &&
                                 word[position] != 'ö'   &&
                                 word[position] != 'ü'   &&
                                 word[position] != 'ß'      )
 			    { /* 
			       * otherwise it must be a white-space
			       * or a sentence character, followed by a
			       * white-space
			       */
			        if (word[position] == '.' ||
			            word[position] == ',' ||
			            word[position] == ';' ||
			            word[position] == ':' ||
			            word[position] == '?' ||
			            word[position] == '!'   )
			        { /* if the actual character is a sentence 
			           * character, the last character must be 
                                   * a letter
                                   */
                                    if (!isalpha(lastCharacter) &&
                                        lastCharacter != 'ä'    &&
                                        lastCharacter != 'ö'    &&
                                        lastCharacter != 'ü'    &&
                                        lastCharacter != 'ß'       )
                                    {
                                        valid = 0;
                                    }
                                }
                                else
                                { /* if the character isn't a letter or a
                                   * sentence character, then a white-space
                                   * is only accepted, if the last character
				   * was a letter
				   */
				    if (isspace(word[position]))
				    {
				        word[position] = '\0';
				        if (!isalpha(lastCharacter) &&
                                             lastCharacter != 'ä'   &&
                                             lastCharacter != 'ö'   &&
                                             lastCharacter != 'ü'   &&
                                             lastCharacter != 'ß'       )
				        {
				            word[--position] = '\0';
				        }
				        if (isalpha(lastCharacter) ||
                                            word[position] == 'ä'  ||
                                            word[position] == 'ö'  ||
                                            word[position] == 'ü'  ||
                                            word[position] == 'ß'  ||
					    word[position] == '.'  ||
        			            word[position] == ','  ||
			                    word[position] == ';'  ||
			                    word[position] == ':'  ||
			                    word[position] == '?'  ||
			                    word[position] == '!'     )
                                        {
                                            if (valid && 1 < position)
                                            {
                                                fputs(word, destinationFile);
                                                fputs("\n", destinationFile);
                                            }
                                        }
                                        position = 0;
                                    }
                                    else
                                    {
                                        valid = 0;
                                    }
                                }
			    }
			    else
			    { /* if the last character is a letter, but
                               * the character for this not, then the
                               * checked part is not a word
			       */
			        if (!isalpha(lastCharacter) &&
                                     lastCharacter != 'ä'   &&
                                     lastCharacter != 'ö'   &&
                                     lastCharacter != 'ü'   &&
                                     lastCharacter != 'ß'       )
			        {
			            valid = 0;
			        }
/*			        else
			        {
			            position++;
			        }
*/			        
			    }
                            if (position != 0)
                            {
    			        lastCharacter = word[position++];
    			    }
			}
                    }
                    if (destinationName != NULL)
                    {
                        fclose(destinationFile);
                    }
                }
                if (sourceName != NULL)
                {
                    fclose(sourceFile);
                }
            }
        }
    }
    if (word != 0)
        free(word);

    exit (result);
}

