From 9470fa03e26dc7e6d4dbac831cf8325b85badc9d Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Fri, 15 Apr 2022 23:42:30 +0200 Subject: [PATCH 1/5] Cleaned comments --- BlogDB.c | 267 +++++++++++-------------------------------------------- 1 file changed, 50 insertions(+), 217 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index c33b6e5..5dc239d 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -34,26 +34,21 @@ static int AuthorIDs(int** arr, MYSQL* con); // Connects to the MariaDB database and returns the connection MYSQL* conn2maria() { - // Initialize the connection MYSQL *con = mysql_init(NULL); - // If successful, continue, else throw error if (con == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); exit(1); } - // Connect with specific details, if successful continue, else throw error - if (mysql_real_connect(con, "localhost", "adam", "1234", - "mydb", 0, NULL, 0) == NULL) + if (mysql_real_connect(con, "localhost", "adam", "1234", "mydb", 0, NULL, 0) == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - // return successful connection return con; } @@ -65,7 +60,6 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags con = conn2maria(); } - // Build a query char* query; if(0 > asprintf(&query, "INSERT INTO blog_posts (title,author_id,date_posted,post,state) VALUES (\"%s\", \"%d\", \"%s\", \"%s\", \"0\")", @@ -75,7 +69,6 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags post )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, query)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -83,10 +76,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags exit(1); } - // Free the memory of the query string free(query); - if (mysql_query(con, "SELECT LAST_INSERT_ID()")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -94,29 +85,23 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); - // For all rows (only one ;P) MYSQL_ROW row; row = mysql_fetch_row(result); - int output = atoi(row[0]); + int postID = atoi(row[0]); - // Ignore all if tags is NULL pointer if (tags != NULL) { - //printf("%d\n", tagSize / sizeof(int)); - // for the size of tags + // for each in tags for (int i = 0; i < tagSize / sizeof(int); i++) { - printf("%d\n", i); - - AssignTag(tags[i], output, con); + AssignTag(tags[i], postID, con); } } - return output; + return postID; } // Edits a post in the database, to the new given values @@ -132,46 +117,38 @@ int EditPost(int postID, char title[], char post[], MYSQL* con) "SELECT * FROM blog_posts WHERE id=(%d)", postID )) exit(1); -// Query the server, if successful continue, else throw error + if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); + free(selectQuery); mysql_close(con); exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); if (result == NULL) { - printf("Hey\n"); + free(selectQuery); + exit(1); } - MYSQL_ROW row; - int tagFound = 1; - - if (mysql_fetch_row(result) == NULL) { - printf("Hey4\n"); - tagFound = 0; + free(selectQuery); + exit(1); } free(selectQuery); char* updateQuery; - - if (!tagFound) { return postID; } - - // Build a query if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)", title, post, postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, updateQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -193,47 +170,33 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) con = conn2maria(); } - // Author ID <= 0 should default to me. - if (authorID <= 0) - { - authorID = 1; - } + if (authorID <= 0) { authorID = 1; } - - // Get current data in string date time_t t = time(NULL); struct tm *tm = localtime(&t); char date[64]; assert(strftime(date, sizeof(date), "%F", tm)); - - // Define a file and buffer FILE* FilePointer; int BufferLength = 8192; char Buffer[BufferLength]; char StringEnd[] = "\0"; - // Open file FilePointer = fopen(filePath, "r"); - // Get first line fgets(Buffer, BufferLength, FilePointer); - // Get the substrings Buffer[0:3] and Buffer[-6:-1] - // Buffer[BufferLength] is the same as "\0" but without warnings + // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] int LineLength = strlen(Buffer); 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]}; - // Check that the firstline starts with "

" and ends with "

" if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) { - printf("%s What is this %s\n", startTest, endTest); + printf("Start Test : '%s'\nEnd Test : '%s'\n", startTest, endTest); return -1; } - - // This is equivalent to Buffer = Buffer[3:-6] - // This is done to remove the

tags + // Remove the

