scanctx.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ----------------------------------------------------------------------------
  2. libconfig - A library for processing structured configuration files
  3. Copyright (C) 2005-2010 Mark A Lindner
  4. This file is part of libconfig.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, see
  15. <http://www.gnu.org/licenses/>.
  16. ----------------------------------------------------------------------------
  17. */
  18. #include "scanctx.h"
  19. #include "wincompat.h"
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #define STRING_BLOCK_SIZE 64
  24. #define CHUNK_SIZE 32
  25. /* ------------------------------------------------------------------------- */
  26. static const char *err_bad_include = "cannot open include file";
  27. static const char *err_include_too_deep = "include file nesting too deep";
  28. /* ------------------------------------------------------------------------- */
  29. static const char *__scanctx_add_filename(struct scan_context *ctx,
  30. const char *filename)
  31. {
  32. unsigned int count = ctx->num_filenames;
  33. const char **f;
  34. for(f = ctx->filenames; count > 0; ++f, --count)
  35. {
  36. if(!strcmp(*f, filename))
  37. {
  38. free((void *)filename);
  39. return(*f); /* already in list */
  40. }
  41. }
  42. if((ctx->num_filenames % CHUNK_SIZE) == 0)
  43. {
  44. ctx->filenames = (const char **)realloc(
  45. (void *)ctx->filenames,
  46. (ctx->num_filenames + CHUNK_SIZE) * sizeof(const char *));
  47. }
  48. ctx->filenames[ctx->num_filenames] = filename;
  49. ++ctx->num_filenames;
  50. return(filename);
  51. }
  52. /* ------------------------------------------------------------------------- */
  53. void scanctx_init(struct scan_context *ctx, const char *top_filename)
  54. {
  55. memset(ctx, 0, sizeof(struct scan_context));
  56. if(top_filename)
  57. ctx->top_filename = __scanctx_add_filename(ctx, strdup(top_filename));
  58. }
  59. /* ------------------------------------------------------------------------- */
  60. const char **scanctx_cleanup(struct scan_context *ctx,
  61. unsigned int *num_filenames)
  62. {
  63. int i;
  64. for(i = 0; i < ctx->depth; ++i)
  65. fclose(ctx->streams[i]);
  66. free((void *)(strbuf_release(&(ctx->string))));
  67. *num_filenames = ctx->num_filenames;
  68. return(ctx->filenames);
  69. }
  70. /* ------------------------------------------------------------------------- */
  71. FILE *scanctx_push_include(struct scan_context *ctx, void *buffer,
  72. const char **error)
  73. {
  74. FILE *fp = NULL;
  75. const char *file;
  76. char *full_file = NULL;
  77. *error = NULL;
  78. if(ctx->depth == MAX_INCLUDE_DEPTH)
  79. {
  80. *error = err_include_too_deep;
  81. return(NULL);
  82. }
  83. file = scanctx_take_string(ctx);
  84. if(ctx->config->include_dir)
  85. {
  86. full_file = (char *)malloc(strlen(ctx->config->include_dir) + strlen(file)
  87. + 2);
  88. strcpy(full_file, ctx->config->include_dir);
  89. strcat(full_file, FILE_SEPARATOR);
  90. strcat(full_file, file);
  91. }
  92. fp = fopen(full_file ? full_file : file, "rt");
  93. free((void *)full_file);
  94. if(fp)
  95. {
  96. ctx->streams[ctx->depth] = fp;
  97. ctx->files[ctx->depth] = __scanctx_add_filename(ctx, file);
  98. ctx->buffers[ctx->depth] = buffer;
  99. ++(ctx->depth);
  100. }
  101. else
  102. {
  103. free((void *)file);
  104. *error = err_bad_include;
  105. }
  106. return(fp);
  107. }
  108. /* ------------------------------------------------------------------------- */
  109. void *scanctx_pop_include(struct scan_context *ctx)
  110. {
  111. void *buffer;
  112. if(ctx->depth == 0)
  113. return(NULL); /* stack underflow */
  114. --(ctx->depth);
  115. buffer = ctx->buffers[ctx->depth];
  116. fclose(ctx->streams[ctx->depth]);
  117. return(buffer);
  118. }
  119. /* ------------------------------------------------------------------------- */
  120. char *scanctx_take_string(struct scan_context *ctx)
  121. {
  122. char *r = strbuf_release(&(ctx->string));
  123. return(r ? r : strdup(""));
  124. }
  125. /* ------------------------------------------------------------------------- */
  126. const char *scanctx_current_filename(struct scan_context *ctx)
  127. {
  128. return((ctx->depth == 0) ? ctx->top_filename : ctx->files[ctx->depth - 1]);
  129. }
  130. /* ------------------------------------------------------------------------- */
  131. /* eof */