summaryrefslogtreecommitdiff
path: root/support/dktools/dk4fwra.ctr
blob: b5fd7b57e6e7c8cedaef3d2d16e903245999832b (plain)
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
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd



%%	header

/**	@file
	Write to file using fwrite(), report errors immediately.
*/

#ifndef STDIO_H_INCLUDED
#include <stdio.h>
#define	STDIO_H_INCLUDED	1
#endif

#ifndef DK4APP_H_INCLUDED
#include "dk4app.h"
#define	DK4APP_H_INCLUDED	1
#endif

#ifdef __cplusplus
extern "C" {
#endif

/**	Write to file, report errors immediately.
	@param	filename	File name to write to.
	@param	ptr	Start address of buffer.
	@param	elsize	Size of each element.
	@param	nelem	Number of elements.
	@param	fipo	File to write to.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success (all elements written completely), 0 on error.
*/
int
dk4fwrite_app(
  const dkChar	*filename,
  const void	*ptr,
  size_t	 elsize,
  size_t	 nelem,
  FILE	 	*fipo,
  dk4_app_t	*app
);

#ifdef __cplusplus
}
#endif



%%	module

#include "dk4fwra.h"

#if DK4_HAVE_ERRNO_H
#ifndef ERRNO_H_INCLUDED
#include <errno.h>
#define	ERRNO_H_INCLUDED 1
#endif
#endif



int
dk4fwrite_app(
  const dkChar	*filename,
  const void	*ptr,
  size_t	 elsize,
  size_t	 nelem,
  FILE	 	*fipo,
  dk4_app_t	*app
)
{
  size_t	 wrb;
  int		 back = 0;
  int		 errc;
  if ((NULL != ptr) && (NULL != fipo) && (0 < elsize) && (0 < nelem)) {
    errno = 0;
    wrb = fwrite(ptr, elsize, nelem, fipo);
    if (wrb == nelem) {
      back = 1;
    } else {
      errc = errno;
      dk4fd_write_error_msg(app, filename, errc);
    }
  } else {				$? "! invalid arguments"
  }
  return back;
}