home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / PTRFUNC.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  3KB  |  165 lines

  1. /* $Header: /nw/tony/src/stevie/src/RCS/ptrfunc.c,v 1.5 89/03/11 22:43:12 tony Exp $
  2.  *
  3.  * The routines in this file attempt to imitate many of the operations
  4.  * that used to be performed on simple character pointers and are now
  5.  * performed on LPTR's. This makes it easier to modify other sections
  6.  * of the code. Think of an LPTR as representing a position in the file.
  7.  * Positions can be incremented, decremented, compared, etc. through
  8.  * the functions implemented here.
  9.  */
  10.  
  11. #include "stevie.h"
  12.  
  13. /*
  14.  * inc(p)
  15.  *
  16.  * Increment the line pointer 'p' crossing line boundaries as necessary.
  17.  * Return 1 when crossing a line, -1 when at end of file, 0 otherwise.
  18.  */
  19. int
  20. inc(lp)
  21. register LPTR    *lp;
  22. {
  23.     register char    *p;
  24.  
  25.     if (lp && lp->linep)
  26.         p = &(lp->linep->s[lp->index]);
  27.     else
  28.         return -1;
  29.  
  30.     if (*p != NUL) {            /* still within line */
  31.         lp->index++;
  32.         return ((p[1] != NUL) ? 0 : 1);
  33.     }
  34.  
  35.     if (lp->linep->next != Fileend->linep) {  /* there is a next line */
  36.         lp->index = 0;
  37.         lp->linep = lp->linep->next;
  38.         return 1;
  39.     }
  40.  
  41.     return -1;
  42. }
  43.  
  44. /*
  45.  * dec(p)
  46.  *
  47.  * Decrement the line pointer 'p' crossing line boundaries as necessary.
  48.  * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
  49.  */
  50. int
  51. dec(lp)
  52. register LPTR    *lp;
  53. {
  54.     if (lp->index > 0) {            /* still within line */
  55.         lp->index--;
  56.         return 0;
  57.     }
  58.  
  59.     if (lp->linep->prev != Filetop->linep) { /* there is a prior line */
  60.         lp->linep = lp->linep->prev;
  61.         lp->index = strlen(lp->linep->s);
  62.         return 1;
  63.     }
  64.  
  65.     lp->index = 0;                /* stick at first char */
  66.     return -1;                /* at start of file */
  67. }
  68.  
  69. /*
  70.  * gchar(lp) - get the character at position "lp"
  71.  */
  72. int
  73. gchar(lp)
  74. register LPTR    *lp;
  75. {
  76.     if (lp && lp->linep)
  77.         return (lp->linep->s[lp->index]);
  78.     else
  79.         return 0;
  80. }
  81.  
  82. /*
  83.  * pchar(lp, c) - put character 'c' at position 'lp'
  84.  */
  85. void
  86. pchar(lp, c)
  87. register LPTR    *lp;
  88. char    c;
  89. {
  90.     lp->linep->s[lp->index] = c;
  91. }
  92.  
  93. /*
  94.  * pswap(a, b) - swap two position pointers
  95.  */
  96. void
  97. pswap(a, b)
  98. register LPTR    *a, *b;
  99. {
  100.     LPTR    tmp;
  101.  
  102.     tmp = *a;
  103.     *a  = *b;
  104.     *b  = tmp;
  105. }
  106.  
  107. /*
  108.  * Position comparisons
  109.  */
  110.  
  111. bool_t
  112. lt(a, b)
  113. register LPTR    *a, *b;
  114. {
  115.     register int    an, bn;
  116.  
  117.     an = LINEOF(a);
  118.     bn = LINEOF(b);
  119.  
  120.     if (an != bn)
  121.         return (an < bn);
  122.     else
  123.         return (a->index < b->index);
  124. }
  125.  
  126. #if 0
  127. bool_t
  128. gt(a, b)
  129. LPTR    *a, *b;
  130. {
  131.     register int an, bn;
  132.  
  133.     an = LINEOF(a);
  134.     bn = LINEOF(b);
  135.  
  136.     if (an != bn)
  137.         return (an > bn);
  138.     else
  139.         return (a->index > b->index);
  140. }
  141. #endif
  142.  
  143. bool_t
  144. equal(a, b)
  145. register LPTR    *a, *b;
  146. {
  147.     return (a->linep == b->linep && a->index == b->index);
  148. }
  149.  
  150. bool_t
  151. ltoreq(a, b)
  152. register LPTR    *a, *b;
  153. {
  154.     return (lt(a, b) || equal(a, b));
  155. }
  156.  
  157. #if 0
  158. bool_t
  159. gtoreq(a, b)
  160. LPTR    *a, *b;
  161. {
  162.     return (gt(a, b) || equal(a, b));
  163. }
  164. #endif
  165.