utils/assemble.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 18 Feb 2009 14:43:16 -0500
changeset 654 9210bd7eb2e2
parent 588 ef3d2efa822d
permissions -rw-r--r--
Patched to compile.

/**
 * MojoShader; generate shader programs from bytecode of compiled
 *  Direct3D shaders.
 *
 * Please see the file LICENSE.txt in the source's root directory.
 *
 *  This file written by Ryan C. Gordon.
 */

#include <stdio.h>
#include <stdlib.h>
#include "mojoshader.h"

static int assemble(const char *fname, const char *buf, int len,
                    const char *outfile)
{
    FILE *io = fopen(outfile, "wb");
    if (io == NULL)
    {
        printf(" ... fopen('%s') failed.\n", outfile);
        return 0;
    } // if

    const MOJOSHADER_parseData *pd;
    int retval = 0;

    pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0,
                             NULL, 0, NULL, NULL, NULL, NULL, NULL);
    if (pd->error_count > 0)
    {
        int i;
        for (i = 0; i < pd->error_count; i++)
        {
            printf("%s:%d: ERROR: %s\n",
                    pd->errors[i].filename ? pd->errors[i].filename : "???",
                    pd->errors[i].error_position,
                    pd->errors[i].error);
        } // for
    } // if
    else
    {
        if (pd->output != NULL)
        {
            if (fwrite(pd->output, pd->output_len, 1, io) != 1)
                printf(" ... fwrite('%s') failed.\n", outfile);
            else if (fclose(io) == EOF)
                printf(" ... fclose('%s') failed.\n", outfile);
            else
                retval = 1;
        } // if
    } // else
    MOJOSHADER_freeParseData(pd);

    return retval;
} // assemble


int main(int argc, char **argv)
{
    int retval = 1;

    if (argc != 3)
        printf("\n\nUSAGE: %s <inputfile> <outputfile>\n\n", argv[0]);
    else
    {
        const char *infile = argv[1];
        const char *outfile = argv[2];
        FILE *io = fopen(infile, "rb");
        if (io == NULL)
            printf(" ... fopen('%s') failed.\n", infile);
        else
        {
            char *buf = (char *) malloc(1000000);
            int rc = fread(buf, 1, 1000000, io);
            fclose(io);
            if (rc == EOF)
                printf(" ... fread('%s') failed.\n", infile);
            else
            {
                if (assemble(infile, buf, rc, outfile))
                    retval = 0;
                else
                    remove(outfile);
                free(buf);
            } // else
        } // for
    } // else

    return retval;
} // main

// end of assemble.c ...