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();