diff --git a/.gitignore b/.gitignore index cd531cf..13697f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # ---> C # Prerequisites +* +!*.* *.d # Object files diff --git a/BlogDB b/BlogDB deleted file mode 100755 index b80ba30..0000000 Binary files a/BlogDB and /dev/null differ diff --git a/BlogDB.c b/BlogDB.c index a54bc39..7ed37d6 100755 --- a/BlogDB.c +++ b/BlogDB.c @@ -19,6 +19,7 @@ static int PullAuthorData(char* strings[4], int authorID, MYSQL* con); static int ChangeAuthor(int postID, int authorID, MYSQL* con); static int TogglePost(int postID, MYSQL* con); static bool IsEnabled(int postID, MYSQL* con); +static int* ActivePostIDs(MYSQL* con); // Connects to the MariaDB database and returns the connection MYSQL* conn2maria() @@ -636,8 +637,6 @@ int TogglePost(int postID, MYSQL* con) int num_fields = mysql_num_fields(result); MYSQL_ROW row; - int postFound = 1; - row = mysql_fetch_row(result); if (row == NULL) @@ -710,7 +709,10 @@ bool IsEnabled(int postID, MYSQL* con) return !state; } -void *ActivePostIDs(MYSQL* con) +// Returns an array of IDs for all the posts that have state=0, +// the last element in the array is set to be negative as to prevent going into unallocated data. +// Remember to free() the returned array +int* ActivePostIDs(MYSQL* con) { @@ -729,17 +731,13 @@ void *ActivePostIDs(MYSQL* con) // Get results MYSQL_RES *result = mysql_store_result(con); - int num_fields = mysql_num_fields(result); - MYSQL_ROW row; - int postFound = 1; - row = mysql_fetch_row(result); int count = atoi(row[0]); + //printf("%d\n", count); - printf("%d\n", count); - + free(result); // Query the server, if successful continue, else throw error if (mysql_query(con, "SELECT id FROM blog_posts WHERE state=(0)")) @@ -749,17 +747,20 @@ void *ActivePostIDs(MYSQL* con) exit(1); } - free(result); - MYSQL_RES *result2 = mysql_store_result(con); - int *arr = (int *)realloc(arr, sizeof(int) * count); + int *arr = (int *)malloc(sizeof(int) * (count + 1)); + printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); - arr[i] = atoi(row[0]); + if (row != NULL) { + arr[i] = atoi(row[0]); + } } + arr[count] = -1; + //return sizeof(int) * count; //return count; @@ -773,9 +774,9 @@ void *ActivePostIDs(MYSQL* con) int main(int argc, char *argv[]) { /* - // ================================= + // =================================== // Example of using 'PullPostData()' - // ================================= + // =================================== // Init array of six strings // Pull data of postID '1' into 'result' @@ -789,10 +790,16 @@ int main(int argc, char *argv[]) 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); @@ -802,19 +809,39 @@ int main(int argc, char *argv[]) // Print contents on a new line printf("%s\n", result[i]); } + // =================================== */ + /* + // =================================== + // Example of using 'ActivePostIDs()' + // =================================== + + // Generate array of IDs + int *num = ActivePostIDs(NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; num[i] > 0; ++i) + { + // Print contents on a new line + printf("%d\n", num[i]); + } + + // Free the memory + free(num); + + // =================================== + */ + + // Open the connection MYSQL *con = conn2maria(); - // ================================= - // Example of using 'PullPostData()' - // ================================= - // AddPost return the inserted rows, NULL pointer for no tags, 0 for size - + // //int tags[] = {1, 2, 3}; //printf("%d", AddPost("Something", 1, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags))); + //AssignTag(6, 42, con); //EditPost(69, "HEY", "THERE BUDDY", con); //printf("%d\n", AddTag("Test", con)); @@ -823,14 +850,8 @@ int main(int argc, char *argv[]) //EditTag(5, "Projects", con); //AddAuthor("Me", "Meson", "email@me.com", con); //ChangeAuthor(62, 2, con); - //TogglePost(51, con); - //printf("%s\n", IsEnabled(51, con) ? "True" : "False"); - - int *num; - - ActivePostIDs(con); - - + //TogglePost(81, con); + //printf("%s\n", IsEnabled(81, con) ? "True" : "False"); // =================================