SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / jer_encoder.c
1 /*-
2  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
5 #include <asn_internal.h>
6 #include <stdio.h>
7 #include <errno.h>
8
9 /*
10  * The JER encoder of any type. May be invoked by the application.
11  */
12 asn_enc_rval_t
13 jer_encode(const asn_TYPE_descriptor_t *td, const void *sptr,
14            asn_app_consume_bytes_f *cb,
15            void *app_key) {
16     asn_enc_rval_t er = {0, 0, 0};
17         asn_enc_rval_t tmper;
18         const char *mname;
19         size_t mlen;
20
21         if(!td || !sptr) goto cb_failed;
22
23         mname = td->xml_tag;
24         mlen = strlen(mname);
25
26         ASN__CALLBACK3("{\n\"", 3, mname, mlen, "\":", 2);
27
28         int xFlag = 0;
29         tmper = td->op->jer_encoder(td, sptr, 1, xFlag, cb, app_key);
30         if(tmper.encoded == -1) return tmper;
31         er.encoded += tmper.encoded;
32
33         ASN__CALLBACK("}", 1);
34         //      ASN__CALLBACK3("</", 2, mname, mlen, ">\n", xcan);
35
36         ASN__ENCODED_OK(er);
37 cb_failed:
38         ASN__ENCODE_FAILED;
39 }
40
41 /*
42  * This is a helper function for jer_fprint, which directs all incoming data
43  * into the provided file descriptor.
44  */
45 static int
46 jer__print2fp(const void *buffer, size_t size, void *app_key) {
47         FILE *stream = (FILE *)app_key;
48
49         if(fwrite(buffer, 1, size, stream) != size)
50                 return -1;
51
52         return 0;
53 }
54
55 int
56 jer_fprint(FILE *stream, const asn_TYPE_descriptor_t *td, const void *sptr) {
57         asn_enc_rval_t er = {0,0,0};
58
59         if(!stream) stream = stdout;
60         if(!td || !sptr)
61                 return -1;
62
63         er = jer_encode(td, sptr, jer__print2fp, stream);
64         if(er.encoded == -1)
65                 return -1;
66
67         return fflush(stream);
68 }
69