mojoshader_preprocessor.c
changeset 657 1268d95644f0
parent 656 ddb1a0b4c443
child 658 22695d3d6b98
equal deleted inserted replaced
656:ddb1a0b4c443 657:1268d95644f0
   593     ctx->include_stack = state->next;
   593     ctx->include_stack = state->next;
   594     put_include(ctx, state);
   594     put_include(ctx, state);
   595 } // pop_source
   595 } // pop_source
   596 
   596 
   597 
   597 
       
   598 static void close_define_include(const char *data, MOJOSHADER_malloc m,
       
   599                                  MOJOSHADER_free f, void *d)
       
   600 {
       
   601     f((void *) data, d);
       
   602 } // close_define_include
       
   603 
       
   604 
   598 Preprocessor *preprocessor_start(const char *fname, const char *source,
   605 Preprocessor *preprocessor_start(const char *fname, const char *source,
   599                             unsigned int sourcelen,
   606                             unsigned int sourcelen,
   600                             MOJOSHADER_includeOpen open_callback,
   607                             MOJOSHADER_includeOpen open_callback,
   601                             MOJOSHADER_includeClose close_callback,
   608                             MOJOSHADER_includeClose close_callback,
   602                             const MOJOSHADER_preprocessorDefine **defines,
   609                             const MOJOSHADER_preprocessorDefine **defines,
   621     ctx->free = f;
   628     ctx->free = f;
   622     ctx->malloc_data = d;
   629     ctx->malloc_data = d;
   623     ctx->open_callback = open_callback;
   630     ctx->open_callback = open_callback;
   624     ctx->close_callback = close_callback;
   631     ctx->close_callback = close_callback;
   625 
   632 
   626     for (i = 0; i < define_count; i++)
   633     // let the usual preprocessor parser sort these out.
   627     {
   634     char *define_include = NULL;
   628         if (!add_define(ctx, defines[i]->identifier, defines[i]->definition, 1))
   635     unsigned int define_include_len = 0;
   629         {
   636     if (define_count > 0)
       
   637     {
       
   638         for (i = 0; i < define_count; i++)
       
   639         {
       
   640             define_include_len += strlen(defines[i]->identifier);
       
   641             define_include_len += strlen(defines[i]->definition);
       
   642             define_include_len += 10;  // "#define<space><space><newline>"
       
   643         } // for
       
   644         define_include_len++;  // for null terminator.
       
   645 
       
   646         define_include = (char *) Malloc(ctx, define_include_len);
       
   647         if (define_include == NULL)
   630             okay = 0;
   648             okay = 0;
   631             break;
   649         else
   632         } // if
   650         {
   633     } // for
   651             char *ptr = define_include;
       
   652             for (i = 0; i < define_count; i++)
       
   653             {
       
   654                 ptr += sprintf(ptr, "#define %s %s\n", defines[i]->identifier,
       
   655                                defines[i]->definition);
       
   656             } // for
       
   657         } // else
       
   658     } // if
   634 
   659 
   635     if ((okay) && (!push_source(ctx, fname, source, sourcelen, 1, NULL)))
   660     if ((okay) && (!push_source(ctx, fname, source, sourcelen, 1, NULL)))
   636         okay = 0;
   661         okay = 0;
       
   662 
       
   663     if ((okay) && (define_include != NULL))
       
   664     {
       
   665         okay = push_source(ctx, "<predefined macros>", define_include,
       
   666                            define_include_len, 1, close_define_include);
       
   667     } // if
   637 
   668 
   638     if (!okay)
   669     if (!okay)
   639     {
   670     {
   640         preprocessor_end((Preprocessor *) ctx);
   671         preprocessor_end((Preprocessor *) ctx);
   641         return NULL;
   672         return NULL;