Example #1
0
GTree*
meta0_utils_list_to_tree(GSList *list)
{
	GSList *l;
	GTree *result = NULL;

	EXTRA_ASSERT(list != NULL);

	result = g_tree_new_full(
			hashstr_quick_cmpdata, NULL,
			g_free, (GDestroyNotify)garray_free);

	for (l=list; l ;l=l->next) {
		struct meta0_info_s *m0i;

		if (!(m0i = l->data))
			continue;

		gchar url[STRLEN_ADDRINFO];
		grid_addrinfo_to_string(&(m0i->addr), url, sizeof(url));

		gsize len = m0i->prefixes_size;
		len = len / 2;
		GArray *pfx = g_array_new(FALSE, FALSE, sizeof(guint16));
		g_array_append_vals(pfx, m0i->prefixes, len);

		g_tree_replace(result, hashstr_create(url), pfx);
	}
	
	return result;
}
static void
add_new_waiting_for (SymbolDBViewLocals *dbvl, gint parent_symbol_id, 
					 const gchar* symbol_name, 
					 gint symbol_id, const GdkPixbuf *pixbuf)
{
	SymbolDBViewLocalsPriv *priv;
	gpointer node;
	
	g_return_if_fail (dbvl != NULL);	
	priv = dbvl->priv;

	/* we don't want a negative parent_symbol_id */
	if (parent_symbol_id < 0)
		return;	
	
	/* check if we already have some children waiting for a 
	 * specific father to be inserted, then add this symbol_id to the list 
	 * (or create a new one)
	 */
	WaitingForSymbol *wfs;
			
	wfs = g_new0 (WaitingForSymbol, 1);
	wfs->child_symbol_id = symbol_id;
	wfs->child_symbol_name = g_strdup (symbol_name);
	wfs->pixbuf = pixbuf;
				
/*	DEBUG_PRINT ("add_new_waiting_for (): looking up waiting_for %d", 
				 parent_symbol_id);*/
	node = g_tree_lookup (priv->waiting_for, GINT_TO_POINTER (parent_symbol_id));
	if (node == NULL) 
	{
		/* no lists already set. Create one. */
		GSList *slist;					
		slist = g_slist_alloc ();			
				
		slist = g_slist_prepend (slist, wfs);
					
		/*DEBUG_PRINT ("add_new_waiting_for (): NEW adding to "
					 "waiting_for [%d]", parent_symbol_id);*/
				
		/* add it to the binary tree. */
		g_tree_insert (priv->waiting_for, GINT_TO_POINTER (parent_symbol_id), 
							   slist);
	}
	else 
	{
		/* found a list */
		GSList *slist;
		slist = (GSList*)node;
		
		/*DEBUG_PRINT ("prepare_for_adding (): NEW adding to "
					 "parent_waiting_for_list [%d] %s",
				 	parent_symbol_id, symbol_name);*/
		slist = g_slist_prepend (slist, wfs);
				
		g_tree_replace (priv->waiting_for, GINT_TO_POINTER (parent_symbol_id), 
						slist);
	}	
}
Example #3
0
void     S52_tree_replace(GTree *tree, gpointer key, gpointer value)
{
#ifdef S52_USE_GLIB2
    g_tree_replace(tree, key, value);
#else
    g_tree_insert(tree, key, value);
#endif
}
Example #4
0
/* traverse function to map names to ports */
static gboolean services_port_trv(gpointer key, gpointer value, gpointer data)
{
  const port_service_t *svc = (const port_service_t *)value;
  GTree *tree = (GTree *)data;
  port_service_t *new_el;
  
  new_el = port_service_new(svc->port, svc->name);
  g_tree_replace(tree, new_el->name, new_el);
  return FALSE;
}
void
cap_file_provider_set_user_comment(struct packet_provider_data *prov, frame_data *fd, const char *new_comment)
{
  if (!prov->frames_user_comments)
    prov->frames_user_comments = g_tree_new_full(frame_cmp, NULL, NULL, g_free);

  /* insert new packet comment */
  g_tree_replace(prov->frames_user_comments, fd, g_strdup(new_comment));

  fd->has_user_comment = TRUE;
}
Example #6
0
void     S52_tree_replace(GTree *tree, gpointer key, gpointer value)
{
#ifdef S52_USE_GLIB2
    g_tree_replace(tree, key, value);
#else
    //g_assert_not_reached();
    //g_tree_remove(tree, key);
    // WARNING: can't load mutiple PLib
    g_tree_insert(tree, key, value);
#endif
}
Example #7
0
static struct object_version_s*
version_get(gboolean init, GTree *t, const struct hashstr_s *k)
{
	struct object_version_s *o;
	o = g_tree_lookup(t, k);
	if (!o && init) {
		o = g_malloc0(sizeof(struct object_version_s));
		o->version = 1;
		g_tree_replace(t, hashstr_dup(k), o);
	}
	return o;
}
Example #8
0
void rpc_service_register(rpc_service_t *service, const char *method_name,
                          rpc_callback *callback, void *data) {
  rpc_method *method = calloc(1, sizeof(rpc_method));
  rpc_name *name = calloc(1, sizeof(rpc_method));

  name->name = method_name;
  name->len = strlen(method_name);

  method->callback = callback;
  method->data = data;
  printf("Registering method '%.*s'\n", (int)name->len, name->name);
  g_tree_replace(service->methods, name, method);
} /* rpc_service_register */
Example #9
0
int main() {
    printf("Create a new GTree using g_tree_new_full().\n");
    GTree *tree = g_tree_new_full((GCompareDataFunc)g_ascii_strcasecmp, NULL,
            (GDestroyNotify)destroy_key, (GDestroyNotify)destroy_val);
    printf("Add items to the tree.\n");
    g_tree_insert(tree, "c", "Captain America");
    g_tree_insert(tree, "i", "Iron Man");
    g_tree_insert(tree, "h", "Hawk Eye");
    g_tree_insert(tree, "s", "Spider Man");
    printf("Tree height from g_tree_height(): %d\n", g_tree_height(tree));
    printf("Number of nodes from g_tree_nnodes(): %d\n", g_tree_nnodes(tree));
    printf("List the tree using g_tree_foreach() and a GTraverseFunc.\n");
    printf("Tree Contents:\n");
    g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL);
    printf("\n");

    printf("Find items using g_tree_lookup().\n");
    printf("The data at 'i' is '%s'\n", g_tree_lookup(tree, "i"));
    printf("%s\n\n", g_tree_lookup(tree, "d") ?
            "Unexpected value!!" : "No data found for 'd'");

    printf("Find items using g_tree_search() and a GCompareFunc.\n");
    gpointer val = g_tree_search(tree, (GCompareFunc)finder, NULL);
    printf("The value is %s\n\n", val);

    printf("Remove an item using g_tree_remove().\n");
    g_tree_remove(tree, "h");
    printf("Number of nodes is now: %d\n", g_tree_nnodes(tree));
    printf("Tree Contents:\n");
    g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL);
    printf("\n");

    printf("Replacing 's', destroy functions will be called.\n");
    g_tree_replace(tree, "s", "Storm");
    printf("Number of nodes is now: %d\n", g_tree_nnodes(tree));
    printf("Tree Contents:\n");
    g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL);
    printf("\n");

    printf("Stealing 'c' with g_tree_steal(), no drstroy functions now.\n");
    g_tree_steal(tree, "c");
    printf("Number of nodes is now: %d\n", g_tree_nnodes(tree));
    printf("Tree Contents:\n");
    g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL);
    printf("\n");

    printf("Destroying the tree using g_tree_destroy().\n");
    g_tree_destroy(tree);
}
Example #10
0
GTree*
meta0_utils_tree_add_url(GTree *tree, const guint8 *b, const gchar *url)
{
	GArray *prefixes;
	hashstr_t *hu;

	HASHSTR_ALLOCA(hu, url);
	prefixes = g_tree_lookup(tree, hu);
	if (!prefixes) {
		prefixes = g_array_new(FALSE, FALSE, 2);
		g_tree_replace(tree, hashstr_dup(hu), prefixes);
	}
	g_array_append_vals(prefixes, b, 1);

	return tree;
}
static void
enumerate_dicts (const char * const lang_tag,
		 const char * const provider_name,
		 const char * const provider_desc,
		 const char * const provider_file,
		 void * user_data)
{
	gchar *lang_name;
	
	GTree *dicts = (GTree *)user_data;
	
	lang_name = create_name_for_language (lang_tag);
	g_return_if_fail (lang_name != NULL);
	
	/* g_print ("%s - %s\n", lang_tag, lang_name); */
	
	g_tree_replace (dicts, g_strdup (lang_tag), lang_name);
}
Example #12
0
static GError*
_list (const char *target, GByteArray *request,
		struct list_result_s *out, gchar ***out_properties)
{
	GError *err = NULL;
	GTree *props = NULL;
	gboolean _cb(gpointer ctx, MESSAGE reply) {
		(void) ctx;

		/* Extract replied aliases */
		GSList *l = NULL;
		GError *e = metautils_message_extract_body_encoded(reply, FALSE, &l, bean_sequence_decoder);
		if (e) {
			GRID_DEBUG("Callback error : %s", e->message);
			err = e;
			return FALSE;
		}

		out->beans = metautils_gslist_precat (out->beans, l);

		/* Extract list flags */
		e = metautils_message_extract_boolean (reply,
				NAME_MSGKEY_TRUNCATED, FALSE, &out->truncated);
		if (e)
			g_clear_error (&e);
		gchar *tok = NULL;
		tok = metautils_message_extract_string_copy (reply, NAME_MSGKEY_NEXTMARKER);
		oio_str_reuse (&out->next_marker, tok);

		/* Extract properties and merge them into the temporary TreeSet. */
		if (out_properties) {
			gchar **names = metautils_message_get_field_names (reply);
			for (gchar **n=names ; n && *n ;++n) {
				if (!g_str_has_prefix (*n, NAME_MSGKEY_PREFIX_PROPERTY))
					continue;
				g_tree_replace (props,
						g_strdup((*n) + sizeof(NAME_MSGKEY_PREFIX_PROPERTY) - 1),
						metautils_message_extract_string_copy(reply, *n));
			}
			if (names) g_strfreev (names);
		}

		return TRUE;
	}
Example #13
0
static GTree *
xmms_volume_map_to_dict (xmms_volume_map_t *vl)
{
	GTree *ret;
	gint i;

	ret = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
	                       NULL, (GDestroyNotify) xmmsv_unref);
	if (!ret) {
		return NULL;
	}

