ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/PrimerMatch/bufferedFile.cc
Revision: 1.1.1.1 (vendor branch)
Committed: Wed Dec 22 21:37:17 2004 UTC (11 years, 11 months ago) by nje01
Branch: MAIN
CVS Tags: HEAD, RELEASE-20041222, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
Initial primer_match import

Line File contents
1 /**************************************************************************
2 * This code is part of the supporting infrastructure for ATA Mapper.
3 * Copyright (C) 2002,2003,2004 Applera Corporation. All rights reserved.
4 * Author: Nathan Edwards
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 (LICENSE.txt) a copy of the GNU General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *************************************************************************/
20
21 //================================================================
22 // Brian Walenz's code. Provides methods for buffered I/O.
23 //
24 //===============================================================
25
26 #include "bufferedFile.h"
27
28 #if !defined(NO_STD_NAMESPACE)
29 using namespace std;
30 #endif
31
32 BufferedFile::BufferedFile() {
33 int buffersize = 1024*1024;
34 _buffer = new char[buffersize];
35 if (_buffer == 0L) {
36 fprintf(stderr, "Couldn't allocate the file buffer\n");
37 exit(1);
38 }
39
40 _bufferMax = buffersize;
41 _bufferLen = 0;
42 _readPos = 0;
43 _size = _pos = 0;
44 }
45
46 BufferedFile::BufferedFile(std::string const &infile, int buffersize) {
47
48 _size = _pos = 0;
49
50 if (buffersize <= 0) {
51 fprintf(stderr, "WARNING: Illegal buffer size;resetting to 1024*1024.\n");
52 buffersize = 1024*1024;
53 }
54
55 _buffer = new char[buffersize];
56 if (_buffer == 0L) {
57 fprintf(stderr, "Couldn't allocate the file buffer\n");
58 exit(1);
59 }
60 _bufferMax = buffersize;
61 openBuf(infile);
62 }
63
64 void BufferedFile::openBuf(std::string const &fname) {
65 struct stat st;
66 if (stat(fname.c_str(),&st)<0) {
67 perror("BufferedFile::openBuf:stat");
68 exit(1);
69 }
70 _size = st.st_size;
71
72 #if ! defined(__CYGWIN__)
73 bufF = fopen(fname.c_str(),"r");
74 #else
75 bufF = fopen(fname.c_str(),"rb");
76 #endif
77 if (!bufF) {
78 cerr << "couldn't open input file: " << fname << endl;
79 exit(1);
80 }
81 _bufferLen = 0;
82 _readPos = 0;
83 }
84
85
86 BufferedFile::~BufferedFile() {
87 delete [] _buffer;
88 fclose(bufF);
89 }
90
91 void BufferedFile::reset() {
92 _bufferLen = 0;
93 _readPos = 0;
94 rewind(bufF);
95 _pos = 0;
96 }
97
98 FILE_POSITION_TYPE BufferedFile::getPos() const {
99 return _pos;
100 }
101
102 void BufferedFile::setPos(FILE_POSITION_TYPE p) {
103 _pos = p;
104 FILE_POSITION_TYPE tg = FTELL(bufF);
105 if (tg > p && tg <= p + _bufferLen) {
106 _readPos = p - (tg - _bufferLen);
107 } else {
108 // bufF.clear();
109 FSEEK(bufF,_bufferMax*(p/_bufferMax),SEEK_SET);
110 _bufferLen = fread(_buffer,sizeof(unsigned char),_bufferMax,bufF);
111 _readPos = p%_bufferMax;
112 }
113 }
114
115 unsigned char BufferedFile::getCharacter(void) {
116 if (_readPos >= _bufferLen) {
117 _bufferLen = fread(_buffer,sizeof(unsigned char),_bufferMax,bufF);
118 _readPos = 0;
119 }
120 _pos++;
121 return(_buffer[_readPos++]);
122 }