GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
xdr.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: dbmi_base
5  * AUTHOR(S): CERL (Joel Jones + possible other original contributors)
6  * Radim Blazek <radim.blazek gmail.com>,
7  * Brad Douglas <rez touchofmadness.com>,
8  * Markus Neteler <neteler itc.it>
9  * PURPOSE: database management functions for modules and drivers
10  * COPYRIGHT: (C) 2003-2006 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General Public
13  * License (>=v2). Read the file COPYING that comes with GRASS
14  * for details.
15  *
16  *****************************************************************************/
17 #include "xdr.h"
18 
19 #ifdef __MINGW32__
20 #define USE_STDIO 0
21 #define USE_READN 1
22 #else
23 #define USE_STDIO 1
24 #define USE_READN 0
25 #endif
26 
27 #ifndef USE_STDIO
28 #include <unistd.h>
29 #endif
30 
31 static FILE *_send, *_recv;
32 
33 #if USE_READN
34 
35 static ssize_t readn(int fd, void *buf, size_t count)
36 {
37  ssize_t total = 0;
38 
39  while (total < count) {
40  ssize_t n = read(fd, (char *)buf + total, count - total);
41 
42  if (n < 0)
43  return n;
44  if (n == 0)
45  break;
46  total += n;
47  }
48 
49  return total;
50 }
51 
52 static ssize_t writen(int fd, const void *buf, size_t count)
53 {
54  ssize_t total = 0;
55 
56  while (total < count) {
57  ssize_t n = write(fd, (const char *)buf + total, count - total);
58 
59  if (n < 0)
60  return n;
61  if (n == 0)
62  break;
63  total += n;
64  }
65 
66  return total;
67 }
68 
69 #endif
70 
71 void db__set_protocol_fds(FILE * send, FILE * recv)
72 {
73  _send = send;
74  _recv = recv;
75 }
76 
77 int db__send(const void *buf, size_t size)
78 {
79 #if USE_STDIO
80  return fwrite(buf, 1, size, _send) == size;
81 #elif USE_READN
82  return writen(fileno(_send), buf, size) == size;
83 #else
84  return write(fileno(_send), buf, size) == size;
85 #endif
86 }
87 
88 int db__recv(void *buf, size_t size)
89 {
90 #if USE_STDIO
91 #ifdef USE_BUFFERED_IO
92  fflush(_send);
93 #endif
94  return fread(buf, 1, size, _recv) == size;
95 #elif USE_READN
96  return readn(fileno(_recv), buf, size) == size;
97 #else
98  return read(fileno(_recv), buf, size) == size;
99 #endif
100 }