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