Ejemplo n.º 1
0
/* glob must be valid UTF-8 */
void
_xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
			    const char  *glob,
			    const char  *mime_type,
			    int          weight,
			    int          case_sensitive)
{
  XdgGlobType type;

  assert (glob_hash != NULL);
  assert (glob != NULL);

  type = _xdg_glob_determine_type (glob);

  switch (type)
    {
    case XDG_GLOB_LITERAL:
      glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
      break;
    case XDG_GLOB_SIMPLE:
      glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, mime_type, weight, case_sensitive);
      break;
    case XDG_GLOB_FULL:
      glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
      break;
    }
}
Ejemplo n.º 2
0
static XdgGlobHashNode *
_xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
			    const char      *text,
			    const char      *mime_type)
{
  XdgGlobHashNode *node;
  xdg_unichar_t character;

  character = _xdg_utf8_to_ucs4 (text);

  if ((glob_hash_node == NULL) ||
      (character < glob_hash_node->character))
    {
      node = _xdg_glob_hash_node_new ();
      node->character = character;
      node->next = glob_hash_node;
      glob_hash_node = node;
    }
  else if (character == glob_hash_node->character)
    {
      node = glob_hash_node;
    }
  else
    {
      XdgGlobHashNode *prev_node;
      int found_node = FALSE;

      /* Look for the first character of text in glob_hash_node, and insert it if we
       * have to.*/
      prev_node = glob_hash_node;
      node = prev_node->next;

      while (node != NULL)
	{
	  if (character < node->character)
	    {
	      node = _xdg_glob_hash_node_new ();
	      node->character = character;
	      node->next = prev_node->next;
	      prev_node->next = node;

	      found_node = TRUE;
	      break;
	    }
	  else if (character == node->character)
	    {
	      found_node = TRUE;
	      break;
	    }
	  prev_node = node;
	  node = node->next;
	}

      if (! found_node)
	{
	  node = _xdg_glob_hash_node_new ();
	  node->character = character;
	  node->next = prev_node->next;
	  prev_node->next = node;
	}
    }

  text = _xdg_utf8_next_char (text);
  if (*text == '\000')
    {
      if (node->mime_type)
	{
	  if (strcmp (node->mime_type, mime_type))
	    {
	      XdgGlobHashNode *child;
	      int found_node = FALSE;
	      
	      child = node->child;
	      while (child && child->character == '\0')
		{
		  if (strcmp (child->mime_type, mime_type) == 0)
		    {
		      found_node = TRUE;
		      break;
		    }
		  child = child->next;
		}

	      if (!found_node)
		{
		  child = _xdg_glob_hash_node_new ();
		  child->character = '\000';
		  child->mime_type = strdup (mime_type);
		  child->child = NULL;
		  child->next = node->child;
		  node->child = child;
		}
	    }
	}
      else
	{
	  node->mime_type = strdup (mime_type);
	}
    }
  else
    {
      node->child = _xdg_glob_hash_insert_text (node->child, text, mime_type);
    }
  return glob_hash_node;
}