3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "text-node.h"
32 /*===========================================*/
33 /* Local function prototypes */
34 /*===========================================*/
36 static glTextNode *extract_text_node (const gchar *text,
39 static gboolean is_empty_field (const glTextNode *text_node,
40 const glMergeRecord *record);
43 /****************************************************************************/
44 /* Expand single node into representative string. */
45 /****************************************************************************/
47 gl_text_node_expand (const glTextNode *text_node,
48 const glMergeRecord *record)
52 if (text_node->field_flag) {
54 return g_strdup_printf ("${%s}", text_node->data);
56 text = gl_merge_eval_key (record, text_node->data);
60 return g_strdup_printf ("%s", "");
64 return g_strdup (text_node->data);
69 /*--------------------------------------------------------------------------*/
70 /* PRIVATE. Is node a field that evaluates empty? */
71 /*--------------------------------------------------------------------------*/
73 is_empty_field (const glTextNode *text_node,
74 const glMergeRecord *record)
79 if ( (record != NULL) && text_node->field_flag) {
80 text = gl_merge_eval_key (record, text_node->data);
81 if ( (text == NULL) || (text[0] == 0) ) {
91 /****************************************************************************/
92 /* Create a single text node from given text. */
93 /****************************************************************************/
95 gl_text_node_new_from_text (const gchar *text)
99 return extract_text_node (text, &n);
103 /*--------------------------------------------------------------------------*/
104 /* PRIVATE. Create a single text node from given text. n = characters used */
105 /*--------------------------------------------------------------------------*/
107 extract_text_node (const gchar *text,
110 glTextNode *text_node;
114 text_node = g_new0 (glTextNode, 1);
116 if (strncmp (text, "${", strlen ("${")) == 0) {
117 /* We are at the beginning of a "FIELD" node */
118 text_node->field_flag = TRUE;
121 for (p = (gchar *)text, m = 0; *p != 0; p++, m++, (*n)++) {
127 text_node->data = g_strndup (text, m);
129 /* We are at the beginning of a literal node */
130 text_node->field_flag = FALSE;
131 for (p = (gchar *)text, *n = 0; *p != 0; p++, (*n)++) {
132 if (strncmp (p, "${", strlen ("${")) == 0)
137 text_node->data = g_strndup (text, *n);
144 /****************************************************************************/
145 /* Copy a single text node. */
146 /****************************************************************************/
148 gl_text_node_dup (const glTextNode *src)
152 if ( src == NULL ) return NULL;
154 dst = g_new0 (glTextNode, 1);
156 dst->field_flag = src->field_flag;
157 dst->data = g_strdup (src->data);
163 /****************************************************************************/
164 /* Free a single text node. */
165 /****************************************************************************/
167 gl_text_node_free (glTextNode **text_node)
169 if ( *text_node == NULL ) return;
171 g_free ((*text_node)->data);
172 (*text_node)->data = NULL;
178 /****************************************************************************/
179 /* Compare 2 text nodes for equality. */
180 /****************************************************************************/
182 gl_text_node_equal (const glTextNode *text_node1,
183 const glTextNode *text_node2)
185 /* First take care of the case of either or both being NULL. */
186 if ( text_node1 == NULL ) {
187 return ( text_node2 == NULL );
189 if ( text_node2 == NULL ) {
194 /* Bail if field flags differ. */
195 if ( text_node1->field_flag != text_node2->field_flag ) {
199 /* Now take care of the case of either or both data fields being NULL. */
200 if ( text_node1->data == NULL ) {
201 return ( text_node2->data == NULL );
203 if ( text_node2->data == NULL ) {
208 /* Field flags are identical, so now compare the data. */
209 return (strcmp (text_node1->data, text_node2->data) == 0);
213 /****************************************************************************/
214 /* Expand text lines into single string. */
215 /****************************************************************************/
217 gl_text_node_lines_expand (GList *lines,
218 const glMergeRecord *record)
220 GList *p_line, *p_node;
221 glTextNode *text_node;
222 gchar *text, *old_text, *expanded_node;
223 gboolean first_line = TRUE;
225 text = g_strdup (""); /* prime pointer for concatenation */
226 for (p_line = lines; p_line != NULL; p_line = p_line->next) {
228 /* special case: something like ${ADDRESS2} = "" on line by itself. */
229 /* in such circumstances ignore the line completely. */
230 p_node = (GList *)p_line->data;
231 if (p_node && p_node->next == NULL) {
232 text_node = (glTextNode *) p_node->data;
233 if ( is_empty_field (text_node, record) ) {
238 /* prepend newline if it's not the first line */
241 text = g_strconcat (text, "\n", NULL);
247 /* expand each node */
248 for (p_node = (GList *) p_line->data; p_node != NULL;
249 p_node = p_node->next) {
250 text_node = (glTextNode *) p_node->data;
252 expanded_node = gl_text_node_expand (text_node, record);
253 text = g_strconcat (text, expanded_node, NULL);
255 g_free (expanded_node);
263 /****************************************************************************/
264 /* Parse a string back into text lines. */
265 /****************************************************************************/
267 gl_text_node_lines_new_from_text (const gchar *text)
269 GList *lines, *nodes;
270 glTextNode *text_node;
276 for (p = (gchar *)text; *p != 0; p += n) {
278 text_node = extract_text_node (p, &n);
279 nodes = g_list_append (nodes, text_node);
282 lines = g_list_append (lines, nodes);
286 if (*(p - 1) != '\n') {
287 lines = g_list_append (lines, nodes);
294 /****************************************************************************/
295 /* Copy a list of text lines. */
296 /****************************************************************************/
298 gl_text_node_lines_dup (GList *src_lines)
300 GList *dst_lines=NULL;
301 GList *p_line, *line, *p_node;
304 for (p_line = src_lines; p_line != NULL; p_line = p_line->next)
307 for (p_node = (GList *) p_line->data; p_node != NULL; p_node = p_node->next)
309 node = gl_text_node_dup ((glTextNode *)p_node->data);
310 line = g_list_append (line, node);
312 dst_lines = g_list_append (dst_lines, line);
319 /****************************************************************************/
320 /* Free a list of text lines. */
321 /****************************************************************************/
323 gl_text_node_lines_free (GList **lines)
325 GList *p_line, *p_node;
326 glTextNode *text_node;
328 for (p_line = *lines; p_line != NULL; p_line = p_line->next)
330 for (p_node = (GList *) p_line->data; p_node != NULL; p_node = p_node->next)
332 text_node = (glTextNode *)p_node->data;
334 gl_text_node_free ( &text_node );
336 g_list_free ((GList *) p_line->data);
340 g_list_free (*lines);
345 /****************************************************************************/
346 /* For debugging: descend and print lines list. */
347 /****************************************************************************/
349 gl_text_node_lines_print (GList * lines )
351 GList *p_line, *p_node;
352 glTextNode *text_node;
355 for (p_line=lines, i_line=0; p_line != NULL; p_line=p_line->next, i_line++) {
356 for (p_node = (GList *) p_line->data, i_node=0; p_node != NULL;
357 p_node = p_node->next, i_node++) {
358 text_node = (glTextNode *) p_node->data;
359 g_print( "LINE[%d], NODE[%d] = { %d, \"%s\" }\n",
361 text_node->field_flag, text_node->data );
371 * Local Variables: -- emacs
373 * c-basic-offset: 8 -- emacs
374 * tab-width: 8 -- emacs
375 * indent-tabs-mode: nil -- emacs