Skip to content

Commit c0d2b30

Browse files
committed
Use unique_ptr and more const
1 parent ee31896 commit c0d2b30

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

jsrc/jeload.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,41 +100,36 @@ jeload(void* callbacks) -> J {
100100
auto
101101
jepath(char* arg, char* lib) -> void {
102102
uint32_t const sz = 4000;
103-
uint32_t len = sz; // Cant be const for function call _NSGetExecutablePath
104103

105104
// C strings need to be used for POSIX APIs and macOS APIs
106-
auto arg2 = new char[sz];
107-
auto arg3 = new char[sz];
108-
// Return for readlinks
109-
int n;
105+
auto const arg2 = std::unique_ptr<char[]>(new char[sz]);
106+
auto const arg3 = std::unique_ptr<char[]>(new char[sz]);
110107

111108
// try host dependent way to get path to executable
112109
// use arg if they fail (arg command in PATH won't work)
113110
#ifdef __MACH__
114111
// Returns 0 if path was copied, otherwise -1 if failed.
115-
if (_NSGetExecutablePath(arg2, &len) != 0) strcat(arg2, arg);
112+
if (uint32_t len = sz; _NSGetExecutablePath(arg2.get(), &len) != 0) strcat(arg2.get(), arg);
116113
#else
117-
n = readlink("/proc/self/exe", arg2, sz);
118-
if (n == -1)
119-
strcpy(arg2, arg);
120-
else
121-
arg2[n] = 0;
114+
{
115+
auto const n = readlink("/proc/self/exe", arg2, sz);
116+
if (n == -1)
117+
strcpy(arg2, arg);
118+
else
119+
arg2[n] = 0;
120+
}
122121
#endif
123122
// arg2 is path (abs or relative) to executable or soft link
124-
n = readlink(arg2, arg3, sz);
125-
123+
auto const n = readlink(arg2.get(), arg3.get(), sz);
126124
if (n == -1)
127-
strcpy(arg3, arg2);
125+
strcpy(arg3.get(), arg2.get());
128126
else
129127
arg3[n] = 0;
130128

131-
if ('/' == *arg3)
132-
path = arg3;
129+
if ('/' == arg3[0])
130+
path = arg3.get();
133131
else
134-
path = std::filesystem::current_path() / arg3;
135-
// Now append path_temp to path, as all POSIX and macOS API calls are done, and free up arg2, arg3, path_temp.
136-
delete[] arg2;
137-
delete[] arg3;
132+
path = std::filesystem::current_path() / arg3.get();
138133

139134
path.remove_filename();
140135

0 commit comments

Comments
 (0)