home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / uupoll068.lha / source / lbio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  3.0 KB  |  145 lines

  1.  
  2. /*
  3.                           lbio.c -- line buffer I/O
  4.  
  5.       This module provides you with the ability to access memory locations
  6.     of character lines in a similar way as you can do with file functions
  7.     of the standard C library.
  8.  
  9.           ___  _______
  10.           /__)(_  /_       Genesis     : Sat Oct 12 22:48:22 1991
  11.          / \____)(___      Last Change : Mon Jan 27 18:00:58 1992
  12.         /                  Class       : ANSI C
  13.  
  14.       Copyright (c) Ralf S. Engelschall, All Rights Reserved.
  15.                                                                             */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #include "defines.h"
  23. #include "cus.h"
  24. #include "lbio.h"
  25.  
  26.  
  27.  
  28. /*    Remember that a "line" here means a stream of chars _between_ SOF
  29.     and the first NL char, _between_ two NL chars or _between_ the last
  30.     NL char and the EOF.
  31.  
  32.     Be sure, that you actually give this functions a area with at least
  33.     ONE line (at least one char)!!!
  34.                                                                             */
  35.  
  36.  
  37. static int linelen(char *cp, char *cpLast)
  38. {
  39.     int cnt;
  40.  
  41.     for (cnt = 0; (*cp != NL) && (cp <= cpLast); cp++, cnt++)
  42.         ; /* void */
  43.  
  44.     return cnt;
  45. }
  46.  
  47.  
  48. struct LineBuffer *lbopen(char *cpFirst, char *cpLast)
  49. {
  50.     LB *lb = NULL;
  51.     char **lpa = NULL;
  52.     char *cp;
  53.     char *cpTmp;
  54.     char **cpp;
  55.     int cnt;
  56.     LB *rc;
  57.  
  58.     if (cpFirst > cpLast)
  59.         CU(NULL);
  60.  
  61.     if ((lb = (LB *)malloc(sizeof(LB))) == NULL)
  62.         CU(NULL);
  63.  
  64.     lb->lb_cpFirst = cpFirst;
  65.     lb->lb_cpLast = cpLast;
  66.  
  67.     for (cnt = 0, cp = cpFirst; cp <= cpLast; cp++)
  68.         if (*cp == NL)
  69.             cnt++;
  70.     if (*(--cp) != NL)
  71.         cnt++;
  72.     lb->lb_nLnMax = cnt;
  73.  
  74.     if ((lpa = (char **)malloc(cnt * sizeof(char *))) == NULL) {
  75.         free(lb);
  76.         CU(NULL);
  77.     }
  78.  
  79.     lb->lb_cppLnFirst = lpa;
  80.     lb->lb_cppLnCur = lpa;
  81.     lb->lb_cppLnLast = lpa + (cnt - 1);
  82.  
  83.     for (cpp = lpa, cp = cpTmp = cpFirst; cp <= cpLast; cp++)
  84.         if (*cp == NL) {
  85.             *cpp++ = cpTmp;
  86.             cpTmp = cp + 1;
  87.         }
  88.     if (*(--cp) != NL)
  89.         *cpp++ = cpTmp;
  90.  
  91.     lb->lb_nLnCur = 1;
  92.     lb->lb_cpLnCur = cpFirst;
  93.     lb->lb_wLnCur = linelen(cpFirst, cpLast);
  94.  
  95.     rc = lb;
  96.     CUS:
  97.  
  98.     return rc;
  99. }
  100.  
  101.  
  102. void lbfree(struct LineBuffer *lb)
  103. {
  104.     free(lb->lb_cppLnFirst);
  105.     free(lb);
  106. }
  107.  
  108.  
  109. int lbseek(struct LineBuffer *lb, signed long wOffs, short wMode)
  110. {
  111.     if (wMode == LBSEEK_SET) {
  112.         if ((wOffs < 0) || (abs(wOffs + 1) > lb->lb_nLnMax))
  113.             return FALSE;
  114.         lb->lb_cppLnCur = lb->lb_cppLnFirst + wOffs;
  115.         lb->lb_nLnCur = wOffs + 1;
  116.         lb->lb_cpLnCur = *(lb->lb_cppLnCur);
  117.         lb->lb_wLnCur = linelen(lb->lb_cpLnCur, lb->lb_cpLast);
  118.         return TRUE;
  119.     }
  120.  
  121.     if (wMode == LBSEEK_CUR) {
  122.         if (   ((lb->lb_nLnCur + wOffs) > lb->lb_nLnMax)
  123.             || ((lb->lb_nLnCur + wOffs) <= 0)            )
  124.             return FALSE;
  125.         lb->lb_cppLnCur += wOffs;
  126.         lb->lb_nLnCur += wOffs;
  127.         lb->lb_cpLnCur = *(lb->lb_cppLnCur);
  128.         lb->lb_wLnCur = linelen(lb->lb_cpLnCur, lb->lb_cpLast);
  129.         return TRUE;
  130.     }
  131.  
  132.     if (wMode == LBSEEK_END) {
  133.         if ((wOffs > 0) || (abs(wOffs + 1) > lb->lb_nLnMax))
  134.             return FALSE;
  135.         lb->lb_cppLnCur = lb->lb_cppLnLast + wOffs;
  136.         lb->lb_nLnCur = lb->lb_nLnMax + wOffs;
  137.         lb->lb_cpLnCur = *(lb->lb_cppLnCur);
  138.         lb->lb_wLnCur = linelen(lb->lb_cpLnCur, lb->lb_cpLast);
  139.         return TRUE;
  140.     }
  141.  
  142.     return FALSE;
  143. }
  144.  
  145.