Now rendering comments
This commit is contained in:
parent
03e358e14d
commit
2b1da096a6
2
Makefile
2
Makefile
@ -18,7 +18,7 @@ $(BD)/libcgic.a:
|
|||||||
cp $(LIBS_DIR)/cgic/libcgic.a $(BD)
|
cp $(LIBS_DIR)/cgic/libcgic.a $(BD)
|
||||||
|
|
||||||
$(BD)/libqments.a:
|
$(BD)/libqments.a:
|
||||||
$(MAKE) -C $(LIBS_DIR)/qments
|
$(MAKE) -C $(LIBS_DIR)/qments static
|
||||||
cp $(LIBS_DIR)/qments/$(BD)/libqments.a $(BD)
|
cp $(LIBS_DIR)/qments/$(BD)/libqments.a $(BD)
|
||||||
|
|
||||||
$(BD)/$(CGI_EXEC): $(OBJ_FILES)
|
$(BD)/$(CGI_EXEC): $(OBJ_FILES)
|
||||||
|
92
src/main.c
92
src/main.c
@ -4,12 +4,17 @@
|
|||||||
#include "comment.h"
|
#include "comment.h"
|
||||||
#include "cgic.h"
|
#include "cgic.h"
|
||||||
#include "driver.h"
|
#include "driver.h"
|
||||||
|
#include "drivers/unix_fs/unix_fs_driver.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define COMMENTS_PER_PAGE 20
|
#define COMMENTS_PER_PAGE 20
|
||||||
#define MAX_NAME 1024
|
#define MAX_NAME 1024
|
||||||
|
|
||||||
|
#define QMENTS_PATH "qments-storage"
|
||||||
|
#define DRIVER_DATA { QMENTS_PATH }
|
||||||
|
#define DRIVER unix_fs_driver
|
||||||
|
|
||||||
void
|
void
|
||||||
fct_callback(char character, void *arg)
|
fct_callback(char character, void *arg)
|
||||||
{
|
{
|
||||||
@ -23,11 +28,13 @@ render_comment(const Comment *comment)
|
|||||||
char *retval;
|
char *retval;
|
||||||
|
|
||||||
buffer = &_buffer;
|
buffer = &_buffer;
|
||||||
fctprintf(fct_callback, buffer, "<div class=\"comment\" id=comment_%d\n", comment->id);
|
sb_init_empty(buffer);
|
||||||
|
|
||||||
|
fctprintf(fct_callback, buffer, "<div class=\"comment\" id=comment_%d>\n", comment->id);
|
||||||
|
|
||||||
/* begin header */
|
/* begin header */
|
||||||
fctprintf(fct_callback, buffer, "<div class=\"comment-header\">\n");
|
fctprintf(fct_callback, buffer, "<div class=\"comment-header\">\n");
|
||||||
fctprintf(fct_callback, buffer, "Posted by: %s", comment->header->user_displayname);
|
fctprintf(fct_callback, buffer, "Posted by: %s\n", comment->header->user_displayname);
|
||||||
fctprintf(fct_callback, buffer, "</div>\n");
|
fctprintf(fct_callback, buffer, "</div>\n");
|
||||||
/* end header */
|
/* end header */
|
||||||
|
|
||||||
@ -54,29 +61,92 @@ free_header(CommentHeader *header)
|
|||||||
free(header->user_displayname);
|
free(header->user_displayname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
allocate_comment(Comment *comment)
|
||||||
|
{
|
||||||
|
comment->header = malloc(sizeof(CommentHeader));
|
||||||
|
allocate_header(comment->header);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
free_comment(Comment *comment)
|
||||||
|
{
|
||||||
|
free_header(comment->header);
|
||||||
|
free(comment->header);
|
||||||
|
free(comment->text);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
fetch_comment(int id, Driver *driver, void *driver_data, Comment *comment)
|
fetch_comment(int id, Driver *driver, void *driver_data, Comment *comment)
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
comment->id = id;
|
comment->id = id;
|
||||||
|
|
||||||
allocate_header(comment->header);
|
allocate_comment(comment);
|
||||||
|
|
||||||
retval = driver->get_header(driver_data, comment->header, id) < 0;
|
retval = driver->get_header(driver_data, comment->header, id);
|
||||||
if (retval < 0) {
|
if (retval < 0) {
|
||||||
goto defer;
|
goto defer;
|
||||||
}
|
}
|
||||||
comment->text = malloc(comment->header->text_length + 1);
|
comment->text = malloc(comment->header->text_length + 1);
|
||||||
|
|
||||||
retval = driver->get_text(driver, comment->text, id);
|
retval = driver->get_text(driver_data, comment->text, id);
|
||||||
defer:
|
defer:
|
||||||
if (retval < 0) {
|
if (retval < 0) {
|
||||||
free_header(comment->header);
|
free_comment(comment);
|
||||||
free(comment->text);
|
|
||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* each page stores comments with ids in [P * (page - 1), P * page)
|
||||||
|
* if page is not specified, then we'll just print last P comments (for now)
|
||||||
|
* TODO: print errors in div
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
print_page()
|
||||||
|
{
|
||||||
|
int page, max_id;
|
||||||
|
int id_begin, id_end, i;
|
||||||
|
Comment comment;
|
||||||
|
UnixFsDriverData driver_data = DRIVER_DATA;
|
||||||
|
Driver driver = DRIVER;
|
||||||
|
|
||||||
|
cgiFormInteger("page", &page, 0);
|
||||||
|
if ((max_id = unix_fs_driver_get_max_id(&driver_data)) < 0) {
|
||||||
|
fprintf(cgiOut, "Error fetching comments\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (page <= 0) {
|
||||||
|
id_end = max_id;
|
||||||
|
id_begin = (max_id > COMMENTS_PER_PAGE ? max_id - COMMENTS_PER_PAGE : 1);
|
||||||
|
} else {
|
||||||
|
/* max_id >= COMMENTS_PER_PAGE * page
|
||||||
|
* perform this check without overflow
|
||||||
|
*/
|
||||||
|
if (max_id / page < COMMENTS_PER_PAGE) {
|
||||||
|
fprintf(cgiOut, "There is no such page\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
id_begin = COMMENTS_PER_PAGE * (page - 1);
|
||||||
|
id_end = id_begin + COMMENTS_PER_PAGE;
|
||||||
|
if (id_end > max_id) {
|
||||||
|
id_end = max_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = id_begin; i != id_end; ++i) {
|
||||||
|
if (fetch_comment(i, &driver, &driver_data, &comment) < 0) {
|
||||||
|
fprintf(cgiOut, "Failed to fetch comment %d\n", i);
|
||||||
|
} else {
|
||||||
|
char *rendered_comment = render_comment(&comment);
|
||||||
|
fputs(rendered_comment, cgiOut);
|
||||||
|
free(rendered_comment);
|
||||||
|
free_comment(&comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
cgiMain()
|
cgiMain()
|
||||||
{
|
{
|
||||||
@ -89,7 +159,13 @@ cgiMain()
|
|||||||
fprintf(cgiOut, "</head>\n");
|
fprintf(cgiOut, "</head>\n");
|
||||||
|
|
||||||
fprintf(cgiOut, "<body>\n");
|
fprintf(cgiOut, "<body>\n");
|
||||||
fprintf(cgiOut,
|
|
||||||
|
/* print_submit_form(); */
|
||||||
|
|
||||||
|
fprintf(cgiOut, "<div class=\"comment-section\">\n");
|
||||||
|
print_page();
|
||||||
|
fprintf(cgiOut, "</div>\n");
|
||||||
|
|
||||||
fprintf(cgiOut, "</body>\n");
|
fprintf(cgiOut, "</body>\n");
|
||||||
|
|
||||||
fprintf(cgiOut, "</html>\n");
|
fprintf(cgiOut, "</html>\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user