/*
 *  Copyright (C)  2000-2001 Marc Wandschneider <mw@kiltdown.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation.
 *
 *  For more information look at the file COPYRIGHT in this package
 *
 */

#include <stdio.h>

/**
 * String stuff.
 */
const char *startWithMeCFileString = 
"/**\n\
 * This file contains localized strings.  Please do not ever directly \n\
 * manipulate the contents of this file, or access any of the methods/members\n\
 * in this file.  Instead, #include <intl.h> and use intlLoadString().\n\
 */\n\
\n\
\n\
const char *__intlStrings[] = {\n";
const char *startWithMeHeaderString = 
"/**\n\
 * This file contains localized IDs.  Please do not ever directly \n\
 * manipulate the contents of this file, or access any of the methods/members\n\
 * in this file.  Instead, #include <intl.h> and use intlLoadString().\n\
 */\n";
const char *endWithMeCFileString =
"    \"\"\n\
};\n";

/**
 * Accelerators stuff.
 */
const char *startWithMeCFileAccelerator = 
"/**\n\
 * This file contains localized accelerators.  Please do not ever directly \n\
 * manipulate the contents of this file, or access any of the methods/members\n\
 * in this file.  Instead, #include <intl.h> and use intlLoadAccelerator().\n\
 */\n\
#include <qnamespace.h>\
\n\
\n\
int __intlAccelerators[] = {\n\
    0,\n";
const char *startWithMeHeaderAccelerator = 
"/**\n\
 * This file contains localized accelerator IDs.  Please do not ever directly \n\
 * manipulate the contents of this file, or access any of the methods/members\n\
 * in this file.  Instead, #include <intl.h> and use intlLoadAccelerator().\n\
 */\n";
const char *endWithMeCFileAccelerator = 
"    0\n\
};\n";


/**
 *=------------------------------------------------------------------------=
 * main
 *=------------------------------------------------------------------------=
 */
int main (int argc, char **argv)
{
    if (argc != 4) {
        fprintf(stderr, "\nUsage: %s [-as] <<output_header>> <<output_.c_file>>\n\
(* INPUT is always stdin *)\n    -a: process accelerators\n    -s: process strings\n\n", argv[0]);
        return -1;
    }

    if (!strcasecmp("-a", argv[1])) {
        return processAccelerators(argv[2], argv[3]);
    } else if (!strcasecmp("-s", argv[1])) {
        return processStrings(argv[2], argv[3]);
    }


}

/**
 *=------------------------------------------------------------------------=
 * processAccelerators
 *=------------------------------------------------------------------------=
 */
int processAccelerators
(
    char *headerName,
    char *cfileName
)
{
    FILE *header, *cfile;
    char buf[4096];
    char outBuf[4096];
    int x, y;
    int val = 1;
    int lineNum = 0;
    int inspace;

    if (!(header = fopen(headerName, "w"))) {
        fprintf(stderr, "file Error opening %s\n", headerName);
        return -1;
    }
    if (!(cfile = fopen(cfileName, "w"))) {
        fprintf(stderr, "file Error opening %s\n", cfileName);
        return -1;
    }

    fprintf(cfile, startWithMeCFileAccelerator);
    fprintf(header, startWithMeHeaderAccelerator);

    while (!feof(stdin)) {
        if (fgets(buf, sizeof(buf), stdin) == NULL) break;

        lineNum++;
        x = 0;

        // if it's a comment line.  skip it.
        //
        if (buf[0] == '#')
            continue;

        if (buf[0] == '\n')
            continue;

        if (buf[0] == '\0')
            continue;

        /**
         * Look for the first characters in the file.
         */
        while (isspace(buf[x]))
            x++;

        /**
         * If the line had nothing but space, then we 're done!!!
         */
        if (buf[x] == '\0' || buf[x] == '\n') continue;

        /**
         * Now extract the name.
         */
        y = 0;
        while (isalnum(buf[x]) || buf[x] == '_') {
            
            outBuf[y++] = buf[x++];
        }
        outBuf[y] = '\0';

        fprintf(header, "#define %s\t\t%d\n", outBuf, val++);

        while (buf[x] != '\0') {
            if (!isspace(buf[x])) break;
            x++;
        }

        if (buf[x] == '\0') {
            fprintf(cfile,  "#error Malformed line #%d on Input -- Ignoring!!!!\n", lineNum);
            fprintf(stderr,  "#error Malformed line #%d on Input -- Ignoring!!!!\n", lineNum);
            continue;
        }

        /**
         * great, now keep going until the end of the line, and we'll call 
         * that our accelerator.
         */
        y = 0;
        inspace = 1;
        while (buf[x] != '\0') {
            if (!isspace(buf[x])) {
                if (isalnum(buf[x]) && inspace) {
                    outBuf[y++] = 'Q'; outBuf[y++] = 't'; outBuf[y++] = ':'; outBuf[y++] = ':';
                }
                outBuf[y++] = buf[x];
                inspace = 0;
            }
            else
                inspace = 1;
            x++;
        }

        outBuf[y] = '\0';
        fprintf(cfile, "    %s,\n", outBuf);

    }

    fprintf(cfile, endWithMeCFileAccelerator);
    fclose(cfile);
    fclose(header);
    return 0;

}

