summaryrefslogtreecommitdiff
path: root/gladeui/glade-name-context.c
blob: 39bded7cb76fa0cc25529ba9b055884025ffc1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * glade-name-context.c
 *
 * Copyright (C) 2008 Tristan Van Berkom.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Tristan Van Berkom <tvb@gnome.org>
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>

#include "glade-id-allocator.h"
#include "glade-name-context.h"

struct _GladeNameContext
{
  GHashTable *name_allocators;

  GHashTable *names;
};

/**
 * glade_name_context_new: (skip)
 *
 * Creates a new name context
 *
 * Returns: a new GladeNameContext
 */
GladeNameContext *
glade_name_context_new (void)
{
  GladeNameContext *context = g_slice_new0 (GladeNameContext);

  context->name_allocators = g_hash_table_new_full (g_str_hash,
                                                    g_str_equal,
                                                    g_free,
                                                    (GDestroyNotify)
                                                    glade_id_allocator_destroy);

  context->names = g_hash_table_new_full (g_str_hash,
                                          g_str_equal, g_free, NULL);

  return context;
}

void
glade_name_context_destroy (GladeNameContext *context)
{
  g_return_if_fail (context != NULL);

  g_hash_table_destroy (context->name_allocators);
  g_hash_table_destroy (context->names);
  g_slice_free (GladeNameContext, context);
}

gchar *
glade_name_context_new_name (GladeNameContext *context,
                             const gchar      *base_name)
{
  GladeIDAllocator *id_allocator;
  const gchar *number;
  gchar *name = NULL, *freeme = NULL;
  guint i = 1;

  g_return_val_if_fail (context != NULL, NULL);
  g_return_val_if_fail (base_name && base_name[0], NULL);

  number = base_name + strlen (base_name);
  while (number > base_name && g_ascii_isdigit (number[-1]))
    --number;

  if (*number)
    {
      freeme = g_strndup (base_name, number - base_name);
      base_name = freeme;
    }

  id_allocator = g_hash_table_lookup (context->name_allocators, base_name);

  if (id_allocator == NULL)
    {
      id_allocator = glade_id_allocator_new ();
      g_hash_table_insert (context->name_allocators,
                           g_strdup (base_name), id_allocator);
    }

  do
    {
      g_free (name);
      i = glade_id_allocator_allocate (id_allocator);
      name = g_strdup_printf ("%s%u", base_name, i);
    }
  while (glade_name_context_has_name (context, name));

  g_free (freeme);
  return name;
}

guint
glade_name_context_n_names (GladeNameContext *context)
{
  g_return_val_if_fail (context != NULL, FALSE);

  return g_hash_table_size (context->names);
}

gboolean
glade_name_context_has_name (GladeNameContext *context, const gchar *name)
{
  g_return_val_if_fail (context != NULL, FALSE);
  g_return_val_if_fail (name && name[0], FALSE);

  return (g_hash_table_lookup (context->names, name) != NULL);
}

gboolean
glade_name_context_add_name (GladeNameContext *context, const gchar *name)
{
  gboolean ret = FALSE;

  g_return_val_if_fail (context != NULL, FALSE);
  g_return_val_if_fail (name && name[0], FALSE);

  if (!glade_name_context_has_name (context, name))
    {
      g_hash_table_insert (context->names, g_strdup (name),
                           GINT_TO_POINTER (TRUE));
      ret = TRUE;
    }

  return ret;
}

void
glade_name_context_release_name (GladeNameContext *context, const gchar *name)
{

  const gchar *first_number = name;
  gchar *end_number, *base_name;
  GladeIDAllocator *id_allocator;
  gunichar ch;
  gint id;

  g_return_if_fail (context != NULL);
  g_return_if_fail (name && name[0]);

  /* Remove from name hash first... */
  g_hash_table_remove (context->names, name);

  do
    {
      ch = g_utf8_get_char (first_number);

      if (ch == 0 || g_unichar_isdigit (ch))
        break;

      first_number = g_utf8_next_char (first_number);
    }
  while (TRUE);

  /* if there is a number - then we have to unallocate it... */
  if (ch == 0)
    return;

  base_name = g_strdup (name);
  *(base_name + (first_number - name)) = 0;

  if ((id_allocator =
       g_hash_table_lookup (context->name_allocators, base_name)) != NULL)
    {
      id = (int) strtol (first_number, &end_number, 10);
      if (*end_number == 0)
        glade_id_allocator_release (id_allocator, id);
    }

  g_free (base_name);
}