Pull request: 32-clean-db-handler #34

Merged
asdam9 merged 5 commits from 32-clean-db-handler into master 2022-04-16 13:33:10 +00:00
1 changed files with 45 additions and 71 deletions
Showing only changes of commit cfb89f14bf - Show all commits

116
BlogDB.c
View File

@ -90,13 +90,12 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
int postID = atoi(row[0]); 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 AssignTag(tags[i], postID, con);
for (int i = 0; i < tagSize / sizeof(int); i++)
{
AssignTag(tags[i], postID, con);
}
} }
return postID; return postID;
@ -171,16 +170,16 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
FILE* FilePointer; FILE* FilePointer;
int BufferLength = 8192; int BufferLength = 8192;
char Buffer[BufferLength]; char Buffer[BufferLength];
char StringEnd[] = "\0"; char StringEndIndicator[] = "\0";
FilePointer = fopen(filePath, "r"); FilePointer = fopen(filePath, "r");
fgets(Buffer, BufferLength, FilePointer); 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); int LineLength = strlen(Buffer);
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], 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], StringEnd[1]}; char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEndIndicator[0]};
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
{ {
@ -188,20 +187,12 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
return -1; return -1;
} }
// Remove the <h3> tags Buffer[LineLength - 6] = StringEndIndicator[0];
for (int i = 4; i < BufferLength; ++i) char* trimBuffer;
{ trimBuffer = &Buffer[4];
if (Buffer[i + 6] == StringEnd[0])
{
Buffer[i - 4] = StringEnd[0];
break;
}
Buffer[i-4] = Buffer[i];
}
// Add a new post with correct title, author and date but with an empty post. // 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)) while (fgets(Buffer, BufferLength, FilePointer))
{ {
@ -233,7 +224,7 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
FILE* FilePointer; FILE* FilePointer;
int BufferLength = 8192; int BufferLength = 8192;
char Buffer[BufferLength]; char Buffer[BufferLength];
char StringEnd[] = "\0"; char StringEndIndicator[] = "\0";
FilePointer = fopen(filePath, "r"); 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] // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1]
int LineLength = strlen(Buffer); int LineLength = strlen(Buffer);
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], 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], StringEnd[1]}; char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEndIndicator[0]};
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
{ {
@ -250,20 +241,13 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
return -1; return -1;
} }
// Remove the <h3> 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. // Edits post with correct title and empties post.
EditPost(postID, Buffer, "", con); EditPost(postID, Title, "", con);
while (fgets(Buffer, BufferLength, FilePointer)) 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); int num_fields = mysql_num_fields(result);
MYSQL_ROW row; 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++)
{
strings[i] = row[i];
}
for(int i = 0; i < num_fields; i++)
{
strings[i] = row[i];
} }
return postID; return postID;
@ -416,17 +398,15 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date
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) 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]); Titles[i][y] = row[1][y];
*(*authors + i) = atoi(row[2]);
*(*dates + i) = atoi(row[3]);
for (int y = 0; y < 255; ++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(); if (con == NULL) con = conn2maria();
char* insertQuery; 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, firstName,
lastName, lastName,
@ -700,13 +679,12 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
int num_fields = mysql_num_fields(result); int num_fields = mysql_num_fields(result);
MYSQL_ROW row; 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; return authorID;
@ -879,11 +857,9 @@ int PostIDs(int** arr, MYSQL* con)
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) 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, // 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); row = mysql_fetch_row(result2);
if (row != NULL) if (row == NULL) break;
{
*(*arr + i) = atoi(row[0]); *(*arr + i) = atoi(row[0]);
}
} }
// Set the last element in the array passed by reference to -1, // 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); row = mysql_fetch_row(result2);
if (row != NULL) if (row == NULL) break;
{
*(*arr + i) = atoi(row[0]); *(*arr + i) = atoi(row[0]);
}
} }
// Set the last element in the array passed by reference to -1, // 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); //TogglePost(81, con);
//printf("%s\n", IsEnabled(81, con) ? "True" : "False"); //printf("%s\n", IsEnabled(81, con) ? "True" : "False");
//AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con); //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); //BlogWrite(con);
//BlogEdit(69, con); //BlogEdit(69, con);