File: FileReader.cpp

package info (click to toggle)
quickplot 0.8.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,544 kB
  • ctags: 1,019
  • sloc: cpp: 10,051; sh: 7,597; makefile: 176
file content (129 lines) | stat: -rw-r--r-- 2,324 bytes parent folder | download | duplicates (4)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */
#include "config.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>

#include <gtkmm.h>

#include "value_t.h"
#include "FileReader.h"
#include "errorStr.h"
#include "Globel.h"

FileReader::FileReader(const char *filename_in, FILE *file_in)
{
  isFree = 0;
  openedFile = 0;
  file = NULL;
  buffer = NULL;
  bufferSize = 0;
  in = 0;
  out = 0;
  error = 0;
  // user end of file
  endOfFile = 0;
  // if end of file is set
  _endOfFile = 0;

  if(!file_in)
    {
      file = fopen(filename_in, "r");
      if(!file)
	{
	  snprintf(errorStr, ERRORSTR_LENGTH,
		   "quickplot ERROR: Failed to open file '%s' for reading: "
		   "system error number %d: %s.",
		   filename_in, errno, strerror(errno));
          if(!opSilent)
            opSpew << errorStr << std::endl;
	  error = 1;
	  endOfFile = 1;
	  return;
	}
      openedFile = 1;
    }
  else
    file = file_in;

}

FileReader::~FileReader(void)
{
  if(buffer)
    {
      free(buffer);
      buffer = NULL;
      bufferSize = 0;
    }
  if(file && openedFile)
    {
      fclose(file);
      file = NULL;
      openedFile = 0;
    }
}


void FileReader::rewind(void)
{
  out = 0;
  endOfFile = 0;
}

void FileReader::freeRewind(void)
{
  isFree = 1;
}

int FileReader::readChar(void)
{
  if(isFree && in == out)
    {
      if(_endOfFile) return EOF;
      
      // no longer buffered.
      int i = fgetc(file);
      if(i == EOF)
	_endOfFile = endOfFile = 1;
      //printf("%c", (char) i);
      return i;
    }
  // enough data in the buffer.
  else if(in > out)
    {
      //printf("%c", (char) buffer[out]);
      return ((int) buffer[out++]);
    }
  else if(_endOfFile)
  {
    return EOF;
  }
  else // If need more data in the buffer.
    {
      // is the buffer big enough
      if(in + 1 > bufferSize)
        buffer = (unsigned char *)
          realloc(buffer, sizeof(unsigned char)*(bufferSize += CHUNK));
      
      int i = fgetc(file);
        
      if(i == EOF)
      {
	_endOfFile = endOfFile = 1;
        return EOF;
      }
      else
      {
        buffer[in++] = ((unsigned char) i);
        out = in;
        //printf("%c", (char) buffer[in-1]);
        return i;
      }
    }
}