add readme
[Praktika/Routenplanung/cpp-base.git] / encode_vector.cpp
1 #include "vector_io.h"
2 #include <string>
3 #include <iostream>
4 #include <limits>
5 using namespace std;
6
7
8 template<class T>
9 void convert_int_data(const string&output_vector_file){
10         string line;
11         vector<T>v;
12         while(getline(cin, line)){
13                 long long x = stoll(line);
14                 if(x < numeric_limits<T>::min())
15                         throw runtime_error("The number \""+line+"\" is too small, min is \""+to_string(numeric_limits<T>::min())+"\"");
16                 if(x > numeric_limits<T>::max())
17                         throw runtime_error("The number \""+line+"\" is too large, max is \""+to_string(numeric_limits<T>::max())+"\"");
18                 v.push_back(x);
19         }
20         save_vector(output_vector_file, v);
21 }
22
23 template<class T>
24 void convert_float_data(const string&output_vector_file){
25         string line;
26         vector<T>v;
27         while(getline(cin, line)){
28                 v.push_back(stold(line));
29         }
30         save_vector(output_vector_file, v);
31 }
32
33 void convert_uint64_data(const string&output_vector_file){
34         string line;
35         vector<unsigned long long>v;
36         while(getline(cin, line)){
37                 v.push_back(stoull(line));
38         }
39         save_vector(output_vector_file, v);
40 }
41
42 string replace_all_substrings(string subject, const string& search, const string& replace) {
43     size_t pos = 0;
44     while ((pos = subject.find(search, pos)) != std::string::npos) {
45          subject.replace(pos, search.length(), replace);
46          pos += replace.length();
47     }
48     return std::move(subject);
49 }
50
51 void convert_string_data(const string&output_vector_file){
52         string line;
53         vector<string>v;
54         while(getline(cin, line)){
55                 v.push_back(replace_all_substrings(replace_all_substrings(line, "\\\\", "\\"), "\\n", "\n"));
56         }
57         save_vector(output_vector_file, v);
58 }
59
60 int main(int argc, char*argv[]){
61         try{
62                 string data_type, output_vector_file;
63                 if(argc != 3){
64                         cerr 
65                                 << "Usage: "<< argv[0] << " data_type output_vector_file\n" 
66                                 << "\n"
67                                 << "Reads textual data from the standard input and writes it in a binary format to output_vector_file. The input data should be one data element per line. The data is only written once an end of file is encountered on the input. data_type can be one of\n"
68                                 << " * int8\n"
69                                 << " * uint8\n"
70                                 << " * int16\n"
71                                 << " * uint16\n"
72                                 << " * int32\n"
73                                 << " * uint32\n"
74                                 << " * int64\n"
75                                 << " * uint64\n"
76                                 << " * float32\n"
77                                 << " * float64\n"
78                                 << " * string" << endl;
79                         return 1;
80                 }else{
81                         data_type = argv[1];
82                         output_vector_file = argv[2];
83                 }
84
85                 if(data_type == "int8")
86                         convert_int_data<signed char>(output_vector_file);
87                 else if(data_type == "uint8")
88                         convert_int_data<unsigned char>(output_vector_file);
89                 else if(data_type == "int16")
90                         convert_int_data<signed short>(output_vector_file);
91                 else if(data_type == "uint16")
92                         convert_int_data<unsigned short>(output_vector_file);
93                 else if(data_type == "int32")
94                         convert_int_data<signed int>(output_vector_file);
95                 else if(data_type == "uint32")
96                         convert_int_data<unsigned int>(output_vector_file);
97                 else if(data_type == "int64")
98                         convert_int_data<signed long long>(output_vector_file);
99                 else if(data_type == "uint64")
100                         convert_uint64_data(output_vector_file);
101                 else if(data_type == "float32")
102                         convert_float_data<float>(output_vector_file);
103                 else if(data_type == "float64")
104                         convert_float_data<double>(output_vector_file);
105                 else if(data_type == "string")
106                         convert_string_data(output_vector_file);
107                 else
108                         throw runtime_error("Unknown data type \""+data_type+"\"");
109         }catch(exception&err){
110                 cerr << "Stopped on exception : "<< err.what() << endl;
111         }
112 }