Pull request: 32-clean-db-handler #34
84
BlogDB.c
84
BlogDB.c
|
|
@ -90,14 +90,13 @@ 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 each in tags
|
||||||
for (int i = 0; i < tagSize / sizeof(int); i++)
|
for (int i = 0; i < tagSize / sizeof(int); i++)
|
||||||
{
|
{
|
||||||
AssignTag(tags[i], postID, con);
|
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,15 +341,13 @@ 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++)
|
for(int i = 0; i < num_fields; i++)
|
||||||
{
|
{
|
||||||
strings[i] = row[i];
|
strings[i] = row[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return postID;
|
return postID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -416,9 +398,8 @@ 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]);
|
*(*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]);
|
||||||
|
|
@ -428,7 +409,6 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date
|
||||||
Titles[i][y] = row[1][y];
|
Titles[i][y] = row[1][y];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Set the last element in the array passed by reference to -1,
|
// Set the last element in the array passed by reference to -1,
|
||||||
// As an ID can never be negative, it can ensure that it never
|
// As an ID can never be negative, it can ensure that it never
|
||||||
|
|
@ -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,15 +679,14 @@ 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)))
|
|
||||||
{
|
if (!(row = mysql_fetch_row(result))) return authorID;
|
||||||
|
|
||||||
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 authorID;
|
return authorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -879,12 +857,10 @@ 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,
|
||||||
// As an ID can never be negative, it can ensure that it never
|
// As an ID can never be negative, it can ensure that it never
|
||||||
|
|
@ -931,11 +907,10 @@ 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,
|
||||||
// As an ID can never be negative, it can ensure that it never
|
// As an ID can never be negative, it can ensure that it never
|
||||||
|
|
@ -982,11 +957,10 @@ 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,
|
||||||
// As an ID can never be negative, it can ensure that it never
|
// As an ID can never be negative, it can ensure that it never
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue