Annotation of os2sdk/demos/apps/sse/sseline.c, revision 1.1.1.2

1.1.1.2 ! root        1: /* Created by Microsoft Corp. 1987  */
        !             2: 
        !             3: #include <os2def.h>
1.1       root        4: #include <ctype.h>
                      5: #include "ssedefs.h"
                      6: 
                      7: 
                      8: 
                      9: 
                     10: /*** addline - allocates and puts lines into segments
                     11:  *
                     12:  *   addline adds a line to a segment and if nessessary
                     13:  *     allocates a new segment.
                     14:  *
                     15:  *   addline (linenum, linelength, line)
                     16:  *
                     17:  *   ENTRY   linenum   - the line number of 'line' in the file
                     18:  *          linelength - the line length of 'line'
                     19:  *          line       - the 'line' to place in the segment
                     20:  *
                     21:  *   EXIT    rc        - return code for allocating a segment
                     22:  *
1.1.1.2 ! root       23:  *   addline places the current line in the segment
1.1       root       24:  *     containing the previous line if possible to
                     25:  *     reduce scattering, otherwise it scans other
1.1.1.2 ! root       26:  *     segments for first fit and if this fails allocates
1.1       root       27:  *     a new segment.
                     28:  *
                     29:  *   WARNING: do not modify the value of 'i' it is the
                     30:  *           result of the segment searches.
                     31:  *
                     32:  *   EFFECTS: LineTable - by adding new lines
                     33:  *           SegTable  - by using free space for the line
                     34:  *                       and via 'allocseg'
                     35:  *           TotalSegs - via 'allocseg'
                     36:  *           memory    - by allocating segments via 'allocseg'
                     37:  */
                     38: 
                     39: short addline(linenum, linelength, line)
1.1.1.2 ! root       40: USHORT linenum;
        !            41: UCHAR  linelength;
        !            42: UCHAR  line[];
1.1       root       43: {
1.1.1.2 ! root       44:     register USHORT i, j;
        !            45:     UCHAR  bytesneeded;
        !            46:     SEL selector;
1.1       root       47:     short         rc;   /* return code of allocseg */
                     48: 
                     49:     bytesneeded = (HEADERSIZE + linelength);
                     50:     rc = 0;
                     51: 
                     52: /*  use segment of previous line to reduce scattering */
                     53:     if (linenum == 0)
1.1.1.2 ! root       54:        selector = SELECTOROF(LineTable[linenum]);
1.1       root       55:     else
1.1.1.2 ! root       56:        selector = SELECTOROF(LineTable[linenum -1]);
1.1       root       57: 
                     58: /*  find segment contianing line number -1 */
                     59:     for(i = 0; (selector != SegTable[i].segment) && (i < TotalSegs); i++);
                     60: 
                     61: /*  scan for first fit if not enough space in segment containing line -1 */
                     62:     if (SegTable[i].free < bytesneeded) {
                     63:        for(i = 0; (SegTable[i].free < bytesneeded) && (i < TotalSegs); i++);
                     64: /*     allocate a segment if not eough space is found */
                     65:        if (i >= TotalSegs)
                     66:            rc = allocseg();
                     67:     }
                     68: 
                     69:     if (rc == 0) {      /* no error allocating a segment */
                     70: /*     point line table at segment */
1.1.1.2 ! root       71:        SELECTOROF(LineTable[linenum]) = SegTable[i].segment;
        !            72:        OFFSETOF(LineTable[linenum]) = SEGSIZE - SegTable[i].free;
1.1       root       73: 
                     74: /*     place line in segment */
                     75:        SegTable[i].free -= bytesneeded;
                     76:        LineTable[linenum]->linelength = linelength;
                     77:        for(j = 0; j < linelength; j++)
                     78:        LineTable[linenum]->firstchar[j] = line[j];
                     79:     }
                     80:     return(rc);
                     81: }
                     82: 
                     83: 
                     84: 
                     85: 
                     86: /*** deleteline - marks lines as deleted
                     87:  *
1.1.1.2 ! root       88:  *   deleteline marks a line as detected in the segment
        !            89:  *     it is contained in and marks the segment containing
1.1       root       90:  *     the line as needing compacting.
                     91:  *
                     92:  *   deleteline (linenum)
                     93:  *
                     94:  *   Entry     linenum - line number of line to mark as deleted
                     95:  *
                     96:  *   EFFECTS: SegTable - by marking the segment containing the line
                     97:  *                        as needing compacting.
                     98:  *                      by marking the line in the segment as deleted.
                     99:  */
                    100: 
                    101: void deleteline(linenum)
