NCDPlaceholderDb.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <ncd/NCDStringIndex.h>
  34. struct NCDPlaceholderDb__entry {
  35. NCD_string_id_t *varnames;
  36. size_t num_names;
  37. };
  38. /**
  39. * Associates variable placeholder numbers to variable names.
  40. * This is populated by {@link NCDInterpProcess_Init} when converting the {@link NCDValue}
  41. * objects in the AST to compact representations in {@link NCDValMem}. Variables are
  42. * replaced with placeholder identifiers (integers), which this object associates
  43. * with their names.
  44. * During interpretation, when a statement is being initialized, the compact form held
  45. * by {@link NCDInterpProcess} is byte-copied, and placeholders are replaced with the
  46. * values of corresponding variables using {@link NCDVal_ReplacePlaceholders}.
  47. */
  48. typedef struct {
  49. struct NCDPlaceholderDb__entry *arr;
  50. size_t count;
  51. size_t capacity;
  52. NCDStringIndex *string_index;
  53. } NCDPlaceholderDb;
  54. /**
  55. * Initializes the placeholder database.
  56. * Returns 1 on success, and 0 on failure.
  57. */
  58. int NCDPlaceholderDb_Init (NCDPlaceholderDb *o, NCDStringIndex *string_index) WARN_UNUSED;
  59. /**
  60. * Frees the placeholder database.
  61. */
  62. void NCDPlaceholderDb_Free (NCDPlaceholderDb *o);
  63. /**
  64. * Adds a variable to the database.
  65. *
  66. * @param varname name of the variable (text, including dots). Must not be NULL.
  67. * @param out_plid on success, the placeholder identifier will be returned here. Must
  68. * not be NULL.
  69. * @return 1 on success, 0 on failure
  70. */
  71. int NCDPlaceholderDb_AddVariable (NCDPlaceholderDb *o, const char *varname, int *out_plid) WARN_UNUSED;
  72. /**
  73. * Retrieves the name of the variable associated with a placeholder identifier.
  74. *
  75. * @param plid placeholder identifier; must have been previously provided by
  76. * {@link NCDPlaceholderDb_AddVariable}.
  77. * @return name of the variable, split by dots. The returned value points to
  78. * an array of pointers to strings, which is terminated by a NULL
  79. * pointer. These all point to internal data in the placeholder
  80. * database; they must not be modified, and remain valid until the
  81. * database is freed.
  82. * Note that there will always be at least one string in the result.
  83. */
  84. void NCDPlaceholderDb_GetVariable (NCDPlaceholderDb *o, int plid, const NCD_string_id_t **out_varnames, size_t *out_num_names);
  85. #endif