~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
16
void print_usage_and_exit(char *prog) {
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
29
void encode(int argc, char *argv[], int verbose_level, bool hex_output) {
30
  for (int i = 0 ; i < argc ; ++i) {
31
    size_t length = strtoul(argv[i], NULL, 0);
32
33
    if (length < 2)
34
      throw std::invalid_argument("Length has to be > 1");
35
36
    unsigned char buf[128];
37
    unsigned char *end= length_encode(length, buf);
38
    ios::fmtflags saved_flags= cout.flags();
39
    if (verbose_level > 0)
40
      cout << "Length " << length << ": ";
41
    if (hex_output)
42
      cout << std::hex << std::setw(2) << std::setfill('0');
43
    unsigned char *ptr= buf;
44
    while (true) {
45
      if (hex_output)
46
        cout << "0x";
47
      cout << (unsigned int) *ptr;
48
      if (++ptr == end)
49
        break;
50
      cout << " ";
51
    }
52
    cout << std::endl;
53
    cout.setf(saved_flags);
54
  }
55
}
56
57
58
void decode(int argc, char *argv[], int verbose_level, bool hex_output) {
59
  unsigned char buf[128];
60
  for (int i = 0 ; i < argc ; ++i)
61
    buf[i]= strtoul(argv[i], NULL, 0);
62
63
  size_t length;
64
  (void) length_decode(buf, &length);
65
66
  ios::fmtflags saved_flags= cout.flags();
67
  if (verbose_level > 0)
68
    cout << "Length ";
69
  if (hex_output)
70
    cout.setf(ios::hex, ios::basefield);
71
  cout << length << std::endl;
72
  cout.setf(saved_flags);
73
}
74
75
76
int main(int argc, char *argv[]) {
77
  enum { NO_ACTION, ENCODE_ACTION, DECODE_ACTION } action= NO_ACTION;
78
79
  static struct option long_options[] = {
80
    { "decode",  0 /* has_arg */, NULL, 'd' },
81
    { "encode",  0 /* has_arg */, NULL, 'e' },
82
    { "verbose", 0 /* has_arg */, NULL, 'v' },
83
    { "hex",     0 /* has_arg */, NULL, 'x' },
84
    { 0, 0, 0, 0 }
85
  };
86
87
  int verbose_level= 0;
88
  bool hex_output= false;
89
  int ch;
90
91
  while ((ch= getopt_long(argc, argv, "devx", long_options, NULL)) != -1) {
92
    switch (ch) {
93
    case 0:
94
    case '?':
95
      print_usage_and_exit(argv[0]);
96
      break;
97
98
    case 'd':
99
      action= DECODE_ACTION;
100
      break;
101
102
    case 'e':
103
      action= ENCODE_ACTION;
104
      break;
105
106
    case 'v':
107
      ++verbose_level;
108
      break;
109
110
    case 'x':
111
      hex_output= true;
112
      break;
113
    }
114
  }
115
116
  try {
117
    switch (action) {
118
    case ENCODE_ACTION:
119
      encode(argc - optind, argv + optind, verbose_level, hex_output);
120
      break;
121
    case DECODE_ACTION:
122
      decode(argc - optind, argv + optind, verbose_level, hex_output);
123
      break;
124
    default:
125
      print_usage_and_exit(argv[0]);
126
      break;
127
    }
128
  }
129
  catch (std::invalid_argument& ex) {
130
    cerr << ex.what() << "\n";
131
    print_usage_and_exit(argv[0]);
132
  }
133
134
  return EXIT_SUCCESS;
135
}