2011-05-03 20:18:35 +00:00
|
|
|
/**
|
|
|
|
* The return value definitions for the PSYC text parsing function.
|
|
|
|
* @see psyc_text()
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_TEXT_NO_SUBST = -1,
|
|
|
|
PSYC_TEXT_COMPLETE = 0,
|
|
|
|
PSYC_TEXT_INCOMPLETE = 1,
|
|
|
|
} psycTextRC;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_TEXT_VALUE_NOT_FOUND = -1,
|
|
|
|
PSYC_TEXT_VALUE_FOUND = 0,
|
|
|
|
} psycTextValueRC;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Struct for keeping PSYC text parser state.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t cursor; ///< current position in the template
|
|
|
|
size_t written; ///< number of bytes written to buffer
|
|
|
|
psycString template; ///< template to parse
|
|
|
|
psycString buffer; ///< buffer for writing to
|
|
|
|
psycString open;
|
|
|
|
psycString close;
|
|
|
|
} psycTextState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for psyc_text() that produces a value for a match.
|
|
|
|
*
|
|
|
|
* The application looks up a match such as _fruit from [_fruit] and
|
|
|
|
* if found writes its current value from its variable store into the
|
|
|
|
* outgoing buffer.. "Apple" for example. The template returns the
|
|
|
|
* number of bytes written. 0 is a legal return value. Should the
|
|
|
|
* callback return -1, psyc_text leaves the original template text as is.
|
|
|
|
*/
|
2011-05-03 21:03:52 +00:00
|
|
|
typedef psycTextValueRC (*psycTextCB)(const char *name, size_t len, psycString *value);
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-05-03 21:03:52 +00:00
|
|
|
static inline
|
|
|
|
void psyc_initTextState (psycTextState *state,
|
|
|
|
char *template, size_t tlen,
|
|
|
|
char *buffer, size_t blen)
|
|
|
|
{
|
|
|
|
state->cursor = state->written = 0;
|
|
|
|
state->template = psyc_newString(template, tlen);
|
|
|
|
state->buffer = psyc_newString(buffer, blen);
|
|
|
|
state->open = psyc_newString("[", 1);
|
|
|
|
state->close = psyc_newString("]", 1);
|
|
|
|
}
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-05-03 21:03:52 +00:00
|
|
|
static inline
|
|
|
|
void psyc_initTextState2 (psycTextState* state,
|
|
|
|
char *template, size_t tlen,
|
|
|
|
char *buffer, size_t blen,
|
|
|
|
char *open, size_t openlen,
|
|
|
|
char *close, size_t closelen)
|
|
|
|
{
|
|
|
|
state->template = psyc_newString(template, tlen);
|
|
|
|
state->buffer = psyc_newString(buffer, blen);
|
|
|
|
state->open = psyc_newString(open, openlen);
|
|
|
|
state->close = psyc_newString(close, closelen);
|
|
|
|
}
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-05-03 21:03:52 +00:00
|
|
|
static inline
|
|
|
|
void psyc_setTextBuffer (psycTextState* state, psycString buffer)
|
|
|
|
{
|
|
|
|
state->buffer = buffer;
|
|
|
|
state->written = 0;
|
|
|
|
}
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-05-03 21:03:52 +00:00
|
|
|
static inline
|
|
|
|
void psyc_setTextBuffer2 (psycTextState* state,
|
|
|
|
char *buffer, size_t length)
|
|
|
|
{
|
|
|
|
psyc_setTextBuffer(state, psyc_newString(buffer, length));
|
|
|
|
}
|
2011-05-03 20:18:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills out text templates by asking a callback for content.
|
|
|
|
*
|
|
|
|
* Copies the contents of the template into the buffer while looking
|
|
|
|
* for braceOpen and braceClose strings and calling the callback for
|
|
|
|
* each enclosed string between these braces. Should the callback
|
|
|
|
* return -1, the original template text is copied as is.
|
|
|
|
*
|
|
|
|
* By default PSYC's "[" and "]" are used but you can provide any other
|
|
|
|
* brace strings such as "${" and "}" or "<!--" and "-->".
|
|
|
|
*
|
|
|
|
* See also http://about.psyc.eu/psyctext
|
|
|
|
*/
|
|
|
|
|
|
|
|
psycTextRC psyc_text(psycTextState *state, psycTextCB getValue);
|