diff --git a/BlogDB.c b/BlogDB.c index 51c20d3..bdb623d 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -12,8 +12,9 @@ static MYSQL* conn2maria(); 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 AddFilePost(int authorID, char filePath[], MYSQL* con); -static int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con); +static int EditFilePost(char filePath[], int postID, MYSQL* con); static int BlogWrite(MYSQL* con); +static int BlogEdit(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 AddTag(char tagTitle[], MYSQL* con); @@ -208,7 +209,7 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) // Define a file and buffer FILE* FilePointer; - int BufferLength = 255; + int BufferLength = 8192; char Buffer[BufferLength]; char StringEnd[] = "\0"; @@ -221,8 +222,8 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) // 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]}; + 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)) { @@ -274,7 +275,7 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) } // Edits a post using file data -int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con) +int EditFilePost(char filePath[], int postID, MYSQL* con) { if (con == NULL) @@ -282,15 +283,9 @@ int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con) con = conn2maria(); } - // Author ID <= 0 should default to me. - if (authorID <= 0) - { - authorID = 1; - } - // Define a file and buffer FILE* FilePointer; - int BufferLength = 255; + int BufferLength = 8192; char Buffer[BufferLength]; char StringEnd[] = "\0"; @@ -303,12 +298,12 @@ int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con) // 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]}; + 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("'%s' '%s'\n", startTest, endTest); return -1; } @@ -334,7 +329,7 @@ int EditFilePost(int authorID, char filePath[], int postID, MYSQL* con) // Build a query char* query; if(0 > asprintf(&query, - "UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)", + "UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)", Buffer, postID )) exit(1); @@ -378,6 +373,33 @@ int BlogWrite(MYSQL* con) return 1; } +// 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); + + fp = fopen(tmpName, "w+"); + fprintf(fp, "

%s

\n%s", result[1], result[2]); + fclose(fp); + + char* command; + if(0 > asprintf(&command, "$EDITOR %s", tmpName)) exit(1); + system(command); + + EditFilePost(tmpName, postID, con); + + remove(tmpName); + + return 1; +} + // Pulls a single post's data into the supplied array int PullPostData(char* strings[6], int postID, MYSQL* con) { @@ -1340,8 +1362,9 @@ 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(1, "/home/adam/Documents/html/linuxenv.html", 42, con); + //EditFilePost("/home/adam/Documents/html/linuxenv.html", 69, con); //BlogWrite(con); + //BlogEdit(69, con); // =================================