Bookmarks don't use dlist anymore.
This commit is contained in:
+68
-68
@@ -20,6 +20,9 @@
|
||||
* final '>' of a tag.
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
@@ -65,19 +68,20 @@
|
||||
* - &<>"' are escaped in titles and sections and saved unescaped.
|
||||
* - ' is escaped as %27 in URLs and saved escaped.
|
||||
*/
|
||||
typedef struct {
|
||||
struct BmRec {
|
||||
int key;
|
||||
int section;
|
||||
char *url;
|
||||
char *title;
|
||||
} BmRec;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
struct BmSec
|
||||
{
|
||||
int section;
|
||||
char *title;
|
||||
|
||||
int o_sec; /* private, for normalization */
|
||||
} BmSec;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
@@ -86,10 +90,10 @@ typedef struct {
|
||||
static char *Header = "Content-type: text/html\n\n";
|
||||
static char *BmFile = NULL;
|
||||
static time_t BmFileTimeStamp = 0;
|
||||
static Dlist *B_bms = NULL;
|
||||
static std::vector< std::unique_ptr< BmRec > > B_bms;
|
||||
static int bm_key = 0;
|
||||
|
||||
static Dlist *B_secs = NULL;
|
||||
static std::vector< std::unique_ptr< BmSec > > B_secs;
|
||||
static int sec_key = 0;
|
||||
|
||||
static int MODIFY_PAGE_NUM = 1;
|
||||
@@ -391,9 +395,11 @@ static int Bms_sec_by_number_cmp(const void *node, const void *key)
|
||||
/*
|
||||
* Return the Bm record by key
|
||||
*/
|
||||
static BmRec *Bms_get(int key)
|
||||
static BmRec *Bms_get(const int key)
|
||||
{
|
||||
return reinterpret_cast< BmRec * >( dList_find_custom(B_bms, INT2VOIDP(key), Bms_node_by_key_cmp) );
|
||||
auto found= std::find_if( begin( B_bms ), end( B_bms ), [key]( const auto &node ) { return node->key == key; } );
|
||||
if( found == end( B_bms ) ) return nullptr;
|
||||
return found->get();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -401,7 +407,9 @@ static BmRec *Bms_get(int key)
|
||||
*/
|
||||
static BmSec *Bms_get_sec(int key)
|
||||
{
|
||||
return reinterpret_cast< BmSec * >( dList_find_custom(B_secs, INT2VOIDP(key), Bms_sec_by_number_cmp) );
|
||||
auto found= std::find_if( begin( B_secs ), end( B_secs ), [key]( const auto &node ) { return node->section == key; } );
|
||||
if( found == end( B_secs ) ) return nullptr;
|
||||
return found->get();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -416,7 +424,7 @@ static void Bms_add(int section, char *url, char *title)
|
||||
bm_node->section = section;
|
||||
bm_node->url = dStrdup(Escape_uri_str(url, "'").c_str());
|
||||
bm_node->title = Escape_html_str(title);
|
||||
dList_append(B_bms, bm_node);
|
||||
B_bms.push_back( std::unique_ptr< BmRec >{ bm_node } );
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -429,25 +437,24 @@ static void Bms_sec_add(char *title)
|
||||
sec_node = dNew(BmSec, 1);
|
||||
sec_node->section = sec_key++;
|
||||
sec_node->title = Escape_html_str(title);
|
||||
dList_append(B_secs, sec_node);
|
||||
B_secs.push_back( std::unique_ptr< BmSec >{ sec_node } );
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a bookmark by its key
|
||||
*/
|
||||
static void Bms_del(int key)
|
||||
static void Bms_del(const int key)
|
||||
{
|
||||
BmRec *bm_node;
|
||||
|
||||
bm_node = reinterpret_cast< BmRec * >( dList_find_custom(B_bms, INT2VOIDP(key), Bms_node_by_key_cmp) );
|
||||
if (bm_node) {
|
||||
dList_remove(B_bms, bm_node);
|
||||
const auto found= std::find_if( begin( B_bms ), end( B_bms ), [key]( const auto &node ) { return node->key == key; } );
|
||||
if (found != end( B_bms ) ) {
|
||||
BmRec *bm_node= (*found).release();
|
||||
B_bms.erase( found );
|
||||
dFree(bm_node->title);
|
||||
dFree(bm_node->url);
|
||||
dFree(bm_node);
|
||||
}
|
||||
if (dList_length(B_bms) == 0)
|
||||
bm_key = 0;
|
||||
if( B_bms.empty() ) bm_key= 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -455,24 +462,26 @@ static void Bms_del(int key)
|
||||
*/
|
||||
static void Bms_sec_del(int section)
|
||||
{
|
||||
BmSec *sec_node;
|
||||
BmRec *bm_node;
|
||||
auto sec_node_i = std::find_if( begin( B_secs ), end( B_secs ),
|
||||
[section]( const auto &node ) { return node->section == section; } );
|
||||
|
||||
sec_node = reinterpret_cast< BmSec * >( dList_find_custom(B_secs, INT2VOIDP(section),
|
||||
Bms_sec_by_number_cmp) );
|
||||
if (sec_node) {
|
||||
dList_remove(B_secs, sec_node);
|
||||
if (sec_node_i != end( B_secs )) {
|
||||
auto sec_node= (*sec_node_i).release();
|
||||
B_secs.erase( sec_node_i );
|
||||
dFree(sec_node->title);
|
||||
dFree(sec_node);
|
||||
|
||||
/* iterate B_bms and remove those that match the section */
|
||||
while ((bm_node = reinterpret_cast< BmRec * >( dList_find_custom(B_bms, INT2VOIDP(section),
|
||||
Bms_node_by_section_cmp) ))) {
|
||||
auto found= end( B_bms );
|
||||
while( ( found= std::find_if( begin( B_bms ), end( B_bms ), [section]( const auto &node )
|
||||
{ return node->section == section; } ) ) != end( B_bms ) )
|
||||
{
|
||||
BmRec *bm_node= found->get();
|
||||
Bms_del(bm_node->key);
|
||||
}
|
||||
}
|
||||
if (dList_length(B_secs) == 0)
|
||||
sec_key = 0;
|
||||
|
||||
if( B_secs.empty() ) sec_key = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -480,11 +489,9 @@ static void Bms_sec_del(int section)
|
||||
*/
|
||||
static void Bms_move(int key, int target_section)
|
||||
{
|
||||
BmRec *bm_node;
|
||||
|
||||
bm_node = reinterpret_cast< BmRec * >( dList_find_custom(B_bms, INT2VOIDP(key), Bms_node_by_key_cmp) );
|
||||
if (bm_node) {
|
||||
bm_node->section = target_section;
|
||||
const auto bm_node = std::find_if( begin( B_bms ), end( B_bms ), [key]( const auto &node ){ return node->key == key; } );
|
||||
if (bm_node != end( B_bms ) ) {
|
||||
(*bm_node)->section = target_section;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,12 +500,11 @@ static void Bms_move(int key, int target_section)
|
||||
*/
|
||||
static void Bms_update_title(int key, char *n_title)
|
||||
{
|
||||
BmRec *bm_node;
|
||||
|
||||
bm_node = reinterpret_cast< BmRec * >( dList_find_custom(B_bms, INT2VOIDP(key), Bms_node_by_key_cmp) );
|
||||
if (bm_node) {
|
||||
dFree(bm_node->title);
|
||||
bm_node->title = Escape_html_str(n_title);
|
||||
const auto bm_node = std::find_if( begin( B_bms ), end( B_bms ), [key]( const auto &node ){ return node->key == key; } );
|
||||
if( bm_node != end( B_bms ) ) {
|
||||
dFree((*bm_node)->title);
|
||||
(*bm_node)->title = Escape_html_str(n_title);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -507,12 +513,10 @@ static void Bms_update_title(int key, char *n_title)
|
||||
*/
|
||||
static void Bms_update_sec_title(int key, char *n_title)
|
||||
{
|
||||
BmSec *sec_node;
|
||||
|
||||
sec_node = reinterpret_cast< BmSec * >( dList_find_custom(B_secs, INT2VOIDP(key), Bms_sec_by_number_cmp) );
|
||||
if (sec_node) {
|
||||
dFree(sec_node->title);
|
||||
sec_node->title = Escape_html_str(n_title);
|
||||
const auto sec_node = std::find_if( begin( B_secs ), end( B_secs ), [key]( const auto &node ){ return node->section == key; } );
|
||||
if (sec_node != end( B_secs )) {
|
||||
dFree((*sec_node)->title);
|
||||
(*sec_node)->title = Escape_html_str(n_title);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -521,16 +525,15 @@ static void Bms_update_sec_title(int key, char *n_title)
|
||||
*/
|
||||
static void Bms_free(void)
|
||||
{
|
||||
BmRec *bm_node;
|
||||
BmSec *sec_node;
|
||||
|
||||
/* free B_bms */
|
||||
while ((bm_node = reinterpret_cast< BmRec * >( dList_nth_data(B_bms, 0) ))) {
|
||||
Bms_del(bm_node->key);
|
||||
while( not empty( B_bms ) )
|
||||
{
|
||||
Bms_del(B_bms.front()->key);
|
||||
}
|
||||
/* free B_secs */
|
||||
while ((sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, 0) ))) {
|
||||
Bms_sec_del(sec_node->section);
|
||||
while( not empty( B_secs ) )
|
||||
{
|
||||
Bms_sec_del(B_secs.front()->section);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,20 +547,19 @@ static void Bms_normalize(void)
|
||||
int i, j;
|
||||
|
||||
/* we need at least one section */
|
||||
if (dList_length(B_secs) == 0)
|
||||
Bms_sec_add("Unclassified");
|
||||
if (B_secs.empty()) Bms_sec_add("Unclassified");
|
||||
|
||||
/* make correlative section numbers */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
sec_node->o_sec = sec_node->section;
|
||||
sec_node->section = i;
|
||||
}
|
||||
|
||||
/* iterate B_secs and make the changes in B_bms */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
if (sec_node->section != sec_node->o_sec) {
|
||||
/* update section numbers */
|
||||
for (j = 0; (bm_node = reinterpret_cast< BmRec * >( dList_nth_data(B_bms, j) )); ++j) {
|
||||
for (j = 0; j < B_bms.size() and ( bm_node = B_bms.at( j ).get() ); ++j) {
|
||||
if (bm_node->section == sec_node->o_sec)
|
||||
bm_node->section = sec_node->section;
|
||||
}
|
||||
@@ -697,7 +699,7 @@ static int Bms_cond_load(void)
|
||||
TimeStamp.st_mtime = 0;
|
||||
}
|
||||
|
||||
if (!BmFileTimeStamp || !dList_length(B_bms) || !dList_length(B_secs) ||
|
||||
if (!BmFileTimeStamp || B_bms.empty() || B_secs.empty() ||
|
||||
BmFileTimeStamp < TimeStamp.st_mtime) {
|
||||
Bms_load();
|
||||
st = 1;
|
||||
@@ -738,7 +740,7 @@ static int Bms_save(void)
|
||||
Bms_normalize();
|
||||
|
||||
/* save sections */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
u_title = Unescape_html_str(sec_node->title);
|
||||
dStr_sprintf(dstr, ":s%d: %s\n", sec_node->section, u_title);
|
||||
fwrite(dstr->str, (size_t)dstr->len, 1, BmTxt);
|
||||
@@ -746,8 +748,8 @@ static int Bms_save(void)
|
||||
}
|
||||
|
||||
/* save bookmarks (section url title) */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (j = 0; (bm_node = reinterpret_cast< BmRec * >( dList_nth_data(B_bms, j) )); ++j) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
for (j = 0; j < B_bms.size() and ( bm_node = B_bms.at( j ).get() ); ++j) {
|
||||
if (bm_node->section == sec_node->section) {
|
||||
u_title = Unescape_html_str(bm_node->title);
|
||||
dStr_sprintf(dstr, "s%d %s %s\n",
|
||||
@@ -855,7 +857,7 @@ static int Bmsrv_send_modify_page(Dsh *sh)
|
||||
if (a_Dpip_dsh_write_str(sh, 0, modifypage_sections_header))
|
||||
return 1;
|
||||
/* write sections */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
if (i > 0) {
|
||||
if (a_Dpip_dsh_write_str(sh, 0, sections_sep))
|
||||
return 1;
|
||||
@@ -875,7 +877,7 @@ static int Bmsrv_send_modify_page(Dsh *sh)
|
||||
return 1;
|
||||
|
||||
/* send bookmark cards */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
/* send card header */
|
||||
l_title = make_one_line_str(sec_node->title);
|
||||
dStr_sprintf(dstr, modifypage_section_card_header,
|
||||
@@ -885,7 +887,7 @@ static int Bmsrv_send_modify_page(Dsh *sh)
|
||||
return 1;
|
||||
|
||||
/* send section's bookmarks */
|
||||
for (j = 0; (bm_node = reinterpret_cast< BmRec * >( dList_nth_data(B_bms, j) )); ++j) {
|
||||
for (j = 0; j < B_bms.size() and ( bm_node = B_bms.at( j ).get() ); ++j) {
|
||||
if (bm_node->section == sec_node->section) {
|
||||
dStr_sprintf(dstr, modifypage_section_card_item,
|
||||
bm_node->key, bm_node->url, bm_node->title);
|
||||
@@ -1420,7 +1422,7 @@ static int send_bm_page(Dsh *sh)
|
||||
if (a_Dpip_dsh_write_str(sh, 0, mainpage_sections_header))
|
||||
return 1;
|
||||
/* write sections */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
if (i > 0) {
|
||||
if (a_Dpip_dsh_write_str(sh, 0, sections_sep))
|
||||
return 1;
|
||||
@@ -1440,7 +1442,7 @@ static int send_bm_page(Dsh *sh)
|
||||
return 1;
|
||||
|
||||
/* send bookmark cards */
|
||||
for (i = 0; (sec_node = reinterpret_cast< BmSec * >( dList_nth_data(B_secs, i) )); ++i) {
|
||||
for (i = 0; i < B_secs.size() and ( sec_node = B_secs.at( i ).get() ); ++i) {
|
||||
/* send card header */
|
||||
l_title = make_one_line_str(sec_node->title);
|
||||
dStr_sprintf(dstr, mainpage_section_card_header,
|
||||
@@ -1450,7 +1452,7 @@ static int send_bm_page(Dsh *sh)
|
||||
return 1;
|
||||
|
||||
/* send section's bookmarks */
|
||||
for (j = 0; (bm_node = reinterpret_cast< BmRec * >( dList_nth_data(B_bms, j) )); ++j) {
|
||||
for (j = 0; j < B_bms.size() and ( bm_node = B_bms.at( j ).get() ); ++j) {
|
||||
if (bm_node->section == sec_node->section) {
|
||||
dStr_sprintf(dstr, mainpage_section_card_item,
|
||||
bm_node->url, bm_node->title);
|
||||
@@ -1632,8 +1634,6 @@ int main(void) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
/* Initialize local data */
|
||||
B_bms = dList_new(512);
|
||||
B_secs = dList_new(32);
|
||||
BmFile = dStrconcat(dGethomedir(), "/.flenser/bm.txt", NULL);
|
||||
/* some OSes may need this... */
|
||||
address_size = sizeof(struct sockaddr_un);
|
||||
|
||||
Reference in New Issue
Block a user