Blame include/exiv2/ini.hpp

Packit Service 21b5d1
// Read an INI file into easy-to-access name/value pairs.
Packit Service 21b5d1
Packit Service 21b5d1
// inih and INIReader are released under the New BSD license (see LICENSE.txt).
Packit Service 21b5d1
// Go to the project home page for more info:
Packit Service 21b5d1
//
Packit Service 21b5d1
// https://github.com/benhoyt/inih
Packit Service 21b5d1
Packit Service 21b5d1
#ifndef __INIREADER_H__
Packit Service 21b5d1
#define __INIREADER_H__
Packit Service 21b5d1
Packit Service 21b5d1
#include "exiv2lib_export.h"
Packit Service 21b5d1
Packit Service 21b5d1
#include <map>
Packit Service 21b5d1
#include <string>
Packit Service 21b5d1
#include <stdio.h>
Packit Service 21b5d1
Packit Service 21b5d1
namespace Exiv2 {
Packit Service 21b5d1
Packit Service 21b5d1
/* inih -- simple .INI file parser
Packit Service 21b5d1
Packit Service 21b5d1
inih is released under the New BSD license (see LICENSE.txt). Go to the project
Packit Service 21b5d1
home page for more info:
Packit Service 21b5d1
Packit Service 21b5d1
https://github.com/benhoyt/inih
Packit Service 21b5d1
Packit Service 21b5d1
*/
Packit Service 21b5d1
Packit Service 21b5d1
#ifndef __INI_H__
Packit Service 21b5d1
#define __INI_H__
Packit Service 21b5d1
Packit Service 21b5d1
/* Make this header file easier to include in C++ code */
Packit Service 21b5d1
#ifdef __cplusplus
Packit Service 21b5d1
extern "C" {
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
//! @brief typedef for prototype of handler function.
Packit Service 21b5d1
typedef int (*ini_handler)(void* user, const char* section,
Packit Service 21b5d1
                           const char* name, const char* value);
Packit Service 21b5d1
Packit Service 21b5d1
//! Typedef for prototype of fgets-style reader function.
Packit Service 21b5d1
typedef char* (*ini_reader)(char* str, int num, void* stream);
Packit Service 21b5d1
Packit Service 21b5d1
/*!
Packit Service 21b5d1
    @brief Parse given INI-style file. May have [section]s, name=value pairs
Packit Service 21b5d1
   (whitespace stripped), and comments starting with ';' (semicolon). Section
Packit Service 21b5d1
   is "" if name=value pair parsed before any section heading. name:value
Packit Service 21b5d1
   pairs are also supported as a concession to Python's configparser.
Packit Service 21b5d1
Packit Service 21b5d1
   For each name=value pair parsed, call handler function with given user
Packit Service 21b5d1
   pointer as well as section, name, and value (data only valid for duration
Packit Service 21b5d1
   of handler call). Handler should return nonzero on success, zero on error.
Packit Service 21b5d1
Packit Service 21b5d1
   @param filename path to file
Packit Service 21b5d1
   @param handler  user defined handler
Packit Service 21b5d1
   @param user     void pointer passed to user handler
Packit Service 21b5d1
Packit Service 21b5d1
   @return 0 on success, line number of first error on parse error (doesn't
Packit Service 21b5d1
   stop on first error), -1 on file open error, or -2 on memory allocation
Packit Service 21b5d1
   error (only when INI_USE_STACK is zero).
Packit Service 21b5d1
Packit Service 21b5d1
   @return 0 on success
Packit Service 21b5d1
*/
Packit Service 21b5d1
int ini_parse(const char* filename, ini_handler handler, void* user);
Packit Service 21b5d1
Packit Service 21b5d1
/*! @brief Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
Packit Service 21b5d1
   close the file when it's finished -- the caller must do that.
Packit Service 21b5d1
Packit Service 21b5d1
   @param file     open "C" file
Packit Service 21b5d1
   @param handler  user defined handler
Packit Service 21b5d1
   @param user     void pointer passed to user handler
Packit Service 21b5d1
   */
Packit Service 21b5d1
int ini_parse_file(FILE* file, ini_handler handler, void* user);
Packit Service 21b5d1
Packit Service 21b5d1
/*! @brief Same as ini_parse(), but takes an ini_reader function pointer instead of
Packit Service 21b5d1
   filename. Used for implementing custom or string-based I/O.
Packit Service 21b5d1
   @param reader   magic
Packit Service 21b5d1
   @param stream   more magic
Packit Service 21b5d1
   @param handler  user defined handler
Packit Service 21b5d1
   @param user     void pointer passed to user handler
Packit Service 21b5d1
Packit Service 21b5d1
   @return 0 on success
Packit Service 21b5d1
*/
Packit Service 21b5d1
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
Packit Service 21b5d1
                     void* user);
Packit Service 21b5d1
Packit Service 21b5d1
/*! @brief Nonzero to allow multi-line value parsing, in the style of Python's
Packit Service 21b5d1
    configparser. If allowed, ini_parse() will call the handler with the same
Packit Service 21b5d1
    name for each subsequent line parsed.
Packit Service 21b5d1
*/
Packit Service 21b5d1
#ifndef INI_ALLOW_MULTILINE
Packit Service 21b5d1
#define INI_ALLOW_MULTILINE 1
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
/*! @brief Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
Packit Service 21b5d1
   the file. See http://code.google.com/p/inih/issues/detail?id=21
Packit Service 21b5d1
*/
Packit Service 21b5d1
#ifndef INI_ALLOW_BOM
Packit Service 21b5d1
#define INI_ALLOW_BOM 1
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
/*! @brief Nonzero to allow inline comments (with valid inline comment characters
Packit Service 21b5d1
   specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
Packit Service 21b5d1
   Python 3.2+ configparser behaviour.
Packit Service 21b5d1
*/
Packit Service 21b5d1
#ifndef INI_ALLOW_INLINE_COMMENTS
Packit Service 21b5d1
#define INI_ALLOW_INLINE_COMMENTS 1
Packit Service 21b5d1
#endif
Packit Service 21b5d1
#ifndef INI_INLINE_COMMENT_PREFIXES
Packit Service 21b5d1
#define INI_INLINE_COMMENT_PREFIXES ";"
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
//! @brief Nonzero to use stack, zero to use heap (malloc/free).
Packit Service 21b5d1
#ifndef INI_USE_STACK
Packit Service 21b5d1
#define INI_USE_STACK 1
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
//! @brief Stop parsing on first error (default is to keep parsing).
Packit Service 21b5d1
#ifndef INI_STOP_ON_FIRST_ERROR
Packit Service 21b5d1
#define INI_STOP_ON_FIRST_ERROR 0
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
//! @brief Maximum line length for any line in INI file.
Packit Service 21b5d1
#ifndef INI_MAX_LINE
Packit Service 21b5d1
#define INI_MAX_LINE 200
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
#ifdef __cplusplus
Packit Service 21b5d1
}
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
#endif /* __INI_H__ */
Packit Service 21b5d1
Packit Service 21b5d1
Packit Service 21b5d1
/*! @brief Read an INI file into easy-to-access name/value pairs. (Note that I've gone
Packit Service 21b5d1
  for simplicity here rather than speed, but it should be pretty decent.)
Packit Service 21b5d1
  */
Packit Service 21b5d1
class EXIV2API INIReader
Packit Service 21b5d1
{
Packit Service 21b5d1
public:
Packit Service 21b5d1
    /*! @brief Construct INIReader and parse given filename. See ini.h for more info
Packit Service 21b5d1
       about the parsing.
Packit Service 21b5d1
    */
Packit Service 21b5d1
    explicit INIReader(const std::string& filename);
Packit Service 21b5d1
Packit Service 21b5d1
    /*! @brief Return the result of ini_parse(), i.e., 0 on success, line number of
Packit Service 21b5d1
        first error on parse error, or -1 on file open error.
Packit Service 21b5d1
    */
Packit Service 21b5d1
    int ParseError();
Packit Service 21b5d1
Packit Service 21b5d1
    /*! @brief Get a string value from INI file, returning default_value if not found.
Packit Service 21b5d1
Packit Service 21b5d1
      @param section name of section
Packit Service 21b5d1
      @param name    name of key
Packit Service 21b5d1
      @param default_value default if not found
Packit Service 21b5d1
Packit Service 21b5d1
      @return value
Packit Service 21b5d1
    */
Packit Service 21b5d1
    std::string Get(std::string section, std::string name,
Packit Service 21b5d1
                    std::string default_value);
Packit Service 21b5d1
Packit Service 21b5d1
    /*! @brief Get an integer (long) value from INI file, returning default_value if
Packit Service 21b5d1
        not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
Packit Service 21b5d1
Packit Service 21b5d1
      @param section name of section
Packit Service 21b5d1
      @param name    name of key
Packit Service 21b5d1
      @param default_value default if not found
Packit Service 21b5d1
Packit Service 21b5d1
      @return value
Packit Service 21b5d1
    */
Packit Service 21b5d1
    long GetInteger(std::string section, std::string name, long default_value);
Packit Service 21b5d1
Packit Service 21b5d1
    /*! @brief Get a real (floating point double) value from INI file, returning
Packit Service 21b5d1
        default_value if not found or not a valid floating point value
Packit Service 21b5d1
        according to strtod().
Packit Service 21b5d1
Packit Service 21b5d1
      @param section name of section
Packit Service 21b5d1
      @param name    name of key
Packit Service 21b5d1
      @param default_value default if not found
Packit Service 21b5d1
Packit Service 21b5d1
      @return value
Packit Service 21b5d1
    */
Packit Service 21b5d1
    double GetReal(std::string section, std::string name, double default_value);
Packit Service 21b5d1
Packit Service 21b5d1
    /*! @brief Get a boolean value from INI file, returning default_value if not found or if
Packit Service 21b5d1
        not a valid true/false value. Valid true values are "true", "yes", "on", "1",
Packit Service 21b5d1
        and valid false values are "false", "no", "off", "0" (not case sensitive).
Packit Service 21b5d1
Packit Service 21b5d1
      @param section name of section
Packit Service 21b5d1
      @param name    name of key
Packit Service 21b5d1
      @param default_value default if not found
Packit Service 21b5d1
Packit Service 21b5d1
      @return value
Packit Service 21b5d1
    */
Packit Service 21b5d1
    bool GetBoolean(std::string section, std::string name, bool default_value);
Packit Service 21b5d1
Packit Service 21b5d1
private:
Packit Service 21b5d1
    int _error;                                                        //!< status
Packit Service 21b5d1
    std::map<std::string, std::string> _values;                        //!< values from file
Packit Service 21b5d1
    static std::string MakeKey(std::string section, std::string name); //!< return key encoded from section/name
Packit Service 21b5d1
    static int ValueHandler(void* user, const char* section, const char* name,
Packit Service 21b5d1
                            const char* value); //!< value handler
Packit Service 21b5d1
};
Packit Service 21b5d1
} // namespace Exiv2
Packit Service 21b5d1
Packit Service 21b5d1
#endif  // __INIREADER_H__