NCDPlaceholderDb.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file NCDPlaceholderDb.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCDPLACEHOLDERDB_H
  30. #define BADVPN_NCDPLACEHOLDERDB_H
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. struct NCDPlaceholderDb__entry {
  34. char *varnames;
  35. size_t num_names;
  36. };
  37. /**
  38. * Associates variable placeholder numbers to variable names.
  39. * This is populated by {@link NCDInterpProcess_Init} when converting the {@link NCDValue}
  40. * objects in the AST to compact representations in {@link NCDValMem}. Variables are
  41. * replaced with placeholder identifiers (integers), which this object associates
  42. * with their names.
  43. * During interpretation, when a statement is being initialized, the compact form held
  44. * by {@link NCDInterpProcess} is byte-copied, and placeholders are replaced with the
  45. * values of corresponding variables using {@link NCDVal_ReplacePlaceholders}.
  46. */
  47. typedef struct {
  48. struct NCDPlaceholderDb__entry *arr;
  49. size_t count;
  50. size_t capacity;
  51. } NCDPlaceholderDb;
  52. /**
  53. * Initializes the placeholder database.
  54. * Returns 1 on success, and 0 on failure.
  55. */
  56. int NCDPlaceholderDb_Init (NCDPlaceholderDb *o) WARN_UNUSED;
  57. /**
  58. * Frees the placeholder database.
  59. */
  60. void NCDPlaceholderDb_Free (NCDPlaceholderDb *o);
  61. /**
  62. * Adds a variable to the database.
  63. *
  64. * @param varname name of the variable (text, including dots). Must not be NULL.
  65. * @param out_plid on success, the placeholder identifier will be returned here. Must
  66. * not be NULL.
  67. * @return 1 on success, 0 on failure
  68. */
  69. int NCDPlaceholderDb_AddVariable (NCDPlaceholderDb *o, const char *varname, int *out_plid) WARN_UNUSED;
  70. /**
  71. * Retrieves the name of the variable associated with a placeholder identifier.
  72. *
  73. * @param plid placeholder identifier; must have been previously provided by
  74. * {@link NCDPlaceholderDb_AddVariable}.
  75. * @return name of the variable, split by dots. The returned value points to
  76. * an array of pointers to strings, which is terminated by a NULL
  77. * pointer. These all point to internal data in the placeholder
  78. * database; they must not be modified, and remain valid until the
  79. * database is freed.
  80. * Note that there will always be at least one string in the result.
  81. */
  82. void NCDPlaceholderDb_GetVariable (NCDPlaceholderDb *o, int plid, const char **out_varnames, size_t *out_num_names);
  83. #endif