GRASS Programmer's Manual 6.4.1(2011)
|
00001 00002 /**************************************************************************** 00003 * 00004 * MODULE: dbmi_base 00005 * AUTHOR(S): CERL (Joel Jones + possible other original contributors) 00006 * Radim Blazek <radim.blazek gmail.com>, 00007 * Brad Douglas <rez touchofmadness.com>, 00008 * Markus Neteler <neteler itc.it> 00009 * PURPOSE: database management functions for modules and drivers 00010 * COPYRIGHT: (C) 2003-2006 by the GRASS Development Team 00011 * 00012 * This program is free software under the GNU General Public 00013 * License (>=v2). Read the file COPYING that comes with GRASS 00014 * for details. 00015 * 00016 *****************************************************************************/ 00017 #include "xdr.h" 00018 00019 #ifdef __MINGW32__ 00020 #define USE_STDIO 0 00021 #define USE_READN 1 00022 #else 00023 #define USE_STDIO 1 00024 #define USE_READN 0 00025 #endif 00026 00027 #ifndef USE_STDIO 00028 #include <unistd.h> 00029 #endif 00030 00031 static FILE *_send, *_recv; 00032 00033 #if USE_READN 00034 00035 static ssize_t readn(int fd, void *buf, size_t count) 00036 { 00037 ssize_t total = 0; 00038 00039 while (total < count) { 00040 ssize_t n = read(fd, (char *)buf + total, count - total); 00041 00042 if (n < 0) 00043 return n; 00044 if (n == 0) 00045 break; 00046 total += n; 00047 } 00048 00049 return total; 00050 } 00051 00052 static ssize_t writen(int fd, const void *buf, size_t count) 00053 { 00054 ssize_t total = 0; 00055 00056 while (total < count) { 00057 ssize_t n = write(fd, (const char *)buf + total, count - total); 00058 00059 if (n < 0) 00060 return n; 00061 if (n == 0) 00062 break; 00063 total += n; 00064 } 00065 00066 return total; 00067 } 00068 00069 #endif 00070 00071 void db__set_protocol_fds(FILE * send, FILE * recv) 00072 { 00073 _send = send; 00074 _recv = recv; 00075 } 00076 00077 int db__send(const void *buf, size_t size) 00078 { 00079 #if USE_STDIO 00080 return fwrite(buf, 1, size, _send) == size; 00081 #elif USE_READN 00082 return writen(fileno(_send), buf, size) == size; 00083 #else 00084 return write(fileno(_send), buf, size) == size; 00085 #endif 00086 } 00087 00088 int db__recv(void *buf, size_t size) 00089 { 00090 #if USE_STDIO 00091 #ifdef USE_BUFFERED_IO 00092 fflush(_send); 00093 #endif 00094 return fread(buf, 1, size, _recv) == size; 00095 #elif USE_READN 00096 return readn(fileno(_recv), buf, size) == size; 00097 #else 00098 return read(fileno(_recv), buf, size) == size; 00099 #endif 00100 }