File: SourceStr.c

package info (click to toggle)
xcircuit 2.0a6-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,960 kB
  • ctags: 3,202
  • sloc: ansic: 30,529; makefile: 51; sh: 2
file content (374 lines) | stat: -rw-r--r-- 11,665 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*************************************<+>*************************************
 *****************************************************************************
 **
 **   File:        SourceStr.c
 **
 **   Project:     X Widgets
 **
 **   Description: Code for TextEdit widget string source
 **
 *****************************************************************************
 **   
 **   Copyright (c) 1988 by Hewlett-Packard Company
 **   Copyright (c) 1987, 1988 by Digital Equipment Corporation, Maynard,
 **             Massachusetts, and the Massachusetts Institute of Technology,
 **             Cambridge, Massachusetts
 **   
 **   Permission to use, copy, modify, and distribute this software 
 **   and its documentation for any purpose and without fee is hereby 
 **   granted, provided that the above copyright notice appear in all 
 **   copies and that both that copyright notice and this permission 
 **   notice appear in supporting documentation, and that the names of 
 **   Hewlett-Packard, Digital or  M.I.T.  not be used in advertising or 
 **   publicity pertaining to distribution of the software without 
 **   written prior permission.
 **   
 **   DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 **   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 **   DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 **   ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 **   WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 **   ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 **   SOFTWARE.
 **   
 *****************************************************************************
 *************************************<+>*************************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <X11/Xlib.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <Xw/Xw.h>
#include <Xw/XwP.h>
#include <Xw/TextEdit.h>
#include <Xw/TextEditP.h>
#include <Xw/SourceP.h>

/* Private StringSource Definitions */

#define MAGICVALUE		-1
#define DEFAULTBUFFERSIZE        512


#define Increment(data, position, direction)\
{\
    if (direction == XwsdLeft) {\
	if (position > 0) \
	    position -= 1;\
    }\
    else {\
	if (position < data->length)\
	    position += 1;\
    }\
}

static long magic_value = MAGICVALUE;

static XtResource stringResources[] = {
    {XtNstring, XtCString, XtRString, sizeof (char *),
        XtOffset(StringSourcePtr, initial_string), XtRString, NULL},
    {XtNmaximumSize, XtCMaximumSize, XtRLong, sizeof (long),
        XtOffset(StringSourcePtr, max_size), XtRLong, (caddr_t)&magic_value},
    {XtNeditType, XtCEditType, XtREditMode, sizeof(XwEditType), 
        XtOffset(StringSourcePtr, editMode), XtRString, "edit"},
};

/*--------------------------------------------------------------------------+*/
static unsigned char Look(data, position, direction)
/*--------------------------------------------------------------------------+*/
  StringSourcePtr data;
  XwTextPosition  position;
  XwScanDirection direction;
{
/* Looking left at pos 0 or right at position data->length returns newline */
    if (direction == XwsdLeft) {
	if (position == 0)
	    return(0);
	else
	    return(data->buffer[position-1]);
    }
    else {
	if (position == data->length)
	    return(0);
	else
	    return(data->buffer[position]);
    }
}

/*--------------------------------------------------------------------------+*/
static int StringReadText (src, pos, text, maxRead)
/*--------------------------------------------------------------------------+*/
  XwTextSource *src;
  int pos;
  XwTextBlock *text;
  int maxRead;
{
    int     charsLeft;
    StringSourcePtr data;

    data = (StringSourcePtr) src->data;
    text->firstPos = pos;
    text->ptr = data->buffer + pos;
    charsLeft = data->length - pos;
    text->length = (maxRead > charsLeft) ? charsLeft : maxRead;
    return pos + text->length;
}

/*--------------------------------------------------------------------------+*/
static XwEditResult StringReplaceText (src, startPos, endPos, text, delta)
/*--------------------------------------------------------------------------+*/
  XwTextSource *src;
  XwTextPosition startPos, endPos;
  XwTextBlock *text;
  int *delta;
{
    StringSourcePtr data;
    int     i, length;

    data = (StringSourcePtr) src->data;
    switch (data->editMode) {
        case XwtextAppend: 
	    if (startPos != endPos || endPos!= data->length)
		return (XweditPosError);
	    break;
	case XwtextRead:
	    return (XweditError);
	case XwtextEdit:
	    break;
	default:
	    return (XweditError);
    }
    length = endPos - startPos;
    *delta = text->length - length;
    if ((data->length + *delta) > data->buffer_size) {
      if (data->max_size_flag)
	return (XweditError);
      else {
	while ((data->length + *delta) > data->buffer_size)
	  data->buffer_size += DEFAULTBUFFERSIZE;
	data->buffer = (unsigned char *)
	  XtRealloc(data->buffer, data->buffer_size);
      }
    };
    

    if (*delta < 0)		/* insert shorter than delete, text getting
				   shorter */
	for (i = startPos; i < data->length + *delta; ++i)
	    data->buffer[i] = data->buffer[i - *delta];
    else
	if (*delta > 0)	{	/* insert longer than delete, text getting
				   longer */
	    for (i = data->length; i > startPos-1; --i)
		data->buffer[i + *delta] = data->buffer[i];
	}
    if (text->length != 0)	/* do insert */
	for (i = 0; i < text->length; ++i)
	    data->buffer[startPos + i] = text->ptr[i];
    data->length = data->length + *delta;
    data->buffer[data->length] = 0;
    return (XweditDone);
}

/*--------------------------------------------------------------------------+*/
static StringSetLastPos (src, lastPos)
/*--------------------------------------------------------------------------+*/
  XwTextSource *src;
  XwTextPosition lastPos;
{
    ((StringSourceData *) (src->data))->length = lastPos;
}

