1 /* copyright 2016 Apache 2 sddekit authors */
5 /* write data to file */
7 typedef struct file_data
{
15 file_apply(sd_out
*out
, double t
,
16 uint32_t nx
, double * restrict x
,
17 uint32_t nc
, double * restrict c
)
20 file_data
*d
= out
->ptr
;
21 fprintf(d
->fd
, "%f %d ", t
, nx
);
23 fprintf(d
->fd
, "%f ", x
[i
]);
24 fprintf(d
->fd
, "%d ", nc
);
26 fprintf(d
->fd
, "%f ", c
[i
]);
32 file_free(sd_out
*out
)
34 file_data
*d
= out
->ptr
;
41 file_is_std(sd_out_file
*file
)
43 return ((file_data
*)file
->ptr
)->isstd
;
47 out_from_file(sd_out_file
*file
)
49 return &(((file_data
*)file
->ptr
)->out_if
);
53 file_get_fd(sd_out_file
*file
)
55 return ((file_data
*)file
->ptr
)->fd
;
59 file_out_defaults
= { .free
= &file_free
, .apply
= &file_apply
};
62 file_defaults
= { .out
= &out_from_file
,.is_std
= &file_is_std
,.get_fd
= &file_get_fd
};
65 sd_out_file_new(FILE *file
, bool is_std
)
68 d
= sd_malloc (sizeof(file_data
));
71 sd_err("alloc out file failed.");
75 file_data zero
= { 0 };
80 d
->file_if
= file_defaults
;
81 d
->out_if
= file_out_defaults
;
82 d
->file_if
.ptr
= d
->out_if
.ptr
= d
;
87 sd_out_file_new_from_name(char *fname
)
90 if ((file
= fopen(fname
, "w"))==NULL
)
92 sd_err("unable to open file for writing.");
95 return sd_out_file_new(file
, false);
99 sd_out_file_new_from_std(FILE *std
)
101 return sd_out_file_new(std
, true);