File: fstream.cpp

package info (click to toggle)
aspell 0.60.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 10,000 kB
  • ctags: 4,862
  • sloc: sh: 48,145; cpp: 22,153; perl: 1,546; ansic: 1,535; makefile: 684; sed: 16
file content (149 lines) | stat: -rw-r--r-- 2,714 bytes parent folder | download | duplicates (8)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// This file is part of The New Aspell
// Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license
// version 2.0 or 2.1.  You should have received a copy of the LGPL
// license along with this library if you did not you can find
// it at http://www.gnu.org/.

#include <stdio.h>
#include <assert.h>

#include "iostream.hpp"

#include "asc_ctype.hpp"
#include "string.hpp"
#include "fstream.hpp"
#include "errors.hpp"

namespace acommon {

  PosibErr<void> FStream::open(ParmStr name, const char * mode)
  {
    assert (file_ == 0);
    file_ = fopen(name,mode);
    if (file_ == 0) {
      if (strpbrk(mode, "wa+") != 0)
	return make_err(cant_write_file, name);
      else
	return make_err(cant_read_file, name);
    } else {
      return no_err;
    }
  }

  void FStream::close()
  {
    if (file_ != 0 && own_)
      fclose(file_);
    file_ = 0;
  }

  int FStream::file_no() 
  {
    return fileno(file_);
  }

  FILE * FStream::c_stream() 
  {
    return file_;
  }

  void FStream::restart()
  {
    flush();
    fseek(file_,0,SEEK_SET);
  }

  void FStream::skipws() 
  {
    int c;
    while (c = getc(file_), c != EOF && asc_isspace(c));
    ungetc(c, file_);
  }

  FStream & FStream::operator>> (String & str)
  {
    skipws();
    int c;
    str = "";
    while (c = getc(file_), c != EOF && !asc_isspace(c))
      str += static_cast<char>(c);
    ungetc(c, file_);
    return *this;
  }

  FStream & FStream::operator<< (ParmStr str)
  {
    fputs(str, file_);
    return *this;
  }

  bool FStream::append_line(String & str, char d)
  {
    int c;
    c = getc(file_);
    if (c == EOF) return false;
    if (c == (int)d) return true;
    str.append(c);
    while (c = getc(file_), c != EOF && c != (int)d) 
      str.append(c);
    return true;
  }

  bool FStream::read(void * str, unsigned int n)
  {
    fread(str,1,n,file_);
    return operator bool();
  }

  void FStream::write(char c)
  {
    putc(c, file_);
  }

  void FStream::write(ParmStr str) 
  {
    fputs(str, file_);
  }

  void FStream::write(const void * str, unsigned int n)
  {
    fwrite(str,1,n,file_);
  }

  FStream & FStream::operator>> (unsigned int & num)
  {
    int r = fscanf(file_, " %u", &num);
    if (r != 1)
      close();
    return *this;
  }


  FStream & FStream::operator<< (unsigned int num)
  {
    fprintf(file_, "%u", num);
    return *this;
  }

  FStream & FStream::operator>> (int & num)
  {
    int r = fscanf(file_, " %i", &num);
    if (r != 1)
      close();
    return *this;
  }


  FStream & FStream::operator<< (int num)
  {
    fprintf(file_, "%i", num);
    return *this;
  }

  FStream & FStream::operator<< (double num)
  {
    fprintf(file_, "%g", num);
    return *this;
  }

}