Mercurial > hg > octave-avbm
annotate gui/src/terminal/BlockArray.cpp @ 13506:c70511cf64ee
Reformatted to GNU Style.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Sun, 17 Jul 2011 22:59:28 +0200 (2011-07-17) |
parents | 86d6c3b90ad7 |
children |
rev | line source |
---|---|
13501 | 1 /* |
2 This file is part of Konsole, an X terminal. | |
3 Copyright 2000 by Stephan Kulow <coolo@kde.org> | |
4 | |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 2 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 02110-1301 USA. | |
19 */ | |
20 | |
21 // Own | |
22 #include "BlockArray.h" | |
23 | |
24 // System | |
25 #include <assert.h> | |
26 #include <sys/mman.h> | |
27 #include <sys/param.h> | |
28 #include <unistd.h> | |
29 #include <stdio.h> | |
30 | |
31 #define KDE_fseek ::fseek | |
32 #define KDE_lseek ::lseek | |
33 | |
34 static int blocksize = 0; | |
35 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
36 BlockArray::BlockArray ():size (0), |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
37 current (size_t (-1)), |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
38 index (size_t (-1)), |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
39 lastmap (0), lastmap_index (size_t (-1)), lastblock (0), ion (-1), length (0) |
13501 | 40 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
41 // lastmap_index = index = current = size_t(-1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
42 if (blocksize == 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
43 blocksize = ((sizeof (Block) / getpagesize ()) + 1) * getpagesize (); |
13501 | 44 |
45 } | |
46 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
47 BlockArray::~BlockArray () |
13501 | 48 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
49 setHistorySize (0); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
50 assert (!lastblock); |
13501 | 51 } |
52 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
53 size_t |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
54 BlockArray::append (Block * block) |
13501 | 55 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
56 if (!size) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
57 return size_t (-1); |
13501 | 58 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
59 ++current; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
60 if (current >= size) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
61 current = 0; |
13501 | 62 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
63 int rc; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
64 rc = KDE_lseek (ion, current * blocksize, SEEK_SET); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
65 if (rc < 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
66 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
67 perror ("HistoryBuffer::add.seek"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
68 setHistorySize (0); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
69 return size_t (-1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
70 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
71 rc = write (ion, block, blocksize); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
72 if (rc < 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
73 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
74 perror ("HistoryBuffer::add.write"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
75 setHistorySize (0); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
76 return size_t (-1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
77 } |
13501 | 78 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
79 length++; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
80 if (length > size) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
81 length = size; |
13501 | 82 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
83 ++index; |
13501 | 84 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
85 delete block; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
86 return current; |
13501 | 87 } |
88 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
89 size_t |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
90 BlockArray::newBlock () |
13501 | 91 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
92 if (!size) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
93 return size_t (-1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
94 append (lastblock); |
13501 | 95 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
96 lastblock = new Block (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
97 return index + 1; |
13501 | 98 } |
99 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
100 Block * |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
101 BlockArray::lastBlock () const |
13501 | 102 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
103 return lastblock; |
13501 | 104 } |
105 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
106 bool |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
107 BlockArray::has (size_t i) const |
13501 | 108 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
109 if (i == index + 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
110 return true; |
13501 | 111 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
112 if (i > index) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
113 return false; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
114 if (index - i >= length) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
115 return false; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
116 return true; |
13501 | 117 } |
118 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
119 const Block * |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
120 BlockArray::at (size_t i) |
13501 | 121 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
122 if (i == index + 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
123 return lastblock; |
13501 | 124 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
125 if (i == lastmap_index) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
126 return lastmap; |
13501 | 127 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
128 if (i > index) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
129 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
130 //kDebug(1211) << "BlockArray::at() i > index\n"; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
131 return 0; |
13501 | 132 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
133 |
13501 | 134 // if (index - i >= length) { |
135 // kDebug(1211) << "BlockArray::at() index - i >= length\n"; | |
136 // return 0; | |
137 // } | |
138 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
139 size_t j = i; // (current - (index - i) + (index/size+1)*size) % size ; |
13501 | 140 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
141 assert (j < size); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
142 unmap (); |
13501 | 143 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
144 Block *block = |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
145 (Block *) mmap (0, blocksize, PROT_READ, MAP_PRIVATE, ion, j * blocksize); |
13501 | 146 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
147 if (block == (Block *) - 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
148 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
149 perror ("mmap"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
150 return 0; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
151 } |
13501 | 152 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
153 lastmap = block; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
154 lastmap_index = i; |
13501 | 155 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
156 return block; |
13501 | 157 } |
158 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
159 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
160 BlockArray::unmap () |
13501 | 161 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
162 if (lastmap) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
163 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
164 int res = munmap ((char *) lastmap, blocksize); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
165 if (res < 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
166 perror ("munmap"); |
13501 | 167 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
168 lastmap = 0; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
169 lastmap_index = size_t (-1); |
13501 | 170 } |
171 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
172 bool |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
173 BlockArray::setSize (size_t newsize) |
13501 | 174 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
175 return setHistorySize (newsize * 1024 / blocksize); |
13501 | 176 } |
177 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
178 bool |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
179 BlockArray::setHistorySize (size_t newsize) |
13501 | 180 { |
181 // kDebug(1211) << "setHistorySize " << size << " " << newsize; | |
182 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
183 if (size == newsize) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
184 return false; |
13501 | 185 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
186 unmap (); |
13501 | 187 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
188 if (!newsize) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
189 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
190 delete lastblock; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
191 lastblock = 0; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
192 if (ion >= 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
193 close (ion); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
194 ion = -1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
195 current = size_t (-1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
196 return true; |
13501 | 197 } |
198 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
199 if (!size) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
200 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
201 FILE *tmp = tmpfile (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
202 if (!tmp) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
203 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
204 perror ("konsole: cannot open temp file.\n"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
205 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
206 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
207 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
208 ion = dup (fileno (tmp)); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
209 if (ion < 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
210 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
211 perror ("konsole: cannot dup temp file.\n"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
212 fclose (tmp); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
213 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
214 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
215 if (ion < 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
216 return false; |
13501 | 217 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
218 assert (!lastblock); |
13501 | 219 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
220 lastblock = new Block (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
221 size = newsize; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
222 return false; |
13501 | 223 } |
224 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
225 if (newsize > size) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
226 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
227 increaseBuffer (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
228 size = newsize; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
229 return false; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
230 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
231 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
232 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
233 decreaseBuffer (newsize); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
234 if (ftruncate (ion, length * blocksize) == -1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
235 perror ("ftruncate"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
236 size = newsize; |
13501 | 237 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
238 return true; |
13501 | 239 } |
240 } | |
241 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
242 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
243 moveBlock (FILE * fion, int cursor, int newpos, char *buffer2) |
13501 | 244 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
245 int res = KDE_fseek (fion, cursor * blocksize, SEEK_SET); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
246 if (res) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
247 perror ("fseek"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
248 res = fread (buffer2, blocksize, 1, fion); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
249 if (res != 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
250 perror ("fread"); |
13501 | 251 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
252 res = KDE_fseek (fion, newpos * blocksize, SEEK_SET); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
253 if (res) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
254 perror ("fseek"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
255 res = fwrite (buffer2, blocksize, 1, fion); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
256 if (res != 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
257 perror ("fwrite"); |
13501 | 258 } |
259 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
260 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
261 BlockArray::decreaseBuffer (size_t newsize) |
13501 | 262 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
263 if (index < newsize) // still fits in whole |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
264 return; |
13501 | 265 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
266 int offset = (current - (newsize - 1) + size) % size; |
13501 | 267 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
268 if (!offset) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
269 return; |
13501 | 270 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
271 // The Block constructor could do somthing in future... |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
272 char *buffer1 = new char[blocksize]; |
13501 | 273 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
274 FILE *fion = fdopen (dup (ion), "w+b"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
275 if (!fion) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
276 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
277 delete[]buffer1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
278 perror ("fdopen/dup"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
279 return; |
13501 | 280 } |
281 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
282 int firstblock; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
283 if (current <= newsize) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
284 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
285 firstblock = current + 1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
286 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
287 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
288 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
289 firstblock = 0; |
13501 | 290 } |
291 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
292 size_t oldpos; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
293 for (size_t i = 0, cursor = firstblock; i < newsize; i++) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
294 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
295 oldpos = (size + cursor + offset) % size; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
296 moveBlock (fion, oldpos, cursor, buffer1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
297 if (oldpos < newsize) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
298 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
299 cursor = oldpos; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
300 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
301 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
302 cursor++; |
13501 | 303 } |
304 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
305 current = newsize - 1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
306 length = newsize; |
13501 | 307 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
308 delete[]buffer1; |
13501 | 309 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
310 fclose (fion); |
13501 | 311 |
312 } | |
313 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
314 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
315 BlockArray::increaseBuffer () |
13501 | 316 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
317 if (index < size) // not even wrapped once |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
318 return; |
13501 | 319 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
320 int offset = (current + size + 1) % size; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
321 if (!offset) // no moving needed |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
322 return; |
13501 | 323 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
324 // The Block constructor could do somthing in future... |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
325 char *buffer1 = new char[blocksize]; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
326 char *buffer2 = new char[blocksize]; |
13501 | 327 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
328 int runs = 1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
329 int bpr = size; // blocks per run |
13501 | 330 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
331 if (size % offset == 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
332 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
333 bpr = size / offset; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
334 runs = offset; |
13501 | 335 } |
336 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
337 FILE *fion = fdopen (dup (ion), "w+b"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
338 if (!fion) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
339 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
340 perror ("fdopen/dup"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
341 delete[]buffer1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
342 delete[]buffer2; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
343 return; |
13501 | 344 } |
345 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
346 int res; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
347 for (int i = 0; i < runs; i++) |
13501 | 348 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
349 // free one block in chain |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
350 int firstblock = (offset + i) % size; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
351 res = KDE_fseek (fion, firstblock * blocksize, SEEK_SET); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
352 if (res) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
353 perror ("fseek"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
354 res = fread (buffer1, blocksize, 1, fion); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
355 if (res != 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
356 perror ("fread"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
357 int newpos = 0; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
358 for (int j = 1, cursor = firstblock; j < bpr; j++) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
359 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
360 cursor = (cursor + offset) % size; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
361 newpos = (cursor - offset + size) % size; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
362 moveBlock (fion, cursor, newpos, buffer2); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
363 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
364 res = KDE_fseek (fion, i * blocksize, SEEK_SET); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
365 if (res) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
366 perror ("fseek"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
367 res = fwrite (buffer1, blocksize, 1, fion); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
368 if (res != 1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
369 perror ("fwrite"); |
13501 | 370 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
371 current = size - 1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
372 length = size; |
13501 | 373 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
374 delete[]buffer1; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
375 delete[]buffer2; |
13501 | 376 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
377 fclose (fion); |
13501 | 378 |
379 } |