	for (i = 0; i < vl->num_channels; i++) {
		xmmsv_t *val;

		val = xmmsv_new_int (vl->values[i]);
		g_tree_replace (ret, (gpointer) vl->names[i], val);
	}

	return ret;
}
Example #14
0
                                    /* TODO this is probably this single piece of code I am most ashamed of.
 * I should learn how to use flex or yacc and do this The Right Way (TM)*/
void services_init(void)
{
  FILE *services = NULL;
  gchar *line;
  gchar **t1 = NULL, **t2 = NULL;
  gchar *str;
  port_service_t *port_service;
  guint i;
  char filename[PATH_MAX];
  port_type_t port_number;	/* udp and tcp are the same */

  if (tcp_services)
    return; /* already loaded */
  
  safe_strncpy(filename, CONFDIR "/services", sizeof(filename));
  if (!(services = fopen (filename, "r")))
    {
      safe_strncpy(filename, "/etc/services", sizeof(filename));
      if (!(services = fopen (filename, "r")))
	{
	  g_my_critical (_
			 ("Failed to open %s. No TCP or UDP services will be recognized"),
			 filename);
	  return;
	}
    }

  g_my_info (_("Reading TCP and UDP services from %s"), filename);

  service_names = g_tree_new_full(services_name_cmp, NULL, NULL, services_tree_free);
  tcp_services = g_tree_new_full(services_port_cmp, NULL, NULL, services_tree_free);
  udp_services = g_tree_new_full(services_port_cmp, NULL, NULL, services_tree_free);

  line = g_malloc (LINESIZE);
  g_assert(line);

  while (fgets (line, LINESIZE, services))
    {
      if (line[0] != '#' && line[0] != ' ' && line[0] != '\n'
	  && line[0] != '\t')
	{
	  gboolean error = FALSE;

	  if (!g_strdelimit (line, " \t\n", ' '))
	    error = TRUE;

	  if (error || !(t1 = g_strsplit (line, " ", 0)))
	    error = TRUE;
	  if (!error && t1[0])
            {
              gchar *told = t1[0];
              t1[0] = g_ascii_strup (told, -1);
              g_free(told);
            }
	  for (i = 1; t1[i] && !strcmp ("", t1[i]); i++)
	    ;

	  if (!error && (str = t1[i]))
	    if (!(t2 = g_strsplit (str, "/", 0)))
	      error = TRUE;

	  if (error || !t2 || !t2[0])
	    error = TRUE;

	  /* TODO The h here is not portable */
	  if (error || !sscanf (t2[0], "%hd", &port_number)
	      || (port_number < 1))
	    error = TRUE;

	  if (error || !t2[1])
	    error = TRUE;

	  if (error
	      || (g_ascii_strcasecmp ("udp", t2[1]) && g_ascii_strcasecmp ("tcp", t2[1])
		  && g_ascii_strcasecmp ("ddp", t2[1]) && g_ascii_strcasecmp ("sctp", t2[1])
                  ))
	    error = TRUE;

	  if (error)
	    g_warning (_("Unable to  parse line %s"), line);
	  else
	    {
#if DEBUG
	      g_my_debug ("Loading service %s %s %d", t2[1], t1[0],
			  port_number);
#endif
	      if (!g_ascii_strcasecmp ("ddp", t2[1]))
		g_my_info (_("DDP protocols not supported in %s"), line);
	      else if (!g_ascii_strcasecmp ("sctp", t2[1]))
		g_my_info (_("SCTP protocols not supported in %s"), line);
              else
                {
                  /* map port to name, to two trees */
		  port_service = port_service_new(port_number, t1[0]);
                  if (!g_ascii_strcasecmp ("tcp", t2[1]))
                    g_tree_replace(tcp_services, 
                                   &(port_service->port), port_service);
                  else if (!g_ascii_strcasecmp ("udp", t2[1]))
                    g_tree_replace(udp_services,
                                   &(port_service->port), port_service);
		}
	    }

	  g_strfreev (t2);
	  t2 = NULL;
	  g_strfreev (t1);
	  t1 = NULL;

	}
    }

  fclose (services);
  g_free (line);

  /* now traverse port->name trees to fill the name->port tree */
  g_tree_foreach(udp_services, services_port_trv, service_names);
  g_tree_foreach(tcp_services, services_port_trv, service_names);

  /* and finally assign preferred services */
  services_fill_preferred();
}				/* services_init */
	void add_header(const gchar *n, gchar *v) {
		EXTRA_ASSERT(!finalized);
		g_tree_replace(headers, g_strdup(n), v);
	}
