Cleaned brackets and spacing

This commit is contained in:
Adam Fordsmand 2022-04-16 00:23:38 +02:00
parent 9470fa03e2
commit 6469b8ecfb
1 changed files with 66 additions and 111 deletions

177
BlogDB.c
View File

@ -31,6 +31,7 @@ static int PostIDs(int** arr, MYSQL* con);
static int ActivePostIDs(int** arr, MYSQL* con); static int ActivePostIDs(int** arr, MYSQL* con);
static int AuthorIDs(int** arr, 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()
{ {
@ -55,10 +56,7 @@ MYSQL* conn2maria()
// Adds a new post to the database, with given values // Adds a new post to the database, with given values
int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con) int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* query; char* query;
if(0 > asprintf(&query, if(0 > asprintf(&query,
@ -107,10 +105,7 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags
// Edits a post in the database, to the new given values // Edits a post in the database, to the new given values
int EditPost(int postID, char title[], char post[], MYSQL* con) int EditPost(int postID, char title[], char post[], MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* selectQuery; char* selectQuery;
if(0 > asprintf(&selectQuery, if(0 > asprintf(&selectQuery,
@ -129,7 +124,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con)
MYSQL_RES *result = mysql_store_result(con); MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result); int num_fields = mysql_num_fields(result);
if (result == NULL) { if (result == NULL)
{
free(selectQuery); free(selectQuery);
exit(1); exit(1);
} }
@ -164,13 +160,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con)
// Adds a post using file data // Adds a post using file data
int AddFilePost(int authorID, char filePath[], MYSQL* con) int AddFilePost(int authorID, char filePath[], MYSQL* con)
{ {
if (con == NULL) con = conn2maria();
if (con == NULL) if (authorID <= 0) authorID = 1;
{
con = conn2maria();
}
if (authorID <= 0) { authorID = 1; }
time_t t = time(NULL); time_t t = time(NULL);
struct tm *tm = localtime(&t); struct tm *tm = localtime(&t);
@ -191,15 +182,17 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEnd[1]}; char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEnd[1]};
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]}; char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]};
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) { if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
{
printf("Start Test : '%s'\nEnd Test : '%s'\n", startTest, endTest); printf("Start Test : '%s'\nEnd Test : '%s'\n", startTest, endTest);
return -1; return -1;
} }
// Remove the <h3> tags // Remove the <h3> tags
for (int i = 4; i < BufferLength; ++i) { for (int i = 4; i < BufferLength; ++i)
{
if (Buffer[i + 6] == StringEnd[0]) { if (Buffer[i + 6] == StringEnd[0])
{
Buffer[i - 4] = StringEnd[0]; Buffer[i - 4] = StringEnd[0];
break; break;
} }
@ -235,11 +228,7 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
// Edits a post using file data // Edits a post using file data
int EditFilePost(char filePath[], int postID, MYSQL* con) int EditFilePost(char filePath[], int postID, MYSQL* con)
{ {
if (con == NULL) con = conn2maria();
if (con == NULL)
{
con = conn2maria();
}
FILE* FilePointer; FILE* FilePointer;
int BufferLength = 8192; int BufferLength = 8192;
@ -255,15 +244,17 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEnd[1]}; char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEnd[1]};
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]}; char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]};
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) { if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
{
printf("Start Test : %s\nEnd Test : %s\n", startTest, endTest); printf("Start Test : %s\nEnd Test : %s\n", startTest, endTest);
return -1; return -1;
} }
// Remove the <h3> tags // Remove the <h3> tags
for (int i = 4; i < BufferLength; ++i) { for (int i = 4; i < BufferLength; ++i)
{
if (Buffer[i + 6] == StringEnd[0]) { if (Buffer[i + 6] == StringEnd[0])
{
Buffer[i - 4] = StringEnd[0]; Buffer[i - 4] = StringEnd[0];
break; break;
} }
@ -299,6 +290,8 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
// Open a temporary file containing the standard template, and add the contents as a post // Open a temporary file containing the standard template, and add the contents as a post
int BlogWrite(MYSQL* con) int BlogWrite(MYSQL* con)
{ {
if (con == NULL) con = conn2maria();
FILE *fp; FILE *fp;
char* tmpName = tmpnam(NULL); char* tmpName = tmpnam(NULL);
@ -320,6 +313,8 @@ int BlogWrite(MYSQL* con)
// Open a temporary file containing the contents of the given post and update the contents as a post // Open a temporary file containing the contents of the given post and update the contents as a post
int BlogEdit(int postID, MYSQL* con) int BlogEdit(int postID, MYSQL* con)
{ {
if (con == NULL) con = conn2maria();
FILE *fp; FILE *fp;
char* tmpName = tmpnam(NULL); char* tmpName = tmpnam(NULL);
@ -344,10 +339,7 @@ int BlogEdit(int postID, MYSQL* con)
// Pulls a single post's data into the supplied array // Pulls a single post's data into the supplied array
int PullPostData(char* strings[6], int postID, MYSQL* con) int PullPostData(char* strings[6], int postID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* query; char* query;
if(0 > asprintf(&query, "SELECT * FROM blog_posts WHERE id=(%d)", postID)) exit(1); if(0 > asprintf(&query, "SELECT * FROM blog_posts WHERE id=(%d)", postID)) exit(1);
@ -416,23 +408,25 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date
*dates = (int *)malloc(sizeof(int) * (count + 1)); *dates = (int *)malloc(sizeof(int) * (count + 1));
*Titles = (char **)malloc((count + 1) * sizeof(char *)); *Titles = (char **)malloc((count + 1) * sizeof(char *));
for (int i = 0; i < count + 1; ++i) { for (int i = 0; i < count + 1; ++i)
{
Titles[i] = malloc(sizeof(char) * 255); Titles[i] = malloc(sizeof(char) * 255);
} }
for (int i = 0; i < count; ++i)
for (int i = 0; i < count; ++i) { {
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
if (row != NULL) { if (row != NULL)
{
*(*IDs + i) = atoi(row[0]); *(*IDs + i) = atoi(row[0]);
*(*authors + i) = atoi(row[2]); *(*authors + i) = atoi(row[2]);
*(*dates + i) = atoi(row[3]); *(*dates + i) = atoi(row[3]);
for (int y = 0; y < 255; ++y) { for (int y = 0; y < 255; ++y)
{
Titles[i][y] = row[1][y]; Titles[i][y] = row[1][y];
} }
} }
} }
@ -451,10 +445,7 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date
// Adds a new tag // Adds a new tag
int AddTag(char tagTitle[], MYSQL* con) int AddTag(char tagTitle[], MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* insertQuery; char* insertQuery;
if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")", if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")",
@ -490,10 +481,7 @@ int AddTag(char tagTitle[], MYSQL* con)
// Removes a tag, and unassigns itself from any and all posts. // Removes a tag, and unassigns itself from any and all posts.
int DeleteTag(int tagID, MYSQL* con) int DeleteTag(int tagID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* deleteQuery; char* deleteQuery;
if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)", if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)",
@ -529,11 +517,7 @@ int DeleteTag(int tagID, MYSQL* con)
// Edit the tag name // Edit the tag name
int EditTag(int tagID, char tagTitle[], MYSQL* con) int EditTag(int tagID, char tagTitle[], MYSQL* con)
{ {
if (con == NULL) con = conn2maria();
if (con == NULL)
{
con = conn2maria();
}
char* selectQuery; char* selectQuery;
if(0 > asprintf(&selectQuery, if(0 > asprintf(&selectQuery,
@ -582,10 +566,7 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con)
// Assign a post a given tag. // Assign a post a given tag.
int AssignTag(int tagID, int postID, MYSQL* con) int AssignTag(int tagID, int postID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* selectQuery; char* selectQuery;
if(0 > asprintf(&selectQuery, "SELECT * FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", if(0 > asprintf(&selectQuery, "SELECT * FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)",
@ -603,7 +584,8 @@ int AssignTag(int tagID, int postID, MYSQL* con)
MYSQL_RES *result = mysql_store_result(con); MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result); int num_fields = mysql_num_fields(result);
if (result == NULL) { if (result == NULL)
{
return postID; return postID;
} }
@ -638,10 +620,7 @@ int AssignTag(int tagID, int postID, MYSQL* con)
// Unassign a tag from the given post // Unassign a tag from the given post
int RetractTag(int tagID, int postID, MYSQL* con) int RetractTag(int tagID, int postID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* deleteQuery; char* deleteQuery;
if (0 > asprintf(&deleteQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", if (0 > asprintf(&deleteQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)",
@ -664,10 +643,7 @@ int RetractTag(int tagID, int postID, MYSQL* con)
// Add an author // Add an author
int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* insertQuery; char* insertQuery;
@ -706,10 +682,7 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con)
// Pull the full name and email of the given author ID // Pull the full name and email of the given author ID
int PullAuthorData(char* strings[4], int authorID, MYSQL* con) int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* query; char* query;
if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1); if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1);
@ -742,11 +715,7 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
// Change the author of a given post // Change the author of a given post
int ChangeAuthor(int postID, int authorID, MYSQL* con) int ChangeAuthor(int postID, int authorID, MYSQL* con)
{ {
if (con == NULL) con = conn2maria();
if (con == NULL)
{
con = conn2maria();
}
char* selectQuery; char* selectQuery;
if(0 > asprintf(&selectQuery, if(0 > asprintf(&selectQuery,
@ -796,10 +765,7 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con)
// Toggles the posts state, which determines whether the code ignores it. // Toggles the posts state, which determines whether the code ignores it.
int TogglePost(int postID, MYSQL* con) int TogglePost(int postID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* selectQuery; char* selectQuery;
if(0 > asprintf(&selectQuery, if(0 > asprintf(&selectQuery,
@ -820,10 +786,7 @@ int TogglePost(int postID, MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
if (row == NULL) if (row == NULL) return postID;
{
return postID;
}
int state = atoi(row[5]); int state = atoi(row[5]);
free(selectQuery); free(selectQuery);
@ -849,10 +812,7 @@ int TogglePost(int postID, MYSQL* con)
// Return true if state is 0, false if it doesn't exist or 1 // Return true if state is 0, false if it doesn't exist or 1
bool IsEnabled(int postID, MYSQL* con) bool IsEnabled(int postID, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
char* selectQuery; char* selectQuery;
if(0 > asprintf(&selectQuery, if(0 > asprintf(&selectQuery,
@ -875,13 +835,11 @@ bool IsEnabled(int postID, MYSQL* con)
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
if (row == NULL) if (row == NULL) return false;
{
return false;
}
int state = atoi(row[5]); int state = atoi(row[5]);
free(selectQuery); free(selectQuery);
return !state; return !state;
} }
@ -889,10 +847,7 @@ bool IsEnabled(int postID, MYSQL* con)
// 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.
int PostIDs(int** arr, MYSQL* con) int PostIDs(int** arr, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts"))
{ {
@ -921,11 +876,13 @@ int PostIDs(int** arr, MYSQL* con)
// Allocate the array that was passed by reference to be one larger than needed // Allocate the array that was passed by reference to be one larger than needed
*arr = (int *)malloc(sizeof(int) * (count + 1)); *arr = (int *)malloc(sizeof(int) * (count + 1));
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i)
{
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
if (row != NULL) { if (row != NULL)
*(*arr + i) = atoi(row[0]); {
*(*arr + i) = atoi(row[0]);
} }
} }
@ -941,10 +898,7 @@ int PostIDs(int** arr, MYSQL* con)
// 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.
int ActivePostIDs(int** arr, MYSQL* con) int ActivePostIDs(int** arr, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)")) if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)"))
{ {
@ -973,11 +927,13 @@ int ActivePostIDs(int** arr, MYSQL* con)
// Allocate the array that was passed by reference to be one larger than needed // Allocate the array that was passed by reference to be one larger than needed
*arr = (int *)malloc(sizeof(int) * (count + 1)); *arr = (int *)malloc(sizeof(int) * (count + 1));
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i)
{
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
if (row != NULL) { if (row != NULL)
*(*arr + i) = atoi(row[0]); {
*(*arr + i) = atoi(row[0]);
} }
} }
@ -993,10 +949,7 @@ int ActivePostIDs(int** arr, MYSQL* con)
// 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.
int AuthorIDs(int** arr, MYSQL* con) int AuthorIDs(int** arr, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
if (mysql_query(con, "SELECT COUNT(*) FROM people")) if (mysql_query(con, "SELECT COUNT(*) FROM people"))
{ {
@ -1025,11 +978,13 @@ int AuthorIDs(int** arr, MYSQL* con)
// Allocate the array that was passed by reference to be one larger than needed // Allocate the array that was passed by reference to be one larger than needed
*arr = (int *)malloc(sizeof(int) * (count + 1)); *arr = (int *)malloc(sizeof(int) * (count + 1));
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i)
{
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
if (row != NULL) { if (row != NULL)
*(*arr + i) = atoi(row[0]); {
*(*arr + i) = atoi(row[0]);
} }
} }