utils/preprocess.c
changeset 660 d82a22c8ab3d
parent 659 e77acde263cc
child 661 a502c4999238
equal deleted inserted replaced
659:e77acde263cc 660:d82a22c8ab3d
    10 #include <stdio.h>
    10 #include <stdio.h>
    11 #include <stdlib.h>
    11 #include <stdlib.h>
    12 #include <string.h>
    12 #include <string.h>
    13 #include "mojoshader.h"
    13 #include "mojoshader.h"
    14 
    14 
       
    15 static const char **include_paths = NULL;
       
    16 static unsigned int include_path_count = 0;
       
    17 
       
    18 
       
    19 static int open_include(MOJOSHADER_includeType inctype, const char *fname,
       
    20                         const char *parent, const char **outdata,
       
    21                         unsigned int *outbytes, MOJOSHADER_malloc m,
       
    22                         MOJOSHADER_free f, void *d)
       
    23 {
       
    24     int i;
       
    25     for (i = 0; i < include_path_count; i++)
       
    26     {
       
    27         const char *path = include_paths[i];
       
    28         const size_t len = strlen(path) + strlen(fname) + 2;
       
    29         char *buf = (char *) m(len, d);
       
    30         if (buf == NULL)
       
    31             return 0;
       
    32 
       
    33         snprintf(buf, len, "%s/%s", path, fname);
       
    34         FILE *io = fopen(buf, "rb");
       
    35         f(buf, d);
       
    36         if (io == NULL)
       
    37             continue;
       
    38 
       
    39         if (fseek(io, 0, SEEK_END) != -1)
       
    40         {
       
    41             const long fsize = ftell(io);
       
    42             if ((fsize == -1) || (fseek(io, 0, SEEK_SET) == -1))
       
    43             {
       
    44                 fclose(io);
       
    45                 return 0;
       
    46             } // if
       
    47 
       
    48             char *data = (char *) m(fsize, d);
       
    49             if (data == NULL)
       
    50             {
       
    51                 fclose(io);
       
    52                 return 0;
       
    53             } // if
       
    54 
       
    55             if (fread(data, fsize, 1, io) != 1)
       
    56             {
       
    57                 f(data, d);
       
    58                 fclose(io);
       
    59                 return 0;
       
    60             } // if
       
    61 
       
    62             fclose(io);
       
    63             *outdata = data;
       
    64             *outbytes = (unsigned int) fsize;
       
    65             return 1;
       
    66         } // if
       
    67     } // for
       
    68 
       
    69     return 0;
       
    70 } // open_include
       
    71 
       
    72 
       
    73 void close_include(const char *data, MOJOSHADER_malloc m,
       
    74                    MOJOSHADER_free f, void *d)
       
    75 {
       
    76     f((void *) data, d);
       
    77 } // close_include
       
    78 
       
    79 
    15 static int preprocess(const char *fname, const char *buf, int len,
    80 static int preprocess(const char *fname, const char *buf, int len,
    16                       const char *outfile,
    81                       const char *outfile,
    17                       const MOJOSHADER_preprocessorDefine *defs,
    82                       const MOJOSHADER_preprocessorDefine *defs,
    18                       unsigned int defcount)
    83                       unsigned int defcount)
    19 {
    84 {
    25     } // if
    90     } // if
    26 
    91 
    27     const MOJOSHADER_preprocessData *pd;
    92     const MOJOSHADER_preprocessData *pd;
    28     int retval = 0;
    93     int retval = 0;
    29 
    94 
    30     pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, NULL,
    95     pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount,
    31                                NULL, NULL, NULL, NULL);
    96                                open_include, close_include, NULL, NULL, NULL);
    32 
    97 
    33     if (pd->error_count > 0)
    98     if (pd->error_count > 0)
    34     {
    99     {
    35         int i;
   100         int i;
    36         for (i = 0; i < pd->error_count; i++)
   101         for (i = 0; i < pd->error_count; i++)
    88                 exit(1);
   153                 exit(1);
    89             } // if
   154             } // if
    90             outfile = arg;
   155             outfile = arg;
    91         } // if
   156         } // if
    92 
   157 
       
   158         if (strcmp(arg, "-I") == 0)
       
   159         {
       
   160             arg = argv[++i];
       
   161             if (arg == NULL)
       
   162             {
       
   163                 printf("no path after '-I'\n");
       
   164                 exit(1);
       
   165             } // if
       
   166             include_paths = (const char **) realloc(include_paths,
       
   167                        (include_path_count+1) * sizeof (char *));
       
   168             include_paths[include_path_count] = arg;
       
   169             include_path_count++;
       
   170         } // if
       
   171 
    93         else if (strncmp(arg, "-D", 2) == 0)
   172         else if (strncmp(arg, "-D", 2) == 0)
    94         {
   173         {
    95             arg += 2;
   174             arg += 2;
    96             char *ident = strdup(arg);
   175             char *ident = strdup(arg);
    97             char *ptr = strchr(ident, '=');
   176             char *ptr = strchr(ident, '=');
   151 
   230 
   152     for (i = 0; i < defcount; i++)
   231     for (i = 0; i < defcount; i++)
   153         free((void *) defs[defcount].identifier);
   232         free((void *) defs[defcount].identifier);
   154     free(defs);
   233     free(defs);
   155 
   234 
       
   235     free(include_paths);
       
   236 
   156     return retval;
   237     return retval;
   157 } // main
   238 } // main
   158 
   239 
   159 // end of preprocess.c ...
   240 // end of preprocess.c ...
   160 
   241