From 7da1908417d2e6e93602b6e98b2d5d369ac8dfd7 Mon Sep 17 00:00:00 2001 From: kelson8 Date: Sat, 16 Nov 2024 23:48:26 -0500 Subject: [PATCH] Add while true loop and switch statement in main. Change CMake generator to MinGW Makefiles. Update gitignore. --- .gitignore | 1 + .vscode/settings.json | 6 +- src/main.c | 187 +++++++++++++++++++++++++++++++++--------- 3 files changed, 155 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 4939f1b..ecfc86b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # New build.tar +src/old # ---> C # Prerequisites diff --git a/.vscode/settings.json b/.vscode/settings.json index b76e333..2a30edb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,8 @@ "pointer_test.h": "c", "struct_test.h": "c", "stdio.h": "c", - "stdlib.h": "c" - } + "stdlib.h": "c", + "mysql.h": "c" + }, + "cmake.generator": "MinGW Makefiles" } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 7b8225d..7677c68 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,4 @@ - #include +#include // #include #include @@ -11,7 +11,6 @@ #include "mysql_test.h" - // Test // https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c #ifdef _WIN32 @@ -20,7 +19,6 @@ #include "stdbool.h" - // To setup this project on windows // 1. Install CMake // 2. Install Ninja with winget in windows 11: "winget install --id=Ninja-build.Ninja -e" @@ -50,14 +48,24 @@ // TODO Look into getting current directory in Windows and Linux // https://stackoverflow.com/questions/27585930/get-the-current-working-directory-in-c-on-windows +// This might be useful for all the preprocessors for each operating system: +// https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor + + // TODO Move this into a different project. #ifdef _WIN32 -void guiTest(){ - +void guiTest() +{ + // WIN32_ } -#endif +#endif //_WIN32 + +#ifdef __linux__ +// TODO Test this +#include +#endif //__linux__ // #define _FLIP_BOOLS // Hmm, very evil @@ -69,40 +77,166 @@ void guiTest(){ // #define false (rand() % 2) #endif //_FLIP_BOOLS +// https://www.reddit.com/r/C_Programming/comments/jjdl7s/difference_between_char_and_char/ +// char welcomeMsg[] = "Welcome to KCNet"; +const char *kcnetMsg = "[KCNet]:"; +const char *welcomeMsg = "Welcome to KCNet"; +const char *errorMsg = "Error:"; + +void flip_bools() +{ +#ifdef _FLIP_BOOLS + bool testb = false; + if (testb) + { + printf("True"); + } + else + { + printf("False"); + } +#else + // Print the message that this isn't enabled + printf("%s %s This is not enabled!\n", kcnetMsg, errorMsg); +#endif //_FLIP_BOOLS +} + // TODO Look into making a gtk C program // https://www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/ // Print out the memory addresses for each item in an array. -void array_test() { +void array_test() +{ // https://www.tutorialspoint.com/cprogramming/c_memory_address.htm // Int array - int numbers[5] = {1,2,3,4,5}; + int numbers[5] = {1, 2, 3, 4, 5}; int i = 0; printf("numbers = %p\n", numbers); - do { + do + { printf("numbers[%u] = %p\n", i, (void *)&numbers[i]); i++; } while (i < 5); printf("sizeof(numbers) = %lu\n", sizeof(numbers)); - } // Removed from path in windows: C:\mingw64\bin // https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm -void check_even_odd(int num) { - (num %2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num); +void check_even_odd(int num) +{ + (num % 2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num); + + // return; } +void user_input() +{ + int value; // Value for the switch loop + int number_to_check; // Number to check for even/odd check in the loop -int main() { - char welcomeMsg[] = "Welcome to KCNet"; - // printf(welcomeMsg); + // Pointer test + int i = 22222; // Random value + int *i_ptr = &i; // Pointer to that random value + // - uint16_t test = -2; + while (true) + { + // for (;;){ + + // Oops this was outside of the loop and breaking it... + // Well I fixed that part of it. + printf("\n%s What would you like to do? " + "1 (Array Test), " + "2 (Check Even or Odd), " + "3 (Print welcome msg), " + "4 (Flip bools {if enabled}), " + "5 (Read file), " + "6 (Print struct), " + "7 (Math test), " + "8 (Pointer test), " + "9 (Get User Profile): ", + kcnetMsg); + scanf("%d", &value); + + switch (value) + { + case 1: + array_test(); + break; + case 2: + printf("Enter a number to check: "); + scanf("%d", &number_to_check); + check_even_odd(number_to_check); + break; + case 3: + printf("%s\n", welcomeMsg); + break; + case 4: + flip_bools(); + break; + case 5: + read_file("test.txt"); + break; + + case 6: + print_struct(); + break; + case 7: + // printf("Test #7"); + mathFunctions(); + break; + case 8: + // printf("Test #8"); + pointer_madness(i_ptr); + break; + + case 9: + // printf("Test #8"); + #ifdef _WIN32 + printf("%s User profile path: %s\n", kcnetMsg, getenv("USERPROFILE")); + #endif // _WIN32 + #ifdef __linux__ + // https://stackoverflow.com/questions/1451825/c-programming-printing-current-user + // https://stackoverflow.com/questions/41976889/c-user-name-in-linux-get-current-user-name + // TODO Fix this + struct passwd *p = getpwuid(getuid()); + if (p != NULL) + { + printf("%s %s\n", kcnetMsg, p->pw_name); + } + else + { + printf("%s The user seems to be invalid.", errorMsg) + } + + + #endif //__linux__ + break; + + default: + printf("Invalid option %d", &value); + break; + } + } + // printf("Enter a number: "); + // scanf("%d", &my_num); + // printf("You entered the number: \"%d\"", my_num); +} + +int main() +{ + + // printf("%s\n", welcomeMsg); + + // uint16_t test = -2; + + user_input(); + // TODO Fix this to work. + // connect_mysql(); // array_test(); // for (int i=0; i<5; i++) { @@ -113,7 +247,6 @@ int main() { // print_struct(); // read_file("test.txt"); - // This doesn't work // uint16_test(); @@ -124,26 +257,6 @@ int main() { // https://stackoverflow.com/questions/12120426/how-do-i-print-uint32-t-and-uint16-t-variables-value // printf("Number: %" PRIu32, test); - // mathFunctions(); - // print_struct(); - int i = 22222; - int* i_ptr = &i; - // pointer_madness(i_ptr); - - connect_mysql(); - -#ifdef _FLIP_BOOLS - bool testb = false; - if (testb) { - printf("True"); - } else { - printf("False"); - } - -#endif //_FLIP_BOOLS - - - // printf_example(); return 0; } \ No newline at end of file