OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
fread_swap.c
Go to the documentation of this file.
1 #include <genutils.h>
2 #include <stdlib.h>
3 
4 size_t fread_swap(int little_endian, void *ptr, size_t size,
5  size_t nmemb, FILE *stream) {
6  size_t result;
7 
8  result = fread(ptr, size, nmemb, stream);
9  if (little_endian != endianess()) {
10  swapc_bytes(ptr, size, nmemb);
11  }
12  return result;
13 }
14 
15 size_t fwrite_swap(int little_endian, const void *ptr, size_t size,
16  size_t nmemb, FILE *stream) {
17  size_t result;
18  void *ptr2;
19 
20  if (little_endian != endianess()) {
21  ptr2 = malloc(size * nmemb);
22  swapc_bytes2(ptr, ptr2, size, nmemb);
23  result = fwrite(ptr2, size, nmemb, stream);
24  free(ptr2);
25  } else {
26  result = fwrite(ptr, size, nmemb, stream);
27  }
28  return result;
29 }
size_t fread_swap(int little_endian, void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: fread_swap.c:4
int endianess(void)
determine endianess
Definition: endianess.c:10
int swapc_bytes(char *in, int nbyte, int ntime)
Definition: swapc_bytes.c:4
size_t fwrite_swap(int little_endian, const void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: fread_swap.c:15
int swapc_bytes2(const char *in, char *out, int nbyte, int ntime)
Definition: swapc_bytes.c:24