All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
misc/convert.cc
Go to the documentation of this file.
1 /* convert.cc
2  */
3 #include "osl/misc/binaryIO.h"
4 #include <boost/foreach.hpp>
5 #include <iostream>
6 #include <vector>
7 #include <cassert>
8 #include <cstdio>
9 #include <unistd.h>
10 
11 bool real_value = false, binary_to_text = false;
12 template <class T>
13 void to_binary()
14 {
15  std::vector<T> data;
16  T value;
17  while (std::cin >> value) {
18  data.push_back(value);
19  assert(value == data.back());
20  }
21  osl::misc::BinaryWriter::write(std::cout, data);
22 }
23 
24 template <class T>
25 void write_line(T value)
26 {
27  std::cout << value << std::endl;
28 }
29 void write_line(double value)
30 {
31  printf("%.8f\n", value);
32 }
33 
34 template <class T>
35 void to_text()
36 {
37  std::vector<T> data;
38  osl::misc::BinaryReader<T> reader(std::cin);
39  while (reader.read(data)) {
40  BOOST_FOREACH(T value, data) {
41  write_line(value);
42  }
43  if (data.size() < reader.blockSize())
44  break;
45  }
46 }
47 
48 int main(int argc, char **argv)
49 {
50  extern int optind;
51  bool error_flag = false;
52  char c;
53  while ((c = getopt(argc, argv, "rth")) != EOF)
54  {
55  switch(c)
56  {
57  case 'r':
58  real_value = true;
59  break;
60  case 't':
61  binary_to_text = true;
62  break;
63  default: error_flag = true;
64  }
65  }
66  argc -= optind;
67  argv += optind;
68  if (error_flag) {
69  std::cerr << "unknown option\n";
70  return 1;
71  }
72 
73  if (binary_to_text) {
74  if (real_value)
75  to_text<double>();
76  else
77  to_text<int>();
78  }
79  else {
80  if (real_value)
81  to_binary<double>();
82  else
83  to_binary<int>();
84  }
85 }
86 // ;;; Local Variables:
87 // ;;; mode:c++
88 // ;;; c-basic-offset:2
89 // ;;; End: