1// Copyright (C) 2018 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef CBORDEVICE_H
5#define CBORDEVICE_H
6
7#include <memory>
8#include <stdio.h>
9
10#define CBOR_API inline
11#define CBOR_PRIVATE_API inline
12#define CBOR_NO_PARSER_API 1
13#include <cbor.h>
14
15class CborDevice
16{
17public:
18 CborDevice(FILE *out) : out(out) {}
19
20 void nextItem(const char *comment = nullptr)
21 {
22 i = 0;
23 if (comment)
24 fprintf(stream: out, format: "\n // %s", comment);
25 }
26
27 static CborError callback(void *self, const void *ptr, size_t len, CborEncoderAppendType t)
28 {
29 auto that = static_cast<CborDevice *>(self);
30 auto data = static_cast<const char *>(ptr);
31 if (t == CborEncoderAppendCborData) {
32 while (len--)
33 that->putByte(c: *data++);
34 } else {
35 while (len--)
36 that->putChar(c: *data++);
37 }
38 return CborNoError;
39 }
40
41private:
42 FILE *out;
43 int i = 0;
44
45 void putNewline()
46 {
47 if ((i++ % 8) == 0)
48 fputs(s: "\n ", stream: out);
49 }
50
51 void putByte(uint8_t c)
52 {
53 putNewline();
54 fprintf(stream: out, format: " 0x%02x, ", c);
55 }
56
57 void putChar(char c)
58 {
59 putNewline();
60 if (uchar(c) < 0x20)
61 fprintf(stream: out, format: " '\\x%x',", uint8_t(c));
62 else if (uchar(c) >= 0x7f)
63 fprintf(stream: out, format: " uchar('\\x%x'),", uint8_t(c));
64 else if (c == '\'' || c == '\\')
65 fprintf(stream: out, format: " '\\%c',", c);
66 else
67 fprintf(stream: out, format: " '%c', ", c);
68 }
69};
70
71#endif // CBORDEVICE_H
72

source code of qtbase/src/tools/moc/cbordevice.h