1.1.1.2 ! root      102: USHORT linenum;
1.1       root      103: {
1.1.1.2 ! root      104:     register USHORT i;
        !           105:     SEL selector;
1.1       root      106: 
                    107:     LineTable[linenum]->deleted = TRUE;
1.1.1.2 ! root      108:     selector = SELECTOROF(LineTable[linenum]);
1.1       root      109: 
                    110:     for(i = 0; (selector != SegTable[i].segment) && (i < TotalSegs); i++);
                    111: 
                    112:     SegTable[i].flags = TRUE;
                    113: 
                    114: }
                    115: 
                    116: 
                    117: 
                    118: 
                    119: /*** flushline - flushes editbuffer to segment
                    120:  *
                    121:  *   flushline rewrites the contents of a line to a segment
                    122:  *     by using deleteline and addline.
                    123:  *
                    124:  *   flushline (linenum, line)
                    125:  *
                    126:  *   ENTRY     linenum - linenumber of line to rewrite.
                    127:  *             line    - line to be rewriten.
                    128:  *
                    129:  *   flushline checks if the current line is beyond the end of
                    130:  *     file, if so it adds the line to the file using addline,
                    131:  *     else it replaces it by deleting the the old copy of the
                    132:  *     line using deleteline and then adding the line back using
                    133:  *     addline. flushline will EXIT the program by calling error25
                    134:  *     if it is unable to add a line to the file or the number of
                    135:  *     lines in the file becomes greater than MAXLINES.
                    136:  *
                    137:  *   EFFECTS: TotalLines - by incrementing it
                    138:  *           SegTable   - by marking the segment containing the line
                    139:  *                          as needing compacting, via 'deleteline'
                    140:  *                        by marking the line in the segment as deleted.
                    141:  *                          via 'deleteline'
                    142:  *           LineTable  - by adding new lines via 'addline'
                    143:  *           SegTable   - by using free space for the line via 'addline'
                    144:  *                        and via 'allocseg'
                    145:  *           TotalSegs  - via 'allocseg'
                    146:  *           memory     - by allocating segments via 'allocseg'
                    147:  */
                    148: 
                    149: flushline(linenum, line)
1.1.1.2 ! root      150: USHORT linenum;
        !           151: UCHAR  line[];
1.1       root      152: {
1.1.1.2 ! root      153:     register UCHAR  i;
1.1       root      154: 
                    155: /*  delete the line if it's not beyond the end of the file */
                    156:     if ( !(linenum == TotalLines))
                    157:        deleteline(linenum);
                    158:     else
                    159:        ++TotalLines;
                    160: 
                    161:     for (i = (LINESIZE -1); !isgraph(line[i]) && inline(i, 0); i--);
                    162: 
                    163:     if ( (addline(linenum, i +1, line) != 0) || !(TotalLines < MAXLINES) )
                    164:        error25(7);    /* call error message and quit */;
                    165: }
                    166: 
                    167: 
                    168: 
                    169: 
                    170: /*** getline - fills line from segment
                    171:  *
                    172:  *   getline copies a 'line' from a segment to the
                    173:  *     address given by line.
                    174:  *
                    175:  *   getline (linenum, line)
                    176:  *
                    177:  *   ENTRY   linenum  -  the line number of 'line' in the file
                    178:  *          line     -  address of where to copy 'line'
                    179:  *
                    180:  *   getline fills the line out to LINESIZE with spaces and
                    181:  *     returns a line of spaces if the linenum is not a valid
                    182:  *     line number.
                    183:  *
                    184:  *   WARNING getline does not check if the address given by line is
                    185:  *          valid.
                    186:  *
                    187:  *   EFFECTS memory - by copying the line to the address given by line.
                    188:  */
                    189: 
                    190: void getline(linenum, line)
1.1.1.2 ! root      191: USHORT linenum;
        !           192: UCHAR  *line;
1.1       root      193: {
1.1.1.2 ! root      194:     register USHORT i;
1.1       root      195: 
                    196:     i = 0;
                    197: 
                    198: /*  copy 'line' to address given by line */
                    199:     if (linenum < TotalLines)  /* a valid line number */
                    200:        for ( ; i < (LineTable[linenum]->linelength); i++)
                    201:            line[i] = LineTable[linenum]->firstchar[i];
                    202: 
                    203: /*  pad line with spaces out to LINESIZE */
                    204:     for ( ; i < LINESIZE; i++)
                    205:        line[i] = ' ';
                    206: 
                    207: }

unix.superglobalmegacorp.com

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