File: convert.cc

package info (click to toggle)
libosl 0.8.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,328 kB
  • sloc: cpp: 114,345; ruby: 1,290; ansic: 915; makefile: 431; perl: 309; sh: 35
file content (88 lines) | stat: -rw-r--r-- 1,519 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* convert.cc
 */
#include "osl/bits/binaryIO.h"
#include <iostream>
#include <vector>
#include <cassert>
#include <cstdio>
#include <unistd.h>

bool real_value = false, binary_to_text = false;
template <class T>
void to_binary()
{
  std::vector<T> data;
  T value;
  while (std::cin >> value) {
    data.push_back(value);
    assert(value == data.back());
  }
  osl::misc::BinaryWriter::write(std::cout, data);
}

template <class T>
void write_line(T value) 
{
  std::cout << value << std::endl;
}
void write_line(double value) 
{
  printf("%.8f\n", value);
}

template <class T>
void to_text()
{
  std::vector<T> data;
  osl::misc::BinaryReader<T> reader(std::cin);
  while (reader.read(data)) {
    for (T value: data) {
      write_line(value);
    }
    if (data.size() < reader.blockSize())
      break;
  }
}

int main(int argc, char **argv)
{
  extern int optind;
  bool error_flag = false;
  char c;
  while ((c = getopt(argc, argv, "rth")) != EOF)
  {
    switch(c)
    {
    case 'r':
      real_value = true;
      break;
    case 't':
      binary_to_text = true;
      break;
    default:	error_flag = true;
    }
  }
  argc -= optind;
  argv += optind;
  if (error_flag) {
    std::cerr << "unknown option\n";
    return 1;
  }

  if (binary_to_text) {
    if (real_value)
      to_text<double>();
    else
      to_text<int>();
  }
  else {
    if (real_value)
      to_binary<double>();
    else
      to_binary<int>();
  }
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: