File: diskfile.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (353 lines) | stat: -rw-r--r-- 7,324 bytes parent folder | download
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// diskfile.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include "localdefs.h"
#include <std.h>
#include <stdarg.h>
#include <sys/file.h>           // needed to determine values of O_RDONLY...
#include <sys/stat.h>
#include "diskfile.h"

#if defined(NeXT)
#include <errno.h>
extern "C" {
extern int  unlink(const char *);
}
#elif defined(sgi)
#ifdef __GNUG__
#include <sys/errno.h>
#else
#include <errno.h>
#endif
#endif

#if defined(linux)
/*extern "C" void setbuffer(FILE*, char*, unsigned long);*/
#else
#if !defined( sgi ) && !defined( sun )
extern "C" int setbuffer(FILE*, char*, int);
#endif
#endif

#ifndef _IOAPPEND
#define _IOAPPEND	00	/* not used in this version of stdio -- DAS */
#endif
#ifndef _IOREAD
#define _IOREAD 01
#endif
#ifndef _IOWRT
#define _IOWRT 02
#endif
#ifndef _IORW
#define	_IORW 0400
#endif

// error handlers

void verbose_File_error_handler(const char* msg)
{
  perror(msg);
  errno = 0;
}

void quiet_File_error_handler(const char*)
{
  errno = 0;
}

void fatal_File_error_handler(const char* msg)
{
  perror(msg);
  exit(1);
}

one_arg_error_handler_t File_error_handler = verbose_File_error_handler;


one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t f)
{
  one_arg_error_handler_t old = File_error_handler;
  File_error_handler = f;
  return old;
}


/*

 Opening files. 

 open(filename, io_mode, access_mode) is done via system open 
 command since fopen doesn't handle all of the cases possible 
 with sys open. After a successful open, fdopen is called to 
 attach an _iobuf to the file descriptor.

 All this requires a few decoding routines that can translate among our
 enumerated types, system flags, and fopen modes.

*/


enum sys_open_cmd_io_mode  // These should be correct for most systems
{                        
  sio_read      = O_RDONLY,
  sio_write     = O_WRONLY,
  sio_readwrite = O_RDWR,
  sio_append    = O_APPEND
};

enum sys_open_cmd_access_mode
{
  sa_create     = O_CREAT,
  sa_truncate   = O_TRUNC,
  sa_createonly = O_EXCL | O_CREAT
};

  
static int open_cmd_arg(io_mode i, access_mode a) // decode modes
{
  int arg;
  switch(i)
  {
  case io_readonly:   arg = sio_read;                   break;
  case io_writeonly:  arg = sio_write;                  break;
  case io_readwrite:  arg = sio_readwrite;              break;
  case io_appendonly: arg = sio_append | sio_write;     break;
  case io_append:     arg = sio_append | sio_readwrite; break;
  default:            return -1;
  };
  switch(a)
  {
  case a_createonly:  return arg | sa_createonly;
  case a_create:      return arg | sa_create | sa_truncate;
  case a_useonly:     return arg;
  case a_use:         return arg | sa_create;
  default:            return -1;
  }
}

char *
DiskFile::fopen_cmd_arg(io_mode i) {
  switch(i) {
  case io_readonly:  return "r";
  case io_writeonly: return "w";
  case io_readwrite: return "r+";
  case io_appendonly:return "a";
  case io_append:    return "a+";
  default:           return 0;
  }
}

DiskFile::DiskFile() { 
	initialize(); 
}

DiskFile::~DiskFile() {
	delete(nm);
	close();
}

void DiskFile::initialize() 
{ 
  fp = 0; nm = 0; stat = 0; state = _bad; rw = 0;
}

// reset class vars after open

void DiskFile::reinitialize(const char* filename)
{
  if (filename != 0)     setname(filename);
  else setname(0);

  if (fp != 0)
  {
    state = _good;
#if defined(linux)
    rw = 01|02;
    if (fp->_flags & _IO_NO_READS)
      rw &= ~01;
    if (fp->_flags & _IO_NO_WRITES)
      rw &= ~02;
#else
    if (fp->_flag & (_IOREAD|_IORW))
      rw |= 01;
    if (fp->_flag & (_IOWRT|_IORW|_IOAPPEND))
      rw |= 02;
#endif
    check_state();
  }
  else
  {
    set(_fail); set(_bad);
    error();
  }
}

void DiskFile::check_state() // ensure fp & state agree about eof
{
  if (fp != 0)
  {
    if (feof(fp))
      set(_eof);
    else
      unset(_eof);
    if (ferror(fp))
      set(_bad);
  }
}

DiskFile& DiskFile::open(const char* filename, io_mode mode, access_mode a) {                                   
	close();
	int open_arg = open_cmd_arg(mode, a);
	if (open_arg != -1) {
		int fd = ::open(filename, open_arg, 0666);
		if (fd >= 0)
			fp = fdopen(fd, fopen_cmd_arg(mode));
	}
	reinitialize(filename);
	return *this;
}

DiskFile&
DiskFile::open(const char* filename, const char* m) {
	close();
	fp = fopen(filename, m);
	reinitialize(filename);
	return *this;
}

DiskFile&
DiskFile::reOpen(const char* mode) {
	return open(DiskFile::name(), mode);	// force use of "real" filename
}

DiskFile&
DiskFile::close() {
	if (fp != 0)
		fclose(fp);
	fp = 0;
	rw = 0;
	set(_bad);
	return *this;
}

DiskFile&
DiskFile::remove() {
	close();
	return failif (nm == 0 || unlink(nm) != 0);
}

DiskFile&
DiskFile::rename(const char* newname) {
	if(::rename(name(), newname) >= 0)
		setname(newname);
	else fail();
	return *this;
}

void DiskFile::setname(const char* newname) {
	if (nm != newname) {
		delete [] nm;
		nm = newstr(newname);
	}
}

DiskFile& DiskFile::setbuf(int buffer_kind) {                  
  if (!is_open())
  {
    set(_fail);
    return *this;
  }
  switch(buffer_kind)
  {
  case _IOFBF:       
#ifdef HAVE_SETVBUF
    setvbuf(fp, 0, _IOFBF, 0);
#endif
    break;           
  case _IONBF:       
    ::setbuf(fp, 0); 
    break;
  case _IOLBF:
#ifdef HAVE_SETLINEBUF
    setlinebuf(fp);
#else
#ifdef HAVE_SETVBUF
    setvbuf(fp, 0, _IOLBF, 0);
#endif
#endif    
    break;
  default:
    break;
  }
  return *this;
}

DiskFile& DiskFile::setbuf(int size, char* buf) {
  if (!is_open())
  {
    set(_fail);
    return *this;
  }
#ifdef HAVE_SETVBUF
  setvbuf(fp, buf, _IOFBF, size);
#else
  setbuffer(fp, buf, size);
#endif
  return *this;
}

// static function

int
DiskFile::exists(const char* filename) {
	int doesExist = true;
	static struct stat s;
	if(::stat(filename, &s) < 0) {
		switch(errno) {
		case ENOENT:
			doesExist = false;
			break;
		default:
			doesExist = false;	// assume false for now until better way found
			break;
		}
	}
	return doesExist;
}

void DiskFile::error() {
  check_state();
  set(_fail);
  if (errno != 0) {
    char error_string[400];
    sprintf(error_string, "File error: %s:", (nm != 0) ? nm : "");
    (*File_error_handler)(error_string);
  }
}