From 9534d412f85eb5ba1c4d6b49c8a0abc1014bd6d9 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Tue, 16 Nov 2021 08:49:06 +0100 Subject: [PATCH 1/3] Corrected the functions. --- BlogDB.c | 115 +++++++++++++++++++++++++++---------------------------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 040fa1a..88afc08 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -13,8 +13,7 @@ static int AddPost(char title[], int authorId, char datePosted[], char post[], i static int EditPost(int postID, char title[], char post[], MYSQL* con); static int AddFilePost(int authorID, char filePath[], MYSQL* con); static int BlogWrite(MYSQL* con); -static int PullPostData(char* strings[6], int postID, MYSQL* con); -static int AddTag(char tagTitle[], MYSQL* con); +static int PullPostData(char* strings[6], int postID, MYSQL* con); static int AddTag(char tagTitle[], MYSQL* con); static int DeleteTag(int tagID, MYSQL* con); static int EditTag(int tagID, char tagTitle[], MYSQL* con); static int AssignTag(int tagID, int postID, MYSQL* con); @@ -24,9 +23,9 @@ static int PullAuthorData(char* strings[4], int authorID, MYSQL* con); static int ChangeAuthor(int postID, int authorID, MYSQL* con); static int TogglePost(int postID, MYSQL* con); static bool IsEnabled(int postID, MYSQL* con); -static int* PostIDs(MYSQL* con); -static int* ActivePostIDs(MYSQL* con); -static int* AuthorIDs(MYSQL* con); +static int PostIDs(int** arr, MYSQL* con); +static int ActivePostIDs(int** arr, MYSQL* con); +static int AuthorIDs(int** arr, MYSQL* con); // Connects to the MariaDB database and returns the connection MYSQL* conn2maria() @@ -828,12 +827,11 @@ bool IsEnabled(int postID, MYSQL* con) return !state; } -// Returns an array of IDs for all posts, +// Returns the count, and pass an array of IDs for all posts by reference, // the last element in the array is set to be negative as to prevent going into unallocated data. // Remember to free() the returned array -int* PostIDs(MYSQL* con) +int PostIDs(int** arr, MYSQL* con) { - if (con == NULL) { con = conn2maria(); @@ -853,7 +851,7 @@ int* PostIDs(MYSQL* con) row = mysql_fetch_row(result); int count = atoi(row[0]); - //printf("%d\n", count); + printf("%d\n", count); free(result); @@ -867,26 +865,25 @@ int* PostIDs(MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); - int *arr = (int *)malloc(sizeof(int) * (count + 1)); + *arr = (int *)malloc(sizeof(int) * (count + 1)); //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); if (row != NULL) { - arr[i] = atoi(row[0]); + *(*arr + i) = atoi(row[0]); } } - arr[count] = -1; - - return arr; + *(*arr + count) = -1; + return count; } -// Returns an array of IDs for all the posts that have state=0, +// Returns the count, and pass an array of IDs for all the posts that have state=0 by reference, // the last element in the array is set to be negative as to prevent going into unallocated data. // Remember to free() the returned array -int* ActivePostIDs(MYSQL* con) +int ActivePostIDs(int** arr, MYSQL* con) { if (con == NULL) { @@ -907,7 +904,7 @@ int* ActivePostIDs(MYSQL* con) row = mysql_fetch_row(result); int count = atoi(row[0]); - //printf("%d\n", count); + printf("%d\n", count); free(result); @@ -921,25 +918,25 @@ int* ActivePostIDs(MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); - int *arr = (int *)malloc(sizeof(int) * (count + 1)); + *arr = (int *)malloc(sizeof(int) * (count + 1)); //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); if (row != NULL) { - arr[i] = atoi(row[0]); + *(*arr + i) = atoi(row[0]); } } - arr[count] = -1; + *(*arr + count) = -1; - return arr; + return count; } -// Returns an array of IDs for all authors, +// Returns the count, and pass an array of IDs for all authors by reference, // the last element in the array is set to be negative as to prevent going into unallocated data. // Remember to free() the returned array -int* AuthorIDs(MYSQL* con) +int AuthorIDs(int** arr, MYSQL* con) { if (con == NULL) { @@ -974,19 +971,19 @@ int* AuthorIDs(MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); - int *arr = (int *)malloc(sizeof(int) * (count + 1)); + *arr = (int *)malloc(sizeof(int) * (count + 1)); //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); if (row != NULL) { - arr[i] = atoi(row[0]); + *(*arr + i) = atoi(row[0]); } } - arr[count] = -1; + *(*arr + count) = -1; - return arr; + return count; } // Main @@ -1031,70 +1028,72 @@ int main(int argc, char *argv[]) // =================================== */ + /* + // =================================== + // Example of using 'PostIDs()' + // =================================== + + // Generate array of IDs + int *postIDs; + int count2 = PostIDs(&postIDs, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count2; ++i) + { + // Print contents on a new line + printf("%d\n", postIDs[i]); + } + + // Free the memory + free(postIDs); + + // =================================== + */ + /* // =================================== // Example of using 'ActivePostIDs()' // =================================== // Generate array of IDs - int *num = ActivePostIDs(NULL); + int *activePostIDs; + int count1 = AuthorIDs(&activePostIDs, NULL); // Loop until element is negative i.e. the last - for (int i = 0; num[i] > 0; ++i) + for (int i = 0; i < count1; ++i) { // Print contents on a new line - printf("%d\n", num[i]); + printf("%d\n", activePostIDs[i]); } // Free the memory - free(num); + free(activePostIDs); // =================================== */ /* // =================================== - // Example of using 'PostIDs()' - // =================================== - - // Generate array of IDs - int *num = PostIDs(NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; num[i] > 0; ++i) - { - // Print contents on a new line - printf("%d\n", num[i]); - } - - // Free the memory - free(num); - - // =================================== - */ - - /* - // =================================== - // Example of using 'PostIDs()' + // Example of using 'AuthorIDs()' // =================================== // Generate array of IDs - int *num = AuthorIDs(NULL); + int *authorIDs; + int count3 = AuthorIDs(&authorIDs, NULL); // Loop until element is negative i.e. the last - for (int i = 0; num[i] > 0; ++i) + for (int i = 0; i < count3; ++i) { // Print contents on a new line - printf("%d\n", num[i]); + printf("%d\n", authorIDs[i]); } // Free the memory - free(num); + free(authorIDs); // =================================== */ - // Open the connection MYSQL *con = conn2maria(); -- 2.30.2 From 33824b31f663b162eca2969f9c3002aba18e1022 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Tue, 16 Nov 2021 09:11:54 +0100 Subject: [PATCH 2/3] Added comments --- BlogDB.c | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 88afc08..e46dfa4 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -850,8 +850,8 @@ int PostIDs(int** arr, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); + // Get the Count int count = atoi(row[0]); - printf("%d\n", count); free(result); @@ -865,18 +865,28 @@ int PostIDs(int** arr, MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); + // Allocate the array that was passed by reference + // To be one larger than I need. *arr = (int *)malloc(sizeof(int) * (count + 1)); - //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + // loop count times. for (int i = 0; i < count; ++i) { + //Get row row = mysql_fetch_row(result2); + + // If the row exists (I counted them, so it must exist.) if (row != NULL) { + // Set the i-th element of the array, that was passed by reference, to be the current row's ID *(*arr + i) = atoi(row[0]); } } + // Set the last element in the array passed by reference to -1, + // As an ID can never be negative, it can ensure that it never + // Enters unknown memory. *(*arr + count) = -1; + // Return the count, for easy looping. return count; } @@ -903,8 +913,8 @@ int ActivePostIDs(int** arr, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); + // Get the Count int count = atoi(row[0]); - printf("%d\n", count); free(result); @@ -918,18 +928,28 @@ int ActivePostIDs(int** arr, MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); + // Allocate the array that was passed by reference + // To be one larger than I need. *arr = (int *)malloc(sizeof(int) * (count + 1)); - //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + // loop count times. for (int i = 0; i < count; ++i) { + //Get row row = mysql_fetch_row(result2); + + // If the row exists (I counted them, so it must exist.) if (row != NULL) { + // Set the i-th element of the array, that was passed by reference, to be the current row's ID *(*arr + i) = atoi(row[0]); } } + // Set the last element in the array passed by reference to -1, + // As an ID can never be negative, it can ensure that it never + // Enters unknown memory. *(*arr + count) = -1; + // Return the count, for easy looping. return count; } @@ -956,8 +976,8 @@ int AuthorIDs(int** arr, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); + // Get the Count int count = atoi(row[0]); - //printf("%d\n", count); free(result); @@ -971,18 +991,28 @@ int AuthorIDs(int** arr, MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); + // Allocate the array that was passed by reference + // To be one larger than I need. *arr = (int *)malloc(sizeof(int) * (count + 1)); - //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + // loop count times. for (int i = 0; i < count; ++i) { + //Get row row = mysql_fetch_row(result2); + + // If the row exists (I counted them, so it must exist.) if (row != NULL) { + // Set the i-th element of the array, that was passed by reference, to be the current row's ID *(*arr + i) = atoi(row[0]); } } + // Set the last element in the array passed by reference to -1, + // As an ID can never be negative, it can ensure that it never + // Enters unknown memory. *(*arr + count) = -1; + // Return the count, for easy looping. return count; } @@ -1035,10 +1065,10 @@ int main(int argc, char *argv[]) // Generate array of IDs int *postIDs; - int count2 = PostIDs(&postIDs, NULL); + int count1 = PostIDs(&postIDs, NULL); // Loop until element is negative i.e. the last - for (int i = 0; i < count2; ++i) + for (int i = 0; i < count1; ++i) { // Print contents on a new line printf("%d\n", postIDs[i]); @@ -1057,10 +1087,10 @@ int main(int argc, char *argv[]) // Generate array of IDs int *activePostIDs; - int count1 = AuthorIDs(&activePostIDs, NULL); + int count2 = ActivePostIDs(&activePostIDs, NULL); // Loop until element is negative i.e. the last - for (int i = 0; i < count1; ++i) + for (int i = 0; i < count2; ++i) { // Print contents on a new line printf("%d\n", activePostIDs[i]); -- 2.30.2 From 9c3bcc3b0b6f90b774832bbc7cc199fd2b35ef65 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Tue, 16 Nov 2021 09:13:22 +0100 Subject: [PATCH 3/3] Fixed where a misstype had merged to lines. --- BlogDB.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/BlogDB.c b/BlogDB.c index e46dfa4..da0c0e7 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -13,7 +13,8 @@ static int AddPost(char title[], int authorId, char datePosted[], char post[], i static int EditPost(int postID, char title[], char post[], MYSQL* con); static int AddFilePost(int authorID, char filePath[], MYSQL* con); static int BlogWrite(MYSQL* con); -static int PullPostData(char* strings[6], int postID, MYSQL* con); static int AddTag(char tagTitle[], MYSQL* con); +static int PullPostData(char* strings[6], int postID, MYSQL* con); +static int AddTag(char tagTitle[], MYSQL* con); static int DeleteTag(int tagID, MYSQL* con); static int EditTag(int tagID, char tagTitle[], MYSQL* con); static int AssignTag(int tagID, int postID, MYSQL* con); -- 2.30.2