mojoshader_preprocessor.c
changeset 656 ddb1a0b4c443
parent 655 0b82fdeebf86
child 657 1268d95644f0
equal deleted inserted replaced
655:0b82fdeebf86 656:ddb1a0b4c443
   539     } // while
   539     } // while
   540 } // free_filename_cache
   540 } // free_filename_cache
   541 
   541 
   542 
   542 
   543 static int push_source(Context *ctx, const char *fname, const char *source,
   543 static int push_source(Context *ctx, const char *fname, const char *source,
   544                        unsigned int srclen, unsigned int linenum, int included)
   544                        unsigned int srclen, unsigned int linenum,
       
   545                        MOJOSHADER_includeClose close_callback)
   545 {
   546 {
   546     IncludeState *state = get_include(ctx);
   547     IncludeState *state = get_include(ctx);
   547     if (state == NULL)
   548     if (state == NULL)
   548         return 0;
   549         return 0;
   549 
   550 
   555             put_include(ctx, state);
   556             put_include(ctx, state);
   556             return 0;
   557             return 0;
   557         } // if
   558         } // if
   558     } // if
   559     } // if
   559 
   560 
   560     state->included = included;
   561     state->close_callback = close_callback;
   561     state->source_base = source;
   562     state->source_base = source;
   562     state->source = source;
   563     state->source = source;
   563     state->token = source;
   564     state->token = source;
   564     state->orig_length = srclen;
   565     state->orig_length = srclen;
   565     state->bytes_left = srclen;
   566     state->bytes_left = srclen;
   576 {
   577 {
   577     IncludeState *state = ctx->include_stack;
   578     IncludeState *state = ctx->include_stack;
   578     if (state == NULL)
   579     if (state == NULL)
   579         return;
   580         return;
   580 
   581 
   581     if (state->included)
   582     if (state->close_callback)
   582     {
   583     {
   583         ctx->close_callback(state->source_base, ctx->malloc,
   584         state->close_callback(state->source_base, ctx->malloc,
   584                             ctx->free, ctx->malloc_data);
   585                               ctx->free, ctx->malloc_data);
   585     } // if
   586     } // if
   586 
   587 
   587     // state->filename is a pointer to the filename cache; don't free it here!
   588     // state->filename is a pointer to the filename cache; don't free it here!
   588 
   589 
   589     while (state->conditional_stack)
   590     while (state->conditional_stack)
   629             okay = 0;
   630             okay = 0;
   630             break;
   631             break;
   631         } // if
   632         } // if
   632     } // for
   633     } // for
   633 
   634 
   634     if ((okay) && (!push_source(ctx, fname, source, sourcelen, 1, 0)))
   635     if ((okay) && (!push_source(ctx, fname, source, sourcelen, 1, NULL)))
   635         okay = 0;
   636         okay = 0;
   636 
   637 
   637     if (!okay)
   638     if (!okay)
   638     {
   639     {
   639         preprocessor_end((Preprocessor *) ctx);
   640         preprocessor_end((Preprocessor *) ctx);
   760     {
   761     {
   761         fail(ctx, "Include callback failed");  // !!! FIXME: better error
   762         fail(ctx, "Include callback failed");  // !!! FIXME: better error
   762         return;
   763         return;
   763     } // if
   764     } // if
   764 
   765 
   765     if (!push_source(ctx, filename, newdata, newbytes, 1, 1))
   766     if (!push_source(ctx, filename, newdata, newbytes, 1, ctx->close_callback))
   766     {
   767     {
   767         assert(ctx->out_of_memory);
   768         assert(ctx->out_of_memory);
   768         ctx->close_callback(newdata, ctx->malloc, ctx->free, ctx->malloc_data);
   769         ctx->close_callback(newdata, ctx->malloc, ctx->free, ctx->malloc_data);
   769     } // if
   770     } // if
   770 } // handle_pp_include
   771 } // handle_pp_include