Initial commit
[Praktika/Routenplanung/cpp-base.git] / decode_vector.cpp
1 #include "vector_io.h"
2 #include <string>
3 #include <iostream>
4 #include <limits>
5 #include <iomanip>
6 using namespace std;
7
8 template<class T>
9 void convert_num_data(const string&input_vector_file){
10         vector<T>v = load_vector<T>(input_vector_file);
11
12         for(auto&x:v)
13                 cout << std::setprecision(std::numeric_limits<T>::digits10+1) << x << '\n';
14 }
15
16 string replace_all_substrings(string subject, const string& search, const string& replace) {
17     size_t pos = 0;
18     while ((pos = subject.find(search, pos)) != std::string::npos) {
19          subject.replace(pos, search.length(), replace);
20          pos += replace.length();
21     }
22     return std::move(subject);
23 }
24
25 void convert_string_data(const string&input_vector_file){
26         vector<string>v = load_vector<string>(input_vector_file);
27
28         for(auto&s:v)
29                 cout << replace_all_substrings(replace_all_substrings(s, "\\", "\\\\"), "\n", "\\n") << '\n';
30 }
31
32 int main(int argc, char*argv[]){
33         try{
34                 string data_type, input_vector_file;
35                 if(argc != 3){
36                         cerr 
37                                 << "Usage: "<< argv[0] << " data_type input_vector_file\n" 
38                                 << "\n"
39                                 << "Reads binary data from input_vector_file and writes the data to the standard output. data_type can be one of\n"
40                                 << " * int8\n"
41                                 << " * uint8\n"
42                                 << " * int16\n"
43                                 << " * uint16\n"
44                                 << " * int32\n"
45                                 << " * uint32\n"
46                                 << " * int64\n"
47                                 << " * uint64\n"
48                                 << " * float32\n"
49                                 << " * float64\n"
50                                 << " * string" << endl;
51                         return 1;
52                 }else{
53                         data_type = argv[1];
54                         input_vector_file = argv[2];
55                 }
56
57                 if(data_type == "int8")
58                         convert_num_data<signed char>(input_vector_file);
59                 else if(data_type == "uint8")
60                         convert_num_data<unsigned char>(input_vector_file);
61                 else if(data_type == "int16")
62                         convert_num_data<signed short>(input_vector_file);
63                 else if(data_type == "uint16")
64                         convert_num_data<unsigned short>(input_vector_file);
65                 else if(data_type == "int32")
66                         convert_num_data<signed int>(input_vector_file);
67                 else if(data_type == "uint32")
68                         convert_num_data<unsigned int>(input_vector_file);
69                 else if(data_type == "int64")
70                         convert_num_data<signed long long>(input_vector_file);
71                 else if(data_type == "uint64")
72                         convert_num_data<unsigned long long>(input_vector_file);
73                 else if(data_type == "float32")
74                         convert_num_data<float>(input_vector_file);
75                 else if(data_type == "float64")
76                         convert_num_data<double>(input_vector_file);
77                 else if(data_type == "string")
78                         convert_string_data(input_vector_file);
79                 else
80                         throw runtime_error("Unknown data type \""+data_type+"\"");
81         }catch(exception&err){
82                 cerr << "Stopped on exception : "<< err.what() << endl;
83         }
84 }