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 to_text()
00026 {
00027   std::vector<T> data;
00028   osl::misc::BinaryReader<T> reader(std::cin);
00029   while (reader.read(data)) {
00030     BOOST_FOREACH(T value, data) {
00031       if (boost::is_same<double,T>::value)
00032         printf("%.8f\n", value);
00033       else
00034         std::cout << value << std::endl;
00035     }
00036     if (data.size() < reader.blockSize())
00037       break;
00038   }
00039 }
00040 
00041 int main(int argc, char **argv)
00042 {
00043   extern int optind;
00044   bool error_flag = false;
00045   char c;
00046   while ((c = getopt(argc, argv, "rth")) != EOF)
00047   {
00048     switch(c)
00049     {
00050     case 'r':
00051       real_value = true;
00052       break;
00053     case 't':
00054       binary_to_text = true;
00055       break;
00056     default:    error_flag = true;
00057     }
00058   }
00059   argc -= optind;
00060   argv += optind;
00061   if (error_flag) {
00062     std::cerr << "unknown option\n";
00063     return 1;
00064   }
00065 
00066   if (binary_to_text) {
00067     if (real_value)
00068       to_text<double>();
00069     else
00070       to_text<int>();
00071   }
00072   else {
00073     if (real_value)
00074       to_binary<double>();
00075     else
00076       to_binary<int>();
00077   }
00078 }
00079 // ;;; Local Variables:
00080 // ;;; mode:c++
00081 // ;;; c-basic-offset:2
00082 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines