DGen/SDL
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Classes | Macros | Functions
ckvp.h File Reference

Simple parser for "key = value" text files, by zamaz. More...

#include <stddef.h>

Go to the source code of this file.

Classes

struct  ckvp_t
 ckvp_t stores the state, it must be initialized with CKVP_INIT before being fed to ckvp_parse(). More...

Macros

#define CKVP_DECL_BEGIN__
#define CKVP_DECL_END__
#define CKVP_OUT_SIZE   511
#define CKVP_OUT_SIZE_STR   "511"
#define CKVP_INIT   { CKVP_NONE, 1, 1, 0x0002, 0, "" }
#define CKVP_NONE   0
 Possible states (previously defined in an enum, but C++ didn't like it)
#define CKVP_OUT_FULL   1
 Buffer "out" is full, must be flushed.
#define CKVP_OUT_KEY   2
 Everything stored until now is a key.
#define CKVP_OUT_VALUE   3
 Everything stored until now is a value.
#define CKVP_ERROR   4
 Error status, check line/column.

Functions

size_t ckvp_parse (ckvp_t *ckvp, size_t size, const char in[])
 ckvp_parse() takes the current state (ckvp), a buffer in[size] and returns the number of characters processed.

Detailed Description

Simple parser for "key = value" text files, by zamaz.

Can also manage the following weird cases:

# comment
# empty lines are ignored
foo = bar # comment
b\
lah = blah ; rah=meh
'with spaces' = "with\ncr"
\x2a = 'do not eat \x2a in simple quotes,'" here we can \x2a"
# EOF

Macro Definition Documentation

#define CKVP_DECL_BEGIN__
#define CKVP_DECL_END__
#define CKVP_ERROR   4

Error status, check line/column.

#define CKVP_INIT   { CKVP_NONE, 1, 1, 0x0002, 0, "" }
#define CKVP_NONE   0

Possible states (previously defined in an enum, but C++ didn't like it)

Nothing to report

#define CKVP_OUT_FULL   1

Buffer "out" is full, must be flushed.

#define CKVP_OUT_KEY   2

Everything stored until now is a key.

#define CKVP_OUT_SIZE   511
#define CKVP_OUT_SIZE_STR   "511"
#define CKVP_OUT_VALUE   3

Everything stored until now is a value.


Function Documentation

size_t ckvp_parse ( ckvp_t ckvp,
size_t  size,
const char  in[] 
)

ckvp_parse() takes the current state (ckvp), a buffer in[size] and returns the number of characters processed.

Each time ckvp_parse() returns, ckvp->state must be checked. If no error occured, ckvp_parse() must be called again with the remaining characters if any, otherwise the next input buffer.

At the end of input, ckvp_parse() must be called with a zero size.

This function doesn't allocate anything.

Parameters:
[in,out]ckvpCurrent state.
sizeNumber of characters in buffer "in".
inInput buffer to parse.
Returns:
Number of characters processed.

State machine definition:

st[current_state][current_character] = next state | action

Special indices for current_character are:

  • 0x100 for action on characters not in the list
  • 0x101 for action when encountering end of input while in the current state (often ACTION_ERROR)