prefread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef prefread_h__
  5. #define prefread_h__
  6. #include "prefapi.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /**
  11. * Callback function used to notify consumer of preference name value pairs.
  12. * The pref name and value must be copied by the implementor of the callback
  13. * if they are needed beyond the scope of the callback function.
  14. *
  15. * @param closure
  16. * user data passed to PREF_InitParseState
  17. * @param pref
  18. * preference name
  19. * @param val
  20. * preference value
  21. * @param type
  22. * preference type (PREF_STRING, PREF_INT, or PREF_BOOL)
  23. * @param defPref
  24. * preference type (true: default, false: user preference)
  25. * @param stickyPref
  26. * default preference marked as a "sticky" pref
  27. */
  28. typedef void (*PrefReader)(void *closure,
  29. const char *pref,
  30. PrefValue val,
  31. PrefType type,
  32. bool defPref,
  33. bool stickyPref);
  34. /**
  35. * Report any errors or warnings we encounter during parsing.
  36. */
  37. typedef void (*PrefParseErrorReporter)(const char* message, int line, bool error);
  38. /* structure fields are private */
  39. typedef struct PrefParseState {
  40. PrefReader reader;
  41. PrefParseErrorReporter reporter;
  42. void *closure;
  43. int state; /* PREF_PARSE_... */
  44. int nextstate; /* sometimes used... */
  45. const char *smatch; /* string to match */
  46. int sindex; /* next char of smatch to check */
  47. /* also, counter in \u parsing */
  48. char16_t utf16[2]; /* parsing UTF16 (\u) escape */
  49. int esclen; /* length in esctmp */
  50. char esctmp[6]; /* raw escape to put back if err */
  51. char quotechar; /* char delimiter for quotations */
  52. char *lb; /* line buffer (only allocation) */
  53. char *lbcur; /* line buffer cursor */
  54. char *lbend; /* line buffer end */
  55. char *vb; /* value buffer (ptr into lb) */
  56. PrefType vtype; /* PREF_STRING,INT,BOOL */
  57. bool fdefault; /* true if (default) pref */
  58. bool fstickydefault; /* true if (sticky) pref */
  59. } PrefParseState;
  60. /**
  61. * PREF_InitParseState
  62. *
  63. * Called to initialize a PrefParseState instance.
  64. *
  65. * @param ps
  66. * PrefParseState instance.
  67. * @param reader
  68. * PrefReader callback function, which will be called once for each
  69. * preference name value pair extracted.
  70. * @param reporter
  71. * PrefParseErrorReporter callback function, which will be called if we
  72. * encounter any errors (stop) or warnings (continue) during parsing.
  73. * @param closure
  74. * PrefReader closure.
  75. */
  76. void PREF_InitParseState(PrefParseState *ps, PrefReader reader,
  77. PrefParseErrorReporter reporter, void *closure);
  78. /**
  79. * PREF_FinalizeParseState
  80. *
  81. * Called to release any memory in use by the PrefParseState instance.
  82. *
  83. * @param ps
  84. * PrefParseState instance.
  85. */
  86. void PREF_FinalizeParseState(PrefParseState *ps);
  87. /**
  88. * PREF_ParseBuf
  89. *
  90. * Called to parse a buffer containing some portion of a preference file. This
  91. * function may be called repeatedly as new data is made available. The
  92. * PrefReader callback function passed PREF_InitParseState will be called as
  93. * preference name value pairs are extracted from the data.
  94. *
  95. * @param ps
  96. * PrefParseState instance. Must have been initialized.
  97. * @param buf
  98. * Raw buffer containing data to be parsed.
  99. * @param bufLen
  100. * Length of buffer.
  101. *
  102. * @return false if buffer contains malformed content.
  103. */
  104. bool PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen);
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* prefread_h__ */