assemble.c
changeset 473 78c418bde2d2
child 474 c2d4e63efdd1
equal deleted inserted replaced
472:e52d487e6d91 473:78c418bde2d2
       
     1 /**
       
     2  * MojoShader; generate shader programs from bytecode of compiled
       
     3  *  Direct3D shaders.
       
     4  *
       
     5  * Please see the file LICENSE.txt in the source's root directory.
       
     6  *
       
     7  *  This file written by Ryan C. Gordon.
       
     8  */
       
     9 
       
    10 #include <stdio.h>
       
    11 #include <stdlib.h>
       
    12 #include "mojoshader.h"
       
    13 
       
    14 static int assemble(const char *buf, const char *outfile)
       
    15 {
       
    16     FILE *io = fopen(outfile, "wb");
       
    17     if (io == NULL)
       
    18     {
       
    19         printf(" ... fopen('%s') failed.\n", outfile);
       
    20         return 0;
       
    21     } // if
       
    22 
       
    23     const MOJOSHADER_parseData *pd;
       
    24     int retval = 0;
       
    25 
       
    26     pd = MOJOSHADER_assemble(buf, NULL, NULL, NULL);
       
    27     if (pd->error != NULL)
       
    28         printf("ERROR: %s\n", pd->error);
       
    29     else
       
    30     {
       
    31         if (pd->output != NULL)
       
    32         {
       
    33             if (fwrite(pd->output, pd->output_len, 1, io) != 1)
       
    34                 printf(" ... fwrite('%s') failed.\n", outfile);
       
    35             else if (fclose(io) == EOF)
       
    36                 printf(" ... fclose('%s') failed.\n", outfile);
       
    37             else
       
    38                 retval = 1;
       
    39         } // if
       
    40     } // else
       
    41     MOJOSHADER_freeParseData(pd);
       
    42 
       
    43     return retval;
       
    44 } // assemble
       
    45 
       
    46 
       
    47 int main(int argc, char **argv)
       
    48 {
       
    49     int retval = 1;
       
    50 
       
    51     if (argc != 3)
       
    52         printf("\n\nUSAGE: %s <d3dasmfile> <outputfile>\n\n", argv[0]);
       
    53     else
       
    54     {
       
    55         const char *infile = argv[1];
       
    56         const char *outfile = argv[2];
       
    57         FILE *io = fopen(infile, "rb");
       
    58         if (io == NULL)
       
    59             printf(" ... fopen('%s') failed.\n", infile);
       
    60         else
       
    61         {
       
    62             char *buf = (char *) malloc(1000000);
       
    63             int rc = fread(buf, 1, 1000000-1, io);
       
    64             fclose(io);
       
    65             if (rc == EOF)
       
    66                 printf(" ... fread('%s') failed.\n", infile);
       
    67             else
       
    68             {
       
    69                 buf[rc] = '\0';
       
    70                 if (assemble(buf, outfile))
       
    71                     retval = 0;
       
    72                 free(buf);
       
    73             } // else
       
    74         } // for
       
    75     } // else
       
    76 
       
    77     return retval;
       
    78 } // main
       
    79 
       
    80 // end of assemble.c ...
       
    81