~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/length.cc

  • Committer: Monty Taylor
  • Date: 2009-01-29 19:04:39 UTC
  • mto: (779.3.29 devel)
  • mto: This revision was merged to the branch mainline in revision 823.
  • Revision ID: mordred@inaugust.com-20090129190439-vfr95s6gaudjacm7
Add timegm which is missing on Solaris.

Show diffs side-by-side

added added

removed removed

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