source: mainline/uspace/app/edit/sheet.c@ c29f20b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c29f20b was c29f20b, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Use variable-size buffer for sheet data.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Copyright (c) 2009 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup edit
30 * @{
31 */
32/**
33 * @file
34 * @brief Prototype implementation of Sheet data structure.
35 *
36 * The sheet is an abstract data structure representing a piece of text.
37 * On top of this data structure we can implement a text editor. It is
38 * possible to implement the sheet such that the editor can make small
39 * changes to large files or files containing long lines efficiently.
40 *
41 * The sheet structure allows basic operations of text insertion, deletion,
42 * retrieval and mapping of coordinates to position in the file and vice
43 * versa. The text that is inserted or deleted can contain tabs and newlines
44 * which are interpreted and properly acted upon.
45 *
46 * This is a trivial implementation with poor efficiency with O(N+n)
47 * insertion and deletion and O(N) mapping (in both directions), where
48 * N is the size of the file and n is the size of the inserted/deleted text.
49 */
50
51#include <stdlib.h>
52#include <string.h>
53#include <errno.h>
54#include <adt/list.h>
55#include <align.h>
56#include <macros.h>
57
58#include "sheet.h"
59
60enum {
61 TAB_WIDTH = 8,
62
63 /** Initial of dat buffer in bytes */
64 INITIAL_SIZE = 32
65};
66
67/** Initialize an empty sheet. */
68int sheet_init(sheet_t *sh)
69{
70 sh->dbuf_size = INITIAL_SIZE;
71 sh->text_size = 0;
72
73 sh->data = malloc(sh->dbuf_size);
74 if (sh->data == NULL)
75 return ENOMEM;
76
77 list_initialize(&sh->tags_head);
78
79 return EOK;
80}
81
82/** Insert text into sheet.
83 *
84 * @param sh Sheet to insert to.
85 * @param pos Point where to insert.
86 * @param dir Whether to insert before or after the point (affects tags).
87 * @param str The text to insert (printable characters, tabs, newlines).
88 *
89 * @return EOK on success or negative error code.
90 *
91 * @note @a dir affects which way tags that were placed on @a pos will
92 * move. If @a dir is @c dir_before, the tags will move forward
93 * and vice versa.
94 */
95int sheet_insert(sheet_t *sh, spt_t *pos, enum dir_spec dir, char *str)
96{
97 char *ipp;
98 size_t sz;
99 link_t *link;
100 tag_t *tag;
101 char *newp;
102
103 sz = str_size(str);
104 if (sh->text_size + sz > sh->dbuf_size) {
105 /* Enlarge data buffer. */
106 newp = realloc(sh->data, sh->dbuf_size * 2);
107 if (newp == NULL)
108 return ELIMIT;
109
110 sh->data = newp;
111 sh->dbuf_size = sh->dbuf_size * 2;
112 }
113
114 ipp = sh->data + pos->b_off;
115
116 /* Copy data. */
117 memmove(ipp + sz, ipp, sh->text_size - pos->b_off);
118 memcpy(ipp, str, sz);
119 sh->text_size += sz;
120
121 /* Adjust tags. */
122
123 link = sh->tags_head.next;
124 while (link != &sh->tags_head) {
125 tag = list_get_instance(link, tag_t, link);
126
127 if (tag->b_off > pos->b_off)
128 tag->b_off += sz;
129 else if (tag->b_off == pos->b_off && dir == dir_before)
130 tag->b_off += sz;
131
132 link = link->next;
133 }
134
135 return EOK;
136}
137
138/** Delete text from sheet.
139 *
140 * Deletes the range of text between two points from the sheet.
141 *
142 * @param sh Sheet to delete from.
143 * @param spos Starting point.
144 * @param epos Ending point.
145 *
146 * @return EOK on success or negative error code.
147 **/
148int sheet_delete(sheet_t *sh, spt_t *spos, spt_t *epos)
149{
150 char *spp;
151 size_t sz;
152 link_t *link;
153 tag_t *tag;
154 char *newp;
155 size_t shrink_size;
156
157 spp = sh->data + spos->b_off;
158 sz = epos->b_off - spos->b_off;
159
160 memmove(spp, spp + sz, sh->text_size - (spos->b_off + sz));
161 sh->text_size -= sz;
162
163 /* Adjust tags. */
164 link = sh->tags_head.next;
165 while (link != &sh->tags_head) {
166 tag = list_get_instance(link, tag_t, link);
167
168 if (tag->b_off >= epos->b_off)
169 tag->b_off -= sz;
170 else if (tag->b_off >= spos->b_off)
171 tag->b_off = spos->b_off;
172
173 link = link->next;
174 }
175
176 /* See if we should free up some memory. */
177 shrink_size = max(sh->dbuf_size / 4, INITIAL_SIZE);
178 if (sh->text_size <= shrink_size && sh->dbuf_size > INITIAL_SIZE) {
179 /* Shrink data buffer. */
180 newp = realloc(sh->data, shrink_size);
181 if (newp == NULL) {
182 /* Failed to shrink buffer... no matter. */
183 return EOK;
184 }
185
186 sh->data = newp;
187 sh->dbuf_size = shrink_size;
188 }
189
190 return EOK;
191}
192
193/** Read text from sheet. */
194void sheet_copy_out(sheet_t *sh, spt_t const *spos, spt_t const *epos,
195 char *buf, size_t bufsize, spt_t *fpos)
196{
197 char *spp;
198 size_t range_sz;
199 size_t copy_sz;
200 size_t off, prev;
201 wchar_t c;
202
203 spp = sh->data + spos->b_off;
204 range_sz = epos->b_off - spos->b_off;
205 copy_sz = (range_sz < bufsize - 1) ? range_sz : bufsize - 1;
206
207 prev = off = 0;
208 do {
209 prev = off;
210 c = str_decode(spp, &off, copy_sz);
211 } while (c != '\0');
212
213 /* Crop copy_sz down to the last full character. */
214 copy_sz = prev;
215
216 memcpy(buf, spp, copy_sz);
217 buf[copy_sz] = '\0';
218
219 fpos->b_off = spos->b_off + copy_sz;
220 fpos->sh = sh;
221}
222
223/** Get point preceding or following character cell. */
224void sheet_get_cell_pt(sheet_t *sh, coord_t const *coord, enum dir_spec dir,
225 spt_t *pt)
226{
227 size_t cur_pos, prev_pos;
228 wchar_t c;
229 coord_t cc;
230
231 cc.row = cc.column = 1;
232 cur_pos = prev_pos = 0;
233 while (true) {
234 if (prev_pos >= sh->text_size) {
235 /* Cannot advance any further. */
236 break;
237 }
238
239 if ((cc.row >= coord->row && cc.column > coord->column) ||
240 cc.row > coord->row) {
241 /* We are past the requested coordinates. */
242 break;
243 }
244
245 prev_pos = cur_pos;
246
247 c = str_decode(sh->data, &cur_pos, sh->text_size);
248 if (c == '\n') {
249 ++cc.row;
250 cc.column = 1;
251 } else if (c == '\t') {
252 cc.column = 1 + ALIGN_UP(cc.column, TAB_WIDTH);
253 } else {
254 ++cc.column;
255 }
256 }
257
258 pt->sh = sh;
259 pt->b_off = (dir == dir_before) ? prev_pos : cur_pos;
260}
261
262/** Get the number of character cells a row occupies. */
263void sheet_get_row_width(sheet_t *sh, int row, int *length)
264{
265 coord_t coord;
266 spt_t pt;
267
268 /* Especially nasty hack */
269 coord.row = row;
270 coord.column = 65536;
271
272 sheet_get_cell_pt(sh, &coord, dir_before, &pt);
273 spt_get_coord(&pt, &coord);
274 *length = coord.column - 1;
275}
276
277/** Get the number of rows in a sheet. */
278void sheet_get_num_rows(sheet_t *sh, int *rows)
279{
280 int cnt;
281 size_t bo;
282
283 cnt = 1;
284 for (bo = 0; bo < sh->dbuf_size; ++bo) {
285 if (sh->data[bo] == '\n')
286 cnt += 1;
287 }
288
289 *rows = cnt;
290}
291
292/** Get the coordinates of an s-point. */
293void spt_get_coord(spt_t const *pos, coord_t *coord)
294{
295 size_t off;
296 coord_t cc;
297 wchar_t c;
298 sheet_t *sh;
299
300 sh = pos->sh;
301 cc.row = cc.column = 1;
302
303 off = 0;
304 while (off < pos->b_off && off < sh->text_size) {
305 c = str_decode(sh->data, &off, sh->text_size);
306 if (c == '\n') {
307 ++cc.row;
308 cc.column = 1;
309 } else if (c == '\t') {
310 cc.column = 1 + ALIGN_UP(cc.column, TAB_WIDTH);
311 } else {
312 ++cc.column;
313 }
314 }
315
316 *coord = cc;
317}
318
319/** Test if two s-points are equal. */
320bool spt_equal(spt_t const *a, spt_t const *b)
321{
322 return a->b_off == b->b_off;
323}
324
325/** Place a tag on the specified s-point. */
326void sheet_place_tag(sheet_t *sh, spt_t const *pt, tag_t *tag)
327{
328 tag->b_off = pt->b_off;
329 tag->sh = sh;
330 list_append(&tag->link, &sh->tags_head);
331}
332
333/** Remove a tag from the sheet. */
334void sheet_remove_tag(sheet_t *sh, tag_t *tag)
335{
336 list_remove(&tag->link);
337}
338
339/** Get s-point on which the tag is located right now. */
340void tag_get_pt(tag_t const *tag, spt_t *pt)
341{
342 pt->b_off = tag->b_off;
343 pt->sh = tag->sh;
344}
345
346/** @}
347 */
Note: See TracBrowser for help on using the repository browser.