forked from SacBase/Stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinFile.c
More file actions
74 lines (59 loc) · 1.47 KB
/
BinFile.c
File metadata and controls
74 lines (59 loc) · 1.47 KB
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
#include <assert.h>
#include "sacinterface.h"
#include "BinFile.h"
sac_int SACbinfopen(int *fd, char *name, sac_int flags)
{
*fd = open (name, flags, S_IRUSR | S_IWUSR);
sac_int error = *fd == -1 ? errno : -1;
return error;
}
sac_int SACbinfclose(int fd)
{
int retVal = close (fd);
int error = retVal == -1 ? errno : -1;
return (sac_int) error;
}
sac_int SAC_O_RDONLY(void)
{
return (sac_int) O_RDONLY;
}
sac_int SAC_O_WRONLY(void)
{
return (sac_int) O_WRONLY;
}
sac_int SAC_O_RDWR(void)
{
return (sac_int) O_RDWR;
}
sac_int SAC_O_CREAT(void)
{
return (sac_int) O_CREAT;
}
sac_int SAC_O_TRUNC(void)
{
return (sac_int) O_TRUNC;
}
SACarg *SACbinfReadDoubleArray (int fd, sac_int dim, sac_int *shp)
{
size_t size = sizeof (double);
for (sac_int i = 0; i < dim; i++) {
size *= (size_t) (shp[i]);
}
double *data = (double *) malloc (size);
ssize_t bytesRead = read (fd, data, size);
if (bytesRead == -1) {
SAC_RuntimeError ("Reading the file failed with error code %d\n", errno);
}
return SACARGcreateFromPointer (SACTYPE__MAIN__double, data, dim, shp);
}
void SACbinfWriteDoubleArray (int fd, sac_int dim, sac_int *shp, double *data)
{
sac_int size = sizeof (double);
for (sac_int i = 0; i < dim; i++) {
size *= (size_t) (shp[i]);
}
ssize_t res = write (fd, data, size);
if (res == -1) {
SAC_RuntimeError ("Reading the file failed with error code %d\n", errno);
}
}