Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
helper.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/helper/helper.cpp (January 8, 2018)
3  *
4  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
5  * http://www.opimedia.be/
6  */
7 
8 // \cond
9 #include <fstream>
10 // \endcond
11 
12 #include "helper.hpp"
13 #include "../sigmaodd/helper.hpp"
14 
15 
16 namespace helper {
17 
18  /* ******************
19  * Private constant *
20  ********************/
21  const char sep_ = '/';
22 
23 
24 
25  /* ***********
26  * Functions *
27  *************/
28  std::string
29  concat_path(std::string path1, std::string path2) {
30  if (path1.empty()) {
31  return path2;
32  }
33  else if (path2.empty()) {
34  return path1;
35  }
36 
37  if ((path1.back() == sep_) && (path2[0] == sep_)) {
38  path2 = path2.substr(1);
39  }
40 
41  return ((path1.back() != sep_) && (path2[0] != sep_)
42  ? path1 + sep_ + path2
43  : path1 + path2);
44  }
45 
46 
47  std::string
48  duration_ms_to_string(double duration_ms) {
49  const double duration_second = duration_ms/1000;
50  const double duration_minute = duration_ms/(1000*60);
51  const double duration_hour = duration_ms/(1000*60*60);
52 
53  return std::to_string(duration_ms) + "ms"
54  + "\t= " + std::to_string(duration_second) + 's'
55  + "\t= " + std::to_string(duration_minute) + 'm'
56  + "\t= " + std::to_string(duration_hour) + 'h';
57  }
58 
59 
60  std::string
61  duration_to_string(std::chrono::duration<double> duration_second) {
62  const double duration = duration_second.count();
63  const double duration_millisecond = duration*1000;
64  const double duration_minute = duration/60;
65  const double duration_hour = duration/(60*60);
66 
67  return std::to_string(duration_millisecond) + "ms"
68  + "\t= " + std::to_string(duration) + 's'
69  + "\t= " + std::to_string(duration_minute) + 'm'
70  + "\t= " + std::to_string(duration_hour) + 'h';
71  }
72 
73 
74  std::string
75  file_to_string(std::string filename) {
76  std::ifstream infile(filename, std::ios::in);
77  const std::string s((std::istreambuf_iterator<char>(infile)),
78  std::istreambuf_iterator<char>());
79 
80  infile.close();
81  if (infile.fail()) {
82  std::cerr << "! Impossible to read file \"" << filename << '"' << std::endl;
83 
84  exit(EXIT_FAILURE);
85  }
86 
87  return s;
88  }
89 
90 
91  std::string
92  get_string(int argc, const char* const argv[], unsigned int i,
93  void (*help_and_exit_function)()) {
94  if (i < static_cast<unsigned int>(argc)) {
95  return argv[i];
96  }
97  else {
98  if (help_and_exit_function != nullptr) {
99  (*help_and_exit_function)();
100  }
101 
102  return ""; // to satisfy the compiler
103  }
104  }
105 
106 
107  unsigned int
108  get_uint(int argc, const char* const argv[], unsigned int i,
109  void (*help_and_exit_function)()) {
110  if (i < static_cast<unsigned int>(argc)) {
111  return static_cast<unsigned int>(std::stoul(argv[i]));
112  }
113  else {
114  if (help_and_exit_function != nullptr) {
115  (*help_and_exit_function)();
116  }
117 
118  return 0; // to satisfy the compiler
119  }
120  }
121 
122 
123  unsigned long
124  get_ulong(int argc, const char* const argv[], unsigned int i,
125  void (*help_and_exit_function)()) {
126  if (i < static_cast<unsigned int>(argc)) {
127  return std::stoul(argv[i]);
128  }
129  else {
130  if (help_and_exit_function != nullptr) {
131  (*help_and_exit_function)();
132  }
133 
134  return 0; // to satisfy the compiler
135  }
136  }
137 
138 
139  bool
140  is_file_exists(std::string filename) {
141  std::ifstream file(filename);
142 
143  return static_cast<bool>(file);
144  }
145 
146 
147  void
149  std::cout <<
150 #ifdef __clang__
151  "clang " STR(__clang_major__) "." STR(__clang_minor__) "." STR(__clang_patchlevel__)
152 #else
153 #ifdef __GNUG__
154  "GNU C++ " STR(__GNUG__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
155 #else
156  "unknown"
157 #endif
158 #endif
159  << "\tsizeof(nat_type): " << sizeof(sigmaodd::nat_type)
160 
161 #ifndef NDEBUG
162  << "\tDEBUG mode!"
163 #endif
164  << std::endl;
165  }
166 
167 
168  std::string
169  remove_last_if_null(const std::string &s) {
170  return (!s.empty() && (s[s.size() - 1] == 0)
171  ? std::string(s.cbegin(), s.cend() - 1)
172  : s);
173  }
174 
175 
176  std::string
177  to_string(bool b) {
178  return (b
179  ? "true"
180  : "false");
181  }
182 
183 } // namespace helper
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
Some generic helper functions for programs.
Definition: helper.cpp:16
std::string remove_last_if_null(const std::string &s)
If s terminates by a null character then return a copy of s without this last character, else return s.
Definition: helper.cpp:169
std::string duration_ms_to_string(double duration_ms)
Return a string with the duration expressed in milliseconds, seconds, minutes and hours...
Definition: helper.cpp:48
unsigned int get_uint(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:108
std::string duration_to_string(std::chrono::duration< double > duration_second)
Return a string with the duration expressed in milliseconds, seconds, minutes and hours...
Definition: helper.cpp:61
bool is_file_exists(std::string filename)
Return true iff the file (or directory) exists.
Definition: helper.cpp:140
#define STR(x)
Convert the value of the macro x to a literal string.
Definition: helper.hpp:104
const char sep_
Definition: helper.cpp:21
std::string get_string(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in string.
Definition: helper.cpp:92
Some generic helper functions for programs.
std::string concat_path(std::string path1, std::string path2)
Return the path composed by path1/path2.
Definition: helper.cpp:29
std::string file_to_string(std::string filename)
Read the file and return its content to a string. If failed then print a error message and exit...
Definition: helper.cpp:75
void print_intern_config_compiler()
Print to stdcout the intern configuration of the compiler.
Definition: helper.cpp:148
unsigned long get_ulong(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:124
std::string to_string(bool b)
Return the string "true" if b, else "false".
Definition: helper.cpp:177