csimple.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zorba/zorbac.h>
#include <zorba/store_manager_c.h>
int
example_1(XQC_Implementation* impl)
{
XQC_Expression* lExpr;
const char* lStringValue;
XQC_Sequence* lResult;
impl->prepare(impl, "for $i in 1 to 10 return $i", NULL, &lExpr);
lExpr->execute(lExpr, NULL, &lResult);
while ( lResult->next(lResult) == XQC_NO_ERROR ) {
lResult->string_value(lResult, &lStringValue);
printf("%s ", lStringValue);
}
printf("\n");
lResult->free(lResult);
lExpr->free(lExpr);
return 1;
}
int
example_2(XQC_Implementation* impl)
{
XQC_Error lError = XQC_NO_ERROR;
XQC_Expression* lExpr;
lError = impl->prepare(impl, "for $x in (1, 2, 3, 4)", NULL, &lExpr);
printf("%d", lError);
return lError == XQC_STATIC_ERROR ? 1 : 0;
}
int
example_3(XQC_Implementation* impl)
{
XQC_Error lError = XQC_NO_ERROR;
XQC_Expression* lExpr;
XQC_Sequence* lResult;
impl->prepare(impl, "1 div 0", NULL, &lExpr);
lError = lExpr->execute(lExpr, NULL, &lResult);
while ( (lError = lResult->next(lResult)) == XQC_NO_ERROR);
lResult->free(lResult);
lExpr->free(lExpr);
return lError == XQC_DYNAMIC_ERROR ? 1 : 0;
}
unsigned int
read_stream(XQC_InputStream* stream, void* buf, unsigned int length)
{
strcpy(buf, "for $i in (1 to 10) return $i");
return 29;
}
void
free_stream(XQC_InputStream* stream)
{
free(stream);
}
int
example_4(XQC_Implementation* impl)
{
XQC_Expression* lExpr;
XQC_InputStream* lStream = (XQC_InputStream*) malloc(sizeof(XQC_InputStream));
XQC_Sequence* lResult;
const char* lStringValue;
lStream->read = read_stream;
lStream->free = free_stream;
impl->prepare_stream(impl, lStream, NULL, &lExpr);
lExpr->execute(lExpr, NULL, &lResult);
while (lResult->next(lResult) == XQC_NO_ERROR) {
lResult->string_value(lResult, &lStringValue);
printf("%s ", lStringValue);
}
lResult->free(lResult);
lExpr->free(lExpr);
return 1;
}
int
csimple(int argc, char** argv)
{
int res = 0;
XQC_Implementation* impl;
void* store = create_store();
if ( zorba_implementation(&impl, store) != XQC_NO_ERROR)
return 1;
printf("executing C example 1\n");
res = example_1(impl);
if (!res) { impl->free(impl); return 1; };
printf("\n");
printf("executing C example 2\n");
res = example_2(impl);
if (!res) { impl->free(impl); return 1; };
printf("\n");
printf("executing C example 3\n");
res = example_3(impl);
if (!res) { impl->free(impl); return 1; };
printf("\n");
printf("executing C example 4\n");
res = example_4(impl);
if (!res) { impl->free(impl); return 1; };
printf("\n");
impl->free(impl);
shutdown_store(store);
return 0;
}