esl_config.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2007-2014, Anthony Minessale II
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of the original author; nor the names of any contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /**
  34. * @defgroup config Config File Parser
  35. * @ingroup config
  36. * This module implements a basic interface and file format parser
  37. *
  38. * <pre>
  39. *
  40. * EXAMPLE
  41. *
  42. * [category1]
  43. * var1 => val1
  44. * var2 => val2
  45. * \# lines that begin with \# are comments
  46. * \#var3 => val3
  47. * </pre>
  48. * @{
  49. */
  50. #ifndef ESL_CONFIG_H
  51. #define ESL_CONFIG_H
  52. #include "esl.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif /* defined(__cplusplus) */
  56. #define ESL_URL_SEPARATOR "://"
  57. #ifdef WIN32
  58. #define ESL_PATH_SEPARATOR "\\"
  59. #ifndef ESL_CONFIG_DIR
  60. #define ESL_CONFIG_DIR "c:\\openesl"
  61. #endif
  62. #define esl_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR))
  63. #else
  64. #define ESL_PATH_SEPARATOR "/"
  65. #ifndef ESL_CONFIG_DIR
  66. #define ESL_CONFIG_DIR "/etc/openesl"
  67. #endif
  68. #define esl_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR))
  69. #endif
  70. /*!
  71. \brief Evaluate the truthfullness of a string expression
  72. \param expr a string expression
  73. \return true or false
  74. */
  75. static __inline__ int esl_true(const char *expr) {
  76. return (expr && (!strcasecmp(expr, "yes")
  77. || !strcasecmp(expr, "on")
  78. || !strcasecmp(expr, "true")
  79. || !strcasecmp(expr, "enabled")
  80. || !strcasecmp(expr, "active")
  81. || !strcasecmp(expr, "allow")
  82. || atoi(expr)));
  83. }
  84. /*!
  85. \brief Evaluate the falsefullness of a string expression
  86. \param expr a string expression
  87. \return true or false
  88. */
  89. static __inline__ int esl_false(const char *expr) {
  90. return (expr && (!strcasecmp(expr, "no")
  91. || !strcasecmp(expr, "off")
  92. || !strcasecmp(expr, "false")
  93. || !strcasecmp(expr, "disabled")
  94. || !strcasecmp(expr, "inactive")
  95. || !strcasecmp(expr, "disallow")
  96. || !atoi(expr)));
  97. }
  98. typedef struct esl_config esl_config_t;
  99. /*! \brief A simple file handle representing an open configuration file **/
  100. struct esl_config {
  101. /*! FILE stream buffer to the opened file */
  102. FILE *file;
  103. /*! path to the file */
  104. char path[512];
  105. /*! current category */
  106. char category[256];
  107. /*! current section */
  108. char section[256];
  109. /*! buffer of current line being read */
  110. char buf[1024];
  111. /*! current line number in file */
  112. int lineno;
  113. /*! current category number in file */
  114. int catno;
  115. /*! current section number in file */
  116. int sectno;
  117. int lockto;
  118. };
  119. /*!
  120. \brief Open a configuration file
  121. \param cfg (esl_config_t *) config handle to use
  122. \param file_path path to the file
  123. \return 1 (true) on success 0 (false) on failure
  124. */
  125. ESL_DECLARE(int) esl_config_open_file(esl_config_t * cfg, const char *file_path);
  126. /*!
  127. \brief Close a previously opened configuration file
  128. \param cfg (esl_config_t *) config handle to use
  129. */
  130. ESL_DECLARE(void) esl_config_close_file(esl_config_t * cfg);
  131. /*!
  132. \brief Retrieve next name/value pair from configuration file
  133. \param cfg (esl_config_t *) config handle to use
  134. \param var pointer to aim at the new variable name
  135. \param val pointer to aim at the new value
  136. */
  137. ESL_DECLARE(int) esl_config_next_pair(esl_config_t * cfg, char **var, char **val);
  138. /*!
  139. \brief Retrieve the CAS bits from a configuration string value
  140. \param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx)
  141. \param outbits pointer to aim at the CAS bits
  142. */
  143. ESL_DECLARE(int) esl_config_get_cas_bits(char *strvalue, unsigned char *outbits);
  144. /** @} */
  145. #ifdef __cplusplus
  146. }
  147. #endif /* defined(__cplusplus) */
  148. #endif /* defined(ESL_CONFIG_H) */
  149. /* For Emacs:
  150. * Local Variables:
  151. * mode:c
  152. * indent-tabs-mode:t
  153. * tab-width:4
  154. * c-basic-offset:4
  155. * End:
  156. * For VIM:
  157. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  158. */