tags for (int i = 4; i < BufferLength; ++i) { if (Buffer[i + 6] == StringEnd[0]) { @@ -247,10 +210,8 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) // Add a new post with correct title, author and date but with an empty post. int postID = AddPost(Buffer, authorID, date, "", NULL, 0, con); - // Concat the rest of the file, line by line to the new post while (fgets(Buffer, BufferLength, FilePointer)) { - // Build a query char* query; if(0 > asprintf(&query, "UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)", @@ -258,7 +219,6 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, query)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -266,11 +226,9 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) exit(1); } - // Free the memory of the query string free(query); } - // return the postID of the new post. return postID; } @@ -283,33 +241,26 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) con = conn2maria(); } - // Define a file and buffer FILE* FilePointer; int BufferLength = 8192; char Buffer[BufferLength]; char StringEnd[] = "\0"; - // Open file FilePointer = fopen(filePath, "r"); - // Get first line fgets(Buffer, BufferLength, FilePointer); - // Get the substrings Buffer[0:3] and Buffer[-6:-1] - // Buffer[BufferLength] is the same as "\0" but without warnings + // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] int LineLength = strlen(Buffer); 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]}; - // Check that the firstline starts with "

" and ends with "

" if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) { - printf("'%s' '%s'\n", startTest, endTest); + printf("Start Test : %s\nEnd Test : %s\n", startTest, endTest); return -1; } - - // This is equivalent to Buffer = Buffer[3:-6] - // This is done to remove the

tags + // Remove the

tags for (int i = 4; i < BufferLength; ++i) { if (Buffer[i + 6] == StringEnd[0]) { @@ -320,13 +271,11 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) Buffer[i-4] = Buffer[i]; } - // Edits post with correct title, but with an empty post. + // Edits post with correct title and empties post. EditPost(postID, Buffer, "", con); - // Concat the rest of the file, line by line to the post while (fgets(Buffer, BufferLength, FilePointer)) { - // Build a query char* query; if(0 > asprintf(&query, "UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)", @@ -334,7 +283,6 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, query)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -342,23 +290,18 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) exit(1); } - // Free the memory of the query string free(query); } - // return the postID of the new post. return postID; } -// 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) { FILE *fp; char* tmpName = tmpnam(NULL); - //fp = tmpfile(); - fp = fopen(tmpName, "w+"); fprintf(fp, "

\n

\n"); fclose(fp); @@ -366,22 +309,20 @@ int BlogWrite(MYSQL* con) char* command; if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1); system(command); - AddFilePost(1, tmpName, con); + + int postID = AddFilePost(1, tmpName, con); remove(tmpName); - return 1; + return postID; } -// 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) { FILE *fp; char* tmpName = tmpnam(NULL); - //fp = tmpfile(); - char *result[6]; PullPostData(result, postID, NULL); @@ -397,7 +338,7 @@ int BlogEdit(int postID, MYSQL* con) remove(tmpName); - return 1; + return postID; } // Pulls a single post's data into the supplied array @@ -408,11 +349,9 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) con = conn2maria(); } - // Build a query char* query; if(0 > asprintf(&query, "SELECT * FROM blog_posts WHERE id=(%d)", postID)) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, query)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -420,21 +359,16 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) exit(1); } - // Free the memory of the query string free(query); - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); - // For all rows (only one ;P) MYSQL_ROW row; - while ((row = mysql_fetch_row(result))) + if ((row = mysql_fetch_row(result))) { - // For all fields in row for(int i = 0; i < num_fields; i++) { - // 'return' the output strings[i] = row[i]; } @@ -443,6 +377,7 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) return postID; } +// Pull all coloums of all posts except for the post coloumn int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con) { if (con == NULL) @@ -450,7 +385,6 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date con = conn2maria(); } - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -458,17 +392,14 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); MYSQL_ROW row; row = mysql_fetch_row(result); - // Get the Count int count = atoi(row[0]); free(result); - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT id,title,author_id,date_posted FROM blog_posts WHERE state=0;")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -479,7 +410,7 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date MYSQL_RES *result2 = mysql_store_result(con); // Allocate the array that was passed by reference - // To be one larger than I need. + // To be one larger than needed *IDs = (int *)malloc(sizeof(int) * (count + 1)); *authors = (int *)malloc(sizeof(int) * (count + 1)); *dates = (int *)malloc(sizeof(int) * (count + 1)); @@ -490,19 +421,10 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date } - //*Titles = (char *)malloc(((count + 1) * 255) * sizeof(char)); - - - // 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]); - *(*IDs + i) = atoi(row[0]); *(*authors + i) = atoi(row[2]); *(*dates + i) = atoi(row[3]); @@ -519,16 +441,14 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date // Enters unknown memory. //*(*arr + count) = -1; *(*IDs + count) = -1; - *(*authors + count) = -1; *(*dates + count) = -1; *(*Titles + count) = ""; - // Return the count, for easy looping. return count; } -// Adds a tag +// Adds a new tag int AddTag(char tagTitle[], MYSQL* con) { if (con == NULL) @@ -537,13 +457,10 @@ int AddTag(char tagTitle[], MYSQL* con) } char* insertQuery; - - // Build a query if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")", tagTitle )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, insertQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -560,16 +477,14 @@ int AddTag(char tagTitle[], MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); - // For all rows (only one ;P) MYSQL_ROW row; row = mysql_fetch_row(result); - int output = atoi(row[0]); + int tagID = atoi(row[0]); - return output; + return tagID; } // Removes a tag, and unassigns itself from any and all posts. @@ -581,13 +496,10 @@ int DeleteTag(int tagID, MYSQL* con) } char* deleteQuery; - - // Build a query if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)", tagID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, deleteQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -598,13 +510,10 @@ int DeleteTag(int tagID, MYSQL* con) free(deleteQuery); char* deleteTagsQuery; - - // Build a query if (0 > asprintf(&deleteTagsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", tagID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, deleteTagsQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -632,7 +541,6 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con) tagID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -640,32 +548,25 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); MYSQL_ROW row; - int tagFound = 1; - if (mysql_fetch_row(result) == NULL) { - tagFound = 0; + free(selectQuery); + return tagID; } free(selectQuery); char* updateQuery; - - if (!tagFound) { return tagID; } - - // Build a query if (0 > asprintf(&updateQuery, "UPDATE tags SET name=(\"%s\") WHERE id=(%d)", tagTitle, tagID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, updateQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -687,13 +588,11 @@ int AssignTag(int tagID, int postID, MYSQL* con) } 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)", tagID, postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -701,34 +600,29 @@ int AssignTag(int tagID, int postID, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); if (result == NULL) { - printf("Hey\n"); + return postID; } MYSQL_ROW row; - int tagFound = 1; - if (mysql_fetch_row(result) == NULL) { - printf("Hey4\n"); - tagFound = 0; + free(selectQuery); + return postID; } free(selectQuery); char* insertQuery; - - if (tagFound) { return postID; } - - // Build a query - if (0 > asprintf(&insertQuery, "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)", tagID, postID )) exit(1); + if (0 > asprintf(&insertQuery, "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)", + tagID, + postID + )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, insertQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -750,14 +644,11 @@ int RetractTag(int tagID, int postID, MYSQL* con) } char* deleteQuery; - - // Build a query if (0 > asprintf(&deleteQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", tagID, postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, deleteQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -780,14 +671,12 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) char* insertQuery; - // Build a query if (0 > asprintf(&insertQuery, "INSERT INTO people (first_name, last_name, email) VALUES (\"%s\", \"%s\", \"%s\")", firstName, lastName, email )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, insertQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -804,16 +693,14 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); - // For all rows (only one ;P) MYSQL_ROW row; row = mysql_fetch_row(result); - int output = atoi(row[0]); + int authorID = atoi(row[0]); - return output; + return authorID; } // Pull the full name and email of the given author ID @@ -824,11 +711,9 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con) con = conn2maria(); } - // Build a query char* query; if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, query)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -836,21 +721,16 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con) exit(1); } - // Free the memory of the query string free(query); - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); - // For all rows (only one ;P) MYSQL_ROW row; - while ((row = mysql_fetch_row(result))) + if ((row = mysql_fetch_row(result))) { - // For all fields in row for(int i = 0; i < num_fields; i++) { - // 'return' the output strings[i] = row[i]; } @@ -874,7 +754,6 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con) postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -882,32 +761,26 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); MYSQL_ROW row; - int postFound = 1; - if (mysql_fetch_row(result) == NULL) { - postFound = 0; + free(selectQuery); + return postID; } free(selectQuery); char* updateQuery; - if (!postFound) { return postID; } - - // Build a query if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)", authorID, postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, updateQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -934,7 +807,6 @@ int TogglePost(int postID, MYSQL* con) postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -942,7 +814,6 @@ int TogglePost(int postID, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); @@ -958,14 +829,11 @@ int TogglePost(int postID, MYSQL* con) free(selectQuery); char* updateQuery; - - // Build a query if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET state=(%d) WHERE id=(%d)", !state, postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, updateQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -992,7 +860,6 @@ bool IsEnabled(int postID, MYSQL* con) postID )) exit(1); - // Query the server, if successful continue, else throw error if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1000,7 +867,6 @@ bool IsEnabled(int postID, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); @@ -1021,7 +887,6 @@ bool IsEnabled(int postID, MYSQL* con) // 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(int** arr, MYSQL* con) { if (con == NULL) @@ -1029,7 +894,6 @@ int PostIDs(int** arr, MYSQL* con) con = conn2maria(); } - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1037,17 +901,14 @@ int PostIDs(int** arr, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); MYSQL_ROW row; row = mysql_fetch_row(result); - // Get the Count int count = atoi(row[0]); free(result); - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT id FROM blog_posts")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1057,18 +918,13 @@ 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. + // Allocate the array that was passed by reference to be one larger than needed *arr = (int *)malloc(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]); } } @@ -1078,13 +934,11 @@ int PostIDs(int** arr, MYSQL* con) // Enters unknown memory. *(*arr + count) = -1; - // Return the count, for easy looping. return count; } // 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(int** arr, MYSQL* con) { if (con == NULL) @@ -1092,7 +946,6 @@ int ActivePostIDs(int** arr, MYSQL* con) con = conn2maria(); } - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1100,17 +953,14 @@ int ActivePostIDs(int** arr, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); MYSQL_ROW row; row = mysql_fetch_row(result); - // Get the Count int count = atoi(row[0]); free(result); - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT id FROM blog_posts WHERE state=(0)")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1120,18 +970,13 @@ 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. + // Allocate the array that was passed by reference to be one larger than needed *arr = (int *)malloc(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]); } } @@ -1141,13 +986,11 @@ int ActivePostIDs(int** arr, MYSQL* con) // Enters unknown memory. *(*arr + count) = -1; - // Return the count, for easy looping. return count; } // 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(int** arr, MYSQL* con) { if (con == NULL) @@ -1155,7 +998,6 @@ int AuthorIDs(int** arr, MYSQL* con) con = conn2maria(); } - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT COUNT(*) FROM people")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1163,17 +1005,14 @@ int AuthorIDs(int** arr, MYSQL* con) exit(1); } - // Get results MYSQL_RES *result = mysql_store_result(con); MYSQL_ROW row; row = mysql_fetch_row(result); - // Get the Count int count = atoi(row[0]); free(result); - // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT id FROM people")) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -1183,18 +1022,13 @@ 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. + // Allocate the array that was passed by reference to be one larger than needed *arr = (int *)malloc(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]); } } @@ -1204,7 +1038,6 @@ int AuthorIDs(int** arr, MYSQL* con) // Enters unknown memory. *(*arr + count) = -1; - // Return the count, for easy looping. return count; } @@ -1341,7 +1174,7 @@ int main(int argc, char *argv[]) free(dates); // =================================== - */ + */ // Open the connection MYSQL *con = conn2maria(); -- 2.30.2 From 6469b8ecfb7a1750eae276a99306dbecbb98b090 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 16 Apr 2022 00:23:38 +0200 Subject: [PATCH 2/5] Cleaned brackets and spacing --- BlogDB.c | 177 +++++++++++++++++++++---------------------------------- 1 file changed, 66 insertions(+), 111 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 5dc239d..41802fb 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -31,6 +31,7 @@ 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() { @@ -55,10 +56,7 @@ MYSQL* conn2maria() // 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) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* 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 int EditPost(int postID, char title[], char post[], MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* 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); int num_fields = mysql_num_fields(result); - if (result == NULL) { + if (result == NULL) + { free(selectQuery); exit(1); } @@ -164,13 +160,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con) // Adds a post using file data int AddFilePost(int authorID, char filePath[], MYSQL* con) { - - if (con == NULL) - { - con = conn2maria(); - } - - if (authorID <= 0) { authorID = 1; } + if (con == NULL) con = conn2maria(); + if (authorID <= 0) authorID = 1; time_t t = time(NULL); 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 endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]}; - if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) { + if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) + { printf("Start Test : '%s'\nEnd Test : '%s'\n", startTest, endTest); return -1; } // Remove the

