File: xdr.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (112 lines) | stat: -rw-r--r-- 2,026 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*!
   \file lib/db/dbmi_base/xdr.c

   \brief DBMI Library (base) - external data representation

   (C) 1999-2009, 2011 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2). Read the file COPYING that comes with GRASS for details.

   \author Joel Jones (CERL/UIUC), Radim Blazek, Brad Douglas, Markus Neteler
   \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
 */

#include "xdr.h"

#ifdef __MINGW32__
#define USE_STDIO 0
#define USE_READN 1
#else
#define USE_STDIO 1
#define USE_READN 0
#endif

#ifndef USE_STDIO
#include <unistd.h>
#endif

static FILE *_send, *_recv;

#if USE_READN

static ssize_t readn(int fd, void *buf, size_t count)
{
    ssize_t total = 0;

    while (total < count) {
        ssize_t n = read(fd, (char *)buf + total, count - total);

        if (n < 0)
            return n;
        if (n == 0)
            break;
        total += n;
    }

    return total;
}

static ssize_t writen(int fd, const void *buf, size_t count)
{
    ssize_t total = 0;

    while (total < count) {
        ssize_t n = write(fd, (const char *)buf + total, count - total);

        if (n < 0)
            return n;
        if (n == 0)
            break;
        total += n;
    }

    return total;
}

#endif

/*!
   \brief ?

   \param send
   \param recv
 */
void db__set_protocol_fds(FILE *send, FILE *recv)
{
    _send = send;
    _recv = recv;
}

/*!
   \brief ?

   \param buf
   \param size

   \return
 */
int db__send(const void *buf, size_t size)
{
#if USE_STDIO
    return fwrite(buf, 1, size, _send) == size;
#elif USE_READN
    return writen(fileno(_send), buf, size) == size;
#else
    return write(fileno(_send), buf, size) == size;
#endif
}

int db__recv(void *buf, size_t size)
{
#if USE_STDIO
#ifdef USE_BUFFERED_IO
    fflush(_send);
#endif
    return fread(buf, 1, size, _recv) == size;
#elif USE_READN
    return readn(fileno(_recv), buf, size) == size;
#else
    return read(fileno(_recv), buf, size) == size;
#endif
}