Tools.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * \class Utils
  3. *
  4. * \brief Contains some useful methods and tricks
  5. *
  6. * \note Accept somme things to be add in
  7. *
  8. * \author $Author: Adrien Carteron$
  9. *
  10. * \version $Revision: 0.5 $
  11. *
  12. * \date $Date: 2014/07/11 $
  13. *
  14. * Contact: acarteron@openmailbox.org
  15. *
  16. * Created on: 2012/11/12
  17. *
  18. *
  19. */
  20. #ifndef TOOLS_HPP
  21. #define TOOLS_HPP
  22. #include <sstream>
  23. #include <cmath>
  24. #include <vector>
  25. #include <algorithm>
  26. class Utils{
  27. private:
  28. public:
  29. /** \brief A method to get current date and time as string.
  30. * \return Current date and time as string
  31. */
  32. static std::string getTime(){
  33. time_t now = time (NULL);
  34. /* la convertir en heure locale */
  35. struct tm tm_now = *localtime (&now);
  36. /* Creer une chaine JJ/MM/AAAA HH:MM:SS */
  37. char s_now[sizeof "AAAA-MM-JJ HH:MM:SS.000"];
  38. strftime (s_now, sizeof s_now, "%Y-%m-%d %H:%M:%S.000", &tm_now);
  39. return toString(s_now);
  40. }
  41. static std::string getDay(){
  42. time_t now = time (NULL);
  43. /* la convertir en heure locale */
  44. struct tm tm_now = *localtime (&now);
  45. /* Creer une chaine JJ/MM/AAAA HH:MM:SS */
  46. char s_now[sizeof "AAAA-MM-JJ"];
  47. strftime (s_now, sizeof s_now, "%Y-%m-%d", &tm_now);
  48. return toString(s_now);
  49. }
  50. /** \brief A method to convert any type of input in string
  51. * \param any type of data
  52. * \return data given as string
  53. *
  54. * HowTo:
  55. * int truc=42;
  56. * string machin=Utils::toString<int>(truc);
  57. *
  58. */
  59. template <typename type>
  60. static std::string toString(type var){
  61. std::ostringstream ss;
  62. ss << var;
  63. return ss.str();
  64. }
  65. /** \brief A method to convert a string as any type of data
  66. * \param a data as string
  67. * \return data given as any type
  68. *
  69. * HowTo:
  70. * string machin="42";
  71. * int truc=Utils::stringTo<int>(machin);
  72. *
  73. */
  74. template <class type>
  75. static type stringTo(std::string val){
  76. type ret;//=NULL;
  77. std::istringstream istr(val);
  78. istr >> ret;
  79. return ret;
  80. }
  81. template <class type>
  82. static int find_in_vector_str(std::vector<type> vect,
  83. type val){
  84. bool found=false;
  85. int indic=-1;
  86. for(size_t i(0);i<vect.size()&&!found;++i){
  87. if(vect[i]==val){
  88. indic=i;
  89. found=true;
  90. }
  91. }
  92. return indic;
  93. }
  94. static int find_in_vector_str(std::vector<std::string> vect,
  95. std::string val){
  96. bool found=false;
  97. int indic=-1;
  98. for(size_t i(0);i<vect.size()&&!found;++i){
  99. if(vect[i].compare(val)==0){
  100. indic=i;
  101. found=true;
  102. }
  103. }
  104. return indic;
  105. }
  106. static void display_progress(std::string message_,
  107. size_t begin_,
  108. size_t end_ ){
  109. std::string display="echo -en \"\r";
  110. display+=message_;
  111. display+=": ";
  112. display+=Utils::toString<size_t>(begin_);
  113. display+="/";
  114. display+=Utils::toString<size_t>(end_);
  115. display+="\"";
  116. std::system(display.c_str());
  117. }
  118. static double GetFloatPrecision(double value, double precision){
  119. return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision));
  120. }
  121. static std::vector<std::string> splitString(std::string str_,std::string delimiter){
  122. size_t pos = 0;
  123. std::vector<std::string> token;//="[";
  124. while ((pos = str_.find(delimiter)) != std::string::npos) {
  125. token.push_back(str_.substr(0, pos));
  126. str_.erase(0, pos + delimiter.length());
  127. }
  128. token.push_back(str_);
  129. return token;
  130. }
  131. static std::vector<std::string> splitString(std::string str_,char delimiter){
  132. size_t pos = 0;
  133. std::vector<std::string> token;//="[";
  134. while ((pos = str_.find(delimiter)) != std::string::npos) {
  135. token.push_back(str_.substr(0, pos));
  136. str_.erase(0, pos + 1);
  137. }
  138. token.push_back(str_);
  139. return token;
  140. }
  141. static std::string trim(std::string& s){
  142. s.erase(std::remove_if(begin(s), end(s),
  143. [l = std::locale{}](auto ch) { return std::isspace(ch, l); }
  144. ), end(s));
  145. return s;
  146. }
  147. };
  148. #endif //UTILS_HPP