Merge pull request 'Added AddFilePost function' (#6) from 2-AddFilePost into master
Reviewed-on: #6
This commit is contained in:
commit
2a83fb61ff
81
BlogDB.c
81
BlogDB.c
|
|
@ -4,10 +4,13 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
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 PullPostData(char* strings[6], int postID, MYSQL* con);
|
||||
static int AddTag(char tagTitle[], MYSQL* con);
|
||||
static int DeleteTag(int tagID, MYSQL* con);
|
||||
|
|
@ -175,6 +178,83 @@ int EditPost(int postID, char title[], char post[], MYSQL* con)
|
|||
return postID;
|
||||
}
|
||||
|
||||
// Adds a post using file data
|
||||
int AddFilePost(int authorID, char filePath[], MYSQL* con)
|
||||
{
|
||||
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
|
||||
// Author ID <= 0 should default to me.
|
||||
if (authorID <= 0)
|
||||
{
|
||||
authorID = 1;
|
||||
}
|
||||
|
||||
|
||||
// Get current data in string date
|
||||
time_t t = time(NULL);
|
||||
struct tm *tm = localtime(&t);
|
||||
char date[64];
|
||||
assert(strftime(date, sizeof(date), "%F", tm));
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
// 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 + 5] == 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.
|
||||
int postID = AddPost(Buffer, authorID, date, "", NULL, 0, con);
|
||||
|
||||
// Concat the rest of the file, line by line to the new 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;
|
||||
}
|
||||
|
||||
// Pulls a single post's data into the supplied array
|
||||
int PullPostData(char* strings[6], int postID, MYSQL* con)
|
||||
{
|
||||
|
|
@ -852,6 +932,7 @@ int main(int argc, char *argv[])
|
|||
//ChangeAuthor(62, 2, con);
|
||||
//TogglePost(81, con);
|
||||
//printf("%s\n", IsEnabled(81, con) ? "True" : "False");
|
||||
//AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con);
|
||||
|
||||
// =================================
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue