File: Storage.C

package info (click to toggle)
waili 19990723-22.3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,568 kB
  • sloc: cpp: 9,964; ansic: 273; makefile: 208; sh: 74
file content (261 lines) | stat: -rw-r--r-- 5,684 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
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
//
//	Data Storage Class
//
//  $Id: Storage.C,v 4.0.2.2.2.1 1999/07/20 16:15:52 geert Exp $
//
//  Copyright (C) 1996-1999 Department of Computer Science, K.U.Leuven, Belgium
//
//  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
//

#include <stdarg.h>
#include <errno.h>

#ifdef __WIN32__
#include "winsock.h"
#else
#include <netinet/in.h>
#endif

#include <sys/param.h>


#include "waili/Storage.h"
#include "waili/Util.h"

const char *Stream::rcsid = "$Id: Storage.C,v 4.0.2.2.2.1 1999/07/20 16:15:52 geert Exp $";


    //  Create a Stream

Stream::Stream(const char *name, const char *mode)
    : File(NULL)
{
    Open(name, mode);
}


    //  Delete a Stream

Stream::~Stream()
{
    if (File)
	Close();
}


    //  Open a Stream

void Stream::Open(const char *name, const char *mode)
{
    if (File)
	Die("%s: Stream `%s' already opened\n", __FUNCTION__, Name);

    u_int nlen = strlen(name);

    if ((nlen > 3) && !strncmp(&name[nlen-3], ".gz", 3)) {
	Compressed = 1;
	char *cmd;
	char *pmode;
	if (!strcmp(mode, "r")) {
	    cmd = new char[nlen+6];
	    pmode = "r";
	    sprintf(cmd, "zcat %s", name);
	} else if (!strcmp(mode, "w")) {
	    cmd = new char[nlen+8];
	    pmode = "w";
	    sprintf(cmd, "gzip > %s", name);
	} else if (!strcmp(mode, "a")) {
	    cmd = new char[nlen+8];
	    pmode = "w";
	    sprintf(cmd, "gzip >> %s", name);
	} else
	    Die("%s: Invalid mode `%s' for a compressed stream\n",
		__FUNCTION__, mode);
	if (!(File = popen(cmd, pmode)))
	    Die("%s: popen() failed for `%s': %s\n", __FUNCTION__, name,
		strerror(errno));
	delete[] cmd;
    } else {
	Compressed = 0;
	if (!(File = fopen(name, mode)))
	    Die("%s: fopen() failed for `%s': %s\n", __FUNCTION__, name,
		strerror(errno));
    }
    Name = new char[nlen+1];
    strcpy(Name, name);
    Mode = new char[strlen(mode)+1];
    strcpy(Mode, mode);
}


    //  Close a Stream

void Stream::Close(void)
{
    if (!File)
	Die("%s: Stream not opened yet\n", __FUNCTION__);
    if (Compressed)
	pclose(File);
    else
	fclose(File);
    File = NULL;
    delete[] Name;
    delete[] Mode;
}


    //  Read Raw Data from a Stream

void Stream::RawRead(void *data, int size)
{
    if (fread((char *)data, size, 1, File) != 1)
	Die("%s: fread() on `%s' failed: %s\n", __FUNCTION__, Name,
	    strerror(errno));
}


    //  Write Raw Data to a Stream

void Stream::RawWrite(const void *data, int size)
{
    if (fwrite((char *)data, size, 1, File) != 1)
	Die("%s: fwrite() on `%s' failed: %s\n", __FUNCTION__, Name,
	    strerror(errno));
}


    //  Format and Write a String to a Stream

void Stream::Printf(const char *fmt, ...)
{
    va_list args;

    va_start(args, fmt);
    vfprintf(File, fmt, args);
    va_end(args);
}


    //  Endianness-aware Routines




#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN)
    #if BYTE_ORDER == BIG_ENDIAN
	#define WAILI_BIG_ENDIAN
    #elif BYTE_ORDER == LITTLE_ENDIAN
	#define WAILI_LITTLE_ENDIAN
    #endif
#elif defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)
    #define WAILI_BIG_ENDIAN
#elif defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN)
    #define WAILI_LITTLE_ENDIAN

#endif
#if !defined(WAILI_BIG_ENDIAN) && !defined(WAILI_LITTLE_ENDIAN)
#error Please fix me
#endif

static inline u64 ntohll(u64 x)
{
#ifdef WAILI_BIG_ENDIAN
    return x;
#else
    u64 hi = ntohl(x & 0xffffffff);
    u64 lo = ntohl(x >> 32);
    return hi<<32 | lo;
#endif
}

static inline u64 htonll(u64 x)
{
#ifdef WAILI_BIG_ENDIAN
    return x;
#else
    u64 hi = htonl(x & 0xffffffff);
    u64 lo = htonl(x >> 32);
    return hi<<32 | lo;
#endif
}


void Stream::Read(u16 *x, u_int cnt)
{
    RawRead(x, cnt*sizeof(*x));
    for (u_int i = 0; i < cnt; i++)
	x[i] = ntohs(x[i]);
}

void Stream::Read(u32 *x, u_int cnt)
{
    RawRead(x, cnt*sizeof(*x));
    for (u_int i = 0; i < cnt; i++)
	x[i] = ntohl(x[i]);
}

void Stream::Read(u64 *x, u_int cnt)
{
    RawRead(x, cnt*sizeof(*x));
    for (u_int i = 0; i < cnt; i++)
	x[i] = ntohll(x[i]);
}

#define TMP_CONVERT_LEN_S	512
#define TMP_CONVERT_LEN_L	256
#define TMP_CONVERT_LEN_LL	128

static union {
    u16 s[TMP_CONVERT_LEN_S];
    u32 l[TMP_CONVERT_LEN_L];
    u64 ll[TMP_CONVERT_LEN_LL];
} tmp_convert;

void Stream::Write(const u16 *x, u_int cnt)
{
    while (cnt > 0) {
	u_int cnt2 = Min(cnt, (u_int)TMP_CONVERT_LEN_S);
	for (u_int i = 0; i < cnt2; i++)
	    tmp_convert.s[i] = htons(x[i]);
	RawWrite(tmp_convert.s, cnt2*sizeof(*x));
	x += cnt2;
	cnt -= cnt2;
    }
}

void Stream::Write(const u32 *x, u_int cnt)
{
    while (cnt > 0) {
	u_int cnt2 = Min(cnt, (u_int)TMP_CONVERT_LEN_L);
	for (u_int i = 0; i < cnt2; i++)
	    tmp_convert.l[i] = htonl(x[i]);
	RawWrite(tmp_convert.l, cnt2*sizeof(*x));
	x += cnt2;
	cnt -= cnt2;
    }
}

void Stream::Write(const u64 *x, u_int cnt)
{
    while (cnt > 0) {
	u_int cnt2 = Min(cnt, (u_int)TMP_CONVERT_LEN_LL);
	for (u_int i = 0; i < cnt2; i++)
	    tmp_convert.ll[i] = htonll(x[i]);
	RawWrite(tmp_convert.ll, cnt2*sizeof(*x));
	x += cnt2;
	cnt -= cnt2;
    }
}