GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
db/dbmi_base/xdr.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/xdr.c
3 
4  \brief DBMI Library (base) - external data representation
5 
6  (C) 1999-2009, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Joel Jones (CERL/UIUC), Radim Blazek, Brad Douglas, Markus Neteler
12  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
13  */
14 
15 #include "xdr.h"
16 
17 #ifdef __MINGW32__
18 #define USE_STDIO 0
19 #define USE_READN 1
20 #else
21 #define USE_STDIO 1
22 #define USE_READN 0
23 #endif
24 
25 #ifndef USE_STDIO
26 #include <unistd.h>
27 #endif
28 
29 static FILE *_send, *_recv;
30 
31 #if USE_READN
32 
33 static ssize_t readn(int fd, void *buf, size_t count)
34 {
35  ssize_t total = 0;
36 
37  while (total < count) {
38  ssize_t n = read(fd, (char *)buf + total, count - total);
39 
40  if (n < 0)
41  return n;
42  if (n == 0)
43  break;
44  total += n;
45  }
46 
47  return total;
48 }
49 
50 static ssize_t writen(int fd, const void *buf, size_t count)
51 {
52  ssize_t total = 0;
53 
54  while (total < count) {
55  ssize_t n = write(fd, (const char *)buf + total, count - total);
56 
57  if (n < 0)
58  return n;
59  if (n == 0)
60  break;
61  total += n;
62  }
63 
64  return total;
65 }
66 
67 #endif
68 
69 /*!
70  \brief ?
71 
72  \param send
73  \param recv
74  */
75 void db__set_protocol_fds(FILE *send, FILE *recv)
76 {
77  _send = send;
78  _recv = recv;
79 }
80 
81 /*!
82  \brief ?
83 
84  \param buf
85  \param size
86 
87  \return
88  */
89 int db__send(const void *buf, size_t size)
90 {
91 #if USE_STDIO
92  return fwrite(buf, 1, size, _send) == size;
93 #elif USE_READN
94  return writen(fileno(_send), buf, size) == size;
95 #else
96  return write(fileno(_send), buf, size) == size;
97 #endif
98 }
99 
100 int db__recv(void *buf, size_t size)
101 {
102 #if USE_STDIO
103 #ifdef USE_BUFFERED_IO
104  fflush(_send);
105 #endif
106  return fread(buf, 1, size, _recv) == size;
107 #elif USE_READN
108  return readn(fileno(_recv), buf, size) == size;
109 #else
110  return read(fileno(_recv), buf, size) == size;
111 #endif
112 }
void db__set_protocol_fds(FILE *send, FILE *recv)
?
int db__recv(void *buf, size_t size)
int db__send(const void *buf, size_t size)
?
int count