2011-05-03 20:21:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-05-08 21:40:26 +00:00
|
|
|
#include <lib.h>
|
2011-05-03 20:18:35 +00:00
|
|
|
#include <psyc/text.h>
|
|
|
|
|
|
|
|
#define BUFSIZE 512
|
|
|
|
|
|
|
|
uint8_t verbose;
|
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycTextValueRC
|
2012-01-04 22:32:01 +00:00
|
|
|
getValueFooBar (void *cls, const char *name, size_t len, PsycString *value)
|
2011-05-03 20:18:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("> getValue: %.*s\n", (int)len, name);
|
|
|
|
value->data = "Foo Bar";
|
|
|
|
value->length = 7;
|
|
|
|
return PSYC_TEXT_VALUE_FOUND;
|
2011-05-03 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycTextValueRC
|
2012-01-04 22:32:01 +00:00
|
|
|
getValueEmpty (void *cls, const char *name, size_t len, PsycString *value)
|
2011-05-03 20:18:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("> getValue: %.*s\n", (int)len, name);
|
|
|
|
value->data = "";
|
|
|
|
value->length = 0;
|
|
|
|
return PSYC_TEXT_VALUE_FOUND;
|
2011-05-03 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycTextValueRC
|
2012-01-04 22:32:01 +00:00
|
|
|
getValueNotFound (void *cls, const char *name, size_t len, PsycString *value)
|
2011-05-03 20:18:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("> getValue: %.*s\n", (int)len, name);
|
|
|
|
return PSYC_TEXT_VALUE_NOT_FOUND;
|
2011-05-03 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
int
|
|
|
|
testText (char *template, size_t tmplen, char *buffer, size_t buflen,
|
|
|
|
PsycString *result, PsycTextCB getValue)
|
2011-05-03 20:18:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycTextState state;
|
|
|
|
size_t length = 0;
|
|
|
|
PsycTextRC ret;
|
|
|
|
|
|
|
|
psyc_text_state_init(&state, template, tmplen, buffer, buflen);
|
|
|
|
do {
|
|
|
|
ret = psyc_text(&state, getValue, NULL);
|
|
|
|
length += psyc_text_bytes_written(&state);
|
|
|
|
switch (ret) {
|
|
|
|
case PSYC_TEXT_INCOMPLETE:
|
|
|
|
if (verbose)
|
|
|
|
printf("# %.*s...\n", (int)length, buffer);
|
|
|
|
psyc_text_buffer_set(&state, buffer + length, BUFSIZE - length);
|
|
|
|
break;
|
|
|
|
case PSYC_TEXT_COMPLETE:
|
|
|
|
if (verbose)
|
|
|
|
printf("%.*s\n", (int)length, buffer);
|
|
|
|
result->length = length;
|
|
|
|
result->data = buffer;
|
|
|
|
return ret;
|
|
|
|
case PSYC_TEXT_NO_SUBST:
|
|
|
|
if (verbose)
|
|
|
|
printf("%.*s\n", (int)tmplen, template);
|
|
|
|
return ret;
|
2011-05-03 20:18:35 +00:00
|
|
|
}
|
2011-11-11 21:18:24 +00:00
|
|
|
}
|
|
|
|
while (ret == PSYC_TEXT_INCOMPLETE);
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
return -2; // shouldn't be reached
|
2011-05-03 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
2011-05-03 20:18:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
verbose = argc > 1;
|
|
|
|
char buffer[BUFSIZE];
|
|
|
|
PsycString result;
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
char *str = "Hello [_foo] & [_bar]!";
|
|
|
|
size_t len = strlen(str);
|
|
|
|
int i;
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
testText(str, len, buffer, BUFSIZE, &result, &getValueFooBar);
|
|
|
|
if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
|
|
|
|
return 1;
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
testText(str, len, buffer, BUFSIZE, &result, &getValueEmpty);
|
|
|
|
if (memcmp(result.data, PSYC_C2ARG("Hello & !")))
|
|
|
|
return 2;
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
if (PSYC_TEXT_NO_SUBST != testText(str, len, buffer, BUFSIZE,
|
|
|
|
&result, &getValueNotFound))
|
|
|
|
return 3;
|
2011-05-03 20:18:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
for (i = 1; i < 22; i++) {
|
|
|
|
testText(str, len, buffer, i, &result, &getValueFooBar);
|
|
|
|
if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
|
|
|
|
return 10 + i;
|
|
|
|
}
|
2011-05-03 21:03:52 +00:00
|
|
|
|
2011-11-30 12:51:50 +00:00
|
|
|
size_t tlen = 0;
|
2011-12-01 13:12:41 +00:00
|
|
|
const char *t = psyc_template(PSYC_MC_NOTICE_CONTEXT_ENTER, &tlen);
|
2011-11-30 12:51:50 +00:00
|
|
|
printf("_notice_context_enter = %s, %ld\n", t, tlen);
|
|
|
|
|
|
|
|
printf("psyc_text passed all tests.\n");
|
2011-05-03 21:03:52 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
return 0;
|
2011-05-03 20:18:35 +00:00
|
|
|
}
|