Added projects tab and local projects list processing. Fxed some memory errors
This commit is contained in:
parent
21f5a392fa
commit
440000c438
@ -9,7 +9,7 @@
|
||||
|
||||
#include "../../include/file_op/file.h"
|
||||
#include "../../include/utils_op/utils.h"
|
||||
#include "../../include/articles_op/process_md.h"
|
||||
#include "../../include/articles_p/process_md.h"
|
||||
|
||||
#define LINE_LENGTH 512
|
||||
|
22
include/projects_p/projects.h
Normal file
22
include/projects_p/projects.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef _PROJECTS_H
|
||||
#define _PROJECTS_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LINE_LENGTH 512
|
||||
|
||||
typedef struct {
|
||||
char *title;
|
||||
char *description;
|
||||
char *lang;
|
||||
char *license;
|
||||
char *url;
|
||||
} project_t;
|
||||
|
||||
ssize_t read_list(project_t **list);
|
||||
char *gen_project_html();
|
||||
void free_proj_list(project_t **list, int len);
|
||||
|
||||
#endif
|
@ -1,4 +1,4 @@
|
||||
#include "../../include/articles_op/article.h"
|
||||
#include "../../include/articles_p/article.h"
|
||||
|
||||
int list_articles(article_info **articles)
|
||||
{
|
||||
@ -50,6 +50,8 @@ int list_articles(article_info **articles)
|
||||
// printf("Found article \"%s\" posted on %ld\n", (*articles)[articles_amount - 1].time, (*articles)[articles_amount - 1].title);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
free(buff);
|
||||
|
||||
return articles_amount;
|
||||
@ -61,6 +63,7 @@ long get_article_contents(article_info *article)
|
||||
if (strcmp(article->title, "No title") == 0)
|
||||
{
|
||||
int line_length = snprintf(NULL, 0, "%ld", article->time) + 1;
|
||||
free(name);
|
||||
name = malloc(line_length);
|
||||
if (name == NULL)
|
||||
{
|
||||
@ -82,6 +85,7 @@ long get_article_contents(article_info *article)
|
||||
snprintf(path, line_length, "./static/articles/%s/%s.md", name, name);
|
||||
|
||||
FILE *file = fopen(path, "r");
|
||||
free(path);
|
||||
if (file == NULL)
|
||||
{
|
||||
perror("Couldn't open article file");
|
||||
@ -98,6 +102,9 @@ long get_article_contents(article_info *article)
|
||||
|
||||
article->content[article->length] = '\0';
|
||||
|
||||
free(name);
|
||||
fclose(file);
|
||||
|
||||
return article->length;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "../../include/articles_op/html.h"
|
||||
#include "../../include/articles_op/article.h"
|
||||
#include "../../include/articles_p/html.h"
|
||||
#include "../../include/articles_p/article.h"
|
||||
#include "../../include/utils_op/utils.h"
|
||||
|
||||
int gen_html_article(article_info article, char **out)
|
||||
@ -20,8 +20,6 @@ int gen_html_article(article_info article, char **out)
|
||||
template_str[i] = fgetc(template);
|
||||
template_str[template_size] = '\0';
|
||||
|
||||
*out = malloc(template_size + article.length + 1);
|
||||
|
||||
char *content;
|
||||
|
||||
process_md(article, &content);
|
||||
@ -38,7 +36,9 @@ int gen_html_article(article_info article, char **out)
|
||||
|
||||
snprintf(*out, line_length, template_str, article.title, article.title, content);
|
||||
|
||||
fclose(template);
|
||||
free(template_str);
|
||||
free(content);
|
||||
|
||||
return line_length;
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
#include "../../include/articles_op/process_md.h"
|
||||
#include "../../include/articles_p/process_md.h"
|
||||
|
||||
int process_md(article_info article, char **out)
|
||||
{
|
||||
*out = malloc(1);
|
||||
(*out)[0] = '\0';
|
||||
char *rest = strdup(article.content);
|
||||
char *rest = strdup(article.content), *free_rest = rest;
|
||||
char *buff;
|
||||
|
||||
int is_in_list = 0;
|
||||
@ -291,5 +291,7 @@ int process_md(article_info article, char **out)
|
||||
}
|
||||
(*out)[strlen(*out) - 1] = '\0';
|
||||
|
||||
free(free_rest);
|
||||
|
||||
return strlen(*out);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
#include "../../include/gallery_op/gallery.h"
|
||||
#include "../../include/gallery_p/gallery.h"
|
||||
#include "../../include/utils_op/utils.h"
|
||||
#include "../../include/file_op/file.h"
|
||||
#include "../../include/utils_op/arr.h"
|
||||
@ -12,7 +12,7 @@ gallery_t *get_album_list()
|
||||
{
|
||||
gallery_t *list = NULL, *curr = list;
|
||||
|
||||
char **albums_titles_list = calloc(0, sizeof(char *));
|
||||
char **albums_titles_list = malloc(0);
|
||||
|
||||
ssize_t albums_am = get_dir_list(&albums_titles_list, GALLERY_ROOT);
|
||||
|
||||
@ -184,11 +184,13 @@ char *gen_gallery_html()
|
||||
|
||||
free(album_content);
|
||||
free(album_html);
|
||||
free(image_template);
|
||||
|
||||
albums_list_item = albums_list_item->next;
|
||||
}
|
||||
|
||||
free(album_template);
|
||||
free(image_template);
|
||||
|
||||
return gallery_content;
|
||||
}
|
@ -2,9 +2,10 @@
|
||||
#include "../../include/utils_op/utils.h"
|
||||
#include "../../include/file_op/file.h"
|
||||
#include "../../include/file_op/mime.h"
|
||||
#include "../../include/articles_op/html.h"
|
||||
#include "../../include/articles_op/article.h"
|
||||
#include "../../include/gallery_op/gallery.h"
|
||||
#include "../../include/articles_p/html.h"
|
||||
#include "../../include/articles_p/article.h"
|
||||
#include "../../include/gallery_p/gallery.h"
|
||||
#include "../../include/projects_p/projects.h"
|
||||
|
||||
/**
|
||||
* @brief Send 404 response
|
||||
@ -237,8 +238,11 @@ void handle_get_request(int fd, char *request)
|
||||
if (id < 0 || strcmp(id_str, remaining) == 0)
|
||||
{
|
||||
res_404(fd, path);
|
||||
|
||||
free(id_str);
|
||||
return;
|
||||
}
|
||||
free(id_str);
|
||||
|
||||
article_info *articles = malloc(0);
|
||||
int amount = list_articles(&articles);
|
||||
@ -256,7 +260,7 @@ void handle_get_request(int fd, char *request)
|
||||
|
||||
get_article_contents(&(articles[id]));
|
||||
|
||||
char *html;
|
||||
char *html = NULL;
|
||||
gen_html_article(articles[id], &html);
|
||||
|
||||
struct header_s *header = gen_header(200, strlen(html), "text/html");
|
||||
@ -267,7 +271,10 @@ void handle_get_request(int fd, char *request)
|
||||
|
||||
printf("Sent article with id=%d\n", id);
|
||||
|
||||
free(articles);
|
||||
free(header->str);
|
||||
free(header);
|
||||
|
||||
free_article_info_arr(&articles, amount);
|
||||
free(html);
|
||||
|
||||
return;
|
||||
@ -300,9 +307,55 @@ void handle_get_request(int fd, char *request)
|
||||
send(fd, res_page, line_length, 0);
|
||||
close(fd);
|
||||
|
||||
printf("Sent gallery page\n");
|
||||
|
||||
free(header->str);
|
||||
free(header);
|
||||
|
||||
free(page_template);
|
||||
free(gallery_content);
|
||||
free(res_page);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp(path, "/projects") == 0 || strcmp(path, "/projects/") == 0)
|
||||
{
|
||||
FILE *page_template_fp = fopen("static/projects/index.html", "r");
|
||||
if (page_template_fp == NULL)
|
||||
{
|
||||
res_404(fd, path);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t page_file_size = get_file_size(page_template_fp) + 1;
|
||||
char *page_template = calloc(1, page_file_size);
|
||||
|
||||
fread(page_template, page_file_size, 1, page_template_fp);
|
||||
fclose(page_template_fp);
|
||||
|
||||
char *projects_content = gen_project_html();
|
||||
|
||||
size_t line_length = snprintf(NULL, 0, page_template, projects_content) + 1;
|
||||
char *res_page = calloc(1, line_length);
|
||||
sprintf(res_page, page_template, projects_content);
|
||||
|
||||
struct header_s *header = gen_header(200, line_length, "text/html");
|
||||
send(fd, header->str, header->size - 1, 0);
|
||||
|
||||
send(fd, res_page, line_length, 0);
|
||||
close(fd);
|
||||
|
||||
printf("Sent projects page\n");
|
||||
|
||||
free(header->str);
|
||||
free(header);
|
||||
|
||||
free(page_template);
|
||||
free(projects_content);
|
||||
free(res_page);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (send_response(fd, path) < 0)
|
||||
|
104
src/projects_p/projects.c
Normal file
104
src/projects_p/projects.c
Normal file
@ -0,0 +1,104 @@
|
||||
#include "../../include/projects_p/projects.h"
|
||||
#include "../../include/utils_op/arr.h"
|
||||
#include "../../include/file_op/file.h"
|
||||
|
||||
ssize_t read_list(project_t **list)
|
||||
{
|
||||
FILE *fp = fopen("static/projects/list.db", "r");
|
||||
if (fp == NULL)
|
||||
{
|
||||
perror("couldn't open projectDB file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *buff = NULL;
|
||||
size_t length = LINE_LENGTH;
|
||||
size_t projects_amount = 0;
|
||||
|
||||
while (getline(&buff, &length, fp) > 0)
|
||||
{
|
||||
if (buff[strlen(buff) - 1] == '\n')
|
||||
buff[strlen(buff) - 1] = '\0';
|
||||
|
||||
if (strlen(buff) == 0)
|
||||
continue;
|
||||
|
||||
projects_amount++;
|
||||
|
||||
*list = realloc(*list, sizeof(project_t) * projects_amount);
|
||||
|
||||
char *tmp = strdup(buff), *rest = tmp;
|
||||
|
||||
(*list)[projects_amount - 1].title = strdup(strtok_r(tmp, ";", &rest));
|
||||
|
||||
(*list)[projects_amount - 1].description = strdup(strtok_r(rest, ";", &rest));
|
||||
|
||||
(*list)[projects_amount - 1].lang = strdup(strtok_r(rest, ";", &rest));
|
||||
|
||||
(*list)[projects_amount - 1].license = strdup(strtok_r(rest, ";", &rest));
|
||||
|
||||
(*list)[projects_amount - 1].url = strdup(strtok_r(rest, ";", &rest));
|
||||
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
free(buff);
|
||||
|
||||
return projects_amount;
|
||||
}
|
||||
|
||||
char *gen_project_html()
|
||||
{
|
||||
FILE *template_fp = fopen("static/projects/item.html", "r");
|
||||
if (template_fp == NULL)
|
||||
{
|
||||
perror("couldn't open project tesplate");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t template_file_size = get_file_size(template_fp) + 1;
|
||||
char *template = calloc(1, template_file_size);
|
||||
|
||||
fread(template, template_file_size, 1, template_fp);
|
||||
fclose(template_fp);
|
||||
|
||||
project_t *list = malloc(0);
|
||||
size_t length = read_list(&list);
|
||||
|
||||
char *content = strdup("");
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
int line_length = snprintf(NULL, 0, template, list[i].url, list[i].title, list[i].description, list[i].lang, list[i].license) + 1;
|
||||
char *project_content = calloc(1, line_length);
|
||||
|
||||
sprintf(project_content, template, list[i].url, list[i].title, list[i].description, list[i].lang, list[i].license);
|
||||
|
||||
content = realloc(content, strlen(content) + line_length);
|
||||
|
||||
content = strcat(content, project_content);
|
||||
|
||||
free(project_content);
|
||||
}
|
||||
|
||||
free_proj_list(&list, length);
|
||||
free(template);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
void free_proj_list(project_t **list, int len)
|
||||
{
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
free((*list)[i].description);
|
||||
free((*list)[i].lang);
|
||||
free((*list)[i].license);
|
||||
free((*list)[i].title);
|
||||
free((*list)[i].url);
|
||||
}
|
||||
|
||||
free(*list);
|
||||
}
|
36
static/projects/index.html
Normal file
36
static/projects/index.html
Normal file
@ -0,0 +1,36 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<title>Projects</title>
|
||||
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<a href="/" id="logo"><img src="/logo.png" alt="logo" /></a>
|
||||
<!-- <a id="modern" href="/modern"
|
||||
title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> -->
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<h1 id="header">My projects</h1>
|
||||
<ul>%s</ul>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
main>ul li {
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
main>ul li>a {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
</body>
|
||||
|
||||
</html>
|
7
static/projects/item.html
Normal file
7
static/projects/item.html
Normal file
@ -0,0 +1,7 @@
|
||||
<li>
|
||||
<a href="%s">
|
||||
<h1>%s</h1>
|
||||
</a>
|
||||
<p>%s - <b><i>%s</i></b></p>
|
||||
<span><i>%s</i></span>
|
||||
</li>
|
3
static/projects/list.db
Normal file
3
static/projects/list.db
Normal file
@ -0,0 +1,3 @@
|
||||
Mshell;A simple yet comfortable UNIX shell;C;MIT License;https://github.com/Dm1tr1y147/mshell
|
||||
thetriangle;A simple program for drawing triangles;C;None;https://github.com/Dm1tr1y147/thetriangle
|
||||
md_offliner;Tool for uploading articles to the server;Lua;MIT License;https://github.com/Dm1tr1y147/md_offliner
|
Loading…
x
Reference in New Issue
Block a user