Annotation of researchv9/X11/src/X.V11R1/lib/Xtk/StringSrc.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.