testparse.c
branchtrunk
changeset 46 ff5a0ec44f00
parent 37 63b5ba0bcda0
child 47 49953c7cb142
equal deleted inserted replaced
45:9565678766aa 46:ff5a0ec44f00
     1 #include <stdio.h>
     1 #include <stdio.h>
     2 #include <stdlib.h>
     2 #include <stdlib.h>
     3 #include "mojoshader.h"
     3 #include "mojoshader.h"
     4 
     4 
       
     5 
       
     6 #if DEBUG_MALLOC
       
     7 static void *Malloc(int len)
       
     8 {
       
     9     void *ptr = malloc(len + sizeof (int));
       
    10     int *store = (int *) ptr;
       
    11     printf("malloc() %d bytes (%p)\n", len, ptr);
       
    12     if (ptr == NULL) return NULL;
       
    13     *store = len;
       
    14     return (void *) (store + 1);
       
    15 } // Malloc
       
    16 
       
    17 
       
    18 static void Free(void *_ptr)
       
    19 {
       
    20     int *ptr = (((int *) ptr) - 1);
       
    21     int *store = (int *) ptr;
       
    22     int len = *store;
       
    23     printf("free() %d bytes (%p)\n", len, ptr);
       
    24     free(ptr);
       
    25 } // Free
       
    26 #else
       
    27 #define Malloc NULL
       
    28 #define Free NULL
       
    29 #endif
       
    30 
       
    31 static const char *shader_type(MOJOSHADER_shaderType s)
       
    32 {
       
    33     switch (s)
       
    34     {
       
    35         case MOJOSHADER_TYPE_UNKNOWN: return "unknown";
       
    36         case MOJOSHADER_TYPE_PIXEL: return "pixel";
       
    37         case MOJOSHADER_TYPE_VERTEX: return "vertex";
       
    38         case MOJOSHADER_TYPE_GEOMETRY: return "geometry";
       
    39     } // switch
       
    40 
       
    41     return "(bogus value?)";
       
    42 } // shader_type
       
    43 
       
    44 
       
    45 static void do_parse(unsigned char *buf, int len, const char *prof)
       
    46 {
       
    47     const MOJOSHADER_parseData *pd;
       
    48 
       
    49     pd = MOJOSHADER_parse(prof, buf, len, Malloc, Free);
       
    50     printf("PROFILE: %s\n", prof);
       
    51     printf("SHADER TYPE: %s\n", shader_type(pd->shader_type));
       
    52     printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver);
       
    53     printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count);
       
    54     if (pd->error != NULL)
       
    55         printf("ERROR: %s\n", pd->error);
       
    56     if (pd->output != NULL)
       
    57         printf("OUTPUT:\n%s\n", pd->output);
       
    58     printf("\n\n");
       
    59     MOJOSHADER_freeParseData(pd);
       
    60 } // do_parse
       
    61 
       
    62 
     5 int main(int argc, char **argv)
    63 int main(int argc, char **argv)
     6 {
    64 {
       
    65     int i;
       
    66 
     7     printf("Compiled against version %d\n", MOJOSHADER_VERSION);
    67     printf("Compiled against version %d\n", MOJOSHADER_VERSION);
     8     printf("Linked against version %d\n", MOJOSHADER_version());
    68     printf("Linked against version %d\n", MOJOSHADER_version());
     9 
    69 
    10     if (argv[1] != NULL)
    70     for (i = 1; i < argc; i++)
    11     {
    71     {
    12         FILE *io = fopen(argv[1], "rb");
    72         FILE *io = fopen(argv[i], "rb");
    13         if (io != NULL)
    73         if (io != NULL)
    14         {
    74         {
    15             unsigned char *buf = (unsigned char *) malloc(1000000);
    75             unsigned char *buf = (unsigned char *) malloc(1000000);
    16             int rc = fread(buf, 1, 1000000, io);
    76             int rc = fread(buf, 1, 1000000, io);
    17             fclose(io);
    77             fclose(io);
    18             MOJOSHADER_parse("d3d", buf, rc, NULL, NULL);
    78             do_parse(buf, rc, MOJOSHADER_PROFILE_D3D);
       
    79             do_parse(buf, rc, MOJOSHADER_PROFILE_GLSL);
    19             free(buf);
    80             free(buf);
    20         } // if
    81         } // if
    21     } // if
    82     } // if
    22 
    83 
    23     return 0;
    84     return 0;