static void xml_print_tree_internal(GNode *node, gpointer data){ NODE_DATA *node_data; node_data = (NODE_DATA *)(node->data); if(node_data->name){ print_indent(node_data->depth); printf("%s", node_data->name); if(node_data->attr != NULL){ GList *list; NODE_ATTR *attr; list = g_list_first(node_data->attr); while(list){ attr = (NODE_ATTR *)(list->data); g_print(" %s=%s", attr->name, attr->value); list = g_list_next(list); } } g_print("\n"); if(G_NODE_IS_LEAF(node)){ print_indent2(node_data->depth); if(node_data->content != NULL){ printf(">%s<\n", node_data->content); } else { printf("NULL\n"); } } else { g_node_children_foreach(node, G_TRAVERSE_ALL, (GNodeForeachFunc)xml_print_tree_internal, (gpointer)NULL); } } else { g_node_children_foreach(node, G_TRAVERSE_ALL, (GNodeForeachFunc)xml_print_tree_internal, (gpointer)NULL); } }
static void check_and_store (GNode* node, gpointer data) { TreeElement* temp = (TreeElement*) node -> data; STOCKINFO* stock_data = (STOCKINFO*) temp -> userdata; /* 얕은 복사 */ regex_t_and_node* temp_data = (regex_t_and_node*) malloc (sizeof (regex_t_and_node)); *temp_data = *(regex_t_and_node*) data; int status = regexec (&(temp_data -> state), stock_data -> symbol, 0, NULL, 0); if (status == 0 || g_node_depth (node) == 2) { /* 얕은 복사 */ /* TreeElement* copy_data = (TreeElement*) malloc (sizeof (TreeElement)); *copy_data = *temp; */ GNode* copy_node = new_tree_node (stock_data, IS_OPENED | IS_ACTIVATED, temp_data -> array); g_node_insert (temp_data -> array, -1, copy_node); temp_data -> array = copy_node; if (!G_NODE_IS_LEAF (node)) { g_node_children_foreach (node, G_TRAVERSE_ALL, check_and_store, (gpointer) temp_data); } /* Recursive call */ } free (temp_data); return; }
static struct lua_cmd_entry *lua_cmd_find(gchar **argv, gint *offp) { gchar **argp; GNode *node; if (main_tree == NULL) return NULL; node = main_tree; argp = argv; for (;;) { node = g_node_first_child(node); if (G_NODE_IS_LEAF(node)) { struct lua_cmd_entry *entry; entry = (struct lua_cmd_entry *) node->data; *offp = argp - argv - 1; return entry; } if (*argp == NULL) return NULL; for (; node != NULL; node = g_node_next_sibling(node)) if (! strcmp(*argp, node->data)) break; if (node == NULL) return NULL; argp++; } }
/** * g_node_children_foreach: * @node: a #GNode * @flags: which types of children are to be visited, one of * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES * @func: the function to call for each visited node * @data: user data to pass to the function * * Calls a function for each of the children of a #GNode. * Note that it doesn't descend beneath the child nodes. */ void g_node_children_foreach (GNode *node, GTraverseFlags flags, GNodeForeachFunc func, gpointer data) { g_return_if_fail (node != NULL); g_return_if_fail (flags <= G_TRAVERSE_MASK); g_return_if_fail (func != NULL); node = node->children; while (node) { GNode *current; current = node; node = current->next; if (G_NODE_IS_LEAF (current)) { if (flags & G_TRAVERSE_LEAFS) func (current, data); } else { if (flags & G_TRAVERSE_NON_LEAFS) func (current, data); } } }
/** * g_node_find_child: * @node: a #GNode * @flags: which types of children are to be searched, one of * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES * @data: the data to find * * Finds the first child of a #GNode with the given data. * * Returns: the found child #GNode, or %NULL if the data is not found */ GNode* g_node_find_child (GNode *node, GTraverseFlags flags, gpointer data) { g_return_val_if_fail (node != NULL, NULL); g_return_val_if_fail (flags <= G_TRAVERSE_MASK, NULL); node = node->children; while (node) { if (node->data == data) { if (G_NODE_IS_LEAF (node)) { if (flags & G_TRAVERSE_LEAFS) return node; } else { if (flags & G_TRAVERSE_NON_LEAFS) return node; } } node = node->next; } return NULL; }
static void xml_destroy_tree_internal(GNode *node, gpointer data){ NODE_DATA *node_data; #ifdef XML_TRACE LOG(LOG_DEBUG, "IN : xml_destroy_tree_internal()"); #endif node_data = (NODE_DATA *)(node->data); if(G_NODE_IS_LEAF(node)){ if(node_data->name) g_free(node_data->name); if(node_data->content) g_free(node_data->content); if(node_data->attr != NULL){ GList *list; NODE_ATTR *attr; list = g_list_first(node_data->attr); while(list){ attr = (NODE_ATTR *)(list->data); if(attr->name) g_free(attr->name); if(attr->value) g_free(attr->value); g_free(attr); list = g_list_next(list); } } } else { g_node_children_foreach(node, G_TRAVERSE_ALL, (GNodeForeachFunc)xml_destroy_tree_internal, (gpointer)NULL); } #ifdef XML_TRACE LOG(LOG_DEBUG, "OUT : xml_destroy_tree_internal()"); #endif }
void open_close_branch (GNode* parent, int flag) { if (!G_NODE_IS_LEAF (parent)) { /* remove open flag */ TreeElement* temp = (TreeElement*) parent -> data; temp -> state_info -= (temp -> state_info & BASIS_OPENED); /* and redefine */ temp -> state_info += flag; g_node_children_foreach (parent, G_TRAVERSE_ALL, activate_node, &flag); } }
se_key_command_t se_modemap_lookup_keybinding( se_modemap *map, se_key_seq keyseq ) { GNode *np = se_modemap_find_match_node( map, keyseq ); if ( np && G_NODE_IS_LEAF(np) ) { se_modemap_data *data = np->data; g_assert( data->cmd ); return data->cmd; } return NULL; }
int se_modemap_keybinding_exists(se_modemap* map, se_key_seq keyseq) { GNode *np = se_modemap_find_match_node( map, keyseq ); if ( np ) { if ( G_NODE_IS_LEAF(np) ) { se_modemap_data *data = np->data; g_assert( data->cmd ); return TRUE; } } return FALSE; }
se_key_command_t se_modemap_lookup_command_ex( se_modemap *map, se_key_seq keyseq ) { GNode *np = se_modemap_find_match_node( map, keyseq ); if ( np ) { if ( G_NODE_IS_LEAF(np) ) { se_modemap_data *data = np->data; return data->cmd; } return se_second_dispatch_command; } return NULL; }
se_key_command_t se_modemap_lookup_command( se_modemap *map, se_key key ) { GNode *np = se_modemap_search_next_level( map, map->root, key ); if ( np ) { if ( G_NODE_IS_LEAF(np) ) { se_modemap_data *data = np->data; g_assert( data->cmd ); return data->cmd; } return se_second_dispatch_command; } return NULL; }
static void ls_print_tree(GNode *node, gboolean deep) { __extension__ void node_walk(GNode *node, guint off, gboolean deep) { node = g_node_first_child(node); for (; node != NULL; node = g_node_next_sibling(node)) { if (G_NODE_IS_LEAF(node)) continue; g_print("%*s%s\n", off, "", (gchar *) node->data); if (deep) node_walk(node, off + 2, deep); } }
static void activate_node (GNode* node, gpointer data) { TreeElement* temp = (TreeElement*) node -> data; STOCKINFO* temp_stock = (STOCKINFO*) temp -> userdata; /* for debugging */ /* remove activate flag */ temp -> state_info -= (temp -> state_info & BASIS_ACTIVATED); /* and redefine */ int flag = *(int*) data; if ((flag & BASIS_OPENED) == IS_CLOSED && !G_NODE_IS_LEAF (node) && (temp -> state_info & BASIS_OPENED) == IS_OPENED) { open_close_branch (node, flag); /* Recursive call */ } if (flag == IS_OPENED) { temp -> state_info += IS_ACTIVATED; } else temp -> state_info += IS_NOT_ACTIVATED; }
static char *generator(const char *text, int state) { static GNode *node; static int len; char *data; if (!state) { node = g_node_first_child(head_node); len = strlen(text); } if (node == NULL || G_NODE_IS_LEAF(node)) return NULL; while (node != NULL) { data = (char *) node->data; node = g_node_next_sibling(node); if (!strncmp(text, data, len)) return g_strdup(data); } return NULL; }
void se_modemap_insert_keybinding_str( se_modemap *map, const char* keys_rep, se_key_command_t cmd ) { g_assert( map && keys_rep && cmd ); se_key_seq keyseq = se_key_seq_from_string( keys_rep ); if ( keyseq.len <= 0 ) { se_warn( "bad key seq" ); return; } if ( se_modemap_keybinding_exists(map, keyseq) ) { /* se_msg("remap an existed keybinding" ); */ } GNode *node = map->root; int need_create_node = FALSE; // trace from which level we need to create nodes for (int i = 0; i < keyseq.len; ++i) { se_key key = keyseq.keys[i]; /* se_debug( "current key %s", se_key_to_string(key) ); */ if ( !need_create_node ) { GNode *key_node = se_modemap_search_next_level(map, node, key); if ( !key_node ) { need_create_node = TRUE; --i; /* se_debug( "set need_create_node and i = %d", i ); */ continue; } /* se_debug( "key %s exists at level %d", se_key_to_string(key), */ /* g_node_depth(node) ); */ node = key_node; se_modemap_data *data = key_node->data; g_assert( data ); g_assert( se_key_is_equal(key, data->key) ); if ( i+1 == keyseq.len ) { // bind to a cmd if ( !G_NODE_IS_LEAF(key_node) ) { // delete sub tree /* se_debug( "delete sub tree" ); */ se_delete_subtree( key_node ); } g_assert( G_NODE_IS_LEAF(key_node) ); data->cmd = cmd; /* se_debug( "rebind key %s", se_key_to_string(key) ); */ } else { if ( G_NODE_IS_LEAF(key_node) ) { g_assert( data->cmd ); data->cmd = NULL; need_create_node = TRUE; // create nodes from next level /* se_debug( "set need_create_node, reset key %s to Null", */ /* se_key_to_string(key) ); */ } else { g_assert( data->cmd == NULL ); } } } else { se_modemap_data *data = g_malloc0( sizeof(se_modemap_data) ); data->key = key; GNode *key_node = g_node_append_data( node, data ); if ( i+1 == keyseq.len ) { // bind to a cmd /* se_debug( "leaf node (level %d) of key %s: bind cmd", */ /* g_node_depth(key_node), se_key_to_string(data->key) ); */ data->cmd = cmd; } node = key_node; } } }
/* Read selections from a common xml-file. Called when loading the plugin. * Returns TRUE if data has been read, FALSE if no data is available * or an error occurred. * This is analog to folder.h::folder_read_list. */ gboolean notification_foldercheck_read_array(void) { gchar *path; GNode *rootnode, *node, *branchnode; XMLNode *xmlnode; gboolean success = FALSE; path = foldercheck_get_array_path(); if(!is_file_exist(path)) { path = NULL; return FALSE; } /* We don't do merging, so if the file existed, clear what we have stored in memory right now.. */ notification_free_folder_specific_array(); /* .. and evaluate the file */ rootnode = xml_parse_file(path); path = NULL; if(!rootnode) return FALSE; xmlnode = rootnode->data; /* Check that root entry is "foldercheckarray" */ if(strcmp2(xmlnode->tag->tag, "foldercheckarray") != 0) { g_warning("wrong foldercheck array file"); xml_free_tree(rootnode); return FALSE; } /* Process branch entries */ for(branchnode = rootnode->children; branchnode != NULL; branchnode = branchnode->next) { GList *list; guint id; SpecificFolderArrayEntry *entry = NULL; xmlnode = branchnode->data; if(strcmp2(xmlnode->tag->tag, "branch") != 0) { g_warning("tag name != \"branch\""); return FALSE; } /* Attributes of the branch nodes */ list = xmlnode->tag->attr; for(; list != NULL; list = list->next) { XMLAttr *attr = list->data; if(attr && attr->name && attr->value && !strcmp2(attr->name, "name")) { id = notification_register_folder_specific_list(attr->value); entry = foldercheck_get_entry_from_id(id); /* We have found something */ success = TRUE; break; } } if((list == NULL) || (entry == NULL)) { g_warning("Did not find attribute \"name\" in tag \"branch\""); continue; /* with next branch */ } /* Now descent into the children of the brach, which are the folderitems */ for(node = branchnode->children; node != NULL; node = node->next) { FolderItem *item = NULL; /* These should all be leaves. */ if(!G_NODE_IS_LEAF(node)) g_warning("Subnodes in \"branch\" nodes should all be leaves. " "Ignoring deeper subnodes."); /* Check if tag is "folderitem" */ xmlnode = node->data; if(strcmp2(xmlnode->tag->tag, "folderitem") != 0) { g_warning("tag name != \"folderitem\""); continue; /* to next node in branch */ } /* Attributes of the leaf nodes */ list = xmlnode->tag->attr; for(; list != NULL; list = list->next) { XMLAttr *attr = list->data; if(attr && attr->name && attr->value && !strcmp2(attr->name, "identifier")) { item = folder_find_item_from_identifier(attr->value); break; } } if((list == NULL) || (item == NULL)) { g_warning("Did not find attribute \"identifier\" in tag " "\"folderitem\""); continue; /* with next leaf node */ } /* Store all FolderItems in the list */ /* We started with a cleared array, so we don't need to check if it's already in there. */ entry->list = g_slist_prepend(entry->list, item); } /* for all subnodes in branch */ } /* for all branches */ return success; }
static gboolean store_into_g_ptr_array (GNode* node, gpointer g_ptr_array) { TreeElement* temp = (TreeElement*) node -> data; STOCKINFO* temp_stock = (STOCKINFO*) temp -> userdata; /* for debugging */ if ((temp -> state_info & BASIS_ACTIVATED) == IS_ACTIVATED) { /* node is the root */ if (g_node_depth (node) == 1) { /* initialize */ temp -> parent = NULL; temp -> lastchild = g_node_last_child (node); /* lastchild */ memset (temp -> base_format, 0x0, sizeof (temp -> base_format)); memset (temp -> format, 0x0, sizeof (temp -> format)); /* base_format */ /* nothing to do */ /* format */ char sign; if (!G_NODE_IS_LEAF (node)) { if ((temp -> state_info & BASIS_OPENED) == IS_OPENED) sign = '-'; else sign = '+'; strncpy (temp -> format, &sign ,1); } } /* node is not root */ else { /* initialize */ memset (temp -> base_format, 0x0, sizeof (temp -> base_format)); memset (temp -> format, 0x0, sizeof (temp -> format)); temp -> lastchild = g_node_last_child (node); /* lastchild */ /* extend the parent's base_format */ GNode* parent = temp -> parent; TreeElement* temp_parent = (TreeElement*) parent -> data; int length = strlen (temp_parent -> base_format); strncpy (temp -> base_format, temp_parent -> base_format, length); /* base_format */ char base_format_link [3]; char* temp_base_format; TreeElement* temp_grand_parent = NULL; if (temp_parent -> parent) { GNode* grand_parent = temp_parent -> parent; temp_grand_parent = grand_parent -> data; if (temp_grand_parent -> lastchild == temp -> parent) temp_base_format = "\t\0"; else temp_base_format = "|\t"; } else { temp_base_format = "\t\0"; } strncpy (base_format_link, temp_base_format, 3); length = strlen (temp -> base_format); strncpy (temp -> base_format + length, base_format_link, 3); /* format */ length = strlen (temp -> base_format); strncpy (temp -> format, temp -> base_format, length); /* ARM */ char* arm; if (node == temp_parent -> lastchild) { arm = "*-"; } else { arm = "|-"; } length = strlen (temp -> format); strncpy (temp -> format + length, arm, 3); /* SIGN */ char sign; if (!G_NODE_IS_LEAF (node)) { if ((temp -> state_info & BASIS_OPENED) == IS_OPENED) sign = '-'; else sign = '+'; length = strlen (temp -> format); strncpy (temp -> format + length, &sign ,1); } else { temp -> state_info += IS_LEAF; } } g_ptr_array_add (g_ptr_array, temp); } return false; }
static void xml_save_file_internal(GNode *node, gpointer data){ FILE *fp; NODE_DATA *node_data; gchar *tmp_p; gchar *utf_str; #ifdef XML_TRACE LOG(LOG_DEBUG, "IN : xml_save_file_internal()"); #endif fp = (FILE *)data; node_data = (NODE_DATA *)(node->data); if(node_data->name){ indent_tag(fp, node_data->depth); fprintf(fp, "<%s",node_data->name); if(node_data->attr != NULL){ GList *list; NODE_ATTR *attr; list = g_list_first(node_data->attr); while(list){ attr = (NODE_ATTR *)(list->data); tmp_p = special_to_encoded(attr->value); utf_str = iconv_convert("utf-8", node_data->doc->encoding, tmp_p); fprintf(fp, " %s=\"%s\"", attr->name, utf_str); g_free(tmp_p); g_free(utf_str); list = g_list_next(list); } } fprintf(fp, ">"); if(G_NODE_IS_LEAF(node)){ if (node_data->content != NULL){ tmp_p = special_to_encoded(node_data->content); utf_str = iconv_convert("utf-8", node_data->doc->encoding, tmp_p); fprintf(fp, "%s", utf_str); g_free(tmp_p); g_free(utf_str); } } else { fprintf(fp, "\n"); g_node_children_foreach(node, G_TRAVERSE_ALL, (GNodeForeachFunc)xml_save_file_internal, (gpointer)fp); indent_tag(fp, node_data->depth); } if(node_data->name) fprintf(fp, "</%s>\n",node_data->name); } else { g_node_children_foreach(node, G_TRAVERSE_ALL, (GNodeForeachFunc)xml_save_file_internal, (gpointer)fp); } #ifdef XML_TRACE LOG(LOG_DEBUG, "OUT : xml_save_file_internal()"); #endif }
gboolean ada_gnode_is_leaf (GNode * node) { return G_NODE_IS_LEAF (node); }