ossp-pkg/xds/docs/encode.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <xds.h>
static void error_exit(int rc, const char* msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
exit(rc);
}
int main()
{
xds_t* xds;
char* buffer;
size_t buffer_size;
xds_int32_t int32 = -42;
xds_uint32_t uint32 = 0x12345678;
const char* string = "This is a test.";
xds = xds_init(XDS_ENCODE);
if (xds == NULL)
error_exit(1, "Failed to initialize XDS context: %s\n", strerror(errno));
if (xds_register(xds, "int32", &xdr_encode_int32, NULL) != XDS_OK ||
xds_register(xds, "uint32", &xdr_encode_uint32, NULL) != XDS_OK ||
xds_register(xds, "string", &xdr_encode_string, NULL) != XDS_OK)
error_exit(1, "Failed to register my encoding engines!\n");
if (xds_encode(xds, "int32 uint32 string", int32, uint32, string) != XDS_OK)
error_exit(1, "xds_encode() failed!\n");
if (xds_getbuffer(xds, XDS_GIFT, (void**)&buffer, &buffer_size) != XDS_OK)
error_exit(1, "getbuffer() failed.\n");
xds_destroy(xds);
write(STDOUT_FILENO, buffer, buffer_size);
free(buffer);
fprintf(stderr, "Encoded data:\n");
fprintf(stderr, "\tint32 = %d\n", int32);
fprintf(stderr, "\tuint32 = 0x%x\n", uint32);
fprintf(stderr, "\tstring = \"%s\"\n", string);
return 0;
}