-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.c
141 lines (107 loc) · 3.03 KB
/
utilities.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
#include "./utilities.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void destroy2DArray(char** arr, const int rows, const int cols) {
size_t i;
for (i = 0; i < rows; i++) {
free(*(arr + i));
}
free(arr);
}
char* getUserInputStr(const char* message, const char* onInvalidMessage, const int strLength,
unsigned char (*validator)(const char* userInput)) {
char* userInput = malloc(strLength * sizeof(char));
if (userInput == NULL) {
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
printf(message);
fflush(stdin);
fgets(userInput, strLength, stdin);
trimStr(userInput);
while (!(*validator)(userInput)) {
puts(onInvalidMessage);
printf(message);
fflush(stdin);
fgets(userInput, strLength, stdin);
trimStr(userInput);
};
return userInput;
}
int isStrIn(const char* str, const char* arr[], const int size) {
size_t i;
for (i = 0; i < size; i++) {
if (strcmpi(str, *(arr + i)) == 0) return 1;
}
return 0;
}
char** new2DArray(const int rows, const int cols) {
size_t i;
size_t j;
char** bidimensionalArr = malloc(rows * sizeof(char*));
if (bidimensionalArr == NULL) {
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < rows; i++) {
*(bidimensionalArr + i) = malloc(cols * sizeof(char));
if (*(bidimensionalArr + i) == NULL) {
for (j = 0; j < i; j++) free(*(bidimensionalArr + j));
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
}
return bidimensionalArr;
}
void sleep(int miliseconds) {
const clock_t startTime = clock();
while (clock() < (startTime + miliseconds))
;
}
int strcmpi(const char* str01, const char* str02) {
size_t i;
const size_t lengthStr01 = strlen(str01);
const size_t lengthStr02 = strlen(str02);
char charStr01;
char charStr02;
int cmp = 1;
if (lengthStr01 != lengthStr02) return lengthStr01 > lengthStr02 ? 1 : -1;
for (i = 0; i < lengthStr01; i++) {
charStr01 = toupper(*(str01 + i));
charStr02 = toupper(*(str02 + i));
cmp = charStr01 - charStr02;
if (cmp != 0) return cmp;
};
return cmp;
}
void trimStr(char* str) {
trimLeftStr(str);
trimRightStr(str);
}
void trimLeftStr(char* str) {
size_t i;
size_t j;
size_t strLength = strlen(str);
size_t counter = 0;
for (i = 0; i < strLength; i++) {
if (!isspace(*(str + i))) break;
counter++;
};
for (j = 0; j < strLength - counter; j++) {
*(str + j) = *(str + j + counter);
};
*(str + strLength - counter) = '\0';
}
void trimRightStr(char* str) {
size_t i;
size_t strLength = strlen(str);
size_t counter = 0;
for (i = strLength - 1; i > 0; i--) {
if (!isspace(*(str + i))) break;
counter++;
}
*(str + strLength - counter) = '\0';
}