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