-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbgc_write_utils.c
More file actions
89 lines (65 loc) · 2.09 KB
/
Copy pathbgc_write_utils.c
File metadata and controls
89 lines (65 loc) · 2.09 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "bgc.h"
/* Easy unformatted FORTRAN reading, with some sanity checking.
* Usage like C fread. but returns size of main object read in or "0" on an error */
size_t
ftwrite( const void const *ptr, const size_t size, const size_t nitems, FILE * stream )
{
int nbytes;
size_t nitem1;
size_t res;
char *msg_init = "ftwrite error";
nbytes = ( int )size *nitems;
assert( nbytes > 0 );
/* unformatted data FORMAT, typically 4-byte boundaries */
res = fwrite( &nbytes, sizeof( int ), 1, stream );
if( res != 1 ) {
fprintf( stderr, "%s: file open? \n", msg_init );
perror( msg_init );
return ( res );
}
nitem1 = fwrite( ptr, size, nitems, stream );
if( nitem1 != nitems ) {
fprintf( stderr, "%s: %lu items requested, %lu items written. \n", msg_init, nitems,
nitem1 );
}
res = fwrite( &nbytes, sizeof( int ), 1, stream );
if( res != 1 ) {
fprintf( stderr, "%s: write error on second byte label\n", msg_init );
perror( msg_init );
return ( res );
}
return ( size_t ) nbytes;
}
void
bgc_write_header( FILE * fp, const OUTPUT_HEADER hdr )
{
size_t res;
/* sanity checks */
assert( sizeof( OUTPUT_HEADER ) == OUTPUT_HEADER_SIZE );
assert( fp != NULL );
/* force BGC header to be at beginning of file! */
rewind( fp );
res = ftwrite( &hdr, sizeof( OUTPUT_HEADER ), 1, fp );
assert( res == OUTPUT_HEADER_SIZE );
}
void
bgc_write_grouplist( FILE * fp, const int ngroups, const int const *nParticlesPerGroup )
{
size_t res;
assert( nParticlesPerGroup != NULL );
res = ftwrite( nParticlesPerGroup, sizeof( int ), ngroups, fp );
assert( res == sizeof( int ) * ngroups );
}
void
bgc_write_pdata( FILE * fp, const unsigned int npart, const int pdata_format,
const void const *pdata )
{
size_t size, res;
assert( pdata != NULL );
size = bgc_sizeof_pdata( pdata_format );
res = ftwrite( pdata, size, npart, fp );
assert( res == size * npart );
}