File: File.cc

package info (click to toggle)
hatman 0.5.0.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,416 kB
  • ctags: 1,971
  • sloc: cpp: 7,871; sh: 330; yacc: 322; ansic: 260; lex: 210; makefile: 104; awk: 80
file content (206 lines) | stat: -rw-r--r-- 5,260 bytes parent folder | download | duplicates (2)
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
/*C* $Id: file.cc,v 1.4 1997/09/26 17:57:57 james Exp $
 *
 * Hatman - The Game of Kings
 * Copyright (C) 1997 James Pharaoh & Timothy Fisken
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *C*/

#include "File.h"
#include "debug.h"
#include "util.h"
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>

//--------------------------------------------------------------------------------------------------------------------------------

File::File()
{
 fp = NULL;
}

File::File(const char* filename, const char* mode)
{
 assert(filename != NULL); assert(mode != NULL);
 fp = NULL;
 open(filename, mode);
}

File::File(FILE* _fp)
{
 assert(_fp != NULL);
 fp = _fp;
 fpWantsClosing = true;
}

#ifndef NDEBUG
File::File(File& object)
 : Object(object)
{
 fatal("You did a File = File, didn't you? Well I can't allow it.\nIt's too silly, I am afraid.\n");
}
#endif

File::~File()
{
 if(fpWantsClosing) close();
}

FILE* File::open(const char* filename, const char* mode)
{
 assert(this != NULL); assert(filename != NULL); assert(mode != NULL);
 close();
 VPRINTF("<file> opening \"%s\" in mode \"%s\"\n", filename, mode);
 fp = fopen(filename, mode);
 if(!fp) setError("%s", errnoStr());
 return fp;
}

int File::close()
{
 assert(this != NULL);
 if(!fp) return 0;
 int ret = fclose(fp);
 fp = NULL;
 return ret;
}

int File::scanf(const char* format, ...)
{
 assert(this != NULL); assert(format != NULL);
 va_list ap;
 va_start(ap, format);
 int ret = vfscanf(fp, format, ap);
 va_end(ap);
 return ret;
}

bool File::read(void* data, size_t size)
{
 if(fread(data, size, 1, fp) != 1)
  {
   if(errno) setError("%s", errnoStr());
   else setError("end of file");
   return false;
  }
 return true;
}

bool File::write(const void* data, size_t size)
{
 if(fwrite(data, size, 1, fp) != 1)
  {
   setError("%s", errnoStr());
   return false;
  }
 return true;
}

//--------------------------------------------------------------------------------------------------------------------------------

File fStdOut(stdout);
File fStdIn(stdin);
File fStdErr(stderr);

//--------------------------------------------------------------------------------------------------------------------------------

Pipe::Pipe()
 : File()
{
}

Pipe::Pipe(const char* filename, const char* mode)
{
 assert(filename != NULL); assert(mode != NULL);
 fp = NULL;
 open(filename, mode);
}

FILE* Pipe::open(const char* filename, const char* mode)
{
 assert(this != NULL); assert(filename != NULL); assert(mode != NULL);
 close();
 VPRINTF("<file> opening \"%s\" as pipe in mode \"%s\"\n", filename, mode);
 fp = popen(filename, mode);
 if(!fp) setError(errnoStr());
 return fp;
}

int Pipe::close()
{
 assert(this != NULL);
 if(!fp) return 0;
 int ret = pclose(fp);
 fp = NULL;
 return ret;
}

//--------------------------------------------------------------------------------------------------------------------------------

GzipPipe::GzipPipe(const char* filename, const char* mode)
{
 assert(filename != NULL); assert(mode != NULL);
 fp = NULL;
 open(filename, mode);
}

FILE* GzipPipe::open(const char* filename, const char* mode)
{
 assert(this != NULL); assert(filename != NULL); assert(mode != NULL);
 if(!strcmp(mode, "r"))
  {
   char* s;
   asprintf(&s, "gzip -dc %s", filename);
   Pipe::open(s, "r"); // assigns fp
   free(s);
   return fp;
  }
 else if(!strcmp(mode, "w"))
  {
   char* s;
   asprintf(&s, "/bin/sh -c \"gzip - > %s\"", filename);
   Pipe::open(s, "w"); // assigns fp
   free(s);
   return fp;
  }

 setError("unknown file mode: \"%s\"", mode);
 return NULL;
}

//--------------------------------------------------------------------------------------------------------------------------------

bool save(const char* filename, Object* object, bool (Object::*write)(File& f))
{
 File* f = gzFilename(filename)? new GzipPipe(filename, "w") : new File(filename, "w");
 if(!f->ready()) { setError("loading %s: %s", filename, errStr); return false; }
 if(!(object->*write)(*f)) { delete f; setError("saving %s: %s", filename, errStr); return false; }
 delete f;
 return true;
}

Object* load(const char* filename, Object* create(File& f))
{
 Object* o = NULL;
 File* f = gzFilename(filename)? new GzipPipe(filename, "r") : new File(filename, "r");
 if(!f->ready()) { setError("loading %s: %s", filename, errStr); return NULL; }
 if((o = create(*f)) == NULL) { delete f; setError("loading %s: %s", filename, errStr); return NULL; }
 return o;
}

//--------------------------------------------------------------------------------------------------------------------------------