2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * text_node.c: text node module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "text_node.h"
31 /*===========================================*/
32 /* Local function prototypes */
33 /*===========================================*/
35 static glTextNode *extract_text_node (gchar * text, gint * n);
38 /*--------------------------------------------------------------------------*/
39 /* Expand single node into representative string. */
40 /*--------------------------------------------------------------------------*/
42 gl_text_node_expand (glTextNode * text_node,
43 glMergeRecord * record)
47 if (text_node->field_flag) {
48 text = gl_merge_eval_key (text_node->data, record);
52 return g_strdup_printf ("FIELD{%s}", text_node->data);
55 return g_strdup (text_node->data);
59 /*--------------------------------------------------------------------------*/
60 /* Create a single text node from given text. */
61 /*--------------------------------------------------------------------------*/
63 gl_text_node_new_from_text (gchar * text)
67 return extract_text_node (text, &n);
70 /*--------------------------------------------------------------------------*/
71 /* PRIVATE. Create a single text node from given text. n = characters used */
72 /*--------------------------------------------------------------------------*/
74 extract_text_node (gchar * text,
77 glTextNode *text_node;
81 text_node = g_new0 (glTextNode, 1);
83 if (strncmp (text, "FIELD{", strlen ("FIELD{")) == 0) {
84 /* We are at the beginning of a "FIELD" node */
85 text_node->field_flag = TRUE;
86 *n = strlen ("FIELD{");
88 for (p = text, m = 0; *p != 0; p++, m++, (*n)++) {
94 text_node->data = g_strndup (text, m);
96 /* We are at the beginning of a literal node */
97 text_node->field_flag = FALSE;
98 for (p = text, *n = 0; *p != 0; p++, (*n)++) {
99 if (strncmp (p, "FIELD{", strlen ("FIELD{")) == 0)
104 text_node->data = g_strndup (text, *n);
110 /*--------------------------------------------------------------------------*/
111 /* Free a single text node. */
112 /*--------------------------------------------------------------------------*/
114 gl_text_node_free (glTextNode ** text_node)
116 g_free ((*text_node)->data);
117 (*text_node)->data = NULL;
122 /*--------------------------------------------------------------------------*/
123 /* Expand text lines into single string. */
124 /*--------------------------------------------------------------------------*/
126 gl_text_node_lines_expand (GList * lines,
127 glMergeRecord * record)
129 GList *p_line, *p_node;
130 glTextNode *text_node;
131 gchar *text, *old_text, *expanded_node;
133 text = g_strdup (""); /* prime pointer for concatenation */
134 for (p_line = lines; p_line != NULL; p_line = p_line->next) {
135 for (p_node = (GList *) p_line->data; p_node != NULL;
136 p_node = p_node->next) {
137 text_node = (glTextNode *) p_node->data;
139 expanded_node = gl_text_node_expand (text_node, record);
140 text = g_strconcat (text, expanded_node, NULL);
142 g_free (expanded_node);
145 text = g_strconcat (text, "\n", NULL);
152 /*--------------------------------------------------------------------------*/
153 /* Parse a string back into text lines. */
154 /*--------------------------------------------------------------------------*/
156 gl_text_node_lines_new_from_text (gchar * text)
158 GList *lines, *nodes;
159 glTextNode *text_node;
165 for (p = text; *p != 0; p += n) {
167 text_node = extract_text_node (p, &n);
168 nodes = g_list_append (nodes, text_node);
171 lines = g_list_append (lines, nodes);
175 if (*(p - 1) != '\n') {
176 lines = g_list_append (lines, nodes);
182 /*--------------------------------------------------------------------------*/
183 /* Free a list of text lines. */
184 /*--------------------------------------------------------------------------*/
186 gl_text_node_lines_free (GList ** lines)
188 GList *p_line, *p_node;
190 for (p_line = *lines; p_line != NULL; p_line = p_line->next) {
191 for (p_node = (GList *) p_line->data; p_node != NULL;
192 p_node = p_node->next) {
193 gl_text_node_free ( (glTextNode **) &(p_node->data) );
195 g_list_free ((GList *) p_line->data);
199 g_list_free (*lines);
203 /****************************************************************************/
204 /* For debugging: descend and print lines list. */
205 /****************************************************************************/
207 gl_text_node_lines_print (GList * lines )
209 GList *p_line, *p_node;
210 glTextNode *text_node;
213 for (p_line=lines, i_line=0; p_line != NULL; p_line=p_line->next, i_line++) {
214 for (p_node = (GList *) p_line->data, i_node=0; p_node != NULL;
215 p_node = p_node->next, i_node++) {
216 text_node = (glTextNode *) p_node->data;
217 g_print( "LINE[%d], NODE[%d] = { %d, \"%s\" }\n",
219 text_node->field_flag, text_node->data );