CrimsonCare
CrimsonCare is a C project designed to provide a robust solution for blood management.
 
Loading...
Searching...
No Matches
misc.c
Go to the documentation of this file.
1
31#include "../include/misc.h"
32
46void displayWelcomeMessage(void) {
47 FILE* file = fopen("resources/assets/misc/cc.txt", "r");
48 if (!file) {
49 return;
50 }
51 char buffer[1024];
52 while (fgets(buffer, sizeof(buffer), file) != NULL) {
53 printf("%s", buffer);
54 }
55 fclose(file);
56}
57
65void displayUserMenu(void) {
66 printf("\n--- CrimsonCare Blood Bank Management System (User) ---\n");
67 printf("1. Buy Blood\n");
68 printf("2. Sell Blood\n");
69 printf("3. Display Blood Stocks\n");
70 printf("4. Admin Panel\n");
71 printf("5. Exit\n");
72 printf("Select an option: ");
73}
74
82void displayAdminMenu(void) {
83 printf("\n--- CrimsonCare Blood Bank Management System (Admin) ---\n");
84 printf("1. Add Hospital\n");
85 printf("2. Update Blood Quantity\n");
86 printf("3. Update Blood Price\n");
87 printf("4. Change Admin Password\n");
88 printf("5. Add Admin\n");
89 printf("6. Delete Admin\n");
90 printf("7. Delete Hospital\n");
91 printf("8. Display Admins\n");
92 printf("9. Display Hospitals\n");
93 printf("10. Display Blood Stocks\n");
94 printf("11. Display Transactions\n");
95 printf("12. Exit\n");
96 printf("Select an option: ");
97}
98
107void clearInputBuffer(void) {
108 int c;
109 while ((c = getchar()) != '\n' && c != EOF);
110}
111
127bool checkUsername(const char* str) {
128 while (*str) {
129 if (!(*str >= 'a' && *str <= 'z') && !(*str >= '0' && *str <= '9')) {
130 return false;
131 }
132 str++;
133 }
134 return true;
135}
136
150bool containsPipe(const char* str) {
151 while (*str) {
152 if (*str == '|') {
153 return true;
154 }
155 str++;
156 }
157 return false;
158}
159
172void getPassword(char* password, size_t size) {
173#ifdef _WIN32
174 size_t i = 0;
175 char ch;
176 while (i < size - 1) {
177
178 ch = getch();
179
180 if (ch == '\r') {
181
182 break;
183 } else if (ch == '\b') {
184
185 if (i > 0) {
186 i--;
187 printf("\b \b");
188
189 }
190 } else {
191 password[i++] = ch;
192 printf("*");
193
194 }
195 }
196 password[i] = '\0';
197
198 printf("\n");
199#else
200
201 struct termios oldt, newt;
202 tcgetattr(STDIN_FILENO, &oldt);
203
204 newt = oldt;
205
206 newt.c_lflag &= ~(ECHO);
207
208 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
209
210 fgets(password, size);
211 password[strcspn(password, "\n")] = 0;
212
213 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
214
215 printf("\n");
216#endif
217}
218
228bool isLeapYear(int year) {
229 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
230}
231
248bool isValidDate(const char* date) {
249 if (strcmp(date, "") == 0) {
250 printf("Error: Date cannot be empty.\n");
251 return false;
252 }
253
254 int year, month, day;
255
256 if (strlen(date) < 8 || strlen(date) > 10) {
257 printf("Error: Invalid date format.\n");
258 return false;
259 }
260
261 if (sscanf(date, "%d-%d-%d", &year, &month, &day) != 3) {
262 printf("Error: Invalid date format.\n");
263 return false;
264 }
265
266 if (month < 1 || month > 12) {
267 printf("Error: Invalid month.\n");
268 return false;
269 }
270
271 int daysInMonth[] = { 31, 28 + (int)isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
272
273 if (day < 1 || day > daysInMonth[month - 1]) {
274 printf("Error: Invalid day.\n");
275 return false;
276 }
277
278 return true;
279}
280
294void formatDate(char* date) {
295 if (strcmp(date, "") == 0) {
296 printf("Error: Date cannot be empty.\n");
297 return;
298 }
299
300 if (!isValidDate(date)) {
301 printf("Error: Invalid date format.\n");
302 return;
303 }
304
305 int year, month, day;
306 sscanf(date, "%d-%d-%d", &year, &month, &day);
307 sprintf(date, "%04d-%02d-%02d", year, month, day);
308}
Misc header file.