OSSP CVS Repository

ossp - Check-in [4274]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 4274
Date: 2001-Aug-02 14:19:14 (local)
2001-Aug-02 12:19:14 (UTC)
User:simons
Branch:
Comment: Example programs used in the documentation.
Tickets:
Inspections:
Files:
ossp-pkg/xds/docs/decode.c      added-> 1.1
ossp-pkg/xds/docs/encode.c      added-> 1.1

ossp-pkg/xds/docs/decode.c -> 1.1

*** /dev/null    Sat Nov 23 06:17:32 2024
--- -    Sat Nov 23 06:17:33 2024
***************
*** 0 ****
--- 1,66 ----
+ #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[1024];
+     size_t buffer_len;
+     int rc;
+ 
+     xds_int32_t  int32;
+     xds_uint32_t uint32;
+     char*        string;
+ 
+     buffer_len = 0;
+     do
+        {
+        rc = read(STDIN_FILENO, buffer + buffer_len, sizeof(buffer) - buffer_len);
+        if (rc < 0)
+            error_exit(1, "read() failed: %s\n", strerror(errno));
+        else if (rc > 0)
+            buffer_len += rc;
+        }
+     while (rc > 0 && buffer_len < sizeof(buffer));
+ 
+     if (buffer_len >= sizeof(buffer))
+        error_exit(1, "Too much input data for our buffer.\n");
+ 
+     xds = xds_init(XDS_DECODE);
+     if (xds == NULL)
+        error_exit(1, "Failed to initialize XDS context: %s\n", strerror(errno));
+ 
+     if (xds_register(xds, "int32",  &xdr_decode_int32, NULL) != XDS_OK ||
+        xds_register(xds, "uint32", &xdr_decode_uint32, NULL) != XDS_OK ||
+        xds_register(xds, "string", &xdr_decode_string, NULL) != XDS_OK)
+        error_exit(1, "Failed to register my decoding engines!\n");
+ 
+     if (xds_setbuffer(xds, XDS_LOAN, buffer, buffer_len) != XDS_OK)
+        error_exit(1, "setbuffer() failed.\n");
+ 
+     if (xds_decode(xds, "int32 uint32 string", &int32, &uint32, &string) != XDS_OK)
+        error_exit(1, "xds_decode() failed!\n");
+ 
+     xds_destroy(xds);
+ 
+     fprintf(stderr, "Decoded data:\n");
+     fprintf(stderr, "\tint32   = %d\n", int32);
+     fprintf(stderr, "\tuint32 = 0x%x\n", uint32);
+     fprintf(stderr, "\tstring = \"%s\"\n", string);
+ 
+     free(string);
+ 
+     return 0;
+     }


ossp-pkg/xds/docs/encode.c -> 1.1

*** /dev/null    Sat Nov 23 06:17:32 2024
--- -    Sat Nov 23 06:17:33 2024
***************
*** 0 ****
--- 1,51 ----
+ #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);
+ 
+     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;
+     }

CVSTrac 2.0.1