LibTgBotPP
 All Classes Namespaces Files Functions Variables Typedefs Macros
std.cc
Go to the documentation of this file.
1 #include "std.h"
2 
3 #include <pwd.h>
4 #include <time.h>
5 #include <unistd.h>
6 
7 #include <algorithm>
8 #include <fstream>
9 #include <sstream>
10 
11 uint64 stoi(std::string st) {
12 
13  uint64 ret;
14 
15  if (st == "") {
16  st = "0";
17  }
18 
19  std::istringstream convert(st);
20  convert >> ret;
21 
22  return(ret);
23 }
24 
25 std::string itos(uint64 i) {
26 
27  return(static_cast<std::ostringstream*>(&(std::ostringstream() << i))->str());
28 }
29 
30 std::string currentDate() {
31 
32  time_t now = time(0);
33  struct tm tstruct;
34  char buf[32];
35  tstruct = *localtime(&now);
36 
37  strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct);
38 
39  return(buf);
40 }
41 
42 std::vector<std::string> split(const std::string &text, char sep) {
43 
44  std::vector<std::string> tokens;
45  int start = 0, end = 0;
46  while ((int)(end = text.find(sep, start)) != (int)std::string::npos) {
47  std::string temp = text.substr(start, end - start);
48  if(temp != "")
49  tokens.push_back(temp);
50  start = end + 1;
51  }
52  std::string temp=text.substr(start);
53  if(temp != "")
54  tokens.push_back(temp);
55 
56  return tokens;
57 }
58 
59 float stof(std::string st) {
60 
61  // Conver , to . first
62  replace(st.begin(), st.end(), ',', '.');
63 
64  return(atof(st.c_str()));
65 }
66 
67 std::string trim(const std::string &s) {
68 
69  std::string::const_iterator it = s.begin();
70  while (it != s.end() && isspace(*it))
71  it++;
72 
73  std::string::const_reverse_iterator rit = s.rbegin();
74  while (rit.base() != it && isspace(*rit))
75  rit++;
76 
77  return std::string(it, rit.base());
78 }
79 
80 std::string gethome() {
81 
82  std::string homedir = getenv("HOME");
83  if (homedir == "") {
84  homedir = getpwuid(getuid())->pw_dir;
85  }
86 
87  return(homedir);
88 }
89 
90 // A simple logger
91 void Log(std::string msg) {
92 
93  std::ofstream datei;
94  datei.open("/tmp/bot.log", std::ios::out | std::ios::app);
95  datei << "Msg: " << msg << std::endl;
96  datei.close();
97 }
std::string start(Telegram::TelegramBot *tg, Telegram::TCommandLine args)
Definition: example01.cc:82
std::vector< std::string > split(const std::string &text, char sep)
Definition: std.cc:42
unsigned long long int uint64
Definition: std.h:12
void Log(std::string msg)
Definition: std.cc:91
std::string gethome()
Definition: std.cc:80
std::string currentDate()
Definition: std.cc:30
uint64 stoi(std::string st)
Definition: std.cc:11
float stof(std::string st)
Definition: std.cc:59
std::string itos(uint64 i)
Definition: std.cc:25
std::string trim(const std::string &s)
Definition: std.cc:67