tags - for (int i = 4; i < BufferLength; ++i) { - - if (Buffer[i + 6] == StringEnd[0]) { + for (int i = 4; i < BufferLength; ++i) + { + if (Buffer[i + 6] == StringEnd[0]) + { Buffer[i - 4] = StringEnd[0]; break; } @@ -235,11 +228,7 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) // Edits a post using file data int EditFilePost(char filePath[], int postID, MYSQL* con) { - - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); FILE* FilePointer; 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 endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]}; - if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) { + if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) + { printf("Start Test : %s\nEnd Test : %s\n", startTest, endTest); return -1; } // Remove the

tags - for (int i = 4; i < BufferLength; ++i) { - - if (Buffer[i + 6] == StringEnd[0]) { + for (int i = 4; i < BufferLength; ++i) + { + if (Buffer[i + 6] == StringEnd[0]) + { Buffer[i - 4] = StringEnd[0]; 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 int BlogWrite(MYSQL* con) { + if (con == NULL) con = conn2maria(); + FILE *fp; 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 int BlogEdit(int postID, MYSQL* con) { + if (con == NULL) con = conn2maria(); + FILE *fp; char* tmpName = tmpnam(NULL); @@ -344,10 +339,7 @@ int BlogEdit(int postID, MYSQL* con) // Pulls a single post's data into the supplied array int PullPostData(char* strings[6], int postID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* query; 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)); *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); } - - for (int i = 0; i < count; ++i) { + for (int i = 0; i < count; ++i) + { row = mysql_fetch_row(result2); - if (row != NULL) { + if (row != NULL) + { *(*IDs + i) = atoi(row[0]); *(*authors + i) = atoi(row[2]); *(*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]; } - } } @@ -451,10 +445,7 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date // Adds a new tag int AddTag(char tagTitle[], MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* insertQuery; 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. int DeleteTag(int tagID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* deleteQuery; if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)", @@ -529,11 +517,7 @@ int DeleteTag(int tagID, MYSQL* con) // Edit the tag name int EditTag(int tagID, char tagTitle[], MYSQL* con) { - - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* selectQuery; if(0 > asprintf(&selectQuery, @@ -582,10 +566,7 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con) // Assign a post a given tag. int AssignTag(int tagID, int postID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* selectQuery; 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); int num_fields = mysql_num_fields(result); - if (result == NULL) { + if (result == NULL) + { return postID; } @@ -638,10 +620,7 @@ int AssignTag(int tagID, int postID, MYSQL* con) // Unassign a tag from the given post int RetractTag(int tagID, int postID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* deleteQuery; 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 int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); 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 int PullAuthorData(char* strings[4], int authorID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* query; 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 int ChangeAuthor(int postID, int authorID, MYSQL* con) { - - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* 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. int TogglePost(int postID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* selectQuery; if(0 > asprintf(&selectQuery, @@ -820,10 +786,7 @@ int TogglePost(int postID, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); - if (row == NULL) - { - return postID; - } + if (row == NULL) return postID; int state = atoi(row[5]); 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 bool IsEnabled(int postID, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); char* selectQuery; if(0 > asprintf(&selectQuery, @@ -875,13 +835,11 @@ bool IsEnabled(int postID, MYSQL* con) row = mysql_fetch_row(result); - if (row == NULL) - { - return false; - } + if (row == NULL) return false; int state = atoi(row[5]); free(selectQuery); + 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. int PostIDs(int** arr, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); 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 *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); - if (row != NULL) { - *(*arr + i) = atoi(row[0]); + if (row != NULL) + { + *(*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. int ActivePostIDs(int** arr, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); 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 *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); - if (row != NULL) { - *(*arr + i) = atoi(row[0]); + if (row != NULL) + { + *(*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. int AuthorIDs(int** arr, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); 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 *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); - if (row != NULL) { - *(*arr + i) = atoi(row[0]); + if (row != NULL) + { + *(*arr + i) = atoi(row[0]); } } -- 2.30.2 From cfb89f14bfb7f0ca13a5a6aa5ffcd076c4a2acf2 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 16 Apr 2022 13:45:41 +0200 Subject: [PATCH 3/5] Cleaned indentation and optimized trim --- BlogDB.c | 116 +++++++++++++++++++++---------------------------------- 1 file changed, 45 insertions(+), 71 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 41802fb..d1d94c6 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -90,13 +90,12 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags row = mysql_fetch_row(result); int postID = atoi(row[0]); - if (tags != NULL) + if (tags == NULL) return postID; + + // for each in tags + for (int i = 0; i < tagSize / sizeof(int); i++) { - // for each in tags - for (int i = 0; i < tagSize / sizeof(int); i++) - { - AssignTag(tags[i], postID, con); - } + AssignTag(tags[i], postID, con); } return postID; @@ -171,16 +170,16 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) FILE* FilePointer; int BufferLength = 8192; char Buffer[BufferLength]; - char StringEnd[] = "\0"; + char StringEndIndicator[] = "\0"; FilePointer = fopen(filePath, "r"); fgets(Buffer, BufferLength, FilePointer); - // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] + // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-5:-1] int LineLength = strlen(Buffer); - 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 startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEndIndicator[0]}; + char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEndIndicator[0]}; if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) { @@ -188,20 +187,12 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) return -1; } - // Remove the

tags - for (int i = 4; i < BufferLength; ++i) - { - if (Buffer[i + 6] == StringEnd[0]) - { - Buffer[i - 4] = StringEnd[0]; - break; - } - - Buffer[i-4] = Buffer[i]; - } + Buffer[LineLength - 6] = StringEndIndicator[0]; + char* trimBuffer; + trimBuffer = &Buffer[4]; // Add a new post with correct title, author and date but with an empty post. - int postID = AddPost(Buffer, authorID, date, "", NULL, 0, con); + int postID = AddPost(trimBuffer, authorID, date, "", NULL, 0, con); while (fgets(Buffer, BufferLength, FilePointer)) { @@ -233,7 +224,7 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) FILE* FilePointer; int BufferLength = 8192; char Buffer[BufferLength]; - char StringEnd[] = "\0"; + char StringEndIndicator[] = "\0"; FilePointer = fopen(filePath, "r"); @@ -241,8 +232,8 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] int LineLength = strlen(Buffer); - 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 startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEndIndicator[0]}; + char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEndIndicator[0]}; if (!(strcmp(startTest, "

") == 0 && strcmp(endTest, "

") == 0)) { @@ -250,20 +241,13 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) return -1; } - // Remove the

tags - for (int i = 4; i < BufferLength; ++i) - { - if (Buffer[i + 6] == StringEnd[0]) - { - Buffer[i - 4] = StringEnd[0]; - break; - } - Buffer[i-4] = Buffer[i]; - } + Buffer[LineLength - 6] = StringEndIndicator[0]; + char* Title; + Title = &Buffer[4]; // Edits post with correct title and empties post. - EditPost(postID, Buffer, "", con); + EditPost(postID, Title, "", con); while (fgets(Buffer, BufferLength, FilePointer)) { @@ -357,13 +341,11 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) int num_fields = mysql_num_fields(result); MYSQL_ROW row; - if ((row = mysql_fetch_row(result))) + if (!(row = mysql_fetch_row(result))) return postID; + + for(int i = 0; i < num_fields; i++) { - for(int i = 0; i < num_fields; i++) - { - strings[i] = row[i]; - } - + strings[i] = row[i]; } return postID; @@ -416,17 +398,15 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); + if (row == NULL) break; - if (row != NULL) + *(*IDs + i) = atoi(row[0]); + *(*authors + i) = atoi(row[2]); + *(*dates + i) = atoi(row[3]); + + for (int y = 0; y < 255; ++y) { - *(*IDs + i) = atoi(row[0]); - *(*authors + i) = atoi(row[2]); - *(*dates + i) = atoi(row[3]); - - for (int y = 0; y < 255; ++y) - { - Titles[i][y] = row[1][y]; - } + Titles[i][y] = row[1][y]; } } @@ -646,7 +626,6 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) if (con == NULL) con = conn2maria(); char* insertQuery; - if (0 > asprintf(&insertQuery, "INSERT INTO people (first_name, last_name, email) VALUES (\"%s\", \"%s\", \"%s\")", firstName, lastName, @@ -700,13 +679,12 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con) int num_fields = mysql_num_fields(result); MYSQL_ROW row; - if ((row = mysql_fetch_row(result))) - { - for(int i = 0; i < num_fields; i++) - { - strings[i] = row[i]; - } + if (!(row = mysql_fetch_row(result))) return authorID; + + for(int i = 0; i < num_fields; i++) + { + strings[i] = row[i]; } return authorID; @@ -879,11 +857,9 @@ int PostIDs(int** arr, MYSQL* con) for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); + if (row == NULL) break; - if (row != NULL) - { - *(*arr + i) = atoi(row[0]); - } + *(*arr + i) = atoi(row[0]); } // Set the last element in the array passed by reference to -1, @@ -931,10 +907,9 @@ int ActivePostIDs(int** arr, MYSQL* con) { row = mysql_fetch_row(result2); - if (row != NULL) - { - *(*arr + i) = atoi(row[0]); - } + if (row == NULL) break; + + *(*arr + i) = atoi(row[0]); } // Set the last element in the array passed by reference to -1, @@ -982,10 +957,9 @@ int AuthorIDs(int** arr, MYSQL* con) { row = mysql_fetch_row(result2); - if (row != NULL) - { - *(*arr + i) = atoi(row[0]); - } + if (row == NULL) break; + + *(*arr + i) = atoi(row[0]); } // Set the last element in the array passed by reference to -1, @@ -1150,7 +1124,7 @@ int main(int argc, char *argv[]) //TogglePost(81, con); //printf("%s\n", IsEnabled(81, con) ? "True" : "False"); //AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con); - //EditFilePost("/home/adam/Documents/html/linuxenv.html", 69, con); + //EditFilePost("/home/adam/Documents/html/linuxenv.html", 1, con); //BlogWrite(con); //BlogEdit(69, con); -- 2.30.2 From dfe3466ea58a073b79de7864fdf8a1fc9aa56405 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 16 Apr 2022 15:26:54 +0200 Subject: [PATCH 4/5] Fixed Consistency --- BlogDB.c | 89 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index d1d94c6..a371103 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -58,8 +58,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags { if (con == NULL) con = conn2maria(); - char* query; - if(0 > asprintf(&query, + char* insertQuery; + if(0 > asprintf(&insertQuery, "INSERT INTO blog_posts (title,author_id,date_posted,post,state) VALUES (\"%s\", \"%d\", \"%s\", \"%s\", \"0\")", title, authorId, @@ -67,14 +67,14 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags post )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, insertQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(insertQuery); if (mysql_query(con, "SELECT LAST_INSERT_ID()")) { @@ -83,10 +83,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags exit(1); } - MYSQL_RES *result = mysql_store_result(con); - int num_fields = mysql_num_fields(result); - MYSQL_ROW row; + MYSQL_RES *result = mysql_store_result(con); row = mysql_fetch_row(result); int postID = atoi(row[0]); @@ -138,7 +136,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con) free(selectQuery); char* updateQuery; - if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)", title, post, postID @@ -173,7 +172,6 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) char StringEndIndicator[] = "\0"; FilePointer = fopen(filePath, "r"); - fgets(Buffer, BufferLength, FilePointer); // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-5:-1] @@ -188,29 +186,29 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) } Buffer[LineLength - 6] = StringEndIndicator[0]; - char* trimBuffer; - trimBuffer = &Buffer[4]; + char* trimmedBufferTitle; + trimmedBufferTitle = &Buffer[4]; // Add a new post with correct title, author and date but with an empty post. - int postID = AddPost(trimBuffer, authorID, date, "", NULL, 0, con); + int postID = AddPost(trimmedBufferTitle, authorID, date, "", NULL, 0, con); while (fgets(Buffer, BufferLength, FilePointer)) { - char* query; - if(0 > asprintf(&query, + char* concatQuery; + if(0 > asprintf(&concatQuery, "UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)", Buffer, postID )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, concatQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(concatQuery); } return postID; @@ -227,7 +225,6 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) char StringEndIndicator[] = "\0"; FilePointer = fopen(filePath, "r"); - fgets(Buffer, BufferLength, FilePointer); // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] @@ -243,29 +240,29 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) Buffer[LineLength - 6] = StringEndIndicator[0]; - char* Title; - Title = &Buffer[4]; + char* trimmedBufferTitle; + trimmedBufferTitle = &Buffer[4]; // Edits post with correct title and empties post. - EditPost(postID, Title, "", con); + EditPost(postID, trimmedBufferTitle, "", con); while (fgets(Buffer, BufferLength, FilePointer)) { - char* query; - if(0 > asprintf(&query, + char* concatQuery; + if(0 > asprintf(&concatQuery, "UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)", Buffer, postID )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, concatQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(concatQuery); } return postID; @@ -284,7 +281,10 @@ int BlogWrite(MYSQL* con) fclose(fp); char* command; - if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1); + if(0 > asprintf(&command, + "${EDITOR:-'vi'} %s", + tmpName + )) exit(1); system(command); int postID = AddFilePost(1, tmpName, con); @@ -354,10 +354,7 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) // Pull all coloums of all posts except for the post coloumn int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) { @@ -463,33 +460,33 @@ int DeleteTag(int tagID, MYSQL* con) { if (con == NULL) con = conn2maria(); - char* deleteQuery; - if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)", + char* deleteTagQuery; + if (0 > asprintf(&deleteTagQuery, "DELETE FROM tags WHERE id=(%d)", tagID )) exit(1); - if (mysql_query(con, deleteQuery)) + if (mysql_query(con, deleteTagQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(deleteQuery); + free(deleteTagQuery); - char* deleteTagsQuery; - if (0 > asprintf(&deleteTagsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", + char* deleteTagAssignmentsQuery; + if (0 > asprintf(&deleteTagAssignmentsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", tagID )) exit(1); - if (mysql_query(con, deleteTagsQuery)) + if (mysql_query(con, deleteTagAssignmentsQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(deleteTagsQuery); + free(deleteTagAssignmentsQuery); return tagID; } @@ -663,17 +660,20 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con) { if (con == NULL) con = conn2maria(); - char* query; - if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1); + char* selectQuery; + if(0 > asprintf(&selectQuery, + "SELECT * FROM people WHERE id=(%d)", + authorID + )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(selectQuery); MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); @@ -722,8 +722,8 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con) free(selectQuery); char* updateQuery; - - if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)", authorID, postID )) exit(1); @@ -770,7 +770,8 @@ int TogglePost(int postID, MYSQL* con) free(selectQuery); char* updateQuery; - if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET state=(%d) WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE blog_posts SET state=(%d) WHERE id=(%d)", !state, postID )) exit(1); -- 2.30.2 From e761269ee9740f0669ad2b6dd10888b95ad09306 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 16 Apr 2022 15:30:17 +0200 Subject: [PATCH 5/5] Moved 'main' function to examples.md --- .gitignore | 1 - BlogDB.c | 200 +++++++--------------------------------------------- examples.md | 164 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 175 deletions(-) create mode 100644 examples.md diff --git a/.gitignore b/.gitignore index b5e3ae7..b776309 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,3 @@ modules.order Module.symvers Mkfile.old dkms.conf - diff --git a/BlogDB.c b/BlogDB.c index a371103..8364a24 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -310,7 +310,10 @@ int BlogEdit(int postID, MYSQL* con) fclose(fp); char* command; - if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1); + if(0 > asprintf(&command, + "${EDITOR:-'vi'} %s", + tmpName + )) exit(1); system(command); EditFilePost(tmpName, postID, con); @@ -326,7 +329,10 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) if (con == NULL) con = conn2maria(); 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); if (mysql_query(con, query)) { @@ -425,7 +431,8 @@ int AddTag(char tagTitle[], MYSQL* con) if (con == NULL) con = conn2maria(); char* insertQuery; - if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")", + if (0 > asprintf(&insertQuery, + "INSERT INTO tags (name) VALUES (\"%s\")", tagTitle )) exit(1); @@ -461,7 +468,8 @@ int DeleteTag(int tagID, MYSQL* con) if (con == NULL) con = conn2maria(); char* deleteTagQuery; - if (0 > asprintf(&deleteTagQuery, "DELETE FROM tags WHERE id=(%d)", + if (0 > asprintf(&deleteTagQuery, + "DELETE FROM tags WHERE id=(%d)", tagID )) exit(1); @@ -475,7 +483,8 @@ int DeleteTag(int tagID, MYSQL* con) free(deleteTagQuery); char* deleteTagAssignmentsQuery; - if (0 > asprintf(&deleteTagAssignmentsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", + if (0 > asprintf(&deleteTagAssignmentsQuery, + "DELETE FROM blog_post_tags WHERE tag_id=(%d)", tagID )) exit(1); @@ -523,7 +532,8 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con) free(selectQuery); char* updateQuery; - if (0 > asprintf(&updateQuery, "UPDATE tags SET name=(\"%s\") WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE tags SET name=(\"%s\") WHERE id=(%d)", tagTitle, tagID )) exit(1); @@ -546,7 +556,8 @@ int AssignTag(int tagID, int postID, MYSQL* con) if (con == NULL) con = conn2maria(); 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)", tagID, postID )) exit(1); @@ -577,9 +588,10 @@ int AssignTag(int tagID, int postID, MYSQL* con) free(selectQuery); char* insertQuery; - if (0 > asprintf(&insertQuery, "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)", - tagID, - postID + if (0 > asprintf(&insertQuery, + "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)", + tagID, + postID )) exit(1); if (mysql_query(con, insertQuery)) @@ -600,7 +612,8 @@ int RetractTag(int tagID, int postID, MYSQL* con) if (con == NULL) con = conn2maria(); 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)", tagID, postID )) exit(1); @@ -623,7 +636,8 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) if (con == NULL) con = conn2maria(); char* insertQuery; - if (0 > asprintf(&insertQuery, "INSERT INTO people (first_name, last_name, email) VALUES (\"%s\", \"%s\", \"%s\")", + if (0 > asprintf(&insertQuery, + "INSERT INTO people (first_name, last_name, email) VALUES (\"%s\", \"%s\", \"%s\")", firstName, lastName, email @@ -971,165 +985,3 @@ int AuthorIDs(int** arr, MYSQL* con) return count; } -// Main -int main(int argc, char *argv[]) -{ - /* - // =================================== - // Example of using 'PullPostData()' - // =================================== - - // Init array of six strings - // Pull data of postID '1' into 'result' - char *result[6]; - PullPostData(result, 42, NULL); - - // Foreach in result - for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) - { - // Print contents on a new line - printf("%s\n", result[i]); - } - - // =================================== - */ - - /* - // =================================== - // Example of using 'PullAuthorData()' - // =================================== - - // Init array of four strings - // Pull data of authorID '1' into 'result' - char *result[4]; - PullAuthorData(result, 1, NULL); - - // Foreach in result - for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) - { - // Print contents on a new line - printf("%s\n", result[i]); - } - // =================================== - */ - - /* - // =================================== - // 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()' - // =================================== - - // Generate array of IDs - int *activePostIDs; - int count2 = ActivePostIDs(&activePostIDs, 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", activePostIDs[i]); - } - - // Free the memory - free(activePostIDs); - - // =================================== - */ - - /* - // =================================== - // Example of using 'AuthorIDs()' - // =================================== - - // Generate array of IDs - int *authorIDs; - int count3 = AuthorIDs(&authorIDs, NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; i < count3; ++i) - { - // Print contents on a new line - printf("%d\n", authorIDs[i]); - } - - // Free the memory - free(authorIDs); - - // =================================== - */ - - /* - // =================================== - // Example of using 'PullPostDescriptions()' - // =================================== - - // Generate array of IDs - int *IDs; - int *authors; - int *dates; - char *Titles[255]; - int count4 = PullPostDescriptions(&IDs, &Titles, &authors, &dates, NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; i < count4; ++i) - { - // Print contents on a new line - printf("%d %s %d %d\n", IDs[i], Titles[i], authors[i], dates[i]); - } - - // Free the memory - free(IDs); - free(authors); - free(dates); - - // =================================== - */ - - // Open the connection - MYSQL *con = conn2maria(); - - // AddPost return the inserted rows, NULL pointer for no tags, 0 for size - // - //int tags[] = {1, 2, 3}; - //printf("%d", AddPost("Something", 2, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags), con)); - - //AssignTag(6, 42, con); - //EditPost(69, "HEY", "THERE BUDDY", con); - //printf("%d\n", AddTag("Test", con)); - //DeleteTag(7, con); - //RetractTag(1, 1, con); - //EditTag(5, "Projects", con); - //AddAuthor("Me", "Meson", "email@me.com", con); - //ChangeAuthor(93, 1, con); - //TogglePost(81, con); - //printf("%s\n", IsEnabled(81, con) ? "True" : "False"); - //AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con); - //EditFilePost("/home/adam/Documents/html/linuxenv.html", 1, con); - //BlogWrite(con); - //BlogEdit(69, con); - - // ================================= - - exit(0); -} diff --git a/examples.md b/examples.md new file mode 100644 index 0000000..ed54f18 --- /dev/null +++ b/examples.md @@ -0,0 +1,164 @@ +```c +// Main +int main(int argc, char *argv[]) +{ + /* + // =================================== + // Example of using 'PullPostData()' + // =================================== + + // Init array of six strings + // Pull data of postID '1' into 'result' + char *result[6]; + PullPostData(result, 42, NULL); + + // Foreach in result + for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) + { + // Print contents on a new line + printf("%s\n", result[i]); + } + + // =================================== + */ + + /* + // =================================== + // Example of using 'PullAuthorData()' + // =================================== + + // Init array of four strings + // Pull data of authorID '1' into 'result' + char *result[4]; + PullAuthorData(result, 1, NULL); + + // Foreach in result + for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) + { + // Print contents on a new line + printf("%s\n", result[i]); + } + // =================================== + */ + + /* + // =================================== + // 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()' + // =================================== + + // Generate array of IDs + int *activePostIDs; + int count2 = ActivePostIDs(&activePostIDs, 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", activePostIDs[i]); + } + + // Free the memory + free(activePostIDs); + + // =================================== + */ + + /* + // =================================== + // Example of using 'AuthorIDs()' + // =================================== + + // Generate array of IDs + int *authorIDs; + int count3 = AuthorIDs(&authorIDs, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count3; ++i) + { + // Print contents on a new line + printf("%d\n", authorIDs[i]); + } + + // Free the memory + free(authorIDs); + + // =================================== + */ + + /* + // =================================== + // Example of using 'PullPostDescriptions()' + // =================================== + + // Generate array of IDs + int *IDs; + int *authors; + int *dates; + char *Titles[255]; + int count4 = PullPostDescriptions(&IDs, &Titles, &authors, &dates, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count4; ++i) + { + // Print contents on a new line + printf("%d %s %d %d\n", IDs[i], Titles[i], authors[i], dates[i]); + } + + // Free the memory + free(IDs); + free(authors); + free(dates); + + // =================================== + */ + + // Open the connection + MYSQL *con = conn2maria(); + + // AddPost return the inserted rows, NULL pointer for no tags, 0 for size + // + //int tags[] = {1, 2, 3}; + //printf("%d", AddPost("Something", 2, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags), con)); + + //AssignTag(6, 42, con); + //EditPost(69, "HEY", "THERE BUDDY", con); + //printf("%d\n", AddTag("Test", con)); + //DeleteTag(7, con); + //RetractTag(1, 1, con); + //EditTag(5, "Projects", con); + //AddAuthor("Me", "Meson", "email@me.com", con); + //ChangeAuthor(93, 1, con); + //TogglePost(81, con); + //printf("%s\n", IsEnabled(81, con) ? "True" : "False"); + //AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con); + //EditFilePost("/home/adam/Documents/html/linuxenv.html", 1, con); + //BlogWrite(con); + //BlogEdit(69, con); + + // ================================= + + exit(0); +} +``` -- 2.30.2