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 /* Copy a single text node. */
112 /*--------------------------------------------------------------------------*/
114 gl_text_node_dup (glTextNode *src)
118 dst = g_new0 (glTextNode, 1);
120 dst->field_flag = src->field_flag;
121 dst->data = g_strdup (src->data);
126 /*--------------------------------------------------------------------------*/
127 /* Free a single text node. */
128 /*--------------------------------------------------------------------------*/
130 gl_text_node_free (glTextNode ** text_node)
132 if ( *text_node == NULL ) return;
134 g_free ((*text_node)->data);
135 (*text_node)->data = NULL;
140 /*--------------------------------------------------------------------------*/
141 /* Expand text lines into single string. */
142 /*--------------------------------------------------------------------------*/
144 gl_text_node_lines_expand (GList * lines,
145 glMergeRecord * record)
147 GList *p_line, *p_node;
148 glTextNode *text_node;
149 gchar *text, *old_text, *expanded_node;
151 text = g_strdup (""); /* prime pointer for concatenation */
152 for (p_line = lines; p_line != NULL; p_line = p_line->next) {
153 for (p_node = (GList *) p_line->data; p_node != NULL;
154 p_node = p_node->next) {
155 text_node = (glTextNode *) p_node->data;
157 expanded_node = gl_text_node_expand (text_node, record);
158 text = g_strconcat (text, expanded_node, NULL);
160 g_free (expanded_node);
162 if ( p_line->next != NULL ) {
164 text = g_strconcat (text, "\n", NULL);
172 /*--------------------------------------------------------------------------*/
173 /* Parse a string back into text lines. */
174 /*--------------------------------------------------------------------------*/
176 gl_text_node_lines_new_from_text (gchar * text)
178 GList *lines, *nodes;
179 glTextNode *text_node;
185 for (p = text; *p != 0; p += n) {
187 text_node = extract_text_node (p, &n);
188 nodes = g_list_append (nodes, text_node);
191 lines = g_list_append (lines, nodes);
195 if (*(p - 1) != '\n') {
196 lines = g_list_append (lines, nodes);
202 /*--------------------------------------------------------------------------*/
203 /* Copy a list of text lines. */
204 /*--------------------------------------------------------------------------*/
206 gl_text_node_lines_dup (GList * src_lines)
208 GList *dst_lines=NULL;
209 GList *p_line, *line, *p_node;
212 for (p_line = src_lines; p_line != NULL; p_line = p_line->next) {
214 for (p_node = (GList *) p_line->data; p_node != NULL;
215 p_node = p_node->next) {
216 node = gl_text_node_dup ((glTextNode *)p_node->data);
217 line = g_list_append (line, node);
219 dst_lines = g_list_append (dst_lines, line);
225 /*--------------------------------------------------------------------------*/
226 /* Free a list of text lines. */
227 /*--------------------------------------------------------------------------*/
229 gl_text_node_lines_free (GList ** lines)
231 GList *p_line, *p_node;
233 for (p_line = *lines; p_line != NULL; p_line = p_line->next) {
234 for (p_node = (GList *) p_line->data; p_node != NULL;
235 p_node = p_node->next) {
236 gl_text_node_free ( (glTextNode **) &(p_node->data) );
238 g_list_free ((GList *) p_line->data);
242 g_list_free (*lines);
246 /****************************************************************************/
247 /* For debugging: descend and print lines list. */
248 /****************************************************************************/
250 gl_text_node_lines_print (GList * lines )
252 GList *p_line, *p_node;
253 glTextNode *text_node;
256 for (p_line=lines, i_line=0; p_line != NULL; p_line=p_line->next, i_line++) {
257 for (p_node = (GList *) p_line->data, i_node=0; p_node != NULL;
258 p_node = p_node->next, i_node++) {
259 text_node = (glTextNode *) p_node->data;
260 g_print( "LINE[%d], NODE[%d] = { %d, \"%s\" }\n",
262 text_node->field_flag, text_node->data );