~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/length.cc

  • Committer: Brian Aker
  • Date: 2010-01-27 18:58:12 UTC
  • Revision ID: brian@gaz-20100127185812-n62n0vwetnx8jrjy
Remove dead code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include "binlog_encoding.h"
 
23
 
 
24
#include <iostream>
 
25
#include <iomanip>
 
26
#include <stdexcept>
 
27
#include <string.h>
 
28
#include <getopt.h>
 
29
 
 
30
using std::ios;
 
31
using std::cout;
 
32
using std::cerr;
 
33
using std::flush;
 
34
 
 
35
static void print_usage_and_exit(char *prog) {
 
36
  const char *name= strrchr(prog, '/');
 
37
  if (name)
 
38
    ++name;
 
39
  else
 
40
    name= "length";
 
41
  cerr << "Usage:\n"
 
42
       << "    " << name << " [ -vvvx ] -e <number> ...\n";
 
43
  cerr << "    " << name << " [ -vvvx ] -d <byte> ...\n";
 
44
  cerr << flush;
 
45
  exit(1);
 
46
}
 
47
 
 
48
static void encode(int argc, char *argv[], int verbose_level, bool hex_output)
 
49
{
 
50
  for (int i = 0 ; i < argc ; ++i) {
 
51
    size_t length = strtoul(argv[i], NULL, 0);
 
52
 
 
53
    if (length < 2)
 
54
      throw std::invalid_argument("Length has to be > 1");
 
55
 
 
56
    unsigned char buf[128];
 
57
    unsigned char *end= length_encode(length, buf);
 
58
    ios::fmtflags saved_flags= cout.flags();
 
59
    if (verbose_level > 0)
 
60
      cout << "Length " << length << ": ";
 
61
    if (hex_output)
 
62
      cout << std::hex << std::setw(2) << std::setfill('0');
 
63
    unsigned char *ptr= buf;
 
64
    while (true) {
 
65
      if (hex_output)
 
66
        cout << "0x";
 
67
      cout << (unsigned int) *ptr;
 
68
      if (++ptr == end)
 
69
        break;
 
70
      cout << " ";
 
71
    }
 
72
    cout << std::endl;
 
73
    cout.setf(saved_flags);
 
74
  }
 
75
}
 
76
 
 
77
 
 
78
static void decode(int argc, char *argv[], int verbose_level, bool hex_output)
 
79
{
 
80
  unsigned char buf[128];
 
81
  for (int i = 0 ; i < argc ; ++i)
 
82
    buf[i]= strtoul(argv[i], NULL, 0);
 
83
 
 
84
  size_t length;
 
85
  (void) length_decode(buf, &length);
 
86
 
 
87
  ios::fmtflags saved_flags= cout.flags();
 
88
  if (verbose_level > 0)
 
89
    cout << "Length ";
 
90
  if (hex_output)
 
91
    cout.setf(ios::hex, ios::basefield);
 
92
  cout << length << std::endl;
 
93
  cout.setf(saved_flags);
 
94
}
 
95
 
 
96
 
 
97
int main(int argc, char *argv[]) {
 
98
  enum { NO_ACTION, ENCODE_ACTION, DECODE_ACTION } action= NO_ACTION;
 
99
 
 
100
  static struct option long_options[] = {
 
101
    { "decode",  0 /* has_arg */, NULL, 'd' },
 
102
    { "encode",  0 /* has_arg */, NULL, 'e' },
 
103
    { "verbose", 0 /* has_arg */, NULL, 'v' },
 
104
    { "hex",     0 /* has_arg */, NULL, 'x' },
 
105
    { 0, 0, 0, 0 }
 
106
  };
 
107
 
 
108
  int verbose_level= 0;
 
109
  bool hex_output= false;
 
110
  int ch;
 
111
 
 
112
  while ((ch= getopt_long(argc, argv, "devx", long_options, NULL)) != -1) {
 
113
    switch (ch) {
 
114
    case 0:
 
115
    case '?':
 
116
      print_usage_and_exit(argv[0]);
 
117
      break;
 
118
 
 
119
    case 'd':
 
120
      action= DECODE_ACTION;
 
121
      break;
 
122
 
 
123
    case 'e':
 
124
      action= ENCODE_ACTION;
 
125
      break;
 
126
 
 
127
    case 'v':
 
128
      ++verbose_level;
 
129
      break;
 
130
 
 
131
    case 'x':
 
132
      hex_output= true;
 
133
      break;
 
134
    }
 
135
  }
 
136
 
 
137
  try {
 
138
    switch (action) {
 
139
    case ENCODE_ACTION:
 
140
      encode(argc - optind, argv + optind, verbose_level, hex_output);
 
141
      break;
 
142
    case DECODE_ACTION:
 
143
      decode(argc - optind, argv + optind, verbose_level, hex_output);
 
144
      break;
 
145
    default:
 
146
      print_usage_and_exit(argv[0]);
 
147
      break;
 
148
    }
 
149
  }
 
150
  catch (std::invalid_argument& ex) {
 
151
    cerr << ex.what() << "\n";
 
152
    print_usage_and_exit(argv[0]);
 
153
  }
 
154
 
 
155
  return EXIT_SUCCESS;
 
156
}