cleanup title string ownership madness in the tree_create_folder_node() API

This commit is contained in:
Vincent Sanders 2013-05-03 21:17:11 +01:00
parent 6c7ca26bdc
commit 5c57a30047
6 changed files with 62 additions and 88 deletions

View File

@ -329,44 +329,33 @@ static void cookies_schedule_callback(const void *scheduled_data)
const struct cookie_data *data = scheduled_data;
struct node *node = NULL;
struct node *cookie_node = NULL;
char *domain_cp;
assert(data != NULL);
node = cookies_find(cookies_tree_root, data->domain);
if (node == NULL) {
domain_cp = strdup(data->domain);
if (domain_cp == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
return;
}
/* ownership of domain_cp passed to tree, if node creation
* does not fail */
node = tree_create_folder_node(cookies_tree,
cookies_tree_root, domain_cp,
cookies_tree_root,
data->domain,
false, false, false);
if (node != NULL) {
tree_set_node_user_callback(node, cookies_node_callback,
tree_set_node_user_callback(node,
cookies_node_callback,
NULL);
tree_set_node_icon(cookies_tree, node, folder_icon);
} else {
free(domain_cp);
}
}
if (node == NULL)
return;
if (node != NULL) {
cookie_node = cookies_find(node, data->name);
if (cookie_node == NULL) {
cookies_create_cookie_node(node, data);
} else {
cookies_update_cookie_node(cookie_node, data);
}
cookie_node = cookies_find(node, data->name);
if (cookie_node == NULL)
cookies_create_cookie_node(node, data);
else
cookies_update_cookie_node(cookie_node, data);
return;
}
}
/**

View File

@ -175,33 +175,24 @@ static bool history_global_initialise_node(const char *title,
time_t base, int days_back)
{
struct tm *full_time;
char *buffer;
struct node *node;
base += days_back * 60 * 60 * 24;
if (title == NULL) {
full_time = localtime(&base);
buffer = strdup(messages_get(weekday_msg_name[full_time->tm_wday]));
} else {
buffer = strdup(title);
title = messages_get(weekday_msg_name[full_time->tm_wday]);
}
if (buffer == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
return false;
}
node = tree_create_folder_node(NULL, NULL, buffer,
false, true, true);
node = tree_create_folder_node(NULL, NULL, title, false, true, true);
if (node == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
free(buffer);
warn_user(messages_get_errorcode(NSERROR_NOMEM), 0);
return false;
}
if (folder_icon != NULL)
if (folder_icon != NULL) {
tree_set_node_icon(global_history_tree, node, folder_icon);
}
tree_set_node_user_callback(node, history_global_node_callback, NULL);
global_history_base_node[global_history_base_node_count] = node;

View File

@ -121,7 +121,6 @@ bool hotlist_initialise(struct tree *tree, const char *hotlist_path,
{
struct node *node;
const struct url_data *url_data;
char *name;
int hlst_loop;
/* Either load or create a hotlist */
@ -143,18 +142,15 @@ bool hotlist_initialise(struct tree *tree, const char *hotlist_path,
return true;
}
/* failed to load hotlist file, use default list */
name = strdup("NetSurf");
if (name == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
return false;
}
node = tree_create_folder_node(hotlist_tree, hotlist_tree_root,
name, true, false, false);
node = tree_create_folder_node(hotlist_tree,
hotlist_tree_root,
messages_get("NetSurf"),
true,
false,
false);
if (node == NULL) {
free(name);
warn_user(messages_get_errorcode(NSERROR_NOMEM), 0);
return false;
}
@ -373,14 +369,7 @@ void hotlist_collapse_addresses(void)
void hotlist_add_folder(bool selected)
{
struct node *node, *parent = NULL;
struct node_element *element;
char *title = strdup("Untitled");
if (title == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
return;
}
creating_node = true;
if (selected == true) {
@ -394,16 +383,21 @@ void hotlist_add_folder(bool selected)
parent = tree_get_default_folder_node(hotlist_tree);
}
node = tree_create_folder_node(hotlist_tree, parent, title,
true, false, false);
node = tree_create_folder_node(hotlist_tree,
parent,
messages_get("Untitled"),
true,
false,
false);
if (node == NULL) {
free(title);
warn_user(messages_get_errorcode(NSERROR_NOMEM), 0);
return;
}
tree_set_node_user_callback(node, hotlist_node_callback, NULL);
tree_set_node_icon(hotlist_tree, node, folder_icon);
element = tree_node_find_element(node, TREE_ELEMENT_TITLE, NULL);
tree_start_edit(hotlist_tree, element);
tree_start_edit(hotlist_tree,
tree_node_find_element(node, TREE_ELEMENT_TITLE, NULL));
}
/**

View File

@ -233,26 +233,19 @@ struct tree *tree_create(unsigned int flags,
const struct treeview_table *callbacks, void *client_data)
{
struct tree *tree;
char *title;
tree = calloc(sizeof(struct tree), 1);
if (tree == NULL) {
LOG(("calloc failed"));
warn_user("NoMemory", 0);
warn_user(messages_get_errorcode(NSERROR_NOMEM), 0);
return NULL;
}
title = strdup("Root");
if (title == NULL) {
LOG(("malloc failed"));
warn_user("NoMemory", 0);
free(tree);
return NULL;
}
tree->root = tree_create_folder_node(NULL, NULL, title,
tree->root = tree_create_folder_node(NULL,
NULL,
messages_get("Root"),
false, false, false);
if (tree->root == NULL) {
free(title);
free(tree);
return NULL;
}
@ -553,18 +546,7 @@ static void tree_recalculate_node_sizes(struct tree *tree, struct node *node,
}
/**
* Creates a folder node with the specified title, and optionally links it into
* the tree.
*
* \param tree the owner tree of 'parent', may be NULL
* \param parent the parent node, or NULL not to link
* \param title the node title (not copied, used directly)
* \param editable if true, the node title will be editable
* \param retain_in_memory if true, the node will stay in memory after deletion
* \param deleted if true, the node is created with the deleted flag
* \return the newly created node.
*/
/* exported interface documented in desktop/tree.h */
struct node *tree_create_folder_node(struct tree *tree, struct node *parent,
const char *title, bool editable, bool retain_in_memory,
bool deleted)
@ -575,16 +557,20 @@ struct node *tree_create_folder_node(struct tree *tree, struct node *parent,
node = calloc(sizeof(struct node), 1);
if (node == NULL) {
LOG(("calloc failed"));
warn_user("NoMemory", 0);
return NULL;
}
node->data.text = strdup(title);
if (node->data.text == NULL) {
free(node);
return NULL;
}
node->folder = true;
node->retain_in_memory = retain_in_memory;
node->deleted = deleted;
node->data.parent = node;
node->data.type = NODE_ELEMENT_TEXT;
node->data.text = title;
node->data.flag = TREE_ELEMENT_TITLE;
node->data.editable = editable;
node->sort = NULL;

View File

@ -137,6 +137,19 @@ void tree_setup_colours(void);
struct tree *tree_create(unsigned int flags,
const struct treeview_table *callbacks,
void *client_data);
/**
* Creates a folder node with the specified title, and optionally links it into
* the tree.
*
* \param tree the owner tree of 'parent', may be NULL
* \param parent the parent node, or NULL not to link
* \param title the node title
* \param editable if true, the node title will be editable
* \param retain_in_memory if true, the node will stay in memory after deletion
* \param deleted if true, the node is created with the deleted flag
* \return the newly created node or NULL on error.
*/
struct node *tree_create_folder_node(struct tree *tree, struct node *parent,
const char *title, bool editable, bool retain_in_memory,
bool deleted);

View File

@ -683,6 +683,7 @@ static bool tree_url_load_directory_cb(dom_node *node, void *ctx)
dir = tree_create_folder_node(tctx->tree, tctx->directory,
title, true, false, false);
free(title);
if (dir == NULL) {
dom_string_unref(name);
return false;