forked from N-BodyShop/gasoline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssio.c
114 lines (98 loc) · 2.44 KB
/
ssio.c
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
#include "ssio.h" /* also defines MAXPATHLEN */
#ifdef COLLISIONS /* pkdgrav version should otherwise match with ss version */
/*
** ssio.c -- DCR 98-09-16
** ------
** Solar System data I/O routines.
*/
#include <string.h>
#include <assert.h>
static const char *
ssioBasename(const char *path)
{
char *p;
assert(path != NULL);
p = strrchr(path,'/');
if (p) return p + 1;
else return path;
}
int
ssioNewExt(const char *infile,const char *inext,
char *outfile,const char *outext)
{
const char *basename;
char *c;
size_t n;
assert(infile != NULL && inext != NULL && outfile != NULL && outext != NULL);
basename = ssioBasename(infile);
if ((c = strrchr(basename,'.')) && strstr(c,inext))
n = c - basename;
else
n = strlen(basename);
if (n + strlen(outext) >= (size_t) MAXPATHLEN)
return 1;
(void) strncpy(outfile,basename,n); /* not null terminated */
(void) strcpy(outfile + n,outext);
return 0;
}
int
ssioOpen(const char *filename,SSIO *ssio,const u_int mode)
{
const char type[][3] = {"r","w","r+"};
const enum xdr_op op[] = {XDR_DECODE,XDR_ENCODE,XDR_ENCODE};
assert(filename != NULL && ssio != NULL);
assert(mode == SSIO_READ || mode == SSIO_WRITE || mode == SSIO_UPDATE);
if (!(ssio->fp = fopen(filename,type[mode])))
return 1;
xdrstdio_create(&ssio->xdrs,ssio->fp,op[mode]);
return 0;
}
int
ssioHead(SSIO *ssio,SSHEAD *head)
{
assert(ssio != NULL && head != NULL);
if (!xdr_double(&ssio->xdrs,&head->time)) return 1;
if (!xdr_int(&ssio->xdrs,&head->n_data)) return 1;
if (!xdr_int(&ssio->xdrs,&head->pad)) return 1;
return 0;
}
int
ssioData(SSIO *ssio,SSDATA *data)
{
int i;
assert(ssio != NULL && data != NULL);
if (!xdr_double(&ssio->xdrs,&data->mass)) return 1;
if (!xdr_double(&ssio->xdrs,&data->radius)) return 1;
for (i=0;i<N_DIM;i++)
if (!xdr_double(&ssio->xdrs,&data->pos[i])) return 1;
for (i=0;i<N_DIM;i++)
if (!xdr_double(&ssio->xdrs,&data->vel[i])) return 1;
for (i=0;i<N_DIM;i++)
if (!xdr_double(&ssio->xdrs,&data->spin[i])) return 1;
if (!xdr_int(&ssio->xdrs,&data->color)) return 1;
if (!xdr_int(&ssio->xdrs,&data->org_idx)) return 1;
return 0;
}
int
ssioClose(SSIO *ssio)
{
assert(ssio != NULL);
xdr_destroy(&ssio->xdrs);
if (fclose(ssio->fp)) return 1;
return 0;
}
int
ssioSetPos(SSIO *ssio,const u_int pos)
{
assert(ssio != NULL);
if (!xdr_setpos(&ssio->xdrs,pos)) return 1;
return 0;
}
void
ssioRewind(SSIO *ssio)
{
assert(ssio != NULL);
rewind(ssio->fp);
}
/* ssio.c */
#endif