Fabcoin Core  0.16.2
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 http://www.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 using namespace std;
16 
17 static bool initEscapes;
18 static std::string escapes[256];
19 
20 static void initJsonEscape()
21 {
22  // Escape all lower control characters (some get overridden with smaller sequences below)
23  for (int ch=0x00; ch<0x20; ++ch) {
24  char tmpbuf[20];
25  snprintf(tmpbuf, sizeof(tmpbuf), "\\u%04x", ch);
26  escapes[ch] = std::string(tmpbuf);
27  }
28 
29  escapes[(int)'"'] = "\\\"";
30  escapes[(int)'\\'] = "\\\\";
31  escapes[(int)'\b'] = "\\b";
32  escapes[(int)'\f'] = "\\f";
33  escapes[(int)'\n'] = "\\n";
34  escapes[(int)'\r'] = "\\r";
35  escapes[(int)'\t'] = "\\t";
36  escapes[(int)'\x7f'] = "\\u007f"; // U+007F DELETE
37 
38  initEscapes = true;
39 }
40 
41 static void outputEscape()
42 {
43  printf( "// Automatically generated file. Do not modify.\n"
44  "#ifndef FABCOIN_UNIVALUE_UNIVALUE_ESCAPES_H\n"
45  "#define FABCOIN_UNIVALUE_UNIVALUE_ESCAPES_H\n"
46  "static const char *escapes[256] = {\n");
47 
48  for (unsigned int i = 0; i < 256; i++) {
49  if (escapes[i].empty()) {
50  printf("\tNULL,\n");
51  } else {
52  printf("\t\"");
53 
54  unsigned int si;
55  for (si = 0; si < escapes[i].size(); si++) {
56  char ch = escapes[i][si];
57  switch (ch) {
58  case '"':
59  printf("\\\"");
60  break;
61  case '\\':
62  printf("\\\\");
63  break;
64  default:
65  printf("%c", escapes[i][si]);
66  break;
67  }
68  }
69 
70  printf("\",\n");
71  }
72  }
73 
74  printf( "};\n"
75  "#endif // FABCOIN_UNIVALUE_UNIVALUE_ESCAPES_H\n");
76 }
77 
78 int main (int argc, char *argv[])
79 {
80  initJsonEscape();
81  outputEscape();
82  return 0;
83 }
84 
int main(int argc, char *argv[])
Definition: gen.cpp:78
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
std::hash for asio::adress
Definition: Common.h:323
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
Definition: picosha2.h:73