Created
March 20, 2026 02:16
-
-
Save mignon-p/cf7a7cf99cc0ab2a4d6bf37e1b9f7707 to your computer and use it in GitHub Desktop.
program to demonstrate lite3 behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Compile with: | |
| * | |
| * gcc -Wall -O -o empty-key empty-key.c `pkg-config --libs --cflags --static lite3` | |
| */ | |
| #include <errno.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include "lite3_context_api.h" | |
| void die (const char *msg, int lineNo) | |
| { | |
| int errnum = errno; | |
| const char *err = strerror (errnum); | |
| if (err == NULL || errnum == 0) { | |
| err = "unspecified error"; | |
| } | |
| fprintf (stderr, "line %d: %s (%d: %s)\n", lineNo, msg, errnum, err); | |
| exit (EXIT_FAILURE); | |
| } | |
| #define DIE(dieMessage) die(dieMessage, __LINE__) | |
| #define CHECK(errIfNegative, dieMessage) \ | |
| do { \ | |
| if ((errIfNegative) < 0) { \ | |
| die(dieMessage, __LINE__); \ | |
| } \ | |
| } while (0) | |
| static const char *type_str (int t) | |
| { | |
| switch (t) { | |
| case LITE3_TYPE_NULL: return "LITE3_TYPE_NULL"; | |
| case LITE3_TYPE_BOOL: return "LITE3_TYPE_BOOL"; | |
| case LITE3_TYPE_I64: return "LITE3_TYPE_I64"; | |
| case LITE3_TYPE_F64: return "LITE3_TYPE_F64"; | |
| case LITE3_TYPE_BYTES: return "LITE3_TYPE_BYTES"; | |
| case LITE3_TYPE_STRING: return "LITE3_TYPE_STRING"; | |
| case LITE3_TYPE_OBJECT: return "LITE3_TYPE_OBJECT"; | |
| case LITE3_TYPE_ARRAY: return "LITE3_TYPE_ARRAY"; | |
| case LITE3_TYPE_INVALID: return "LITE3_TYPE_INVALID"; | |
| default: return "unknown"; | |
| } | |
| } | |
| int main (int argc, char **argv) | |
| { | |
| lite3_ctx *ctx = lite3_ctx_create(); | |
| if (ctx == NULL) { | |
| DIE("lite3_ctx_create"); | |
| } | |
| /* Create an object containing one key/value pair, where | |
| * the key is the empty string, and the value is null. */ | |
| int ret = lite3_ctx_init_obj (ctx); | |
| CHECK(ret, "lite3_ctx_init_obj"); | |
| ret = lite3_ctx_set_null(ctx, 0, ""); | |
| CHECK(ret, "lite3_ctx_set_null"); | |
| printf ("Created message of length %u\n", | |
| (unsigned int) (ctx->buflen)); | |
| /* Now try to iterate over the object we just created. */ | |
| lite3_iter it; | |
| ret = lite3_ctx_iter_create(ctx, 0, &it); | |
| CHECK(ret, "lite3_ctx_iter_create"); | |
| int i; | |
| for (i = 0; ; i++) { | |
| lite3_str key_str; | |
| size_t val_ofs; | |
| printf ("fetching %dth key/value pair...\n", i); | |
| ret = lite3_ctx_iter_next(ctx, &it, &key_str, &val_ofs); | |
| if (ret == LITE3_ITER_DONE) { | |
| break; | |
| } | |
| CHECK(ret, "lite3_ctx_iter_next"); | |
| const char *key = LITE3_STR(ctx->buf, key_str); | |
| if (key == NULL) { | |
| DIE("LITE3_STR"); | |
| } | |
| printf (" key = '%s'; value is at offset %u\n", | |
| key, (unsigned int) val_ofs); | |
| lite3_val *val = (lite3_val *) (ctx->buf + val_ofs); | |
| int t = lite3_val_type (val); | |
| printf (" value is of type %d (%s)\n", t, type_str(t)); | |
| } | |
| printf ("Done!\n"); | |
| lite3_ctx_destroy (ctx); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment