1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 Intel Corporation.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef CBORDEVICE_H
30 #define CBORDEVICE_H
31 
32 #include <memory>
33 #include <stdio.h>
34 
35 #define CBOR_API            inline
36 #define CBOR_PRIVATE_API    inline
37 #define CBOR_NO_PARSER_API  1
38 #include <cbor.h>
39 
40 class CborDevice
41 {
42 public:
CborDevice(FILE * out)43     CborDevice(FILE *out) : out(out) {}
44 
45     void nextItem(const char *comment = nullptr)
46     {
47         i = 0;
48         if (comment)
49             fprintf(out, "\n    // %s", comment);
50     }
51 
callback(void * self,const void * ptr,size_t len,CborEncoderAppendType t)52     static CborError callback(void *self, const void *ptr, size_t len, CborEncoderAppendType t)
53     {
54         auto that = static_cast<CborDevice *>(self);
55         auto data = static_cast<const char *>(ptr);
56         if (t == CborEncoderAppendCborData) {
57             while (len--)
58                 that->putByte(*data++);
59         } else {
60             while (len--)
61                 that->putChar(*data++);
62         }
63         return CborNoError;
64     }
65 
66 private:
67     FILE *out;
68     int i = 0;
69 
putNewline()70     void putNewline()
71     {
72         if ((i++ % 8) == 0)
73             fputs("\n   ", out);
74     }
75 
putByte(uint8_t c)76     void putByte(uint8_t c)
77     {
78         putNewline();
79         fprintf(out, " 0x%02x, ", c);
80     }
81 
putChar(char c)82     void putChar(char c)
83     {
84         putNewline();
85         if (uchar(c) < 0x20)
86             fprintf(out, " '\\x%x',", uint8_t(c));
87         else if (uchar(c) >= 0x7f)
88             fprintf(out, " uchar('\\x%x'),", uint8_t(c));
89         else if (c == '\'' || c == '\\')
90             fprintf(out, " '\\%c',", c);
91         else
92             fprintf(out, " '%c', ", c);
93     }
94 };
95 
96 #endif // CBORDEVICE_H
97