update readme

This commit is contained in:
zhangzifa
2019-02-22 10:09:48 +08:00
committed by Jackson Tian
parent eb7ab33440
commit f877a9ef39
10 changed files with 456 additions and 156 deletions

View File

@@ -1,11 +1,30 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
class utUtils {
public:
void get_dir_exec(char* dir, char* exec) {
#ifdef _WIN32
char BufferFileName[MAX_PATH];
memset(BufferFileName,0,MAX_PATH);
if (GetModuleFileName(NULL, BufferFileName, MAX_PATH)) {
if (exec) {
snprintf(exec, strlen(BufferFileName), "%s", BufferFileName);
}
std::string ss(BufferFileName);
int idx = ss.find_last_of("\\");
BufferFileName[idx] = '\0';
snprintf(dir, strlen(BufferFileName), "%s", BufferFileName);
}
return;
#else
char* filename = nullptr;
if (readlink("/proc/self/exe", dir, 1024) < 0) {
dir[0] = '\0';
@@ -23,8 +42,29 @@ class utUtils {
*filename = '\0';
return;
#endif
}
std::string get_env(const std::string env) {
#ifdef _WIN32
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, env.c_str()) == 0 && buf != nullptr) {
std::string var(buf);
free(buf);
return var;
} else {
return std::string();
}
#else
char* value = getenv(env.c_str());
if (value == nullptr) {
return std::string();
}
return std::string(value);
#endif
}
};