Otclient  14/8/2020
unixplatform.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2016 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef WIN32
24 
25 #include "platform.h"
26 #include <cstring>
27 #include <fstream>
28 #include <unistd.h>
30 
31 #include <sys/stat.h>
32 #include <execinfo.h>
33 
34 void Platform::processArgs(std::vector<std::string>& args)
35 {
36  //nothing todo, linux args are already utf8 encoded
37 }
38 
39 bool Platform::spawnProcess(std::string process, const std::vector<std::string>& args)
40 {
41  struct stat sts;
42  if(stat(process.c_str(), &sts) == -1 && errno == ENOENT)
43  return false;
44 
45  pid_t pid = fork();
46  if(pid == -1)
47  return false;
48 
49  if(pid == 0) {
50  char* cargs[args.size()+2];
51  cargs[0] = (char*)process.c_str();
52  for(uint i=1;i<=args.size();++i)
53  cargs[i] = (char*)args[i-1].c_str();
54  cargs[args.size()+1] = nullptr;
55 
56  if(execv(process.c_str(), cargs) == -1)
57  _exit(EXIT_FAILURE);
58  }
59 
60  return true;
61 }
62 
64 {
65  return getpid();
66 }
67 
68 bool Platform::isProcessRunning(const std::string& name)
69 {
70  return false;
71 }
72 
73 bool Platform::killProcess(const std::string& name)
74 {
75  return false;
76 }
77 
78 std::string Platform::getTempPath()
79 {
80  return "/tmp/";
81 }
82 
84 {
85  std::string res;
86  char cwd[2048];
87  if(getcwd(cwd, sizeof(cwd)) != nullptr) {
88  res = cwd;
89  res += "/";
90  }
91  return res;
92 }
93 
94 bool Platform::copyFile(std::string from, std::string to)
95 {
96  return system(stdext::format("/bin/cp '%s' '%s'", from, to).c_str()) == 0;
97 }
98 
99 bool Platform::fileExists(std::string file)
100 {
101  struct stat buffer;
102  return (stat(file.c_str(), &buffer) == 0);
103 }
104 
105 bool Platform::removeFile(std::string file)
106 {
107  if(unlink(file.c_str()) == 0)
108  return true;
109  return false;
110 }
111 
113 {
114  struct stat attrib;
115  if(stat(file.c_str(), &attrib) == 0)
116  return attrib.st_mtime;
117  return 0;
118 }
119 
120 void Platform::openUrl(std::string url)
121 {
122  if(url.find("http://") == std::string::npos)
123  url.insert(0, "http://");
124  system(stdext::format("xdg-open %s", url).c_str());
125 }
126 
127 std::string Platform::getCPUName()
128 {
129  std::string line;
130  std::ifstream in("/proc/cpuinfo");
131  while(getline(in, line)) {
132  auto strs = stdext::split(line, ":");
133  std::string first = strs[0];
134  std::string second = strs[1];
135  stdext::trim(first);
136  stdext::trim(second);
137  if(strs.size() == 2 && first == "model name")
138  return second;
139  }
140  return std::string();
141 }
142 
144 {
145  std::string line;
146  std::ifstream in("/proc/meminfo");
147  while(getline(in, line)) {
148  auto strs = stdext::split(line, ":");
149  std::string first = strs[0];
150  std::string second = strs[1];
151  stdext::trim(first);
152  stdext::trim(second);
153  if(strs.size() == 2 && first == "MemTotal")
154  return stdext::unsafe_cast<double>(second.substr(0, second.length() - 3)) * 1000.0;
155  }
156  return 0;
157 }
158 
159 std::string Platform::getOSName()
160 {
161  std::string line;
162  std::ifstream in("/etc/issue");
163  if(getline(in, line)) {
164  std::size_t end = line.find('\\');
165  std::string res = line.substr(0, end);
166  stdext::trim(res);
167  return res;
168  }
169  return std::string();
170 }
171 
172 std::string Platform::traceback(const std::string& where, int level, int maxDepth)
173 {
174  std::stringstream ss;
175 
176  ss << "\nC++ stack traceback:";
177  if(!where.empty())
178  ss << "\n\t[C++]: " << where;
179 
180  void* buffer[maxDepth + level + 1];
181  int numLevels = backtrace(buffer, maxDepth + level + 1);
182  char **tracebackBuffer = backtrace_symbols(buffer, numLevels);
183  if(tracebackBuffer) {
184  for(int i = 1 + level; i < numLevels; i++) {
185  std::string line = tracebackBuffer[i];
186  if(line.find("__libc_start_main") != std::string::npos)
187  break;
188  std::size_t demanglePos = line.find("(_Z");
189  if(demanglePos != std::string::npos) {
190  demanglePos++;
191  int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
192  std::string funcName = line.substr(demanglePos, len);
193  line.replace(demanglePos, len, stdext::demangle_name(funcName.c_str()));
194  }
195  ss << "\n\t" << line;
196  }
197  free(tracebackBuffer);
198  }
199 
200  return ss.str();
201 }
202 
203 #endif
Platform::getCurrentDir
std::string getCurrentDir()
Definition: unixplatform.cpp:83
Platform::getCPUName
std::string getCPUName()
Definition: unixplatform.cpp:127
stdext.h
Platform::removeFile
bool removeFile(std::string file)
Definition: unixplatform.cpp:105
Platform::getTempPath
std::string getTempPath()
Definition: unixplatform.cpp:78
Platform::getOSName
std::string getOSName()
Definition: unixplatform.cpp:159
Platform::openUrl
void openUrl(std::string url)
Definition: unixplatform.cpp:120
ticks_t
int64 ticks_t
Definition: types.h:43
Platform::killProcess
bool killProcess(const std::string &name)
Definition: unixplatform.cpp:73
stdext::format
std::string format()
Definition: format.h:82
Platform::copyFile
bool copyFile(std::string from, std::string to)
Definition: unixplatform.cpp:94
stdext::demangle_name
const char * demangle_name(const char *name)
Demangle names for GNU g++ compiler.
Definition: demangle.cpp:45
Platform::traceback
std::string traceback(const std::string &where, int level=1, int maxDepth=32)
Definition: unixplatform.cpp:172
Platform::getFileModificationTime
ticks_t getFileModificationTime(std::string file)
Definition: unixplatform.cpp:112
uint
unsigned int uint
Definition: types.h:31
Platform::processArgs
void processArgs(std::vector< std::string > &args)
Definition: unixplatform.cpp:34
stdext::trim
void trim(std::string &str)
Definition: string.cpp:226
Platform::fileExists
bool fileExists(std::string file)
Definition: unixplatform.cpp:99
platform.h
stdext::split
std::vector< std::string > split(const std::string &str, const std::string &separators)
Definition: string.cpp:273
Platform::isProcessRunning
bool isProcessRunning(const std::string &name)
Definition: unixplatform.cpp:68
Platform::getTotalSystemMemory
double getTotalSystemMemory()
Definition: unixplatform.cpp:143
Platform::spawnProcess
bool spawnProcess(std::string process, const std::vector< std::string > &args)
Definition: unixplatform.cpp:39
Platform::getProcessId
int getProcessId()
Definition: unixplatform.cpp:63