GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gtable.c
1 /********************************************************************\
2  * gtable.c -- glib -- basic datatype for 2D array of values *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA [email protected] *
20  * *
21 \********************************************************************/
22 
23 #include "config.h"
24 
25 #include "gtable.h"
26 
27 
28 struct GTable
29 {
30  GArray *array;
31 
32  guint entry_size;
33 
34  int rows;
35  int cols;
36 
37  g_table_entry_constructor constructor;
38  g_table_entry_destroyer destroyer;
39 
40  gpointer user_data;
41 };
42 
43 GTable *
44 g_table_new (guint entry_size,
45  g_table_entry_constructor constructor,
46  g_table_entry_destroyer destroyer,
47  gpointer user_data)
48 {
49  GTable *gtable;
50 
51  gtable = g_new(GTable, 1);
52 
53  gtable->array = g_array_new(FALSE, FALSE, entry_size);
54 
55  gtable->entry_size = entry_size;
56 
57  gtable->rows = 0;
58  gtable->cols = 0;
59 
60  gtable->constructor = constructor;
61  gtable->destroyer = destroyer;
62 
63  gtable->user_data = user_data;
64 
65  return gtable;
66 }
67 
68 void
69 g_table_destroy (GTable *gtable)
70 {
71  if (gtable == NULL)
72  return;
73 
74  g_table_resize (gtable, 0, 0);
75 
76  g_array_free (gtable->array, TRUE);
77 
78  gtable->array = NULL;
79 
80  g_free(gtable);
81 }
82 
83 gpointer
84 g_table_index (GTable *gtable, int row, int col)
85 {
86  guint index;
87 
88  if (gtable == NULL)
89  return NULL;
90  if ((row < 0) || (col < 0))
91  return NULL;
92  if (row >= gtable->rows)
93  return NULL;
94  if (col >= gtable->cols)
95  return NULL;
96 
97  index = ((row * gtable->cols) + col) * gtable->entry_size;
98 
99  return &gtable->array->data[index];
100 }
101 
102 void
103 g_table_resize (GTable *gtable, int rows, int cols)
104 {
105  guint old_len;
106  guint new_len;
107 
108  if (gtable == NULL)
109  return;
110  if ((rows < 0) || (cols < 0))
111  return;
112 
113  old_len = gtable->array->len;
114  new_len = rows * cols;
115 
116  if (new_len == old_len)
117  return;
118 
119  /* If shrinking, destroy extra cells */
120  if ((new_len < old_len) && gtable->destroyer)
121  {
122  gchar *entry;
123  guint i;
124 
125  entry = &gtable->array->data[new_len * gtable->entry_size];
126  for (i = new_len; i < old_len; i++)
127  {
128  gtable->destroyer(entry, gtable->user_data);
129  entry += gtable->entry_size;
130  }
131  }
132 
133  /* Change the size */
134  g_array_set_size(gtable->array, new_len);
135 
136  /* If expanding, construct the new cells */
137  if ((new_len > old_len) && gtable->constructor)
138  {
139  gchar *entry;
140  guint i;
141 
142  entry = &gtable->array->data[old_len * gtable->entry_size];
143  for (i = old_len; i < new_len; i++)
144  {
145  gtable->constructor(entry, gtable->user_data);
146  entry += gtable->entry_size;
147  }
148  }
149 
150  gtable->rows = rows;
151  gtable->cols = cols;
152 }
153 
154 int
155 g_table_rows (GTable *gtable)
156 {
157  if (gtable == NULL)
158  return 0;
159 
160  return gtable->rows;
161 }
162 
163 int
164 g_table_cols (GTable *gtable)
165 {
166  if (gtable == NULL)
167  return 0;
168 
169  return gtable->cols;
170 }
Definition: gtable.c:28