1d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Generate array-of-const-string from text file.
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *  Copyright (C) 2006-2007  Peter Johnson
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE.
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdio.h>
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdlib.h>
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <string.h>
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define MAXLINE 1024
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgmain(int argc, char *argv[])
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    FILE *in, *out;
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int i;
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *str;
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *strp;
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t len;
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (argc < 4) {
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        fprintf(stderr, "Usage: %s <string> <outfile> <file> [<file> ...]\n",
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                argv[0]);
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return EXIT_FAILURE;
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    out = fopen(argv[2], "wt");
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (!out) {
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        fprintf(stderr, "Could not open `%s'.\n", argv[2]);
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return EXIT_FAILURE;
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    str = malloc(MAXLINE);
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fprintf(out, "/* This file auto-generated from %s by genstring.c"
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                 " - don't edit it */\n\n"
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                 "static const char *%s[] = {\n", argv[3], argv[1]);
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for (i=3; i<argc; i++) {
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        in = fopen(argv[i], "rt");
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (!in) {
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            fprintf(stderr, "Could not open `%s'.\n", argv[i]);
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            fclose(out);
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            remove(argv[2]);
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return EXIT_FAILURE;
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        while (fgets(str, MAXLINE, in)) {
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            strp = str;
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            /* strip off trailing whitespace */
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            len = strlen(strp);
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            while (len > 0 && (strp[len-1] == ' ' || strp[len-1] == '\t' ||
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               strp[len-1] == '\n')) {
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                strp[len-1] = '\0';
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                len--;
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            }
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            /* output as string to output file */
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            fprintf(out, "    \"");
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            while (*strp != '\0') {
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                if (*strp == '\\' || *strp == '"')
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    fputc('\\', out);
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                fputc(*strp, out);
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                strp++;
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            }
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            fprintf(out, "\",\n");
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        fclose(in);
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fprintf(out, "};\n");
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fclose(out);
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    free(str);
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return EXIT_SUCCESS;
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
103