From e761269ee9740f0669ad2b6dd10888b95ad09306 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 16 Apr 2022 15:30:17 +0200 Subject: [PATCH] Moved 'main' function to examples.md --- .gitignore | 1 - BlogDB.c | 200 +++++++--------------------------------------------- examples.md | 164 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 175 deletions(-) create mode 100644 examples.md diff --git a/.gitignore b/.gitignore index b5e3ae7..b776309 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,3 @@ modules.order Module.symvers Mkfile.old dkms.conf - diff --git a/BlogDB.c b/BlogDB.c index a371103..8364a24 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -310,7 +310,10 @@ int BlogEdit(int postID, MYSQL* con) fclose(fp); char* command; - if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1); + if(0 > asprintf(&command, + "${EDITOR:-'vi'} %s", + tmpName + )) exit(1); system(command); EditFilePost(tmpName, postID, con); @@ -326,7 +329,10 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) if (con == NULL) con = conn2maria(); char* query; - if(0 > asprintf(&query, "SELECT * FROM blog_posts WHERE id=(%d)", postID)) exit(1); + if(0 > asprintf(&query, + "SELECT * FROM blog_posts WHERE id=(%d)", + postID + )) exit(1); if (mysql_query(con, query)) { @@ -425,7 +431,8 @@ int AddTag(char tagTitle[], MYSQL* con) if (con == NULL) con = conn2maria(); char* insertQuery; - if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")", + if (0 > asprintf(&insertQuery, + "INSERT INTO tags (name) VALUES (\"%s\")", tagTitle )) exit(1); @@ -461,7 +468,8 @@ int DeleteTag(int tagID, MYSQL* con) if (con == NULL) con = conn2maria(); char* deleteTagQuery; - if (0 > asprintf(&deleteTagQuery, "DELETE FROM tags WHERE id=(%d)", + if (0 > asprintf(&deleteTagQuery, + "DELETE FROM tags WHERE id=(%d)", tagID )) exit(1); @@ -475,7 +483,8 @@ int DeleteTag(int tagID, MYSQL* con) free(deleteTagQuery); char* deleteTagAssignmentsQuery; - if (0 > asprintf(&deleteTagAssignmentsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", + if (0 > asprintf(&deleteTagAssignmentsQuery, + "DELETE FROM blog_post_tags WHERE tag_id=(%d)", tagID )) exit(1); @@ -523,7 +532,8 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con) free(selectQuery); char* updateQuery; - if (0 > asprintf(&updateQuery, "UPDATE tags SET name=(\"%s\") WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE tags SET name=(\"%s\") WHERE id=(%d)", tagTitle, tagID )) exit(1); @@ -546,7 +556,8 @@ int AssignTag(int tagID, int postID, MYSQL* con) if (con == NULL) con = conn2maria(); char* selectQuery; - if(0 > asprintf(&selectQuery, "SELECT * FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", + if(0 > asprintf(&selectQuery, + "SELECT * FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", tagID, postID )) exit(1); @@ -577,9 +588,10 @@ int AssignTag(int tagID, int postID, MYSQL* con) free(selectQuery); char* insertQuery; - if (0 > asprintf(&insertQuery, "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)", - tagID, - postID + if (0 > asprintf(&insertQuery, + "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)", + tagID, + postID )) exit(1); if (mysql_query(con, insertQuery)) @@ -600,7 +612,8 @@ int RetractTag(int tagID, int postID, MYSQL* con) if (con == NULL) con = conn2maria(); char* deleteQuery; - if (0 > asprintf(&deleteQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", + if (0 > asprintf(&deleteQuery, + "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)", tagID, postID )) exit(1); @@ -623,7 +636,8 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con) if (con == NULL) con = conn2maria(); 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, lastName, email @@ -971,165 +985,3 @@ int AuthorIDs(int** arr, MYSQL* con) return count; } -// Main -int main(int argc, char *argv[]) -{ - /* - // =================================== - // Example of using 'PullPostData()' - // =================================== - - // Init array of six strings - // Pull data of postID '1' into 'result' - char *result[6]; - PullPostData(result, 42, NULL); - - // Foreach in result - for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) - { - // Print contents on a new line - printf("%s\n", result[i]); - } - - // =================================== - */ - - /* - // =================================== - // Example of using 'PullAuthorData()' - // =================================== - - // Init array of four strings - // Pull data of authorID '1' into 'result' - char *result[4]; - PullAuthorData(result, 1, NULL); - - // Foreach in result - for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) - { - // Print contents on a new line - printf("%s\n", result[i]); - } - // =================================== - */ - - /* - // =================================== - // Example of using 'PostIDs()' - // =================================== - - // Generate array of IDs - int *postIDs; - int count1 = PostIDs(&postIDs, NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; i < count1; ++i) - { - // Print contents on a new line - printf("%d\n", postIDs[i]); - } - - // Free the memory - free(postIDs); - - // =================================== - */ - - /* - // =================================== - // Example of using 'ActivePostIDs()' - // =================================== - - // Generate array of IDs - int *activePostIDs; - int count2 = ActivePostIDs(&activePostIDs, NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; i < count2; ++i) - { - // Print contents on a new line - printf("%d\n", activePostIDs[i]); - } - - // Free the memory - free(activePostIDs); - - // =================================== - */ - - /* - // =================================== - // Example of using 'AuthorIDs()' - // =================================== - - // Generate array of IDs - int *authorIDs; - int count3 = AuthorIDs(&authorIDs, NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; i < count3; ++i) - { - // Print contents on a new line - printf("%d\n", authorIDs[i]); - } - - // Free the memory - free(authorIDs); - - // =================================== - */ - - /* - // =================================== - // Example of using 'PullPostDescriptions()' - // =================================== - - // Generate array of IDs - int *IDs; - int *authors; - int *dates; - char *Titles[255]; - int count4 = PullPostDescriptions(&IDs, &Titles, &authors, &dates, NULL); - - // Loop until element is negative i.e. the last - for (int i = 0; i < count4; ++i) - { - // Print contents on a new line - printf("%d %s %d %d\n", IDs[i], Titles[i], authors[i], dates[i]); - } - - // Free the memory - free(IDs); - free(authors); - free(dates); - - // =================================== - */ - - // Open the connection - MYSQL *con = conn2maria(); - - // AddPost return the inserted rows, NULL pointer for no tags, 0 for size - // - //int tags[] = {1, 2, 3}; - //printf("%d", AddPost("Something", 2, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags), con)); - - //AssignTag(6, 42, con); - //EditPost(69, "HEY", "THERE BUDDY", con); - //printf("%d\n", AddTag("Test", con)); - //DeleteTag(7, con); - //RetractTag(1, 1, con); - //EditTag(5, "Projects", con); - //AddAuthor("Me", "Meson", "email@me.com", con); - //ChangeAuthor(93, 1, con); - //TogglePost(81, con); - //printf("%s\n", IsEnabled(81, con) ? "True" : "False"); - //AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con); - //EditFilePost("/home/adam/Documents/html/linuxenv.html", 1, con); - //BlogWrite(con); - //BlogEdit(69, con); - - // ================================= - - exit(0); -} diff --git a/examples.md b/examples.md new file mode 100644 index 0000000..ed54f18 --- /dev/null +++ b/examples.md @@ -0,0 +1,164 @@ +```c +// Main +int main(int argc, char *argv[]) +{ + /* + // =================================== + // Example of using 'PullPostData()' + // =================================== + + // Init array of six strings + // Pull data of postID '1' into 'result' + char *result[6]; + PullPostData(result, 42, NULL); + + // Foreach in result + for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) + { + // Print contents on a new line + printf("%s\n", result[i]); + } + + // =================================== + */ + + /* + // =================================== + // Example of using 'PullAuthorData()' + // =================================== + + // Init array of four strings + // Pull data of authorID '1' into 'result' + char *result[4]; + PullAuthorData(result, 1, NULL); + + // Foreach in result + for (int i = 0; i < sizeof(result)/sizeof(result[0]); i++) + { + // Print contents on a new line + printf("%s\n", result[i]); + } + // =================================== + */ + + /* + // =================================== + // Example of using 'PostIDs()' + // =================================== + + // Generate array of IDs + int *postIDs; + int count1 = PostIDs(&postIDs, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count1; ++i) + { + // Print contents on a new line + printf("%d\n", postIDs[i]); + } + + // Free the memory + free(postIDs); + + // =================================== + */ + + /* + // =================================== + // Example of using 'ActivePostIDs()' + // =================================== + + // Generate array of IDs + int *activePostIDs; + int count2 = ActivePostIDs(&activePostIDs, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count2; ++i) + { + // Print contents on a new line + printf("%d\n", activePostIDs[i]); + } + + // Free the memory + free(activePostIDs); + + // =================================== + */ + + /* + // =================================== + // Example of using 'AuthorIDs()' + // =================================== + + // Generate array of IDs + int *authorIDs; + int count3 = AuthorIDs(&authorIDs, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count3; ++i) + { + // Print contents on a new line + printf("%d\n", authorIDs[i]); + } + + // Free the memory + free(authorIDs); + + // =================================== + */ + + /* + // =================================== + // Example of using 'PullPostDescriptions()' + // =================================== + + // Generate array of IDs + int *IDs; + int *authors; + int *dates; + char *Titles[255]; + int count4 = PullPostDescriptions(&IDs, &Titles, &authors, &dates, NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; i < count4; ++i) + { + // Print contents on a new line + printf("%d %s %d %d\n", IDs[i], Titles[i], authors[i], dates[i]); + } + + // Free the memory + free(IDs); + free(authors); + free(dates); + + // =================================== + */ + + // Open the connection + MYSQL *con = conn2maria(); + + // AddPost return the inserted rows, NULL pointer for no tags, 0 for size + // + //int tags[] = {1, 2, 3}; + //printf("%d", AddPost("Something", 2, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags), con)); + + //AssignTag(6, 42, con); + //EditPost(69, "HEY", "THERE BUDDY", con); + //printf("%d\n", AddTag("Test", con)); + //DeleteTag(7, con); + //RetractTag(1, 1, con); + //EditTag(5, "Projects", con); + //AddAuthor("Me", "Meson", "email@me.com", con); + //ChangeAuthor(93, 1, con); + //TogglePost(81, con); + //printf("%s\n", IsEnabled(81, con) ? "True" : "False"); + //AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con); + //EditFilePost("/home/adam/Documents/html/linuxenv.html", 1, con); + //BlogWrite(con); + //BlogEdit(69, con); + + // ================================= + + exit(0); +} +```