//  CHUMLAUT.C
    //  Program to convert umlauts to HTML entities.
    //  Author: Peter Meyer
    //  Created: 2003-07-07
    //  Last mod.: 2003-07-08
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define true (1)
    
    unsigned char *umlauts[][2] = { { "", "&auml;" }, { "", "&Auml;" },
                                    { "", "&ouml;" }, { "", "&Ouml;" },
                                    { "", "&uuml;" }, { "", "&Uuml;" },
                                    { "", "&szlig;" }, { "", "" } };
    
    
    /*-----------------------------*/
    void main(int argc, char *argv[])
    {
    int i, j, k, m, n;
    int ch;
    FILE *infile, *outfile;
    
    printf("CHUMLAUT 1.1 by Hermetic Systems\n");
    
    if ( argc < 3 )
        {
        printf("Use CHUMLAUT infile outfile\n");
        exit(0);
        }
    
    if ( !strcmp(strlwr(argv[1]),strlwr(argv[2])) )
        {
        printf("Output filename must be different from input filename.\n");
        exit(1);
        }
    
    if ( ( infile = fopen(argv[1],"rb") ) == NULL )
        {
        printf("Unable to open input file %s\n",argv[1]);
        exit(1);
        }
    
    printf("%s opened for reading.\n",argv[1]);
    
    if ( ( outfile = fopen(argv[2],"wb") ) == NULL )
        {
        printf("Unable to open output file %s\n",argv[2]);
        fclose(infile);
        exit(1);
        }
    
    printf("%s opened for writing.\n",argv[2]);
    
    n = 0;
    while ( *umlauts[n][0] )
        n++;
    
    n--;
    
    m = 0;
    
    while ( true )
        {
        if ( ( ch = fgetc(infile) ) == EOF )
            break;
    
        for ( i=0; i<n; i++ )
            {
            if ( ch == *umlauts[i][0] )
                {
                //  ch has umlaut.
                k = strlen(umlauts[i][1]);
                for ( j=0; j<k; j++ )
                    {
                    if ( fputc(umlauts[i][1][j],outfile) == EOF )
                        {
                        printf("Unable to write to output file %s\n",argv[2]);
                        exit(1);
                        }
                    }
                m++;
                break;
                }
            }
    
        if ( i == n )
            {
            //  ch does not have umlaut.
            if ( fputc(ch,outfile) == EOF )
                {
                printf("Unable to write to output file %s\n",argv[2]);
                exit(1);
                }
            }
        }
    
    fclose(infile);
    fclose(outfile);
    
    printf("%d umlauts converted.\n",m);
    }