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_writeDoubles(int fd, int useXdr, const double *i, int nofNum) 00012 { 00013 int firstTime = 1; 00014 XDR xdrEncodeStream; 00015 char xdrDoubleBuf[G3D_XDR_DOUBLE_LENGTH * 1024]; 00016 u_int n; 00017 00018 if (nofNum <= 0) 00019 G3d_fatalError("G3d_writeDoubles: nofNum out of range"); 00020 00021 if (useXdr == G3D_NO_XDR) { 00022 if (write(fd, i, sizeof(double) * nofNum) != sizeof(double) * nofNum) { 00023 G3d_error("G3d_writeDoubles: writing to file failed"); 00024 return 0; 00025 } 00026 else { 00027 return 1; 00028 } 00029 } 00030 00031 00032 if (firstTime) { 00033 xdrmem_create(&xdrEncodeStream, xdrDoubleBuf, 00034 G3D_XDR_DOUBLE_LENGTH * 1024, XDR_ENCODE); 00035 firstTime = 1; 00036 } 00037 00038 do { 00039 n = nofNum % 1024; 00040 if (n == 0) 00041 n = 1024; 00042 00043 if (!xdr_setpos(&xdrEncodeStream, 0)) { 00044 G3d_error("G3d_writeDoubles: positioning xdr failed"); 00045 return 0; 00046 } 00047 00048 if (!xdr_vector(&xdrEncodeStream, (char *)i, n, sizeof(double), 00049 (xdrproc_t) xdr_double)) { 00050 G3d_error("G3d_writeDoubles: writing xdr failed"); 00051 return 0; 00052 } 00053 00054 if (write(fd, xdrDoubleBuf, G3D_XDR_DOUBLE_LENGTH * n) != 00055 G3D_XDR_DOUBLE_LENGTH * n) { 00056 G3d_error("G3d_writeDoubles: writing xdr to file failed"); 00057 return 0; 00058 } 00059 00060 nofNum -= n; 00061 i += n; 00062 } while (nofNum); 00063 00064 return 1; 00065 } 00066 00067 /*---------------------------------------------------------------------------*/ 00068 00069 int G3d_readDoubles(int fd, int useXdr, double *i, int nofNum) 00070 { 00071 int firstTime = 1; 00072 XDR xdrDecodeStream; 00073 char xdrDoubleBuf[G3D_XDR_DOUBLE_LENGTH * 1024]; 00074 u_int n; 00075 00076 if (nofNum <= 0) 00077 G3d_fatalError("G3d_readDoubles: nofNum out of range"); 00078 00079 if (useXdr == G3D_NO_XDR) { 00080 if (read(fd, i, sizeof(double) * nofNum) != sizeof(double) * nofNum) { 00081 G3d_error("G3d_readDoubles: reading from file failed"); 00082 return 0; 00083 } 00084 else { 00085 return 1; 00086 } 00087 } 00088 00089 if (firstTime) { 00090 xdrmem_create(&xdrDecodeStream, xdrDoubleBuf, 00091 G3D_XDR_DOUBLE_LENGTH * 1024, XDR_DECODE); 00092 firstTime = 1; 00093 } 00094 00095 do { 00096 n = nofNum % 1024; 00097 if (n == 0) 00098 n = 1024; 00099 00100 if (read(fd, xdrDoubleBuf, G3D_XDR_DOUBLE_LENGTH * n) != 00101 G3D_XDR_DOUBLE_LENGTH * n) { 00102 G3d_error("G3d_readDoubles: reading xdr from file failed"); 00103 return 0; 00104 } 00105 00106 if (!xdr_setpos(&xdrDecodeStream, 0)) { 00107 G3d_error("G3d_readDoubles: positioning xdr failed"); 00108 return 0; 00109 } 00110 00111 if (!xdr_vector(&xdrDecodeStream, (char *)i, n, sizeof(double), 00112 (xdrproc_t) xdr_double)) { 00113 G3d_error("G3d_readDoubles: reading xdr failed"); 00114 return 0; 00115 } 00116 00117 nofNum -= n; 00118 i += n; 00119 } while (nofNum); 00120 00121 return 1; 00122 }