-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.cpp
More file actions
177 lines (151 loc) · 4.44 KB
/
tools.cpp
File metadata and controls
177 lines (151 loc) · 4.44 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (C) 2016 Alberto Pires de Oliveira Neto
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "./tools.h"
int debug_enabled = is_debug_enabled();
int is_debug_enabled(void) {
char *debug_str = getenv(MHFTP_DEBUG);
if (debug_str != NULL)
return 1;
else
return 0;
}
/* Prints an Hex dump to stdout. */
void hex_dump(const void *v_buffer , int size) {
unsigned char buffer[17];
unsigned const char *x_buffer = static_cast<unsigned const char*>(v_buffer);
int i, j, c;
c = 0;
for (i = 0 ; i < size ; i++) {
printf("%.2X " , (x_buffer[i] & 0xff));
c++;
if (c == 16) {
memset(buffer, 0, sizeof(buffer));
c--;
memcpy(buffer, &x_buffer[i-c], 16);
for (j = 0 ; j < 16 ; j++)
if (buffer[j]<0x20 || buffer[j]>0x7f) buffer[j] = 0x2e;
printf("- %s\n" , buffer);
c = 0;
}
}
if (c != 0) {
memset(buffer , 0 , sizeof(buffer));
memcpy(buffer , &x_buffer[i-c] , c);
for (j = 0 ; j < c ; j++)
if (buffer[j]<0x20 || buffer[j]>0x7f) buffer[j] = 0x2e;
printf("%*s" , ((16-c)*3) , "");
printf("- %s\n" , buffer);
c = 0;
}
printf("\n");
}
void CopyString(char *dst_str, const char *src_str) {
if (dst_str == NULL) {
cout << "CopyString Error: dst_str == NULL" << endl;
return;
}
if (src_str == NULL) {
cout << "CopyString Error: src_str == NULL" << endl;
return;
}
if (dst_str == src_str) {
cout << "CopyString Src ptr is equal to Dst ptr. Huge mistake!!! ";
cout << "FIX IT NOW DUMBASS!!!" << endl;
return;
}
snprintf(dst_str, strlen(src_str)+1, "%s", src_str);
}
void CopyString(char *dst_str, const char *src_str, int len) {
if (dst_str == NULL) {
cout << "Error: dst_str == NULL" << endl;
return;
}
if (src_str == NULL) {
cout << "Error: str_str == NULL" << endl;
return;
}
snprintf(dst_str, len+1, "%s", src_str);
}
void StrCat(char *dst_str, const char *src_str) {
int dst_len = strlen(dst_str);
int len = dst_len + strlen(src_str);
len++; // NULL
snprintf(&dst_str[dst_len], len, "%s", src_str);
}
/** reol : 1 == Reset End of Line.
*/
void PrintColor(const char *str, const char *color, int reol) {
char *mhcolor = getenv(MHFTP_NOCOLOR);
if (mhcolor == NULL) {
printf("%s%s", color, str);
if (reol == 1) cout << C_RESET;
} else {
printf("%s", str);
}
}
void PrintColor(const char *str, const char *color) {
PrintColor(str, color, 1);
}
const char *AbsoluteFile(const char *full_path) {
const char *abs_file;
bool separator_found = false;
for (int i = strlen(full_path) ; i > 0 ; i--) {
if (full_path[i] == '/') {
separator_found = true;
abs_file = strdup(&full_path[i+1]);
break;
}
}
if (!separator_found)
abs_file = strdup(full_path);
return abs_file;
}
bool file_exists(const char *file_name) {
struct stat buffer;
return stat(file_name, &buffer) == 0;
}
long i32toLong(int32_t i) { // NOLINT(runtime/int)
return i;
}
long i64toLong(int64_t i) { // NOLINT(runtime/int)
return i;
}
bool fileExists(const char *fname) {
if (access(fname, F_OK) != -1) {
// file exists
return true;
}
return false;
}
int SuffixToInt(const char *str_number) {
int len = strlen(str_number);
char aux[64];
memset(aux, 0, sizeof(aux));
char suffix = str_number[len-1];
if (suffix >= 0x30 && suffix <= 0x39) {
return atoi(str_number);
}
suffix = suffix | 0x20;
strncpy(aux, str_number, len-1);
switch (suffix) {
case 'k' : return atoi(aux) * MH_K;
break;
case 'm' : return atoi(aux) * MH_M;
break;
default : return -1;
}
return -1;
}