Example #16
0
static void
sqlx_save_id(sqlx_cache_t *cache, sqlx_base_t *base)
{
	gpointer pointer_index = GINT_TO_POINTER(base->index + 1);
	g_tree_replace(cache->bases_by_name, base->name, pointer_index);
}
Example #17
0
int
main (int   argc,
      char *argv[])
{
  gint i;
  GTree *tree;
  gboolean removed;
  char c, d;
  char *p;

  tree = g_tree_new (my_compare);

  for (i = 0; chars[i]; i++)
    g_tree_insert (tree, &chars[i], &chars[i]);

  g_tree_foreach (tree, my_traverse, NULL);

  g_assert (g_tree_nnodes (tree) == strlen (chars));
  g_assert (g_tree_height (tree) == 6);
  
  p = chars;
  g_tree_foreach (tree, check_order, &p);

  for (i = 0; i < 26; i++)
    {
      removed = g_tree_remove (tree, &chars[i + 10]);
      g_assert (removed);
    }

  c = '\0';
  removed = g_tree_remove (tree, &c);
  g_assert (removed == FALSE);

  g_tree_foreach (tree, my_traverse, NULL);

  g_assert (g_tree_nnodes (tree) == strlen (chars2));
  g_assert (g_tree_height (tree) == 6);

  p = chars2;
  g_tree_foreach (tree, check_order, &p);

  for (i = 25; i >= 0; i--)
    g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);

  p = chars;
  g_tree_foreach (tree, check_order, &p);

  c = '0';
  p = g_tree_lookup (tree, &c); 
  g_assert (p && *p == c);

  c = 'A';
  p = g_tree_lookup (tree, &c);
  g_assert (p && *p == c);

  c = 'a';
  p = g_tree_lookup (tree, &c);
  g_assert (p && *p == c);

  c = 'z';
  p = g_tree_lookup (tree, &c);
  g_assert (p && *p == c);

  c = '!';
  p = g_tree_lookup (tree, &c);
  g_assert (p == NULL);

  c = '=';
  p = g_tree_lookup (tree, &c);
  g_assert (p == NULL);

  c = '|';
  p = g_tree_lookup (tree, &c);
  g_assert (p == NULL);

  c = '0';
  p = g_tree_search (tree, my_search, &c); 
  g_assert (p && *p == c);

  c = 'A';
  p = g_tree_search (tree, my_search, &c);
  g_assert (p && *p == c);

  c = 'a';
  p = g_tree_search (tree, my_search, &c);
  g_assert (p &&*p == c);

  c = 'z';
  p = g_tree_search (tree, my_search, &c);
  g_assert (p && *p == c);

  c = '!';
  p = g_tree_search (tree, my_search, &c);
  g_assert (p == NULL);

  c = '=';
  p = g_tree_search (tree, my_search, &c);
  g_assert (p == NULL);

  c = '|';
  p = g_tree_search (tree, my_search, &c);
  g_assert (p == NULL);


  g_tree_destroy (tree);

  tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL, 
			  my_key_destroy, 
			  my_value_destroy);

  for (i = 0; chars[i]; i++)
    g_tree_insert (tree, &chars[i], &chars[i]);
  
  c = '0';
  g_tree_insert (tree, &c, &c);
  g_assert (destroyed_key == &c);
  g_assert (destroyed_value == &chars[0]);
  destroyed_key = NULL;
  destroyed_value = NULL;

  d = '1';
  g_tree_replace (tree, &d, &d);
  g_assert (destroyed_key == &chars[1]);
  g_assert (destroyed_value == &chars[1]);
  destroyed_key = NULL;
  destroyed_value = NULL;

  c = '2';
  removed = g_tree_remove (tree, &c);
  g_assert (removed);
  g_assert (destroyed_key == &chars[2]);
  g_assert (destroyed_value == &chars[2]);
  destroyed_key = NULL;
  destroyed_value = NULL;

  c = '3';
  removed = g_tree_steal (tree, &c);
  g_assert (removed);
  g_assert (destroyed_key == NULL);
  g_assert (destroyed_value == NULL);

  return 0;
}