-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.c
294 lines (239 loc) · 5.98 KB
/
framework.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include <unistd.h>
#include <time.h>
#include "framework.h";
#define INITIAL_BUFFER_SIZE 64
/**
* Function to clear console
*/
void CLS() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
/**
* Function to clear console after user hittet ENTER
*/
void CLS_ENTER() {
sleep(2);
getchar();
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
/**
* Function to get user Inputs (desgined to be used in a loop for multi inputs)
*
* @param int line Line Number (in for loop it would be i+1)
* @return char* the user input
*/
char* userInput(int line) {
size_t bufferSize = INITIAL_BUFFER_SIZE;
char* input = (char*)malloc(bufferSize);
if (input == NULL) {
printf("Fehler: Speicher konnte nicht reserviert werden.\n");
exit(EXIT_FAILURE);
}
size_t length = 0;
int ch;
if (line != -1) {
printf("%d| ", line);
}
while ((ch = getchar()) != '\n' && ch != EOF) {
input[length++] = ch;
if (length >= bufferSize) {
bufferSize *= 2;
input = (char*)realloc(input, bufferSize);
if (input == NULL) {
printf("Fehler: Speicher konnte nicht erweitert werden.\n");
exit(EXIT_FAILURE);
}
}
}
input[length] = '\0';
return input;
}
/**
* Function to get simple oneline user input
*
* @return char* the user input
*/
char* getInput() {
size_t bufferSize = INITIAL_BUFFER_SIZE;
char* input = (char*)malloc(bufferSize);
if (input == NULL) {
printf("Fehler: Speicher konnte nicht reserviert werden.\n");
exit(EXIT_FAILURE);
}
size_t length = 0;
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
input[length++] = ch;
if (length >= bufferSize) {
bufferSize *= 2;
input = (char*)realloc(input, bufferSize);
if (input == NULL) {
printf("Fehler: Speicher konnte nicht erweitert werden.\n");
exit(EXIT_FAILURE);
}
}
}
input[length] = '\0';
return input;
}
/**
* Function to log messages with different log levels
*
* @param const char* level Log level (e.g., "INFO", "WARN", "ERROR")
* @param const char* message Message to log
*/
void logMessage(const char* level, const char* message) {
printf("[%s] %s\n", level, message);
}
/**
* Function to log messages to a file
*
* @param const char* filename Log file name
* @param const char* level Log level
* @param const char* message Message to log
*/
void logToFile(const char* filename, const char* level, const char* message) {
FILE* file = fopen(filename, "a");
if (file == NULL) {
printf("Fehler: Logdatei konnte nicht geöffnet werden.\n");
return;
}
fprintf(file, "[%s] %s\n", level, message);
fclose(file);
}
/**
* Function to trim leading and trailing whitespace from a string
*
* @param char* str String to trim
* @return char* Trimmed string
*/
char* trimWhitespace(char* str) {
char* end;
while (isspace((unsigned char)*str)) str++;
if (*str == '\0') {
return str;
}
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
*(end + 1) = '\0';
return str;
}
/**
* Function to convert a string to uppercase
*
* @param char* str String to convert
* @return char* Converted string
*/
char* toUpperCase(char* str) {
for (char* p = str; *p; ++p) {
*p = toupper(*p);
}
return str;
}
/**
* Function to read a file into a string
*
* @param const char* filename File to read
* @return char* Content of the file
*/
char* readFile(const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Fehler: Datei konnte nicht geöffnet werden.\n");
return NULL;
}
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
char* content = (char*)malloc(size + 1);
if (content == NULL) {
printf("Fehler: Speicher konnte nicht reserviert werden.\n");
fclose(file);
return NULL;
}
fread(content, 1, size, file);
content[size] = '\0';
fclose(file);
return content;
}
/**
* Function to write a string to a file
*
* @param const char* filename File to write
* @param const char* content Content to write
*/
void writeFile(const char* filename, const char* content) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("Fehler: Datei konnte nicht geöffnet werden.\n");
return;
}
fprintf(file, "%s", content);
fclose(file);
}
#include <time.h>
/**
* Function to get the current timestamp as a string
*
* @return char* Current timestamp
*/
char* getTimestamp() {
time_t now = time(NULL);
struct tm* t = localtime(&now);
char* timestamp = (char*)malloc(20);
if (timestamp == NULL) {
printf("Fehler: Speicher konnte nicht reserviert werden.\n");
exit(EXIT_FAILURE);
}
strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", t);
return timestamp;
}
/**
* Function to find the maximum of two numbers
*
* @param int a First number
* @param int b Second number
* @return int Maximum number
*/
int maximum(int a, int b) {
return (a > b) ? a : b;
}
/**
* Function to find the minimum of two numbers
*
* @param int a First number
* @param int b Second number
* @return int Minimum number
*/
int minimum(int a, int b) {
return (a < b) ? a : b;
}
/**
* Function to find the maximum value in an array of integers
*
* @param int* array Array of integers
* @param size_t size Size of the array
* @return int Maximum value
*/
int arrayMax(int* array, size_t size) {
int maxVal = array[0];
for (size_t i = 1; i < size; i++) {
if (array[i] > maxVal) {
maxVal = array[i];
}
}
return maxVal;
}