|
|
1.1 root 1: /* $Header: StringSrc.c,v 1.2 87/07/16 15:15:24 toddb Exp $ */
2: #ifndef lint
3: static char *sccsid = "@(#)StringSource.c 1.9 2/25/87";
4: #endif lint
5:
6: /*
7: * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
8: *
9: * All Rights Reserved
10: *
11: * Permission to use, copy, modify, and distribute this software and its
12: * documentation for any purpose and without fee is hereby granted,
13: * provided that the above copyright notice appear in all copies and that
14: * both that copyright notice and this permission notice appear in
15: * supporting documentation, and that the name of Digital Equipment
16: * Corporation not be used in advertising or publicity pertaining to
17: * distribution of the software without specific, written prior permission.
18: *
19: *
20: * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
21: * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
22: * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
23: * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
24: * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
25: * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26: * SOFTWARE.
27: */
28:
29:
30: /* File: StringSource.c */
31:
32: #include <sys/types.h>
33: #include <sys/stat.h>
34: #include <sys/file.h>
35: #include "Xlib.h"
36: #include "Intrinsic.h"
37: #include "Text.h"
38: #include "TextDisp.h"
39:
40: /* Private StringSource Definitions */
41:
42:
43: typedef struct _StringSourceData {
44: char *str;
45: XtTextPosition length, maxLength;
46: XtEditType editMode;
47: } StringSourceData, *StringSourcePtr;
48:
49: #define Increment(data, position, direction)\
50: {\
51: if (direction == XtsdLeft) {\
52: if (position > 0) \
53: position -= 1;\
54: }\
55: else {\
56: if (position < data->length)\
57: position += 1;\
58: }\
59: }
60:
61: char Look(data, position, direction)
62: StringSourcePtr data;
63: XtTextPosition position;
64: ScanDirection direction;
65: {
66: /* Looking left at pos 0 or right at position data->length returns newline */
67: if (direction == XtsdLeft) {
68: if (position == 0)
69: return(0);
70: else
71: return(data->str[position-1]);
72: }
73: else {
74: if (position == data->length)
75: return(0);
76: else
77: return(data->str[position]);
78: }
79: }
80:
81: static int StringReadText (src, pos, text, maxRead)
82: XtTextSource *src;
83: int pos;
84: XtTextBlock *text;
85: int maxRead;
86: {
87: int charsLeft;
88: StringSourcePtr data;
89:
90: data = (StringSourcePtr) src->data;
91: text->firstPos = pos;
92: text->ptr = data->str + pos;
93: charsLeft = data->length - pos;
94: text->length = (maxRead > charsLeft) ? charsLeft : maxRead;
95: return pos + text->length;
96: }
97:
98: static int StringReplaceText (src, startPos, endPos, text, delta)
99: XtTextSource *src;
100: XtTextPosition startPos, endPos;
101: XtTextBlock *text;
102: int *delta;
103: {
104: StringSourcePtr data;
105: int i, length, tlength;
106:
107: data = (StringSourcePtr) src->data;
108: switch (data->editMode) {
109: case XttextAppend:
110: if (startPos != endPos || endPos!= data->length)
111: return (POSITIONERROR);
112: break;
113: case XttextRead:
114: return (EDITERROR);
115: case XttextEdit:
116: break;
117: default:
118: return (EDITERROR);
119: }
120: length = endPos - startPos;
121:
122: if (data->length - length + text->length > data->maxLength)
123: tlength = data->maxLength - data->length + length;
124: else
125: tlength = text->length;
126:
127: *delta = tlength - length;
128: if (*delta < 0) /* insert shorter than delete, text getting
129: shorter */
130: for (i = startPos; i < data->length + *delta; ++i)
131: data->str[i] = data->str[i - *delta];
132: else
133: if (*delta > 0) { /* insert longer than delete, text getting
134: longer */
135: for (i = data->length; i > startPos-1; --i)
136: data->str[i + *delta] = data->str[i];
137: }
138: if (tlength != 0) /* do insert */
139: for (i = 0; i < tlength; ++i)
140: data->str[startPos + i] = text->ptr[i];
141: data->length = data->length + *delta;
142: data->str[data->length] = 0;
143: return (EDITDONE);
144: }
145:
146: static StringSetLastPos (src, lastPos)
147: XtTextSource *src;
148: XtTextPosition lastPos;
149: {
150: ((StringSourceData *) (src->data))->length = lastPos;
151: }
152:
153: static XtTextPosition StringScan (src, pos, sType, dir, count, include)
154: XtTextSource *src;
155: XtTextPosition pos;
156: ScanType sType;
157: ScanDirection dir;
158: int count;
159: Boolean include;
160: {
161: StringSourcePtr data;
162: XtTextPosition position;
163: int i, whiteSpace;
164: char c;
165: int ddir = (dir == XtsdRight) ? 1 : -1;
166:
167: data = (StringSourcePtr) src->data;
168: position = pos;
169: switch (sType) {
170: case XtstPositions:
171: if (!include && count > 0)
172: count -= 1;
173: for (i = 0; i < count; i++) {
174: Increment(data, position, dir);
175: }
176: break;
177: case XtstWhiteSpace:
178:
179: for (i = 0; i < count; i++) {
180: whiteSpace = -1;
181: while (position >= 0 && position <= data->length) {
182: c = Look(data, position, dir);
183: if ((c == ' ') || (c == '\t') || (c == '\n')){
184: if (whiteSpace < 0) whiteSpace = position;
185: } else if (whiteSpace >= 0)
186: break;
187: position += ddir;
188: }
189: }
190: if (!include) {
191: if(whiteSpace < 0 && dir == XtsdRight) whiteSpace = data->length;
192: position = whiteSpace;
193: }
194: break;
195: case XtstEOL:
196: for (i = 0; i < count; i++) {
197: while (position >= 0 && position <= data->length) {
198: if (Look(data, position, dir) == '\n')
199: break;
200: if(((dir == XtsdRight) && (position == data->length)) ||
201: (dir == XtsdLeft) && ((position == 0)))
202: break;
203: Increment(data, position, dir);
204: }
205: if (i + 1 != count)
206: Increment(data, position, dir);
207: }
208: if (include) {
209: /* later!!!check for last char in file # eol */
210: Increment(data, position, dir);
211: }
212: break;
213: case XtstFile:
214: if (dir == XtsdLeft)
215: position = 0;
216: else
217: position = data->length;
218: }
219: if (position < 0) position = 0;
220: if (position > data->length) position = data->length;
221: return(position);
222: }
223:
224: static XtEditType StringGetEditType(src)
225: XtTextSource *src;
226: {
227: StringSourcePtr data;
228: data = (StringSourcePtr) src->data;
229: return(data->editMode);
230: }
231:
232: /* Public routines */
233:
234: int *XtStringSourceCreate (str, maxLength, mode)
235: char *str;
236: int maxLength;
237: XtEditType mode;
238: {
239: XtTextSource *src;
240: StringSourcePtr data;
241: int i;
242:
243: src = (XtTextSource *) XtMalloc(sizeof(XtTextSource));
244: src->read = StringReadText;
245: src->replace = StringReplaceText;
246: src->setLastPos = StringSetLastPos;
247: src->scan = StringScan;
248: src->editType = StringGetEditType;
249: src->data = (int *) (XtMalloc(sizeof(StringSourceData)));
250: data = (StringSourcePtr) src->data;
251: i = 0;
252: while (str[i] != 0)
253: ++i;
254: data->str = str;
255: data->length = i;
256: data->maxLength = maxLength;
257: data->editMode = mode;
258: src->data = (int *) (data);
259: return(int *) src;
260: }
261:
262: void XtStringSourceDestroy (src)
263: XtTextSource *src;
264: {
265: XtFree((char *) src->data);
266: XtFree((char *) src);
267: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.