File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_tlv.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Mon Jan 24 17:22:03 2022 UTC (2 years, 4 months ago) by misho
Branches: MAIN
CVS tags: elwix5_3, HEAD, ELWIX5_2
version 5.2

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <string.h>
    4: #include <unistd.h>
    5: #include <fcntl.h>
    6: #include <elwix.h>
    7: 
    8: 
    9: static void
   10: display(array_t *arr)
   11: {
   12: 	register int i;
   13: 
   14: 	for (i = 0; i < array_Size(arr); i++) {
   15: 		if (AIT_TYPE(array(arr, i, ait_val_t*)) == string)
   16: 			printf("arr[%d]=%s (%d) type=%d\n", i, AIT_ADDR(array(arr, i, ait_val_t*)), 
   17: 					AIT_LEN(array(arr, i, ait_val_t*)), 
   18: 					AIT_TYPE(array(arr, i, ait_val_t*)));
   19: 		else if (AIT_TYPE(array(arr, i, ait_val_t*)) == f32) 
   20: 			printf("arr[%d]#%f (%d) type=%d\n", i, AIT_GET_F32(array(arr, i, ait_val_t*)), 
   21: 					AIT_LEN(array(arr, i, ait_val_t*)), 
   22: 					AIT_TYPE(array(arr, i, ait_val_t*)));
   23: 		else if (AIT_TYPE(array(arr, i, ait_val_t*)) == f64) 
   24: 			printf("arr[%d]#%lf (%d) type=%d\n", i, AIT_GET_F64(array(arr, i, ait_val_t*)), 
   25: 					AIT_LEN(array(arr, i, ait_val_t*)), 
   26: 					AIT_TYPE(array(arr, i, ait_val_t*)));
   27: 		else
   28: 			printf("arr[%d]#%ld (%d) type=%d\n", i, AIT_RAW(array(arr, i, ait_val_t*)), 
   29: 					AIT_LEN(array(arr, i, ait_val_t*)), 
   30: 					AIT_TYPE(array(arr, i, ait_val_t*)));
   31: 	}
   32: }
   33: 
   34: int
   35: main(int argc, char **argv)
   36: {
   37: 	array_t *arr;
   38: 	u_char buf[BUFSIZ];
   39: 	int f, len, ret = 0;
   40: 
   41: 	arr = ait_array2vars((const char**) argv, 42);
   42: 	if (!arr)
   43: 		return 1;
   44: 
   45: 	display(arr);
   46: 
   47: 	printf("\nNeeded buffer size = %d\n", ait_vars2tlv(NULL, 0, arr));
   48: 	printf("Serialized data to buffer = %d\n", (len = ait_vars2tlv(buf, sizeof buf, arr)));
   49: 
   50: 	f = open("test_tlv.bin", O_CREAT | O_WRONLY, 0644);
   51: 	if (f == -1) {
   52: 		ret = 2;
   53: 		goto err;
   54: 	}
   55: 	if ((len = write(f, buf, len)) == -1) {
   56: 		ret = 3;
   57: 		goto err;
   58: 	} else
   59: 		printf("Wrote %d bytes to file test_tlv.bin\n", len);
   60: 
   61: 	ait_freeVars(&arr);
   62: 
   63: 	arr = ait_tlv2vars(buf, len);
   64: 	if (!arr) {
   65: 		ret = 4;
   66: 		goto err;
   67: 	}
   68: 	printf("\n>>> Built vars array from TLV buffer\n\n");
   69: 	display(arr);
   70: 
   71: 	arr = ait_tlv2vars(buf, sizeof buf);
   72: 	if (!arr) {
   73: 		ret = 5;
   74: 		goto err;
   75: 	}
   76: 	printf("\n>>> Built vars array from TLV buffer with zeros at the end\n\n");
   77: 	display(arr);
   78: err:
   79: 	if (f > 2)
   80: 		close(f);
   81: 	ait_freeVars(&arr);
   82: 	return ret;
   83: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>