convert.cc
Go to the documentation of this file.
00001 /* convert.cc
00002  */
00003 #include "osl/misc/binaryIO.h"
00004 #include <boost/foreach.hpp>
00005 #include <iostream>
00006 #include <vector>
00007 #include <cassert>
00008 #include <cstdio>
00009 #include <unistd.h>
00010 
00011 bool real_value = false, binary_to_text = false;
00012 template <class T>
00013 void to_binary()
00014 {
00015   std::vector<T> data;
00016   T value;
00017   while (std::cin >> value) {
00018     data.push_back(value);
00019     assert(value == data.back());
00020   }
00021   osl::misc::BinaryWriter::write(std::cout, data);
00022 }
00023 
00024 template <class T>
00025 void write_line(T value) 
00026 {
00027   std::cout << value << std::endl;
00028 }
00029 void write_line(double value) 
00030 {
00031   printf("%.8f\n", value);
00032 }
00033 
00034 template <class T>
00035 void to_text()
00036 {
00037   std::vector<T> data;
00038   osl::misc::BinaryReader<T> reader(std::cin);
00039   while (reader.read(data)) {
00040     BOOST_FOREACH(T value, data) {
00041       write_line(value);
00042     }
00043     if (data.size() < reader.blockSize())
00044       break;
00045   }
00046 }
00047 
00048 int main(int argc, char **argv)
00049 {
00050   extern int optind;
00051   bool error_flag = false;
00052   char c;
00053   while ((c = getopt(argc, argv, "rth")) != EOF)
00054   {
00055     switch(c)
00056     {
00057     case 'r':
00058       real_value = true;
00059       break;
00060     case 't':
00061       binary_to_text = true;
00062       break;
00063     default:    error_flag = true;
00064     }
00065   }
00066   argc -= optind;
00067   argv += optind;
00068   if (error_flag) {
00069     std::cerr << "unknown option\n";
00070     return 1;
00071   }
00072 
00073   if (binary_to_text) {
00074     if (real_value)
00075       to_text<double>();
00076     else
00077       to_text<int>();
00078   }
00079   else {
00080     if (real_value)
00081       to_binary<double>();
00082     else
00083       to_binary<int>();
00084   }
00085 }
00086 // ;;; Local Variables:
00087 // ;;; mode:c++
00088 // ;;; c-basic-offset:2
00089 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines