~drizzle-trunk/drizzle/development

992.1.9 by Monty Taylor
Include fix for solaris make distcheck.
1
#include <drizzled/global.h>
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
2
3
#include "binlog_encoding.h"
4
5
#include <iostream>
6
#include <iomanip>
7
#include <stdexcept>
992.1.9 by Monty Taylor
Include fix for solaris make distcheck.
8
#include <string.h>
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
9
#include <getopt.h>
10
11
using std::ios;
12
using std::cout;
13
using std::cerr;
14
using std::flush;
15
1093.5.1 by Monty Taylor
Fixed a build error.
16
static void print_usage_and_exit(char *prog) {
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
17
  const char *name= strrchr(prog, '/');
18
  if (name)
19
    ++name;
20
  else
21
    name= "length";
22
  cerr << "Usage:\n"
23
       << "    " << name << " [ -vvvx ] -e <number> ...\n";
24
  cerr << "    " << name << " [ -vvvx ] -d <byte> ...\n";
25
  cerr << flush;
26
  exit(1);
27
}
28
1093.5.1 by Monty Taylor
Fixed a build error.
29
static void encode(int argc, char *argv[], int verbose_level, bool hex_output)
30
{
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
31
  for (int i = 0 ; i < argc ; ++i) {
32
    size_t length = strtoul(argv[i], NULL, 0);
33
34
    if (length < 2)
35
      throw std::invalid_argument("Length has to be > 1");
36
37
    unsigned char buf[128];
38
    unsigned char *end= length_encode(length, buf);
39
    ios::fmtflags saved_flags= cout.flags();
40
    if (verbose_level > 0)
41
      cout << "Length " << length << ": ";
42
    if (hex_output)
43
      cout << std::hex << std::setw(2) << std::setfill('0');
44
    unsigned char *ptr= buf;
45
    while (true) {
46
      if (hex_output)
47
        cout << "0x";
48
      cout << (unsigned int) *ptr;
49
      if (++ptr == end)
50
        break;
51
      cout << " ";
52
    }
53
    cout << std::endl;
54
    cout.setf(saved_flags);
55
  }
56
}
57
58
1093.5.1 by Monty Taylor
Fixed a build error.
59
static void decode(int argc, char *argv[], int verbose_level, bool hex_output)
60
{
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
61
  unsigned char buf[128];
62
  for (int i = 0 ; i < argc ; ++i)
63
    buf[i]= strtoul(argv[i], NULL, 0);
64
65
  size_t length;
66
  (void) length_decode(buf, &length);
67
68
  ios::fmtflags saved_flags= cout.flags();
69
  if (verbose_level > 0)
70
    cout << "Length ";
71
  if (hex_output)
72
    cout.setf(ios::hex, ios::basefield);
73
  cout << length << std::endl;
74
  cout.setf(saved_flags);
75
}
76
77
78
int main(int argc, char *argv[]) {
79
  enum { NO_ACTION, ENCODE_ACTION, DECODE_ACTION } action= NO_ACTION;
80
81
  static struct option long_options[] = {
82
    { "decode",  0 /* has_arg */, NULL, 'd' },
83
    { "encode",  0 /* has_arg */, NULL, 'e' },
84
    { "verbose", 0 /* has_arg */, NULL, 'v' },
85
    { "hex",     0 /* has_arg */, NULL, 'x' },
86
    { 0, 0, 0, 0 }
87
  };
88
89
  int verbose_level= 0;
90
  bool hex_output= false;
91
  int ch;
92
93
  while ((ch= getopt_long(argc, argv, "devx", long_options, NULL)) != -1) {
94
    switch (ch) {
95
    case 0:
96
    case '?':
97
      print_usage_and_exit(argv[0]);
98
      break;
99
100
    case 'd':
101
      action= DECODE_ACTION;
102
      break;
103
104
    case 'e':
105
      action= ENCODE_ACTION;
106
      break;
107
108
    case 'v':
109
      ++verbose_level;
110
      break;
111
112
    case 'x':
113
      hex_output= true;
114
      break;
115
    }
116
  }
117
118
  try {
119
    switch (action) {
120
    case ENCODE_ACTION:
121
      encode(argc - optind, argv + optind, verbose_level, hex_output);
122
      break;
123
    case DECODE_ACTION:
124
      decode(argc - optind, argv + optind, verbose_level, hex_output);
125
      break;
126
    default:
127
      print_usage_and_exit(argv[0]);
128
      break;
129
    }
130
  }
131
  catch (std::invalid_argument& ex) {
132
    cerr << ex.what() << "\n";
133
    print_usage_and_exit(argv[0]);
134
  }
135
136
  return EXIT_SUCCESS;
137
}