/*--------------------------------------------------------------------------+*/
static XwTextPosition StringGetLastPos (src)
/*--------------------------------------------------------------------------+*/
  XwTextSource *src;
{
    return( ((StringSourceData *) (src->data))->length );
}

/*--------------------------------------------------------------------------+*/
static XwTextPosition StringScan (src, pos, sType, dir, count, include)
/*--------------------------------------------------------------------------+*/
  XwTextSource	  *src;
  XwTextPosition  pos;
  XwScanType	  sType;
  XwScanDirection dir;
  int		  count;
  Boolean	  include;
{
    StringSourcePtr data;
    XwTextPosition position;
    int     i, whiteSpace;
    unsigned char c;
    int ddir = (dir == XwsdRight) ? 1 : -1;

    data = (StringSourcePtr) src->data;
    position = pos;
    switch (sType) {
	case XwstPositions: 
	    if (!include && count > 0)
		count -= 1;
	    for (i = 0; i < count; i++) {
		Increment(data, position, dir);
	    }
	    break;
	case XwstWhiteSpace: 

	    for (i = 0; i < count; i++) {
		whiteSpace = -1;
		while (position >= 0 && position <= data->length) {
		    c = Look(data, position, dir);
		    if ((c == ' ') || (c == '\t') || (c == '\n')){
		        if (whiteSpace < 0) whiteSpace = position;
		    } else if (whiteSpace >= 0)
			break;
		    position += ddir;
		}
	    }
	    if (!include) {
		if(whiteSpace < 0 && dir == XwsdRight) whiteSpace = data->length;
		position = whiteSpace;
	    }
	    break;
	case XwstEOL: 
	    for (i = 0; i < count; i++) {
		while (position >= 0 && position <= data->length) {
		    if (Look(data, position, dir) == '\n')
			break;
		    if(((dir == XwsdRight) && (position == data->length)) || 
			(dir == XwsdLeft) && ((position == 0)))
			break;
		    Increment(data, position, dir);
		}
		if (i + 1 != count)
		    Increment(data, position, dir);
	    }
	    if (include) {
	    /* later!!!check for last char in file # eol */
		Increment(data, position, dir);
	    }
	    break;
	case XwstLast: 
	    if (dir == XwsdLeft)
		position = 0;
	    else
		position = data->length;
    }
    if (position < 0) position = 0;
    if (position > data->length) position = data->length;
    return(position);
}

/*--------------------------------------------------------------------------+*/
static XwEditType StringGetEditType(src)
/*--------------------------------------------------------------------------+*/
  XwTextSource *src;
{
    StringSourcePtr data;
    data = (StringSourcePtr) src->data;
    return(data->editMode);
} 

/*--------------------------------------------------------------------------+*/
static Boolean XwStringSourceCheckData(src)
/*--------------------------------------------------------------------------+*/
    XwTextSource *src;
{   int  initial_size = 0;
    StringSourcePtr data = (StringSourcePtr)src->data;

    data->max_size_flag = (data->max_size != MAGICVALUE);
    
    if (data->initial_string == NULL) {
	if (data->max_size_flag) 
	  data->buffer_size = data->max_size;
	else 
	  data->buffer_size = DEFAULTBUFFERSIZE;
	if (!data->buffer) data->length = 0;
      }
    else {
      initial_size = XwStrlen(data->initial_string);
      if (data->max_size_flag) {
	if (data->max_size < initial_size) {
	  XtWarning("Initial string size larger than XtNmaximumSize");
	  data->max_size = initial_size;
	}
	data->buffer_size = data->max_size;
      }
      else {
	data->buffer_size =
	  (initial_size < DEFAULTBUFFERSIZE) ? DEFAULTBUFFERSIZE :
	    ((initial_size / DEFAULTBUFFERSIZE) + 1) * DEFAULTBUFFERSIZE;
      };
      data->length = initial_size;
    };

    if (data->buffer && initial_size) 
      data->buffer =
	(unsigned char *) XtRealloc(data->buffer, data->buffer_size);
    else if (!data->buffer)
      data->buffer = (unsigned char *) XtMalloc(data->buffer_size);
    
    if (initial_size) {
      strcpy(data->buffer, data->initial_string);
      data->initial_string = NULL;
    }
}  

/***** Public routines *****/

/*--------------------------------------------------------------------------+*/
void XwStringSourceDestroy (src)
/*--------------------------------------------------------------------------+*/
  XwTextSource *src;
{
    XtFree((char *) src->data);
    XtFree((char *) src);
}

/*--------------------------------------------------------------------------+*/
XwTextSource *XwStringSourceCreate (w, args, argCount)
/*--------------------------------------------------------------------------+*/
    Widget w;
    ArgList args;
    int     argCount;
{
    XwTextSource *src;
    StringSourcePtr data;


    src = XtNew(XwTextSource);
    src->read = StringReadText;
    src->replace = StringReplaceText;
    src->setLastPos = StringSetLastPos;
    src->getLastPos = StringGetLastPos;    
    src->scan = StringScan;
    src->editType = StringGetEditType;
    src->resources = stringResources;
    src->resource_num = XtNumber(stringResources);
    src->check_data = XwStringSourceCheckData;
    src->destroy = XwStringSourceDestroy;
    data = XtNew(StringSourceData);
    data->editMode = XwtextRead;
    data->buffer = NULL;
    data->initial_string = NULL;
    data->length = 0;
    data->buffer_size = 0;
    data->max_size = 0;
    data->max_size_flag = 0;
    src->data = (int *)data;

    /* Use the name given to the TextEdit widget this source will go with.
       This could be a problem if we allow multiple views on one source */

    XtGetSubresources (w, data, XtNstringSrc, "StringSrc",
        stringResources, XtNumber(stringResources), args, argCount);

    src->data = (int *) (data);

    XwStringSourceCheckData(src);

    return src;
}