Back to index

indicator-appmenu  12.10.0
Classes | Functions
hudstringlist.c File Reference
#include "hudstringlist.h"
#include <string.h>

Go to the source code of this file.

Classes

struct  _HudStringList
 SECTION:hudstringlist : HudStringList : a refcounted list of strings. More...

Functions

void hud_string_list_unref (HudStringList *list)
 hud_string_list_unref: : (allow-none): a #HudStringList, possibly NULL
HudStringList * hud_string_list_ref (HudStringList *list)
 hud_string_list_ref: : (allow-none): a #HudStringList, possibly NULL
HudStringList * hud_string_list_cons (const gchar *head, HudStringList *tail)
 hud_string_list_cons: : a string for the head item : (allow-none): the tail #HudStringList, possibly NULL
const gchar * hud_string_list_get_head (HudStringList *list)
 hud_string_list_get_head: : a non-empty (non-NULL) #HudStringList
HudStringList * hud_string_list_get_tail (HudStringList *list)
 hud_string_list_get_tail: : a non-empty (non-NULL) #HudStringList
gchar * hud_string_list_pretty_print (HudStringList *list)
 hud_string_list_pretty_print: : (allow-none): a #HudStringList, possibly NULL
HudStringList * hud_string_list_cons_label (const gchar *label, HudStringList *tail)
 hud_string_list_cons_label: : (allow-none): a menuitem label : (allow-none): the tail #HudStringList, possibly NULL

Class Documentation

struct _HudStringList

SECTION:hudstringlist : HudStringList : a refcounted list of strings.

HudStringList is a refcounted list of strings.

Borrowing heavily on conventions of many functional programming languages, a list is a head element connected to a tail list (ie: the rest of the items).

A NULL pointer is considered to be a valid empty list.

Each list node is refcounted, and holds a reference on its 'tail' list. This allows common tails to be shared.

This mechanism is ideally suited to the HUD which is interested in displaying items of the form "File > New" and "File > Open". In this case, these items would be represented (in reverse) by the lists ['Open', 'File'] and ['New', 'File'] with the common tail portion shared between both items.

Each #HudStringList node uses only one variable-sized block of memory. The reference count and pointer to the 'tail' are stored in a header, followed by the 'head' string data. HudStringList:

This is an opaque structure type.

Definition at line 56 of file hudstringlist.c.

Class Members
gchar head
gint ref_count
HudStringList * tail

Function Documentation

HudStringList* hud_string_list_cons ( const gchar *  head,
HudStringList *  tail 
)

hud_string_list_cons: : a string for the head item : (allow-none): the tail #HudStringList, possibly NULL

Create a new list with as the first item and as the rest of the items.

A reference is taken on .

Returns: (transfer full): a new list

Definition at line 111 of file hudstringlist.c.

{
  HudStringList *list;
  gsize headlen;

  headlen = strlen (head);

  list = g_malloc (G_STRUCT_OFFSET (HudStringList, head) + headlen + 1);
  list->tail = hud_string_list_ref (tail);
  /* coverity[secure_coding] */
  strcpy (list->head, head);
  list->ref_count = 1;

  return list;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HudStringList* hud_string_list_cons_label ( const gchar *  label,
HudStringList *  tail 
)

hud_string_list_cons_label: : (allow-none): a menuitem label : (allow-none): the tail #HudStringList, possibly NULL

Slight "magic" helper function for doing the right thing with prepending menu labels.

is processed, removing mnemonic prefixes (ie: '_' characters) and then the function acts, essentially as hud_string_list_cons().

Returns: (transfer full): a new #HudStringList

Definition at line 199 of file hudstringlist.c.

{
  HudStringList *list;
  gint i = 0, j = 0;
  gsize headlen;

  /* For simplicity, over-allocate.  In practice, this will only
   * ever waste one byte at most (since we will only remove one
   * underscore).
   */
  headlen = strlen (label);

  list = g_malloc (G_STRUCT_OFFSET (HudStringList, head) + headlen + 1);
  list->tail = hud_string_list_ref (tail);
  list->ref_count = 1;

  while (label[j])
    {
      if (label[j] == '_' && label[j + 1])
        j++;

      list->head[i++] = label[j++];
    }
  g_assert (i <= headlen);
  list->head[i] = '\0';

  return list;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const gchar* hud_string_list_get_head ( HudStringList *  list)

hud_string_list_get_head: : a non-empty (non-NULL) #HudStringList

Gets the head string of the list.

Returns: the head element, as a normal C string

Definition at line 137 of file hudstringlist.c.

{
  return list->head;
}

Here is the caller graph for this function:

HudStringList* hud_string_list_get_tail ( HudStringList *  list)

hud_string_list_get_tail: : a non-empty (non-NULL) #HudStringList

Gets the tail of the list.

Returns: (transfer none): the tail of the list

Definition at line 151 of file hudstringlist.c.

{
  return list->tail;
}

Here is the caller graph for this function:

gchar* hud_string_list_pretty_print ( HudStringList *  list)

hud_string_list_pretty_print: : (allow-none): a #HudStringList, possibly NULL

Pretty-prints the list.

This function is intended only for debugging purposes.

Returns: the pretty-printed list

Definition at line 167 of file hudstringlist.c.

{
  GString *string;

  string = g_string_new (NULL);
  while (list)
    {
      g_string_prepend (string, list->head);

      list = list->tail;

      if (list)
        g_string_prepend (string, " > ");
    }

  return g_string_free (string, FALSE);
}
HudStringList* hud_string_list_ref ( HudStringList *  list)

hud_string_list_ref: : (allow-none): a #HudStringList, possibly NULL

Increases the reference count on .

Returns: a new reference to the list

Definition at line 90 of file hudstringlist.c.

{
  if (list)
    g_atomic_int_inc (&list->ref_count);

  return list;
}

Here is the caller graph for this function:

void hud_string_list_unref ( HudStringList *  list)

hud_string_list_unref: : (allow-none): a #HudStringList, possibly NULL

Decreases the reference count on , possibly freeing it.

Definition at line 72 of file hudstringlist.c.

{
  if (list && g_atomic_int_dec_and_test (&list->ref_count))
    {
      hud_string_list_unref (list->tail);
      g_free (list);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function: