Merge pull request '26-rework' (#27) from 26-rework into master

Reviewed-on: #27
This commit is contained in:
Adam Fordsmand 2021-11-16 08:17:28 +00:00
commit c3cdc1195e
1 changed files with 91 additions and 61 deletions

152
BlogDB.c
View File

@ -13,7 +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 EditPost(int postID, char title[], char post[], MYSQL* con);
static int AddFilePost(int authorID, char filePath[], MYSQL* con); static int AddFilePost(int authorID, char filePath[], MYSQL* con);
static int BlogWrite(MYSQL* con); static int BlogWrite(MYSQL* con);
static int PullPostData(char* strings[6], int postID, MYSQL* con); static int PullPostData(char* strings[6], int postID, MYSQL* con);
static int AddTag(char tagTitle[], MYSQL* con); static int AddTag(char tagTitle[], MYSQL* con);
static int DeleteTag(int tagID, MYSQL* con); static int DeleteTag(int tagID, MYSQL* con);
static int EditTag(int tagID, char tagTitle[], MYSQL* con); static int EditTag(int tagID, char tagTitle[], MYSQL* con);
@ -24,9 +24,9 @@ static int PullAuthorData(char* strings[4], int authorID, MYSQL* con);
static int ChangeAuthor(int postID, int authorID, MYSQL* con); static int ChangeAuthor(int postID, int authorID, MYSQL* con);
static int TogglePost(int postID, MYSQL* con); static int TogglePost(int postID, MYSQL* con);
static bool IsEnabled(int postID, MYSQL* con); static bool IsEnabled(int postID, MYSQL* con);
static int* PostIDs(MYSQL* con); static int PostIDs(int** arr, MYSQL* con);
static int* ActivePostIDs(MYSQL* con); static int ActivePostIDs(int** arr, MYSQL* con);
static int* AuthorIDs(MYSQL* con); static int AuthorIDs(int** arr, MYSQL* con);
// Connects to the MariaDB database and returns the connection // Connects to the MariaDB database and returns the connection
MYSQL* conn2maria() MYSQL* conn2maria()
@ -828,12 +828,11 @@ bool IsEnabled(int postID, MYSQL* con)
return !state; 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. // the last element in the array is set to be negative as to prevent going into unallocated data.
// Remember to free() the returned array // Remember to free() the returned array
int* PostIDs(MYSQL* con) int PostIDs(int** arr, MYSQL* con)
{ {
if (con == NULL) if (con == NULL)
{ {
con = conn2maria(); con = conn2maria();
@ -852,8 +851,8 @@ int* PostIDs(MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
// Get the Count
int count = atoi(row[0]); int count = atoi(row[0]);
//printf("%d\n", count);
free(result); free(result);
@ -867,26 +866,35 @@ int* PostIDs(MYSQL* con)
MYSQL_RES *result2 = mysql_store_result(con); MYSQL_RES *result2 = mysql_store_result(con);
int *arr = (int *)malloc(sizeof(int) * (count + 1)); // Allocate the array that was passed by reference
//printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); // To be one larger than I need.
*arr = (int *)malloc(sizeof(int) * (count + 1));
// loop count times.
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
//Get row
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
// If the row exists (I counted them, so it must exist.)
if (row != NULL) { if (row != NULL) {
arr[i] = atoi(row[0]); // 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]);
} }
} }
arr[count] = -1; // 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
return arr; // Enters unknown memory.
*(*arr + count) = -1;
// Return the count, for easy looping.
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. // the last element in the array is set to be negative as to prevent going into unallocated data.
// Remember to free() the returned array // Remember to free() the returned array
int* ActivePostIDs(MYSQL* con) int ActivePostIDs(int** arr, MYSQL* con)
{ {
if (con == NULL) if (con == NULL)
{ {
@ -906,8 +914,8 @@ int* ActivePostIDs(MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
// Get the Count
int count = atoi(row[0]); int count = atoi(row[0]);
//printf("%d\n", count);
free(result); free(result);
@ -921,25 +929,35 @@ int* ActivePostIDs(MYSQL* con)
MYSQL_RES *result2 = mysql_store_result(con); MYSQL_RES *result2 = mysql_store_result(con);
int *arr = (int *)malloc(sizeof(int) * (count + 1)); // Allocate the array that was passed by reference
//printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); // To be one larger than I need.
*arr = (int *)malloc(sizeof(int) * (count + 1));
// loop count times.
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
//Get row
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
// If the row exists (I counted them, so it must exist.)
if (row != NULL) { if (row != NULL) {
arr[i] = atoi(row[0]); // 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]);
} }
} }
arr[count] = -1; // 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 arr; // Return the count, for easy looping.
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. // the last element in the array is set to be negative as to prevent going into unallocated data.
// Remember to free() the returned array // Remember to free() the returned array
int* AuthorIDs(MYSQL* con) int AuthorIDs(int** arr, MYSQL* con)
{ {
if (con == NULL) if (con == NULL)
{ {
@ -959,8 +977,8 @@ int* AuthorIDs(MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
// Get the Count
int count = atoi(row[0]); int count = atoi(row[0]);
//printf("%d\n", count);
free(result); free(result);
@ -974,19 +992,29 @@ int* AuthorIDs(MYSQL* con)
MYSQL_RES *result2 = mysql_store_result(con); MYSQL_RES *result2 = mysql_store_result(con);
int *arr = (int *)malloc(sizeof(int) * (count + 1)); // Allocate the array that was passed by reference
//printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); // To be one larger than I need.
*arr = (int *)malloc(sizeof(int) * (count + 1));
// loop count times.
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
//Get row
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
// If the row exists (I counted them, so it must exist.)
if (row != NULL) { if (row != NULL) {
arr[i] = atoi(row[0]); // 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]);
} }
} }
arr[count] = -1; // 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 arr; // Return the count, for easy looping.
return count;
} }
// Main // Main
@ -1031,70 +1059,72 @@ int main(int argc, char *argv[])
// =================================== // ===================================
*/ */
/*
// ===================================
// Example of using 'PostIDs()'
// ===================================
// Generate array of IDs
int *postIDs;
int count1 = PostIDs(&postIDs, NULL);
// Loop until element is negative i.e. the last
for (int i = 0; i < count1; ++i)
{
// Print contents on a new line
printf("%d\n", postIDs[i]);
}
// Free the memory
free(postIDs);
// ===================================
*/
/* /*
// =================================== // ===================================
// Example of using 'ActivePostIDs()' // Example of using 'ActivePostIDs()'
// =================================== // ===================================
// Generate array of IDs // Generate array of IDs
int *num = ActivePostIDs(NULL); int *activePostIDs;
int count2 = ActivePostIDs(&activePostIDs, NULL);
// Loop until element is negative i.e. the last // Loop until element is negative i.e. the last
for (int i = 0; num[i] > 0; ++i) for (int i = 0; i < count2; ++i)
{ {
// Print contents on a new line // Print contents on a new line
printf("%d\n", num[i]); printf("%d\n", activePostIDs[i]);
} }
// Free the memory // Free the memory
free(num); free(activePostIDs);
// =================================== // ===================================
*/ */
/* /*
// =================================== // ===================================
// Example of using 'PostIDs()' // Example of using 'AuthorIDs()'
// ===================================
// 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()'
// =================================== // ===================================
// Generate array of IDs // Generate array of IDs
int *num = AuthorIDs(NULL); int *authorIDs;
int count3 = AuthorIDs(&authorIDs, NULL);
// Loop until element is negative i.e. the last // 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 // Print contents on a new line
printf("%d\n", num[i]); printf("%d\n", authorIDs[i]);
} }
// Free the memory // Free the memory
free(num); free(authorIDs);
// =================================== // ===================================
*/ */
// Open the connection // Open the connection
MYSQL *con = conn2maria(); MYSQL *con = conn2maria();