GADGET-4
io_streamcount.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * \copyright This file is part of the GADGET4 N-body/SPH code developed
3  * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4  * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5  *******************************************************************************/
6 
12 #ifndef IO_STREAMCOUNT_H
13 #define IO_STREAMCOUNT_H
14 
15 #include <errno.h>
16 
18 {
19  public:
20  long long byte_count = 0;
21 
33  size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
34  {
35  size_t nwritten;
36 
37  if(!stream)
38  return 0;
39 
40  if(size * nmemb > 0)
41  {
42  if((nwritten = fwrite(ptr, size, nmemb, stream)) != nmemb)
43  {
44  Terminate("I/O error (fwrite) has occured: %s\n", strerror(errno));
45  }
46  }
47  else
48  nwritten = 0;
49 
50  byte_count += size * nmemb;
51 
52  return nwritten;
53  }
54 
66  size_t my_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
67  {
68  size_t nread;
69 
70  if(!stream)
71  return 0;
72 
73  if(size * nmemb > 0)
74  {
75  if((nread = fread(ptr, size, nmemb, stream)) != nmemb)
76  {
77  if(feof(stream))
78  {
79  Terminate("I/O error (fread) has occured: end of file\n");
80  }
81  else
82  Terminate("I/O error (fread) has occured: %s\n", strerror(errno));
83  }
84  }
85  else
86  nread = 0;
87 
88  byte_count += size * nmemb;
89 
90  return nread;
91  }
92 
93  void reset_io_byte_count(void) { byte_count = 0; }
94 
95  long long get_io_byte_count(void) { return byte_count; }
96 };
97 
98 #endif
long long byte_count
size_t my_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
A wrapper for the fread() function.
void reset_io_byte_count(void)
long long get_io_byte_count(void)
size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
A wrapper for the fwrite() function.
#define Terminate(...)
Definition: macros.h:19