Bitcoin Core  22.99.0
P2P Digital Currency
gen.cpp
Go to the documentation of this file.
1 // Copyright 2014 BitPay Inc.
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://opensource.org/licenses/mit-license.php.
4 
5 //
6 // To re-create univalue_escapes.h:
7 // $ g++ -o gen gen.cpp
8 // $ ./gen > univalue_escapes.h
9 //
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include "univalue.h"
14 
15 static bool initEscapes;
16 static std::string escapes[256];
17 
18 static void initJsonEscape()
19 {
20  // Escape all lower control characters (some get overridden with smaller sequences below)
21  for (int ch=0x00; ch<0x20; ++ch) {
22  char tmpbuf[20];
23  snprintf(tmpbuf, sizeof(tmpbuf), "\\u%04x", ch);
24  escapes[ch] = std::string(tmpbuf);
25  }
26 
27  escapes[(int)'"'] = "\\\"";
28  escapes[(int)'\\'] = "\\\\";
29  escapes[(int)'\b'] = "\\b";
30  escapes[(int)'\f'] = "\\f";
31  escapes[(int)'\n'] = "\\n";
32  escapes[(int)'\r'] = "\\r";
33  escapes[(int)'\t'] = "\\t";
34  escapes[(int)'\x7f'] = "\\u007f"; // U+007F DELETE
35 
36  initEscapes = true;
37 }
38 
39 static void outputEscape()
40 {
41  printf( "// Automatically generated file. Do not modify.\n"
42  "#ifndef BITCOIN_UNIVALUE_UNIVALUE_ESCAPES_H\n"
43  "#define BITCOIN_UNIVALUE_UNIVALUE_ESCAPES_H\n"
44  "static const char *escapes[256] = {\n");
45 
46  for (unsigned int i = 0; i < 256; i++) {
47  if (escapes[i].empty()) {
48  printf("\tnullptr,\n");
49  } else {
50  printf("\t\"");
51 
52  unsigned int si;
53  for (si = 0; si < escapes[i].size(); si++) {
54  char ch = escapes[i][si];
55  switch (ch) {
56  case '"':
57  printf("\\\"");
58  break;
59  case '\\':
60  printf("\\\\");
61  break;
62  default:
63  printf("%c", escapes[i][si]);
64  break;
65  }
66  }
67 
68  printf("\",\n");
69  }
70  }
71 
72  printf( "};\n"
73  "#endif // BITCOIN_UNIVALUE_UNIVALUE_ESCAPES_H\n");
74 }
75 
76 int main (int argc, char *argv[])
77 {
79  outputEscape();
80  return 0;
81 }
82 
escapes
static std::string escapes[256]
Definition: gen.cpp:16
string.h
tinyformat::printf
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1079
initJsonEscape
static void initJsonEscape()
Definition: gen.cpp:18
main
int main(int argc, char *argv[])
Definition: gen.cpp:76
univalue.h
outputEscape
static void outputEscape()
Definition: gen.cpp:39
initEscapes
static bool initEscapes
Definition: gen.cpp:15