/**
 *=------------------------------------------------------------------------=
 * processStrings
 *=------------------------------------------------------------------------=
 */
int processStrings
(
    char *headerName,
    char *cfileName
)
{
    FILE *header, *cfile;
    char buf[4096];
    char outBuf[4096];
    int x, y;
    int val = 0;
    int lineNum = 0;

    if (!(header = fopen(headerName, "w"))) {
        fprintf(stderr, "file Error opening %s\n", headerName);
        return -1;
    }
    if (!(cfile = fopen(cfileName, "w"))) {
        fprintf(stderr, "file Error opening %s\n", cfileName);
        return -1;
    }

    fprintf(cfile, startWithMeCFileString);
    fprintf(header, startWithMeHeaderString);

    while (!feof(stdin)) {
        if (fgets(buf, sizeof(buf), stdin) == NULL) break;

        lineNum++;
        x = 0;

        // if it's a comment line.  skip it.
        //
        if (buf[0] == '#')
            continue;

        if (buf[0] == '\n')
            continue;

        if (buf[0] == '\0')
            continue;

        /**
         * Look for the first characters in the file.
         */
        while (isspace(buf[x]))
            x++;

        /**
         * If the line had nothing but space, then we 're done!!!
         */
        if (buf[x] == '\0' || buf[x] == '\n') continue;

        /**
         * Now extract the name.
         */
        y = 0;
        while (isalnum(buf[x]) || buf[x] == '_') {
            
            outBuf[y++] = buf[x++];
        }
        outBuf[y] = '\0';

        fprintf(header, "#define %s\t\t%d\n", outBuf, val++);

        /**
         * Look for the open quotes.
         */
        while (buf[x] != '\0') {
            if (buf[x++] == '"') break;
        }
        if (buf[x] == '\0') {
            fprintf(cfile,  "#error Malformed line #%d on Input -- Ignoring!!!!\n", lineNum);
            fprintf(stderr,  "#error Malformed line #%d on Input -- Ignoring!!!!\n", lineNum);
            continue;
        }

        y = 0;
        while (buf[x] != '"' && buf[x] != '\n' && buf[x] != '\0') {
            outBuf[y] = buf[x];
            /**
             * If this is an escaped quote, make it so that we don't end the 
             * loop yet.
             */
            if (buf[x] == '\\') {
                if (buf[x + 1] == '"') {
                    outBuf[++y] = buf[++x];
                } else if (buf[x + 1] == '\n') {
                    if (fgets(buf, sizeof(buf), stdin) == NULL) break;
                    x = 0; 
                    lineNum++;
                    continue;
                }
            }
            x++;y++;
        }
        outBuf[y] = '\0';

        fprintf(cfile, "    \"%s\",\n", outBuf);
    }

    fprintf(cfile, endWithMeCFileString);

    fclose(cfile);
    fclose(header);
    return 0;
}




syntax highlighted by Code2HTML, v. 0.9.1