File: file.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (206 lines) | stat: -rw-r--r-- 4,687 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 1991 Stanford University
 * Copyright (c) 1991 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Stanford and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Stanford and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */

#include <OS/file.h>
#include <OS/string.h>
#include <OS/types.h>
#include <assert.h>
#include <fcntl.h>
#include <cstdio>
#include <sys/stat.h>

#ifdef sgi
#include <sys/mman.h>
#endif

#ifdef __alpha
extern "C" {
#include <sys/mman.h>
}
#endif

/* no standard place for these */
extern "C" {
#ifdef __P
    extern int close __P ((int));
#else
    extern int close (int);
#endif
#if defined(sgi)
    extern int read(int, void*, unsigned int);
#endif
#if defined(sun) && !defined(__SYSENT_H)
#if !defined(__svr4__)
    extern int read(int, void*, unsigned int);
#endif
#endif
#if defined(AIXV3)
    extern int read(int, char*, unsigned int);
#endif
#if defined(apollo)
    extern long read(int, void*, unsigned int);
#endif
#if defined(__DECCXX)
    extern int read(int, void*, unsigned int);
#endif
}

#if defined(hpux) || defined(__GNUC__)
#include <unistd.h>
#endif

class FileInfo {
public:
    CopyString* name_;
    int fd_;
    char* map_;
    struct stat info_;
    off_t pos_;
    char* buf_;
    unsigned int limit_;

    FileInfo(CopyString*, int fd);
};

FileInfo::FileInfo(CopyString* s, int fd) {
    name_ = s;
    fd_ = fd;
    pos_ = 0;
    limit_ = 0;
    map_ = nil;
    buf_ = nil;
}

File::File(FileInfo* i) {
#if !defined(hpux)
    assert(i != nil);
#endif
    rep_ = i;
}

File::~File() {
    close();
    delete rep_->name_;
    delete rep_;
}

const String* File::name() const {
    return rep_->name_;
}

long File::length() const {
    return rep_->info_.st_size;
}

void File::close() {
    FileInfo* i = rep_;
    if (i->fd_ >= 0) {
	if (i->map_ != nil) {
#if defined(sgi) || defined(__alpha)
	    munmap(i->map_, int(i->info_.st_size));
#endif
	}
	if (i->buf_ != nil) {
	    delete[] i->buf_;
	}
	::close(i->fd_);
	i->fd_ = -1;
    }
}

void File::limit(unsigned int buffersize) {
    rep_->limit_ = buffersize;
}

FileInfo* File::rep() const { return rep_; }

/* class InputFile */

InputFile::InputFile(FileInfo* i) : File(i) { }
InputFile::~InputFile() { }

InputFile* InputFile::open(const String& name) {
    CopyString* s = new CopyString(name);
    /* cast to workaround DEC C++ prototype bug */
    int fd = ::open((char*)s->string(), O_RDONLY);
    if (fd < 0) {
	delete s;
	return nil;
    }
    FileInfo* i = new FileInfo(s, fd);
    if (fstat(fd, &i->info_) < 0) {
	delete s;
	delete i;
	return nil;
    }
    return new InputFile(i);
}

int InputFile::read(const char*& start) {
    FileInfo* i = rep();
    int len = (int)(i->info_.st_size);
    if (i->pos_ >= len) {
	return 0;
    }
    if (i->limit_ != 0 && len > i->limit_) {
	len = (int)(i->limit_);
    }
#if defined(sgi) || defined(__alpha)
    i->map_ = (char*)mmap(0, len, PROT_READ, MAP_PRIVATE, i->fd_, i->pos_);
    if ((unsigned long)(i->map_) == (unsigned long)-1) {
	return -1;
    }
    start = i->map_;
#else
    if (i->buf_ == nil) {
	i->buf_ = new char[len];
    }
    start = i->buf_;
    len = ::read(i->fd_, i->buf_, len);
#endif
    i->pos_ += len;
    return len;
}

/* class StdInput */

StdInput::StdInput() : InputFile(new FileInfo(new CopyString("-stdin"), 0)) { }
StdInput::~StdInput() { }

long StdInput::length() const { return -1; }

int StdInput::read(const char*& start) {
    FileInfo* i = rep();
    if (i->buf_ == nil) {
	if (i->limit_ == 0) {
	    i->limit_ = BUFSIZ;
	}
	i->buf_ = new char[i->limit_];
    }
    int nbytes = ::read(i->fd_, (char*)i->buf_, i->limit_);
    if (nbytes > 0) {
	start = (const char*)(i->buf_);
    }
    return nbytes;
}