File: xMultiLineBuf.cpp

package info (click to toggle)
xirc 2.0-3
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 2,624 kB
  • ctags: 2,093
  • sloc: cpp: 21,094; makefile: 1,033
file content (166 lines) | stat: -rw-r--r-- 4,091 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/***************************************************************************
**    xMultiLineBuf.cpp  $Revision: 1.2 $ - $Name: V2-18 $ 
**    Class for manipulating Multiple lines of text
**
**    Copyright (C) 1996 Joseph Croft <jcroft@unicomp.net>
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Library General Public
**    License as published by the Free Software Foundation; either
**    version 2 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Library General Public License for more details.
**
**    You should have received a copy of the GNU Library General Public
**    License along with this library; if not, write to the Free
**    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
 ***************************************************************************/
#include <stdio.h>
#include "xMultiLineBuf.h"

static int dbg = 0;

xMultiLineBuf::xMultiLineBuf(int height)
{
   pBeg = new QString[height];
   pEnd = pBeg + height - 1;
   pHead = pTail = pBeg;
}

xMultiLineBuf::~xMultiLineBuf()
{
   if (pBeg)
      delete [] pBeg;
}

const QString *xMultiLineBuf::nextLine(QString *pStr, bool used)
{
   if (used && pHead == pTail)
      return(pHead);
   else if (used && (pStr + 1) == pHead)
      return(pTail);
   else if (pStr == pEnd)
      return(pBeg);
   else
      return(++pStr);
}

const QString *xMultiLineBuf::prevLine(const QString *pStr, bool used)
{
   if (pHead == pTail)
      return(pHead);
   else if (pStr == pTail && used)
   {
      if (pHead == pBeg)
         return(pEnd);
      else
         return(pHead - 1);
   }
   else if (pStr == pBeg)
      return(pEnd);
   else
      return(--pStr);
}

const QString *xMultiLineBuf::getNext(const QString *pStr, int &changed)
{
   changed = FALSE;
   if (pHead == pTail)
      return(pStr);
   if (pHead == pTail || (pStr + 1) == pHead || 
       (pStr == pEnd && pHead == pBeg))
      return(pStr);
   else
   {
      changed = TRUE;
      if (pStr == pEnd)
         return(pBeg);
      else
         return(++pStr);
   }
}

const QString *xMultiLineBuf::getPrev(const QString *pStr, int &changed)
{
   changed = FALSE;
   if (pHead == pTail || pStr == pTail)
      return(pStr);
   else
   {
      changed = TRUE;
      if (pStr == pBeg)
         return(pEnd);
      else
         return(--pStr);
   }
}

const QString *xMultiLineBuf::putString(const char *pString)
{
   const QString *rv;
   
   if (dbg) fprintf(stdout, "MultiLineBuf::putString():Adding String: |%s|\n\r", (char *)pString);
   rv = pHead;
   *pHead = pString;
   if (dbg) fprintf(stdout, "MultiLineBuf::putString():*pHead = |%s|\n\r", (const char *)*pHead);
   if (((const QString *)pHead = nextLine(pHead)) == pTail)
      (const QString *)pTail = nextLine(pTail);
   return(rv);
}

const QString *xMultiLineBuf::pointLine(int ofs, int &retOfs, int &retLines)
{
   const QString *rv;
   
   rv = pTail;
   if (pHead == pTail)
   {
      retLines = retOfs = 0;
      return(pHead);
   }
   else if (ofs <= 0)
      rv = lastLine();
   else if (ofs >= getLinesUsed())
   {
      retOfs = 0;
      retLines = getLinesUsed();
      return(pTail);
   }
   else
   {
      if (ofs >= (pHead - pBeg))
         rv = pEnd - (ofs - (pHead - pBeg));
      else
         rv = pHead - (ofs + 1);
      if (rv >= pTail)
         retOfs = rv - pTail;
      else
         retOfs = (rv - pBeg) + (pEnd - pHead);
      retLines = getLinesUsed() - retOfs;
   }
   return(rv);
}

const QString *xMultiLineBuf::lastLine()
{
   if (pHead == pTail)
      return(pHead);
   else if (pHead == pBeg)
      return(pEnd);
   else
      return(pHead - 1);
}

int xMultiLineBuf::getLinesUsed()
{
   if (pHead == pTail)
      return(0);
   else if (pHead > pTail)
      return (pHead - pTail);
   else
      return((pEnd - pTail) + (pHead - pBeg) + 1);
}