Created
April 4, 2012 18:23
-
-
Save MariusSchiffer/2304436 to your computer and use it in GitHub Desktop.
blog fcgicmvc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "store.h" | |
#include "xordllist.h" | |
void store_init(Blog_storage_t* storage) | |
{ | |
storage->users = malloc(sizeof(XorDLList)); | |
xordllist_init(storage->users, NULL, NULL); | |
storage->blogposts = malloc(sizeof(XorDLList)); | |
xordllist_init(storage->blogposts, NULL, NULL); | |
storage->usercount = 0; | |
storage->postcount = 0; | |
// TODO: Add importing from disk. | |
} | |
void store_add_blogpost(Blog_storage_t* storage, Blogpost_t* post) | |
{ | |
post->id = ++storage->postcount; | |
xordllist_cons(storage->blogposts, post, XORDLLIST_DIRECTION_FORWARD); | |
} | |
void store_add_user(Blog_storage_t* storage, User_t* user) | |
{ | |
user->id = ++storage->usercount; | |
xordllist_cons(storage->users, post, XORDLLIST_DIRECTION_FORWARD); | |
} | |
void store_get_user(Blog_storage_t* storage, char* user) | |
{ | |
char* comp; | |
XorDLListIter *iter; | |
iter = malloc(sizeof(XorDLListIter)); | |
xordllist_iter_init(iter, storage->users, XORDLLIST_DIRECTION_FORWARD); | |
while (xordllist_iter_head(iter) != NULL) | |
{ | |
safestr_cat_string(((User_t*)xordllist_iter_head(iter))->name, comp); | |
if (!strcmp(comp,user)) { | |
return (User_t*)xordllist_iter_head(iter); | |
} | |
xordllist_iter_next(iter); | |
} | |
return NULL; | |
} | |
void store_list_posts(Blog_storage_t* storage, int limit) | |
{ | |
XorDLListIter *iter; | |
int i = 0; | |
iter = malloc(sizeof(XorDLListIter)); | |
xordllist_iter_init(iter, storage->blogposts, XORDLLIST_DIRECTION_FORWARD); | |
while (xordllist_iter_head(iter) != NULL && i < limit) | |
{ | |
return (Blogpost_t)xordllist_iter_head(iter); | |
xordllist_iter_next(iter); | |
i++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* store.h | |
* Provides a storage interface specifically for the blog app | |
*/ | |
#include "xordllist.h" | |
typedef struct { | |
int id; | |
SafeStr name; | |
} User_t; | |
typedef struct { | |
int id; | |
User_t *author; | |
SafeStr title; | |
SafeStr content; | |
} Blogpost_t; | |
typedef struct { | |
XorDLList *users; // just temporary, probably by far the slowest thing | |
int usercount; | |
XorDLList *blogposts; | |
int postcount; | |
// tags and stuff? | |
} Blog_storage_t; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment