Coverage Report

Created: 2025-07-11 06:34

/src/aspell/common/fstream.cpp
Line
Count
Source (jump to first uncovered line)
1
// This file is part of The New Aspell
2
// Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license
3
// version 2.0 or 2.1.  You should have received a copy of the LGPL
4
// license along with this library if you did not you can find
5
// it at http://www.gnu.org/.
6
7
#include <stdio.h>
8
#include <assert.h>
9
10
#include "iostream.hpp"
11
12
#include "asc_ctype.hpp"
13
#include "string.hpp"
14
#include "fstream.hpp"
15
#include "errors.hpp"
16
17
namespace acommon {
18
19
  PosibErr<void> FStream::open(ParmStr name, const char * mode)
20
19.9k
  {
21
19.9k
    assert (file_ == 0);
22
19.9k
    file_ = fopen(name,mode);
23
19.9k
    if (file_ == 0) {
24
4.34k
      if (strpbrk(mode, "wa+") != 0)
25
0
  return make_err(cant_write_file, name);
26
4.34k
      else
27
4.34k
  return make_err(cant_read_file, name);
28
15.5k
    } else {
29
15.5k
      return no_err;
30
15.5k
    }
31
19.9k
  }
32
33
  void FStream::close()
34
21.9k
  {
35
21.9k
    if (file_ != 0 && own_)
36
15.5k
      fclose(file_);
37
21.9k
    file_ = 0;
38
21.9k
  }
39
40
  int FStream::file_no() 
41
2.04k
  {
42
2.04k
    return fileno(file_);
43
2.04k
  }
44
45
  FILE * FStream::c_stream() 
46
0
  {
47
0
    return file_;
48
0
  }
49
50
  void FStream::restart()
51
1.00k
  {
52
1.00k
    flush();
53
1.00k
    fseek(file_,0,SEEK_SET);
54
1.00k
  }
55
56
  void FStream::skipws() 
57
0
  {
58
0
    int c;
59
0
    while (c = getc(file_), c != EOF && asc_isspace(c));
60
0
    ungetc(c, file_);
61
0
  }
62
63
  FStream & FStream::operator>> (String & str)
64
0
  {
65
0
    skipws();
66
0
    int c;
67
0
    str = "";
68
0
    while (c = getc(file_), c != EOF && !asc_isspace(c))
69
0
      str += static_cast<char>(c);
70
0
    ungetc(c, file_);
71
0
    return *this;
72
0
  }
73
74
  FStream & FStream::operator<< (ParmStr str)
75
0
  {
76
0
    fputs(str, file_);
77
0
    return *this;
78
0
  }
79
80
  bool FStream::append_line(String & str, char d)
81
3.76M
  {
82
3.76M
    int c;
83
3.76M
    c = getc(file_);
84
3.76M
    if (c == EOF) return false;
85
3.76M
    if (c == (int)d) return true;
86
3.71M
    str.append(c);
87
89.2M
    while (c = getc(file_), c != EOF && c != (int)d) 
88
85.4M
      str.append(c);
89
3.71M
    return true;
90
3.76M
  }
91
92
  bool FStream::read(void * str, unsigned int n)
93
12.2k
  {
94
12.2k
    fread(str,1,n,file_);
95
12.2k
    return operator bool();
96
12.2k
  }
97
98
  void FStream::write(char c)
99
0
  {
100
0
    putc(c, file_);
101
0
  }
102
103
  void FStream::write(ParmStr str) 
104
0
  {
105
0
    fputs(str, file_);
106
0
  }
107
108
  void FStream::write(const void * str, unsigned int n)
109
0
  {
110
0
    fwrite(str,1,n,file_);
111
0
  }
112
113
  FStream & FStream::operator>> (unsigned int & num)
114
0
  {
115
0
    int r = fscanf(file_, " %u", &num);
116
0
    if (r != 1)
117
0
      close();
118
0
    return *this;
119
0
  }
120
121
122
  FStream & FStream::operator<< (unsigned long num)
123
0
  {
124
0
    fprintf(file_, "%lu", num);
125
0
    return *this;
126
0
  }
127
128
  FStream & FStream::operator<< (unsigned int num)
129
0
  {
130
0
    fprintf(file_, "%u", num);
131
0
    return *this;
132
0
  }
133
134
  FStream & FStream::operator>> (int & num)
135
0
  {
136
0
    int r = fscanf(file_, " %i", &num);
137
0
    if (r != 1)
138
0
      close();
139
0
    return *this;
140
0
  }
141
142
143
  FStream & FStream::operator<< (int num)
144
0
  {
145
0
    fprintf(file_, "%i", num);
146
0
    return *this;
147
0
  }
148
149
  FStream & FStream::operator<< (double num)
150
0
  {
151
0
    fprintf(file_, "%g", num);
152
0
    return *this;
153
0
  }
154
155
}