forked from Danielbet21/Amusement_Park
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneral.c
108 lines (90 loc) · 1.99 KB
/
General.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
#include "General.h"
char* getStrExactName(const char* msg)
{
char* str;
char temp[MAX_STR_LEN];
if (msg != NULL) {
printf("%s", msg);
}
myGets(temp, MAX_STR_LEN);
str = getDynStr(temp);
return str;
}
char* getDynStr(char* str)
{
char* theStr;
theStr = (char*)malloc((strlen(str) + 1) * sizeof(char));
if (!theStr)
return NULL;
strcpy(theStr, str);
return theStr;
}
char* myGets(char* buffer, int size)
{
char* ok;
if (buffer != NULL && size > 0)
{
do { //skip only '\n' strings
ok = fgets(buffer, size, stdin);
} while (ok && ((strlen(buffer) <= 1) && (isspace(buffer[0]))));
if (ok)
{
char* back = buffer + strlen(buffer);
//trim end spaces
while ((buffer < back) && (isspace(*--back)));
*(back + 1) = '\0';
return buffer;
}
buffer[0] = '\0';
}
return NULL;
}
char** splitCharsToWords(char* str, int* pCount, int* pTotalLength)
{
char temp[255];
char* delimiters = " ";
char* word;
int count = 0;
strcpy(temp, str);
char** wordsArray = NULL;
*pTotalLength = 0;
word = strtok(temp, delimiters);
while (word != NULL)
{
char** temp = (char**)realloc(wordsArray, (count + 1) * sizeof(char*));
if (!temp)
return NULL;
wordsArray = temp;
wordsArray[count] = getDynStr(word);
count++;
*pTotalLength += (int)strlen(word);
word = strtok(NULL, delimiters);
}
*pCount = count;
return wordsArray;
}
// general function to activate a function on each element of an array
void generalArrayFunction(void* arr, int size, size_t typeSize, void* (*func)(void* element)) {
if (!arr) {
return;
}
for (int i = 0; i < size; i++)
{
// use the function on each element of the array
func((char*)(arr)+i * typeSize);
}
}
int randomNum(int min, int max) {
return rand() % (max - min + 1) + min;
}
int validName(char* name) {
if (name == NULL || strlen(name) == 0) {
return 0;
}
for (int i = 0; i < strlen(name); i++) {
if (((name[i] < 'A' || name[i] > 'Z') && (name[i] < 'a' || name[i] > 'z')) && name[i] != ' ') {
return 0;
}
}
return 1;
}