Merge pull request ''EditFilePost' function' (#30) from 29-EditFilePost into master

Reviewed-on: #30
This commit is contained in:
Adam Fordsmand 2022-04-15 16:39:26 +00:00
commit 0722c62814
1 changed files with 85 additions and 1 deletions

View File

@ -12,6 +12,7 @@ static MYSQL* conn2maria();
static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con); static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con);
static int EditPost(int postID, char title[], char post[], MYSQL* con); static int EditPost(int postID, char title[], char post[], MYSQL* con);
static int AddFilePost(int authorID, char filePath[], MYSQL* con); static int AddFilePost(int authorID, char filePath[], MYSQL* con);
static int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con);
static int BlogWrite(MYSQL* con); static int BlogWrite(MYSQL* con);
static int PullPostData(char* strings[6], int postID, MYSQL* con); static int PullPostData(char* strings[6], int postID, MYSQL* con);
static int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con); static int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con);
@ -272,6 +273,88 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
return postID; return postID;
} }
// Edits a post using file data
int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con)
{
if (con == NULL)
{
con = conn2maria();
}
// Author ID <= 0 should default to me.
if (authorID <= 0)
{
authorID = 1;
}
// Define a file and buffer
FILE* FilePointer;
int BufferLength = 255;
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
int LineLength = strlen(Buffer);
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], Buffer[BufferLength]};
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], Buffer[BufferLength]};
// Check that the firstline starts with "<h3>" and ends with "</h3>"
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) {
printf("%s What is this %s\n", startTest, endTest);
return -1;
}
// This is equivalent to Buffer = Buffer[3:-6]
// This is done to 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];
}
// Edits post with correct title, but with an empty 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\n\") WHERE id=(%d)",
Buffer,
postID
)) exit(1);
// Query the server, if successful continue, else throw error
if (mysql_query(con, query))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(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, // Open a temporary file containing the standard template,
// and add the contents as a post // and add the contents as a post
int BlogWrite(MYSQL* con) int BlogWrite(MYSQL* con)
@ -1114,7 +1197,7 @@ int main(int argc, char *argv[])
// Init array of six strings // Init array of six strings
// Pull data of postID '1' into 'result' // Pull data of postID '1' into 'result'
char *result[6]; char *result[6];
PullPostData(result, 1, NULL); PullPostData(result, 42, NULL);
// Foreach in result // Foreach in result
for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++)
@ -1257,6 +1340,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(1, "/home/adam/Documents/html/linuxenv.html", 42, con);
//BlogWrite(con); //BlogWrite(con);
// ================================= // =================================