"Fossies" - the Fresh Open Source Software Archive 
Member "qdiff-0.9.1/trotfile.cc" (21 Oct 2008, 3562 Bytes) of package /linux/privat/old/qdiff-0.9.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "trotfile.cc" see the
Fossies "Dox" file reference documentation.
1 /*GPL*START*
2 *
3 * Copyright (C) 1998 by Johannes Overmann <overmann@iname.com>
4 * Copyright (C) 2008 by Tong Sun <suntong001@users.sourceforge.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * *GPL*END*/
20
21 #include "trotfile.h"
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25
26 TROTFile::TROTFile(const char *filename, int num_buf, int buf_size)
27 :numbuf(num_buf), bufsize(buf_size), bufbits(0), bufmask(0), nummask(0),
28 offmask(0), off(new int[numbuf]),
29 buf(new uchar *[numbuf]), _size(0), fname(filename), file(0)
30 {
31 bool nonreg = false;
32
33 // check numbuf and bufsize
34 if((!isPowerOf2(numbuf)) || (!isPowerOf2(bufsize)))
35 fatalError("numbuf and bufsize must be powers of two!\n");
36
37 // calc masks
38 bufmask = bufsize-1;
39 offmask = ~bufmask;
40 bufbits = intLog2(bufsize);
41 nummask = numbuf-1;
42
43 // get file existance, type and _size
44 struct stat a;
45 if(stat(filename, &a))
46 userError("file '%s' does not exist!\n", filename);
47 if(!S_ISREG(a.st_mode)) {
48 userWarning("'%s' is not a regular file\n", filename);
49 nonreg = true;
50 }
51 _size = a.st_size;
52
53 // open file
54 file = fopen(filename, "rb");
55 if(file==0)
56 userError("error while opening file '%s' for reading!\n", filename);
57
58 if(nonreg) {
59 // get size of nonregular file
60 int s;
61 char tmp;
62
63 for(s=1; s>0; s<<=1) {
64 if((fseek(file, s , SEEK_SET)!=0)||(fread(&tmp, 1, 1, file)!=1)) break;
65 }
66 if(s<=0)
67 userError("file '%s' has zero size or is too large\n", filename);
68
69 int lo = s/2;
70 int hi = s;
71 while((lo+1) < hi) {
72 s = (lo+hi)/2;
73 if((fseek(file, s, SEEK_SET)!=0)||(fread(&tmp, 1, 1, file)!=1)) {
74 hi = s;
75 } else {
76 lo = s;
77 }
78 }
79 rewind(file);
80
81 _size = hi;
82 userWarning("assuming size %d (%.1fk, %0.1fm) for file '%s'\n",
83 _size, double(_size)/1024.0, double(_size)/1024.0/1024.0,
84 filename);
85 }
86
87 // alloc buffers
88 for(int i=0; i<numbuf; i++) {
89 buf[i] = new uchar[bufsize];
90 off[i] = -1; // invalidate buffer
91 }
92
93
94 }
95
96
97 TROTFile::~TROTFile() {
98 fclose(file);
99 for(int i=0; i<numbuf; i++)
100 delete[] buf[i];
101 delete[] off;
102 delete[] buf;
103 }
104
105
106 int TROTFile::intLog2(int i) const {
107 if(i<=0) fatalError("IntLog2: i must be >0! (was %d)", i);
108 int r;
109 for(r=0; i!=1; r++, i>>=1) ;
110 return r;
111 }
112
113
114 bool TROTFile::isPowerOf2(int i) const {
115 if(i<=0) return false;
116 for(; (i&1)==0; i>>=1) ;
117 if(i==1) return true;
118 else return false;
119 }
120
121
122 void TROTFile::loadBuf(int offset, int buffer) {
123 if(fseek(file, offset, SEEK_SET))
124 fatalError("LoadBuf: fseek failed!\n");
125 int len = bufsize;
126 if(offset==(_size&offmask)) len = _size & bufmask;
127 int r = fread(buf[buffer], 1, len, file);
128 if(r != len)
129 fatalError("LoadBuf: fread failed!\n");
130 off[buffer] = offset;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147