source: trunk/GDE/MAFFT/mafft-7.055-with-extensions/extensions/mxscarna_src/probconsRNA/FileBuffer.h

Last change on this file was 10371, checked in by aboeckma, 11 years ago

updated mafft version. Added extensions (no svn ignore, yet)

File size: 2.6 KB
Line 
1/////////////////////////////////////////////////////////////////
2// FileBuffer.h
3//
4// Buffered file reading.
5/////////////////////////////////////////////////////////////////
6
7
8#ifndef FILEBUFFER_H
9#define FILEBUFFER_H
10
11#include <string>
12#include <fstream>
13#include <iostream>
14
15using namespace std;
16
17const int BufferSize = 1000;
18
19/////////////////////////////////////////////////////////////////
20// FileBuffer
21//
22// Class for buffering file reading.
23/////////////////////////////////////////////////////////////////
24namespace MXSCARNA {
25class FileBuffer {
26  ifstream file;
27  char buffer[BufferSize];
28  int currPos;
29  int size;
30  bool isEOF;
31  bool isValid;
32  bool canUnget;
33
34 public:
35
36  // Some common routines
37
38  FileBuffer (const char *filename) : file (filename), currPos (0), size (0), isEOF (false), isValid (!file.fail()), canUnget (false){}
39  ~FileBuffer (){ close(); }
40  bool fail () const { return !isValid; }
41  bool eof () const { return (!isValid || isEOF); }
42  void close(){ file.close(); isValid = false; }
43
44  /////////////////////////////////////////////////////////////////
45  // FileBuffer::Get()
46  //
47  // Retrieve a character from the file buffer.  Returns true if
48  // and only if a character is read.
49  /////////////////////////////////////////////////////////////////
50
51  bool Get (char &ch){
52
53    // check to make sure that there's more stuff in the file
54    if (!isValid || isEOF) return false;
55
56    // if the buffer is empty, it's time to reload it
57    if (currPos == size){
58      file.read (buffer, BufferSize);
59      size = file.gcount();
60      isEOF = (size == 0);
61      currPos = 0;
62      if (isEOF) return false;
63    }
64
65    // store the read character
66    ch = buffer[currPos++];
67    canUnget = true;
68    return true;
69  }
70
71  /////////////////////////////////////////////////////////////////
72  // FileBuffer::UnGet()
73  //
74  // Unretrieve the most recently read character from the file
75  // buffer.  Note that this allows only a one-level undo.
76  /////////////////////////////////////////////////////////////////
77
78  void UnGet (){
79    assert (canUnget);
80    assert (isValid);
81    assert (currPos > 0);
82    currPos--;
83    assert (currPos < size);
84    isEOF = false;
85    canUnget = false;
86  }
87
88  /////////////////////////////////////////////////////////////////
89  // FileBuffer::GetLine()
90  //
91  // Retrieve characters of text until a newline character is
92  // encountered.  Terminates properly on end-of-file condition.
93  /////////////////////////////////////////////////////////////////
94
95  void GetLine (string &s){
96    char ch;
97    s = "";
98    while (Get (ch) && ch != '\n')
99      s += ch;
100  }
101
102};
103}
104#endif
Note: See TracBrowser for help on using the repository browser.