GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <sys/types.h> 00004 #include <unistd.h> 00005 #include <rpc/types.h> 00006 #include <rpc/xdr.h> 00007 #include "G3d_intern.h" 00008 00009 /*---------------------------------------------------------------------------*/ 00010 00011 int G3d_writeInts(int fd, int useXdr, const int *i, int nofNum) 00012 { 00013 int firstTime = 1; 00014 XDR xdrEncodeStream; 00015 char xdrIntBuf[G3D_XDR_INT_LENGTH * 1024]; 00016 u_int n; 00017 00018 if (nofNum <= 0) 00019 G3d_fatalError("G3d_writeInts: nofNum out of range"); 00020 00021 if (useXdr == G3D_NO_XDR) { 00022 if (write(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) { 00023 G3d_error("G3d_writeInts: writing to file failed"); 00024 return 0; 00025 } 00026 else { 00027 return 1; 00028 } 00029 } 00030 00031 if (firstTime) { 00032 xdrmem_create(&xdrEncodeStream, xdrIntBuf, G3D_XDR_INT_LENGTH * 1024, 00033 XDR_ENCODE); 00034 firstTime = 1; 00035 } 00036 00037 do { 00038 n = nofNum % 1024; 00039 if (n == 0) 00040 n = 1024; 00041 00042 if (!xdr_setpos(&xdrEncodeStream, 0)) { 00043 G3d_error("G3d_writeInts: positioning xdr failed"); 00044 return 0; 00045 } 00046 00047 if (!xdr_vector(&xdrEncodeStream, (char *)i, n, sizeof(int), 00048 (xdrproc_t) xdr_int)) { 00049 G3d_error("G3d_writeInts: writing xdr failed"); 00050 return 0; 00051 } 00052 00053 if (write(fd, xdrIntBuf, G3D_XDR_INT_LENGTH * n) != 00054 G3D_XDR_INT_LENGTH * n) { 00055 G3d_error("G3d_writeInts: writing xdr to file failed"); 00056 return 0; 00057 } 00058 00059 nofNum -= n; 00060 i += n; 00061 } while (nofNum); 00062 00063 return 1; 00064 } 00065 00066 /*---------------------------------------------------------------------------*/ 00067 00068 int G3d_readInts(int fd, int useXdr, int *i, int nofNum) 00069 { 00070 int firstTime = 1; 00071 XDR xdrDecodeStream; 00072 char xdrIntBuf[G3D_XDR_INT_LENGTH * 1024]; 00073 u_int n; 00074 00075 if (nofNum <= 0) 00076 G3d_fatalError("G3d_readInts: nofNum out of range"); 00077 00078 if (useXdr == G3D_NO_XDR) { 00079 if (read(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) { 00080 G3d_error("G3d_readInts: reading from file failed"); 00081 return 0; 00082 } 00083 else { 00084 return 1; 00085 } 00086 } 00087 00088 if (firstTime) { 00089 xdrmem_create(&xdrDecodeStream, xdrIntBuf, G3D_XDR_INT_LENGTH * 1024, 00090 XDR_DECODE); 00091 firstTime = 1; 00092 } 00093 00094 do { 00095 n = nofNum % 1024; 00096 if (n == 0) 00097 n = 1024; 00098 00099 if (read(fd, xdrIntBuf, G3D_XDR_INT_LENGTH * n) != 00100 G3D_XDR_INT_LENGTH * n) { 00101 G3d_error("G3d_readInts: reading xdr from file failed"); 00102 return 0; 00103 } 00104 00105 if (!xdr_setpos(&xdrDecodeStream, 0)) { 00106 G3d_error("G3d_readInts: positioning xdr failed"); 00107 return 0; 00108 } 00109 00110 if (!xdr_vector(&xdrDecodeStream, (char *)i, n, sizeof(int), 00111 (xdrproc_t) xdr_int)) { 00112 G3d_error("G3d_readInts: reading xdr failed"); 00113 return 0; 00114 } 00115 00116 nofNum -= n; 00117 i += n; 00118 } while (nofNum); 00119 00120 return 1; 00121 }