simple-discuss/src/main.c

99 lines
2.0 KiB
C
Raw Normal View History

2023-07-03 14:58:09 +03:00
#include "printf.h"
#include "string_buffer.h"
#include "utils.h"
#include "comment.h"
#include "cgic.h"
#include "driver.h"
#include <stdlib.h>
#define COMMENTS_PER_PAGE 20
#define MAX_NAME 1024
void
fct_callback(char character, void *arg)
{
sb_add_char((StringBuffer*) arg, character);
}
char *
render_comment(const Comment *comment)
{
StringBuffer _buffer, *buffer;
char *retval;
buffer = &_buffer;
fctprintf(fct_callback, buffer, "<div class=\"comment\" id=comment_%d\n", comment->id);
/* begin header */
fctprintf(fct_callback, buffer, "<div class=\"comment-header\">\n");
fctprintf(fct_callback, buffer, "Posted by: %s", comment->header->user_displayname);
fctprintf(fct_callback, buffer, "</div>\n");
/* end header */
fctprintf(fct_callback, buffer, "%s", comment->text);
fctprintf(fct_callback, buffer, "</div>\n");
SB_MOVE(buffer, retval);
return retval;
}
void
allocate_header(CommentHeader *header)
{
header->user_sid = malloc(MAX_NAME);
header->user_displayname = malloc(MAX_NAME);
}
void
free_header(CommentHeader *header)
{
free(header->user_sid);
free(header->user_displayname);
}
int
fetch_comment(int id, Driver *driver, void *driver_data, Comment *comment)
{
int retval;
comment->id = id;
allocate_header(comment->header);
retval = driver->get_header(driver_data, comment->header, id) < 0;
if (retval < 0) {
goto defer;
}
comment->text = malloc(comment->header->text_length + 1);
retval = driver->get_text(driver, comment->text, id);
defer:
if (retval < 0) {
free_header(comment->header);
free(comment->text);
}
return retval;
}
int
cgiMain()
{
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<html>\n");
fprintf(cgiOut, "<head>\n");
fprintf(cgiOut, "<title> Simple discuss powered by qments\n");
fprintf(cgiOut, "</head>\n");
fprintf(cgiOut, "<body>\n");
fprintf(cgiOut,
fprintf(cgiOut, "</body>\n");
fprintf(cgiOut, "</html>\n");
return 0;
}