9 void convert_int_data(const string&output_vector_file){
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())+"\"");
20 save_vector(output_vector_file, v);
24 void convert_float_data(const string&output_vector_file){
27 while(getline(cin, line)){
28 v.push_back(stold(line));
30 save_vector(output_vector_file, v);
33 void convert_uint64_data(const string&output_vector_file){
35 vector<unsigned long long>v;
36 while(getline(cin, line)){
37 v.push_back(stoull(line));
39 save_vector(output_vector_file, v);
42 string replace_all_substrings(string subject, const string& search, const string& replace) {
44 while ((pos = subject.find(search, pos)) != std::string::npos) {
45 subject.replace(pos, search.length(), replace);
46 pos += replace.length();
48 return std::move(subject);
51 void convert_string_data(const string&output_vector_file){
54 while(getline(cin, line)){
55 v.push_back(replace_all_substrings(replace_all_substrings(line, "\\\\", "\\"), "\\n", "\n"));
57 save_vector(output_vector_file, v);
60 int main(int argc, char*argv[]){
62 string data_type, output_vector_file;
65 << "Usage: "<< argv[0] << " data_type output_vector_file\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"
78 << " * string" << endl;
82 output_vector_file = argv[2];
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);
108 throw runtime_error("Unknown data type \""+data_type+"\"");
109 }catch(exception&err){
110 cerr << "Stopped on exception : "<< err.what() << endl;