-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.cpp
131 lines (110 loc) · 4.33 KB
/
utils.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright 2009 Luis Henrique O. Rios
*
* This file is part of YAFS.
*
* YAFS 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.
*
* YAFS 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 YAFS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "types.h"
#include "utils.h"
#include "unicode.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#undef WIN32_LEAN_AND_MEAN
#else
#include <unistd.h>
#endif
string ExecutableDirectoryUtils::executable_directory_uri_utf8;
string ExecutableDirectoryUtils::executable_directory_utf8;
string ExecutableDirectoryUtils::executable_directory_native_encoding;
bool LogUtils::enabled = false;
#ifdef UNIX_SYSTEM
void handleFatalError(int status, int errnum, const char* message) {
cerr << "yafs: " << message << ": " << strerror(errnum) << endl;
cerr.flush();
exit(status);
}
#endif
void ExecutableDirectoryUtils::Initialize(char* command_line_executable_path){
#ifdef WIN_SYSTEM
char executable_path_ansi[4096];
GetModuleFileNameA(NULL, executable_path_ansi, sizeof(executable_path_ansi));
wchar_t executable_path_unicode[4096];
GetModuleFileNameW(NULL, executable_path_unicode, sizeof(executable_path_unicode));
/* Removes the executable name. */
int len = (int) strlen(executable_path_ansi);
int i;
for (i = len - 1; i >= 0; i--) {
if (executable_path_ansi[i] == '\\') break;
}
if (i > -1) {
executable_path_ansi[i + 1] = '\0';
}
len = (int) wcslen(executable_path_unicode);
for (i = len - 1; i >= 0; i--) {
if (executable_path_unicode[i] == '\\') break;
}
if (i >= -1) {
executable_path_unicode[i + 1] = '\0';
}
executable_directory_native_encoding = string(executable_path_ansi);
char* executable_path_utf8 = Unicode::ConvertFromUTF16ToUTF8(executable_path_unicode);
executable_directory_utf8 = string((char*)executable_path_utf8);
delete[] executable_path_utf8;
executable_directory_uri_utf8 = string("file://") + string("/") + executable_directory_utf8;
std::replace(executable_directory_uri_utf8.begin(), executable_directory_uri_utf8.end(), '\\', '/');
#elif UNIX_SYSTEM
char working_directory[4096];
working_directory[0] = '\0';
char* result = getcwd(working_directory, sizeof(working_directory));
if (result == NULL) {
handleFatalError(EXIT_FAILURE, errno, "failed after executing getcwd");
}
size_t working_directory_length = strlen(working_directory);
if (working_directory[working_directory_length - 1] != '/') {
working_directory[working_directory_length++] = '/';
}
if (command_line_executable_path != NULL) {
size_t command_line_executable_path_length = strlen(command_line_executable_path);
/* Removes the executable name. */
int i;
for (i = command_line_executable_path_length - 1; i >= 0; i--) {
if (command_line_executable_path[i] == '/') break;
}
if (i > -1) {
command_line_executable_path[i + 1] = '\0';
command_line_executable_path_length = i + 1;
}
/* Is it not absolute? */
if (command_line_executable_path[0] != '/') {
memcpy(working_directory + working_directory_length, command_line_executable_path, command_line_executable_path_length + 1);
working_directory_length += command_line_executable_path_length;
} else {
memcpy(working_directory, command_line_executable_path, command_line_executable_path_length + 1);
working_directory_length = command_line_executable_path_length;
}
}
/* Assumes it is using UFT-8. */
executable_directory_utf8 = string(working_directory);
if (executable_directory_utf8[executable_directory_utf8.length() - 1] != '/'){
executable_directory_utf8 += "/";
}
executable_directory_uri_utf8 = string("file://") + executable_directory_utf8;
executable_directory_native_encoding = executable_directory_utf8;
#endif
}