-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
116 lines (105 loc) · 3.18 KB
/
utils.c
File metadata and controls
116 lines (105 loc) · 3.18 KB
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
//
// Created by koech on 6/15/2023.
//
#include "utils.h"
void printInt(void* value){
printf("%d", ((int*)value));
}
void printChar(void* value){
printf("%c", (char*)value);
}
void printPairInts(void* value){
Pair* temp = (Pair*)value;
printf("{%d, %d}", (int*)temp->first, (int*)temp->second);
}
void printPairFloats(void* value){
Pair* temp = (Pair*)value;
printf("{%f, %f}", (float*)temp->first, (float*)temp->second);
}
void printPairChars(void* value){
Pair* temp = (Pair*)value;
printf("{%f, %f}", (char*)temp->first, (char*)temp->second);
}
char* concatenateStrings(const char* str1, const char* str2) {
size_t len1 = strlen(str1);
size_t len2 = strlen(str2);
size_t resultLen = len1 + len2 + 1; // +1 for null terminator
char* result = (char*)malloc(resultLen * sizeof(char));
if (result == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
strcpy(result, str1);
strcat(result, str2);
return result;
}
char* normalize_path(const char* path) {
if (path == NULL) return NULL;
size_t len = strlen(path);
char* normalized = (char*)malloc((len + 1) * sizeof(char));
if (normalized == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
strcpy(normalized, path);
#ifdef _WIN32
// Replace forward slashes with backslashes
for (size_t i = 0; i < len; ++i) {
if (normalized[i] == '/') {
normalized[i] = '\\';
}
}
#else
// Replace backslashes with forward slashes
for (size_t i = 0; i < len; ++i) {
if (normalized[i] == '\\') {
normalized[i] = '/';
}
}
#endif
#ifdef _WIN32
// Check if the executable is located in a different directory
char exePath[MAX_PATH];
GetModuleFileName(NULL, exePath, MAX_PATH);
char* exeDir = strrchr(exePath, '\\');
if (exeDir != NULL) {
*exeDir = '\0';
char* temp = normalized;
size_t exeDirLen = strlen(exePath);
normalized = (char*)malloc((len + exeDirLen + 2) * sizeof(char));
if (normalized == NULL) {
fprintf(stderr, "Memory allocation failed\n");
free(temp);
return NULL;
}
strcpy(normalized, exePath);
strcat(normalized, "\\");
strcat(normalized, temp);
free(temp);
}
#else
// Check if the executable is located in a different directory
char exePath[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", exePath, sizeof(exePath));
if (count != -1) {
exePath[count] = '\0';
char* exeDir = strrchr(exePath, '/');
if (exeDir != NULL) {
*exeDir = '\0';
char* temp = normalized;
size_t exeDirLen = strlen(exePath);
normalized = (char*)malloc((len + exeDirLen + 2) * sizeof(char));
if (normalized == NULL) {
fprintf(stderr, "Memory allocation failed\n");
free(temp);
return NULL;
}
strcpy(normalized, exePath);
strcat(normalized, "/");
strcat(normalized, temp);
free(temp);
}
}
#endif
return normalized;
}