SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / NativeInteger_print.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <NativeInteger.h>
8
9 /*
10  * INTEGER specific human-readable output.
11  */
12 int
13 NativeInteger_print(const asn_TYPE_descriptor_t *td, const void *sptr,
14                     int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
15     const asn_INTEGER_specifics_t *specs =
16         (const asn_INTEGER_specifics_t *)td->specifics;
17     const long *native = (const long *)sptr;
18     char scratch[32];  /* Enough for 64-bit int */
19     int ret;
20
21     (void)td;      /* Unused argument */
22     (void)ilevel;  /* Unused argument */
23
24     if(native) {
25         long value = *native;
26         ret = snprintf(scratch, sizeof(scratch),
27                        (specs && specs->field_unsigned) ? "%lu" : "%ld", value);
28         assert(ret > 0 && (size_t)ret < sizeof(scratch));
29         if(cb(scratch, ret, app_key) < 0) return -1;
30         if(specs && (value >= 0 || !specs->field_unsigned)) {
31             const asn_INTEGER_enum_map_t *el =
32                 INTEGER_map_value2enum(specs, value);
33             if(el) {
34                 if(cb(" (", 2, app_key) < 0) return -1;
35                 if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1;
36                 if(cb(")", 1, app_key) < 0) return -1;
37             }
38         }
39         return 0;
40     } else {
41         return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
42     }
43 }