Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
2.7 KiB

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