Moved 'main' function to examples.md
This commit is contained in:
parent
dfe3466ea5
commit
e761269ee9
|
|
@ -54,4 +54,3 @@ modules.order
|
||||||
Module.symvers
|
Module.symvers
|
||||||
Mkfile.old
|
Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
|
|
|
||||||
196
BlogDB.c
196
BlogDB.c
|
|
@ -310,7 +310,10 @@ int BlogEdit(int postID, MYSQL* con)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
char* command;
|
char* command;
|
||||||
if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1);
|
if(0 > asprintf(&command,
|
||||||
|
"${EDITOR:-'vi'} %s",
|
||||||
|
tmpName
|
||||||
|
)) exit(1);
|
||||||
system(command);
|
system(command);
|
||||||
|
|
||||||
EditFilePost(tmpName, postID, con);
|
EditFilePost(tmpName, postID, con);
|
||||||
|
|
@ -326,7 +329,10 @@ int PullPostData(char* strings[6], int postID, MYSQL* con)
|
||||||
if (con == NULL) con = conn2maria();
|
if (con == NULL) con = conn2maria();
|
||||||
|
|
||||||
char* query;
|
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))
|
if (mysql_query(con, query))
|
||||||
{
|
{
|
||||||
|
|
@ -425,7 +431,8 @@ int AddTag(char tagTitle[], MYSQL* con)
|
||||||
if (con == NULL) con = conn2maria();
|
if (con == NULL) con = conn2maria();
|
||||||
|
|
||||||
char* insertQuery;
|
char* insertQuery;
|
||||||
if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")",
|
if (0 > asprintf(&insertQuery,
|
||||||
|
"INSERT INTO tags (name) VALUES (\"%s\")",
|
||||||
tagTitle
|
tagTitle
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
||||||
|
|
@ -461,7 +468,8 @@ int DeleteTag(int tagID, MYSQL* con)
|
||||||
if (con == NULL) con = conn2maria();
|
if (con == NULL) con = conn2maria();
|
||||||
|
|
||||||
char* deleteTagQuery;
|
char* deleteTagQuery;
|
||||||
if (0 > asprintf(&deleteTagQuery, "DELETE FROM tags WHERE id=(%d)",
|
if (0 > asprintf(&deleteTagQuery,
|
||||||
|
"DELETE FROM tags WHERE id=(%d)",
|
||||||
tagID
|
tagID
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
||||||
|
|
@ -475,7 +483,8 @@ int DeleteTag(int tagID, MYSQL* con)
|
||||||
free(deleteTagQuery);
|
free(deleteTagQuery);
|
||||||
|
|
||||||
char* deleteTagAssignmentsQuery;
|
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
|
tagID
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
||||||
|
|
@ -523,7 +532,8 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con)
|
||||||
free(selectQuery);
|
free(selectQuery);
|
||||||
|
|
||||||
char* updateQuery;
|
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,
|
tagTitle,
|
||||||
tagID
|
tagID
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
@ -546,7 +556,8 @@ int AssignTag(int tagID, int postID, MYSQL* con)
|
||||||
if (con == NULL) con = conn2maria();
|
if (con == NULL) con = conn2maria();
|
||||||
|
|
||||||
char* selectQuery;
|
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,
|
tagID,
|
||||||
postID
|
postID
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
@ -577,7 +588,8 @@ int AssignTag(int tagID, int postID, MYSQL* con)
|
||||||
free(selectQuery);
|
free(selectQuery);
|
||||||
|
|
||||||
char* insertQuery;
|
char* insertQuery;
|
||||||
if (0 > asprintf(&insertQuery, "INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)",
|
if (0 > asprintf(&insertQuery,
|
||||||
|
"INSERT INTO blog_post_tags (tag_id,blog_post_id) VALUES (%d, %d)",
|
||||||
tagID,
|
tagID,
|
||||||
postID
|
postID
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
@ -600,7 +612,8 @@ int RetractTag(int tagID, int postID, MYSQL* con)
|
||||||
if (con == NULL) con = conn2maria();
|
if (con == NULL) con = conn2maria();
|
||||||
|
|
||||||
char* deleteQuery;
|
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,
|
tagID,
|
||||||
postID
|
postID
|
||||||
)) exit(1);
|
)) exit(1);
|
||||||
|
|
@ -623,7 +636,8 @@ 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,
|
||||||
email
|
email
|
||||||
|
|
@ -971,165 +985,3 @@ int AuthorIDs(int** arr, MYSQL* con)
|
||||||
return count;
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
```
|
||||||
Loading…
Reference in New Issue