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

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

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