File: FileBuffer.h

package info (click to toggle)
probalign 1.4-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 368 kB
  • sloc: cpp: 4,964; makefile: 27; sh: 12
file content (104 lines) | stat: -rwxr-xr-x 2,614 bytes parent folder | download | duplicates (27)
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
/////////////////////////////////////////////////////////////////
// FileBuffer.h
//
// Buffered file reading.
/////////////////////////////////////////////////////////////////


#ifndef FILEBUFFER_H
#define FILEBUFFER_H

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

const int BufferSize = 1000;

/////////////////////////////////////////////////////////////////
// FileBuffer
//
// Class for buffering file reading.
/////////////////////////////////////////////////////////////////

class FileBuffer {
  ifstream file;
  char buffer[BufferSize];
  int currPos;
  int size;
  bool isEOF;
  bool isValid;
  bool canUnget;

 public:

  // Some common routines

  FileBuffer (const char *filename) : file (filename), currPos (0), size (0), isEOF (false), isValid (!file.fail()), canUnget (false){}
  ~FileBuffer (){ close(); }
  bool fail () const { return !isValid; }
  bool eof () const { return (!isValid || isEOF); }
  void close(){ file.close(); isValid = false; }

  /////////////////////////////////////////////////////////////////
  // FileBuffer::Get()
  //
  // Retrieve a character from the file buffer.  Returns true if
  // and only if a character is read.
  /////////////////////////////////////////////////////////////////

  bool Get (char &ch){

    // check to make sure that there's more stuff in the file
    if (!isValid || isEOF) return false;

    // if the buffer is empty, it's time to reload it
    if (currPos == size){
      file.read (buffer, BufferSize);
      size = file.gcount();
      isEOF = (size == 0);
      currPos = 0;
      if (isEOF) return false;
    }

    // store the read character
    ch = buffer[currPos++];
    canUnget = true;
    return true;
  }

  /////////////////////////////////////////////////////////////////
  // FileBuffer::UnGet()
  //
  // Unretrieve the most recently read character from the file
  // buffer.  Note that this allows only a one-level undo.
  /////////////////////////////////////////////////////////////////

  void UnGet (){
    assert (canUnget);
    assert (isValid);
    assert (currPos > 0);
    currPos--;
    assert (currPos < size);
    isEOF = false;
    canUnget = false;
  }

  /////////////////////////////////////////////////////////////////
  // FileBuffer::GetLine()
  //
  // Retrieve characters of text until a newline character is
  // encountered.  Terminates properly on end-of-file condition.
  /////////////////////////////////////////////////////////////////

  void GetLine (string &s){
    char ch;
    s = "";
    while (Get (ch) && ch != '\n')
      s += ch;
  }

};

#endif