summaryrefslogtreecommitdiff
path: root/tools.cpp
blob: 5e2c66068001cd4cdb04584a8ae301a60d1562a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright 2018 Joaquín Cuéllar

This file is part of GSResolver.

GSResolver is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GSResolver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GSResolver.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "tools.h"

std::vector<std::string> ret_paragraph_with_lines_return(std::string original,unsigned int line_width, int max_lines)
{
  std::vector<std::string> my_paragraph;
  std::string tmp;
  std::size_t new_pos;
  int current_line=0;

  tmp = original;
  while((tmp.length() > line_width) && (current_line < max_lines))
    {
      new_pos = tmp.substr(0,line_width).find_last_of(" ");
      my_paragraph.push_back(tmp.substr(0,new_pos));
      tmp = tmp.substr(new_pos+1);
      current_line++;
    }
  my_paragraph.push_back(tmp);
  return my_paragraph;
}

std::string getCurrentDate()
{
  std::string myDate;
  std::string myTime;
  time_t t = time(NULL);
  tm* timePtr = localtime(&t);

  myDate = std::to_string(timePtr->tm_year+1900);
  myDate += '-';

  if(timePtr->tm_mon+1 <10)
      myDate += '0';
  myDate += std::to_string(timePtr->tm_mon+1);
  
  myDate += '-';

  if(timePtr->tm_mday <10)
      myDate += '0';
  myDate += std::to_string(timePtr->tm_mday);

  if(timePtr->tm_hour < 10)
      myTime += '0';
  myTime += std::to_string(timePtr->tm_hour);
  
  myTime += ':';
  
  if(timePtr->tm_min < 10)
      myTime += '0';
  myTime += std::to_string(timePtr->tm_min);

  myTime += ':';
  
  if(timePtr->tm_sec < 10)
      myTime += '0';

  if(timePtr->tm_sec > 59)
      myTime += "59";
  else
      myTime += std::to_string(timePtr->tm_sec);

  std::string myWhole = myDate + " " + myTime;
  
  return myWhole;
}

int log_txt(std::string message)
{
  //TODO, hardcoded
  std::string path = "./logs";
  //std::string security_check = "mkdir " + path + " > /dev/null";
  int ret = 0;
  std::string file_name = path + "/" +  getCurrentDate().substr(0,10) + ".log";  
  std::ofstream my_file;

  mkdir(path.c_str(),0755);

  my_file.open(file_name,std::ios::app);

  if(my_file.is_open())
   {
      my_file << getCurrentDate().substr(11) << " > " << message << "\n";
      ret = 0;
    }
  else
    ret = 1;

  my_file.close();

  return ret;
}