1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (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 along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18     Last edit by hansen on Fri Mar 18 22:21:38 2005
19 ****************************************************************************/
20 #include <stdlib.h>
21 #include <string.h>
22 #include "tkgate.h"
23 
new_TextBlock()24 TextBlock *new_TextBlock()
25 {
26   TextBlock *tb = (TextBlock*) ob_malloc(sizeof(TextBlock),"TextBlock");
27 
28   tb->tb_first = tb->tb_last = 0;
29   tb->tb_curLine = 1;
30   tb->tb_curChar = 0;
31 
32   return tb;
33 }
34 
delete_TextBlock(TextBlock * tb)35 void delete_TextBlock(TextBlock *tb)
36 {
37   TextBlock_flush(tb);
38   ob_free(tb);
39 }
40 
41 
TextBlock_copyRange(TextBlock * dst,TextLine * start,TextLine * stop)42 void TextBlock_copyRange(TextBlock *dst,TextLine *start,TextLine *stop)
43 {
44   TextLine *line;
45 
46   TextBlock_flush(dst);
47   for (line = start;line;line = line->next) {
48     TextBlock_addLine(dst,line->text);
49     if (line == stop) break;
50   }
51 }
52 
TextBlock_copy(TextBlock * dst,TextBlock * src)53 void TextBlock_copy(TextBlock *dst, TextBlock *src)
54 {
55   TextLine *line;
56 
57   TextBlock_flush(dst);
58   for (line = src->tb_first;line;line = line->next)
59     TextBlock_addLine(dst,line->text);
60 }
61 
62 /*****************************************************************************
63  *
64  * Initialize a text position (TextPos) to beginning of a text block.
65  *
66  *****************************************************************************/
TextBlock_getStartTP(TextBlock * tb,TextPos * tp)67 void TextBlock_getStartTP(TextBlock *tb,TextPos *tp)
68 {
69   tp->tp_line = tb->tb_first;
70   tp->tp_pos = 0;
71 }
72 
73 
74 /*****************************************************************************
75  *
76  * Append a line onto a text block.
77  *
78  * Parameters:
79  *      tb		Text block in which to add text
80  *      text		Line of text to be added
81  *
82  *****************************************************************************/
TextBlock_addLine(TextBlock * tb,const char * text)83 void TextBlock_addLine(TextBlock *tb,const char *text)
84 {
85   TextLine *tl = (TextLine*) ob_malloc(sizeof(TextLine),"TextLine");
86 
87   ob_touch(tb);
88 
89   tl->text = ob_strdup(text);
90   tl->next = 0;
91 
92   if (!tb->tb_first)
93     tb->tb_first = tb->tb_last = tl;
94   else {
95     ob_touch(tb->tb_last);
96     tb->tb_last->next = tl;
97     tb->tb_last = tl;
98   }
99 
100 }
101 
102 /*****************************************************************************
103  *
104  * Prepend a line onto a text block.
105  *
106  * Parameters:
107  *      tb		Text block in which to add text
108  *      text		Line of text to be added
109  *
110  *****************************************************************************/
TextBlock_prependLine(TextBlock * tb,const char * text)111 void TextBlock_prependLine(TextBlock *tb,const char *text)
112 {
113   TextLine *tl = (TextLine*) ob_malloc(sizeof(TextLine),"TextLine");
114 
115   ob_touch(tb);
116 
117   tl->text = ob_strdup(text);
118   tl->next = 0;
119 
120   if (!tb->tb_first)
121     tb->tb_first = tb->tb_last = tl;
122   else {
123     tl->next = tb->tb_first;
124     tb->tb_first = tl;
125   }
126 
127 }
128 
129 /*****************************************************************************
130  *
131  * Flush the contents of a text buffer
132  *
133  * Parameters:
134  *      tb		Text block to be flushed
135  *
136  *****************************************************************************/
TextBlock_flush(TextBlock * tb)137 void TextBlock_flush(TextBlock *tb)
138 {
139   TextLine *tl,*next_tl;
140 
141   ob_touch(tb);
142 
143   for (tl = tb->tb_first;tl;tl = next_tl) {
144     next_tl = tl->next;
145     ob_free(tl->text);
146     ob_free(tl);
147   }
148 
149   tb->tb_first = tb->tb_last = 0;
150 }
151