add_subdirectory(source/unres/src_CSA)
add_subdirectory(source/xdrfpdb/src)
add_subdirectory(source/xdrfpdb/src-M)
-
+ add_subdirectory(source/wham/src)
endif(UNRES_WITH_MPI)
add_subdirectory(source/unres/src_MIN)
#
-# CMake project file for UNRES with MD for single chains
+# CMake project file for UNRES CSA version
#
enable_language (Fortran)
#========================================
if(UNRES_WITH_MPI)
# binary with mpi
- set(UNRES_BIN "unres_${Fortran_COMPILER_NAME}_MPICH_${UNRES_FF}.exe")
+ set(UNRES_BIN "unres_${Fortran_COMPILER_NAME}_MPICH_${UNRES_MD_FF}.exe")
else(UNRES_WITH_MPI)
# binary without mpi
- set(UNRES_BIN "unres_${Fortran_COMPILER_NAME}_single_${UNRES_FF}.exe")
+ set(UNRES_BIN "unres_${Fortran_COMPILER_NAME}_single_${UNRES_MD_FF}.exe")
endif(UNRES_WITH_MPI)
#=========================================
--- /dev/null
+#
+# CMake project file for WHAM single chain version
+#
+
+enable_language (Fortran)
+
+#================================
+# Set source file lists
+#================================
+set(UNRES_WHAM_SRC0
+ wham_multparm.F
+ bxread.F
+ xread.F
+ cxread.F
+ enecalc1.F
+ energy_p_new.F
+ initialize_p.F
+ molread_zs.F
+ openunits.F
+ readrtns.F
+ arcos.f
+ cartder.f
+ cartprint.f
+ chainbuild.f
+ geomout.F
+ icant.f
+ intcor.f
+ int_from_cart.f
+ make_ensemble1.F
+ matmult.f
+ misc.f
+ mygetenv.F
+ parmread.F
+ pinorm.f
+ printmat.f
+ rescode.f
+ setup_var.f
+ slices.F
+ store_parm.F
+ timing.F
+ wham_calc1.F
+ readrtns_compar.F
+ readpdb.f
+ fitsq.f
+ contact.f
+ elecont.f
+ contfunc.f
+ cont_frag.f
+ conf_compar.F
+ match_contact.f
+ angnorm.f
+ odlodc.f
+ promienie.f
+ qwolynes.f
+ read_ref_str.F
+ rmscalc.f
+ secondary.f
+ proc_cont.f
+ define_pairs.f
+ mysort.f
+)
+
+set(UNRES_WHAM_PP_SRC
+ bxread.F
+ chainbuild.F
+ conf_compar.F
+ cxread.F
+ enecalc1.F
+ energy_p_new.F
+ geomout.F
+ initialize_p.F
+ make_ensemble1.F
+ molread_zs.F
+ mygetenv.F
+ openunits.F
+ parmread.F
+ read_ref_str.F
+ readrtns_compar.F
+ readrtns.F
+ slices.F
+ store_parm.F
+ timing.F
+ wham_calc1.F
+ wham_multparm.F
+ xread.F
+ proc_proc.c
+)
+
+
+#================================================
+# Set comipiler flags for different sourcefiles
+#================================================
+if (Fortran_COMPILER_NAME STREQUAL "ifort")
+ set(FFLAGS0 "-mcmodel=medium -g -CB -I. -I${CMAKE_CURRENT_SOURCE_DIR}/include_unres" )
+elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
+ set(FFLAGS0 "-c -I. " )
+ set(FFLAGS1 "-c -g -I. " )
+ set(FFLAGS2 "-c -I. ")
+ #set(FFLAGS3 "-c -w -O3 -ipo -ipo_obj -opt_report" )
+ set(FFLAGS3 "-c -I. " )
+endif (Fortran_COMPILER_NAME STREQUAL "ifort")
+
+
+#=========================================
+# Add MPI compiler flags
+#=========================================
+if(UNRES_WITH_MPI)
+ set(FFLAGS0 "${FFLAGS0} -I${MPIF_INCLUDE_DIRECTORIES}")
+endif(UNRES_WITH_MPI)
+
+set_property(SOURCE ${UNRES_WHAM_SRC0} PROPERTY COMPILE_FLAGS ${FFLAGS0} )
+
+#=========================================
+# WHAM preprocesor flags
+#=========================================
+
+set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
+
+#=========================================
+# System specific flags
+#=========================================
+if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+ set(CPPFLAGS "${CPPFLAGS} -DLINUX")
+endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+
+#=========================================
+# Compiler specific flags
+#=========================================
+
+if (Fortran_COMPILER_NAME STREQUAL "ifort")
+ # Add ifort preprocessor flags
+ set(CPPFLAGS "${CPPFLAGS} -DPGI")
+elseif (Fortran_COMPILER_NAME STREQUAL "f95")
+ # Add new gfortran flags
+ set(CPPFLAGS "${CPPFLAGS} -DG77")
+elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
+ # Add old gfortran flags
+ set(CPPFLAGS "${CPPFLAGS} -DG77")
+endif (Fortran_COMPILER_NAME STREQUAL "ifort")
+
+#=========================================
+# Add MPI preprocessor flags
+#=========================================
+set(CPPFLAGS "${CPPFLAGS} -DMPI")
+
+#=========================================
+# Add 64-bit specific preprocessor flags
+#=========================================
+if (architektura STREQUAL "64")
+ set(CPPFLAGS "${CPPFLAGS} -DAMD64")
+endif (architektura STREQUAL "64")
+
+#=========================================
+# Apply preprocesor flags to *.F files
+#=========================================
+set_property(SOURCE ${UNRES_WHAM_PP_SRC} PROPERTY COMPILE_DEFINITIONS ${CPPFLAGS} )
+
+
+#========================================
+# Setting binary name
+#========================================
+set(UNRES_WHAM_BIN "wham_${Fortran_COMPILER_NAME}.exe")
+
+#=========================================
+# cinfo.f stupid workaround for cmake
+# - shame on me ]:)
+#=========================================
+set_property(SOURCE compinfo.c PROPERTY CMAKE_C_FLAGS "-c" )
+add_executable(compinfo-wham compinfo.c)
+set_target_properties(compinfo-wham PROPERTIES OUTPUT_NAME compinfo)
+
+set(UNRES_CINFO_DIR "${CMAKE_CURRENT_BINARY_DIR}" )
+add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f
+ COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/cinfo.f ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f
+ COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/COMMON.IOUNITS ${CMAKE_CURRENT_BINARY_DIR}/COMMON.IOUNITS
+ COMMAND ${CMAKE_CURRENT_BINARY_DIR}/compinfo | true
+ DEPENDS compinfo-wham )
+set_property(SOURCE ${UNRES_CINFO_DIR}/cinfo.f PROPERTY COMPILE_FLAGS ${FFLAGS0} )
+
+#=========================================
+# Set full unres CSA sources
+#=========================================
+set(UNRES_WHAM_SRCS ${UNRES_WHAM_SRC0} ${UNRES_CINFO_DIR}/cinfo.f proc_proc.c)
+
+#=========================================
+# Build the binary
+#=========================================
+add_executable(UNRES_WHAM_BIN ${UNRES_WHAM_SRCS} )
+set_target_properties(UNRES_WHAM_BIN PROPERTIES OUTPUT_NAME ${UNRES_WHAM_BIN})
+
+#set_property(TARGET ${UNRES_BIN} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/unres/MD )
+#add_dependencies (${UNRES_BIN} ${UNRES_XDRFLIB})
+
+#=========================================
+# Link libraries
+#=========================================
+# link MPI library (libmpich.a)
+target_link_libraries( UNRES_WHAM_BIN ${MPIF_LIBRARIES} )
+# link libxdrf.a
+target_link_libraries( UNRES_WHAM_BIN xdrf )
+
+#=========================================
+# TESTS
+#=========================================
+
+#-- Copy all the data files from the test directory into the source directory
+#SET(UNRES_TEST_FILES
+# ala10.inp
+# )
+
+#FOREACH (UNRES_TEST_FILE ${UNRES_TEST_FILES})
+# SET (unres_test_dest "${CMAKE_CURRENT_BINARY_DIR}/${UNRES_TEST_FILE}")
+# MESSAGE (STATUS " Copying ${UNRES_TEST_FILE} from ${CMAKE_SOURCE_DIR}/examples/unres/MD/ff_gab/${UNRES_TEST_FILE} to ${unres_test_dest}")
+# ADD_CUSTOM_COMMAND (
+# TARGET ${UNRES_BIN}
+# POST_BUILD
+# COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/examples/unres/MD/ff_gab/${UNRES_TEST_FILE} ${unres_test_dest}
+# )
+#ENDFOREACH (UNRES_TEST_FILE ${UNRES_TEST_FILES})
+
+#=========================================
+# Generate data test files
+#=========================================
+# test_single_ala.sh
+#=========================================
+
+#FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/test_single_ala.sh
+#"#!/bin/sh
+#export POT=GB
+#export PREFIX=ala10
+#-----------------------------------------------------------------------------
+#UNRES_BIN=./${UNRES_BIN}
+#-----------------------------------------------------------------------------
+#DD=${CMAKE_SOURCE_DIR}/PARAM
+#export BONDPAR=$DD/bond.parm
+#export THETPAR=$DD/thetaml.5parm
+#export ROTPAR=$DD/scgauss.parm
+#export TORPAR=$DD/torsion_631Gdp.parm
+#export TORDPAR=$DD/torsion_double_631Gdp.parm
+#export ELEPAR=$DD/electr_631Gdp.parm
+#export SIDEPAR=$DD/sc_GB_opt.1gab_3S_qclass5no310-shan2-sc-16-10-8k
+#export FOURIER=$DD/fourier_opt.parm.1igd_hc_iter3_3
+#export SCPPAR=$DD/scp.parm
+#export SCCORPAR=$DD/rotcorr_AM1.parm
+#export PATTERN=$DD/patterns.cart
+#-----------------------------------------------------------------------------
+#$UNRES_BIN
+#")
+
+#=========================================
+# ala10.inp
+#=========================================
+
+#file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/ala10.inp
+#"ala10 unblocked
+#SEED=-1111333 MD ONE_LETTER rescale_mode=2 PDBOUT
+#nstep=15000 ntwe=100 ntwx=1000 dt=0.1 lang=0 tbf t_bath=300 damax=1.0 &
+#reset_moment=1000 reset_vel=1000 MDPDB
+#WLONG=1.35279 WSCP=1.59304 WELEC=0.71534 WBOND=1.00000 WANG=1.13873 &
+#WSCLOC=0.16258 WTOR=1.98599 WTORD=1.57069 WCORRH=0.42887 WCORR5=0.00000 &
+#WCORR6=0.00000 WEL_LOC=0.16036 WTURN3=1.68722 WTURN4=0.66230 WTURN6=0.00000 &
+#WVDWPP=0.11371 WHPB=1.00000 &
+#CUTOFF=7.00000 WCORR4=0.00000
+#12
+#XAAAAAAAAAAX
+# 0
+# 0
+# 90.0000 90.0000 90.0000 90.000 90.000 90.000 90.000 90.000
+# 90.0000 90.0000
+# 180.0000 180.0000 180.0000 180.000 180.000 180.000 180.000 180.000
+# 180.0000
+# 110.0000 110.0000 110.0000 100.000 110.000 100.000 110.000 110.000
+# 110.0000 110.0000
+# -120.0000 -120.0000 -120.000 -120.000 -120.000 -120.000 -120.000 -120.000
+# -120.0000 -120.0000
+#")
+
+
+# Add tests
+
+#if(NOT UNRES_WITH_MPI)
+
+# add_test(NAME UNRES_MD_Ala10 COMMAND sh ${CMAKE_CURRENT_BINARY_DIR}/test_single_ala.sh )
+
+#endif(NOT UNRES_WITH_MPI)
fprintf(out," write(iout,*)'Version %d.%-d build %d'\n",iv1,iv2,iv3);
uname(&Name);
time(&Tp);
-system("rm tmptmp; whoami > tmptmp");
+system("whoami > tmptmp");
in1=fopen("tmptmp","r");
fscanf(in1,"%s",buf1);
fclose(in1);
+system("rm tmptmp");
p1=ctime(&Tp);
p1[strlen(p1)-1]='\0';
fprintf(out," write(iout,*)'compiled %s'\n",p1);
fprintf(out," write(iout,*)'compiled by %s@%s'\n",buf1,Name.nodename);
fprintf(out," write(iout,*)'OS name: %s '\n",Name.sysname);
fprintf(out," write(iout,*)'OS release: %s '\n",Name.release);
-fprintf(out," write(iout,*)'OS version: %s '\n",Name.version);
+fprintf(out," write(iout,*)'OS version:',\n");
+fprintf(out," & ' %s '\n",Name.version);
fprintf(out," write(iout,*)'flags:'\n");
in1=fopen("Makefile","r");
while(fgets(buf,498,in1) != NULL)
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = xlc
-CPPC = xlc
-
-CFLAGS= -I. -O2 -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# do nothing special to FORTRAN function names
-#
-define(`FUNCTION',`$1')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
-
+++ /dev/null
-/* Copyright (C) 1991-1993,1995-2003,2004,2005 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
-
- The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- The GNU C Library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
-
-#ifndef _FEATURES_H
-#define _FEATURES_H 1
-
-/* These are defined by the user (or the compiler)
- to specify the desired environment:
-
- __STRICT_ANSI__ ISO Standard C.
- _ISOC99_SOURCE Extensions to ISO C89 from ISO C99.
- _POSIX_SOURCE IEEE Std 1003.1.
- _POSIX_C_SOURCE If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;
- if >=199309L, add IEEE Std 1003.1b-1993;
- if >=199506L, add IEEE Std 1003.1c-1995;
- if >=200112L, all of IEEE 1003.1-2004
- _XOPEN_SOURCE Includes POSIX and XPG things. Set to 500 if
- Single Unix conformance is wanted, to 600 for the
- upcoming sixth revision.
- _XOPEN_SOURCE_EXTENDED XPG things and X/Open Unix extensions.
- _LARGEFILE_SOURCE Some more functions for correct standard I/O.
- _LARGEFILE64_SOURCE Additional functionality from LFS for large files.
- _FILE_OFFSET_BITS=N Select default filesystem interface.
- _BSD_SOURCE ISO C, POSIX, and 4.3BSD things.
- _SVID_SOURCE ISO C, POSIX, and SVID things.
- _GNU_SOURCE All of the above, plus GNU extensions.
- _REENTRANT Select additionally reentrant object.
- _THREAD_SAFE Same as _REENTRANT, often used by other systems.
- _FORTIFY_SOURCE If set to numeric value > 0 additional security
- measures are defined, according to level.
-
- The `-ansi' switch to the GNU C compiler defines __STRICT_ANSI__.
- If none of these are defined, the default is to have _SVID_SOURCE,
- _BSD_SOURCE, and _POSIX_SOURCE set to one and _POSIX_C_SOURCE set to
- 199506L. If more than one of these are defined, they accumulate.
- For example __STRICT_ANSI__, _POSIX_SOURCE and _POSIX_C_SOURCE
- together give you ISO C, 1003.1, and 1003.2, but nothing else.
-
- These are defined by this file and are used by the
- header files to decide what to declare or define:
-
- __USE_ISOC99 Define ISO C99 things.
- __USE_POSIX Define IEEE Std 1003.1 things.
- __USE_POSIX2 Define IEEE Std 1003.2 things.
- __USE_POSIX199309 Define IEEE Std 1003.1, and .1b things.
- __USE_POSIX199506 Define IEEE Std 1003.1, .1b, .1c and .1i things.
- __USE_XOPEN Define XPG things.
- __USE_XOPEN_EXTENDED Define X/Open Unix things.
- __USE_UNIX98 Define Single Unix V2 things.
- __USE_XOPEN2K Define XPG6 things.
- __USE_LARGEFILE Define correct standard I/O things.
- __USE_LARGEFILE64 Define LFS things with separate names.
- __USE_FILE_OFFSET64 Define 64bit interface as default.
- __USE_BSD Define 4.3BSD things.
- __USE_SVID Define SVID things.
- __USE_MISC Define things common to BSD and System V Unix.
- __USE_GNU Define GNU extensions.
- __USE_REENTRANT Define reentrant/thread-safe *_r functions.
- __USE_FORTIFY_LEVEL Additional security measures used, according to level.
- __FAVOR_BSD Favor 4.3BSD things in cases of conflict.
-
- The macros `__GNU_LIBRARY__', `__GLIBC__', and `__GLIBC_MINOR__' are
- defined by this file unconditionally. `__GNU_LIBRARY__' is provided
- only for compatibility. All new code should use the other symbols
- to test for features.
-
- All macros listed above as possibly being defined by this file are
- explicitly undefined if they are not explicitly defined.
- Feature-test macros that are not defined by the user or compiler
- but are implied by the other feature-test macros defined (or by the
- lack of any definitions) are defined by the file. */
-
-
-/* Undefine everything, so we get a clean slate. */
-#undef __USE_ISOC99
-#undef __USE_POSIX
-#undef __USE_POSIX2
-#undef __USE_POSIX199309
-#undef __USE_POSIX199506
-#undef __USE_XOPEN
-#undef __USE_XOPEN_EXTENDED
-#undef __USE_UNIX98
-#undef __USE_XOPEN2K
-#undef __USE_LARGEFILE
-#undef __USE_LARGEFILE64
-#undef __USE_FILE_OFFSET64
-#undef __USE_BSD
-#undef __USE_SVID
-#undef __USE_MISC
-#undef __USE_GNU
-#undef __USE_REENTRANT
-#undef __USE_FORTIFY_LEVEL
-#undef __FAVOR_BSD
-#undef __KERNEL_STRICT_NAMES
-
-/* Suppress kernel-name space pollution unless user expressedly asks
- for it. */
-#ifndef _LOOSE_KERNEL_NAMES
-# define __KERNEL_STRICT_NAMES
-#endif
-
-/* Always use ISO C things. */
-#define __USE_ANSI 1
-
-/* Convenience macros to test the versions of glibc and gcc.
- Use them like this:
- #if __GNUC_PREREQ (2,8)
- ... code requiring gcc 2.8 or later ...
- #endif
- Note - they won't work for gcc1 or glibc1, since the _MINOR macros
- were not defined then. */
-#if defined __GNUC__ && defined __GNUC_MINOR__
-# define __GNUC_PREREQ(maj, min) \
- ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
-#else
-# define __GNUC_PREREQ(maj, min) 0
-#endif
-
-
-/* If _BSD_SOURCE was defined by the user, favor BSD over POSIX. */
-#if defined _BSD_SOURCE && \
- !(defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || \
- defined _XOPEN_SOURCE || defined _XOPEN_SOURCE_EXTENDED || \
- defined _GNU_SOURCE || defined _SVID_SOURCE)
-# define __FAVOR_BSD 1
-#endif
-
-/* If _GNU_SOURCE was defined by the user, turn on all the other features. */
-#ifdef _GNU_SOURCE
-# undef _ISOC99_SOURCE
-# define _ISOC99_SOURCE 1
-# undef _POSIX_SOURCE
-# define _POSIX_SOURCE 1
-# undef _POSIX_C_SOURCE
-# define _POSIX_C_SOURCE 199506L
-# undef _XOPEN_SOURCE
-# define _XOPEN_SOURCE 600
-# undef _XOPEN_SOURCE_EXTENDED
-# define _XOPEN_SOURCE_EXTENDED 1
-# undef _LARGEFILE64_SOURCE
-# define _LARGEFILE64_SOURCE 1
-# undef _BSD_SOURCE
-# define _BSD_SOURCE 1
-# undef _SVID_SOURCE
-# define _SVID_SOURCE 1
-#endif
-
-/* If nothing (other than _GNU_SOURCE) is defined,
- define _BSD_SOURCE and _SVID_SOURCE. */
-#if (!defined __STRICT_ANSI__ && !defined _ISOC99_SOURCE && \
- !defined _POSIX_SOURCE && !defined _POSIX_C_SOURCE && \
- !defined _XOPEN_SOURCE && !defined _XOPEN_SOURCE_EXTENDED && \
- !defined _BSD_SOURCE && !defined _SVID_SOURCE)
-# define _BSD_SOURCE 1
-# define _SVID_SOURCE 1
-#endif
-
-/* This is to enable the ISO C99 extension. Also recognize the old macro
- which was used prior to the standard acceptance. This macro will
- eventually go away and the features enabled by default once the ISO C99
- standard is widely adopted. */
-#if (defined _ISOC99_SOURCE || defined _ISOC9X_SOURCE \
- || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
-# define __USE_ISOC99 1
-#endif
-
-/* If none of the ANSI/POSIX macros are defined, use POSIX.1 and POSIX.2
- (and IEEE Std 1003.1b-1993 unless _XOPEN_SOURCE is defined). */
-#if ((!defined __STRICT_ANSI__ || (_XOPEN_SOURCE - 0) >= 500) && \
- !defined _POSIX_SOURCE && !defined _POSIX_C_SOURCE)
-# define _POSIX_SOURCE 1
-# if defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) < 500
-# define _POSIX_C_SOURCE 2
-# else
-# define _POSIX_C_SOURCE 199506L
-# endif
-#endif
-
-#if defined _POSIX_SOURCE || _POSIX_C_SOURCE >= 1 || defined _XOPEN_SOURCE
-# define __USE_POSIX 1
-#endif
-
-#if defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 2 || defined _XOPEN_SOURCE
-# define __USE_POSIX2 1
-#endif
-
-#if (_POSIX_C_SOURCE - 0) >= 199309L
-# define __USE_POSIX199309 1
-#endif
-
-#if (_POSIX_C_SOURCE - 0) >= 199506L
-# define __USE_POSIX199506 1
-#endif
-
-#if (_POSIX_C_SOURCE - 0) >= 200112L
-# define __USE_XOPEN2K 1
-#endif
-
-#ifdef _XOPEN_SOURCE
-# define __USE_XOPEN 1
-# if (_XOPEN_SOURCE - 0) >= 500
-# define __USE_XOPEN_EXTENDED 1
-# define __USE_UNIX98 1
-# undef _LARGEFILE_SOURCE
-# define _LARGEFILE_SOURCE 1
-# if (_XOPEN_SOURCE - 0) >= 600
-# define __USE_XOPEN2K 1
-# undef __USE_ISOC99
-# define __USE_ISOC99 1
-# endif
-# else
-# ifdef _XOPEN_SOURCE_EXTENDED
-# define __USE_XOPEN_EXTENDED 1
-# endif
-# endif
-#endif
-
-#ifdef _LARGEFILE_SOURCE
-# define __USE_LARGEFILE 1
-#endif
-
-#ifdef _LARGEFILE64_SOURCE
-# define __USE_LARGEFILE64 1
-#endif
-
-#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
-# define __USE_FILE_OFFSET64 1
-#endif
-
-#if defined _BSD_SOURCE || defined _SVID_SOURCE
-# define __USE_MISC 1
-#endif
-
-#ifdef _BSD_SOURCE
-# define __USE_BSD 1
-#endif
-
-#ifdef _SVID_SOURCE
-# define __USE_SVID 1
-#endif
-
-#ifdef _GNU_SOURCE
-# define __USE_GNU 1
-#endif
-
-#if defined _REENTRANT || defined _THREAD_SAFE
-# define __USE_REENTRANT 1
-#endif
-
-#if _FORTIFY_SOURCE > 0 && __GNUC_PREREQ (4, 1) && __OPTIMIZE__ > 0
-# if _FORTIFY_SOURCE == 1
-# define __USE_FORTIFY_LEVEL 1
-# elif _FORTIFY_SOURCE > 1
-# define __USE_FORTIFY_LEVEL 2
-# endif
-#endif
-
-/* We do support the IEC 559 math functionality, real and complex. */
-#define __STDC_IEC_559__ 1
-#define __STDC_IEC_559_COMPLEX__ 1
-
-/* wchar_t uses ISO 10646-1 (2nd ed., published 2000-09-15) / Unicode 3.1. */
-#define __STDC_ISO_10646__ 200009L
-
-/* This macro indicates that the installed library is the GNU C Library.
- For historic reasons the value now is 6 and this will stay from now
- on. The use of this variable is deprecated. Use __GLIBC__ and
- __GLIBC_MINOR__ now (see below) when you want to test for a specific
- GNU C library version and use the values in <gnu/lib-names.h> to get
- the sonames of the shared libraries. */
-#undef __GNU_LIBRARY__
-#define __GNU_LIBRARY__ 6
-
-/* Major and minor version number of the GNU C library package. Use
- these macros to test for features in specific releases. */
-#define __GLIBC__ 2
-#define __GLIBC_MINOR__ 3
-
-#define __GLIBC_PREREQ(maj, min) \
- ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= ((maj) << 16) + (min))
-
-/* Decide whether a compiler supports the long long datatypes. */
-#if defined __GNUC__ \
- || (defined __PGI && defined __i386__ ) \
- || (defined __INTEL_COMPILER && (defined __i386__ || defined __ia64__)) \
- || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
-# define __GLIBC_HAVE_LONG_LONG 1
-#endif
-
-/* This is here only because every header file already includes this one. */
-#ifndef __ASSEMBLER__
-# ifndef _SYS_CDEFS_H
-# include <sys/cdefs.h>
-# endif
-
-/* If we don't have __REDIRECT, prototypes will be missing if
- __USE_FILE_OFFSET64 but not __USE_LARGEFILE[64]. */
-# if defined __USE_FILE_OFFSET64 && !defined __REDIRECT
-# define __USE_LARGEFILE 1
-# define __USE_LARGEFILE64 1
-# endif
-
-#endif /* !ASSEMBLER */
-
-/* Decide whether we can define 'extern inline' functions in headers. */
-#if __GNUC_PREREQ (2, 7) && defined __OPTIMIZE__ \
- && !defined __OPTIMIZE_SIZE__ && !defined __NO_INLINE__
-# define __USE_EXTERN_INLINES 1
-#endif
-
-
-/* This is here only because every header file already includes this one.
- Get the definitions of all the appropriate `__stub_FUNCTION' symbols.
- <gnu/stubs.h> contains `#define __stub_FUNCTION' when FUNCTION is a stub
- that will always return failure (and set errno to ENOSYS). */
-#include <gnu/stubs.h>
-
-
-#endif /* features.h */
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-
-
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* xdrfproc) (int *, void *, int *);
-
-void
-xdrfbool (xdrid, pb, ret)
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-xdrfchar (xdrid, cp, ret)
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-xdrfdouble (xdrid, dp, ret)
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-xdrffloat (xdrid, fp, ret)
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-xdrfint (xdrid, ip, ret)
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-xdrflong (xdrid, lp, ret)
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-xdrfshort (xdrid, sp, ret)
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-xdrfuchar (xdrid, ucp, ret)
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-xdrfulong (xdrid, ulp, ret)
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-xdrfushort (xdrid, usp, ret)
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-xdrf3dfcoord (xdrid, fp, size, precision, ret)
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-xdrfstring (xdrid, sp_ptr, maxsize, ret, sp_len)
-int *xdrid, *ret;
-char * sp_ptr; int sp_len;
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((sp_len) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, sp_ptr, sp_len)) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( sp_ptr, sp_len, tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-xdrfwrapstring (xdrid, sp_ptr, ret, sp_len)
-int *xdrid, *ret;
-char * sp_ptr; int sp_len;
-{
- char *tsp;
- int maxsize;
- maxsize = (sp_len) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, sp_ptr, sp_len)) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( sp_ptr, sp_len, tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-xdrfopaque (xdrid, cp, ccnt, ret)
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-xdrfsetpos (xdrid, pos, ret)
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-xdrf (xdrid, pos)
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-xdrfvector (xdrid, cp, size, elproc, ret)
-int *xdrid, *ret;
-char *cp;
-int *size;
-xdrfproc elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-xdrfclose (xdrid, ret)
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-xdrfopen (xdrid, fp_ptr, mode_ptr, ret, fp_len, mode_len)
-int *xdrid;
-char * fp_ptr; int fp_len;
-char * mode_ptr; int mode_len;
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), fp_ptr, fp_len)) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), mode_ptr,
- mode_len)) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static int magicints[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
- 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
- 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
- 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
- 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
- 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
- 8388607, 10568983, 13316085, 16777216 };
-
-#define FIRSTIDX 9
-/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - encode num into buf using the specified number of bits
- |
- | This routines appends the value of num to the bits already present in
- | the array buf. You need to give it the number of bits to use and you
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(int buf[], int num_of_bits, int num) {
-
- unsigned int cnt, lastbyte;
- int lastbits;
- unsigned char * cbuf;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
- lastbits = buf[1];
- lastbyte =(unsigned int) buf[2];
- while (num_of_bits >= 8) {
- lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
- cbuf[cnt++] = lastbyte >> lastbits;
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- lastbyte = (lastbyte << num_of_bits) | num;
- lastbits += num_of_bits;
- if (lastbits >= 8) {
- lastbits -= 8;
- cbuf[cnt++] = lastbyte >> lastbits;
- }
- }
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- if (lastbits>0) {
- cbuf[cnt] = lastbyte << (8 - lastbits);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const int size) {
- unsigned int num = 1;
- int num_of_bits = 0;
-
- while (size >= num && num_of_bits < 32) {
- num_of_bits++;
- num <<= 1;
- }
- return num_of_bits;
-}
-
-/*___________________________________________________________________________
- |
- | sizeofints - calculate 'bitsize' of compressed ints
- |
- | given the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
- int i, num;
- unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
- num_of_bytes = 1;
- bytes[0] = 1;
- num_of_bits = 0;
- for (i=0; i < num_of_ints; i++) {
- tmp = 0;
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- num = 1;
- num_of_bytes--;
- while (bytes[num_of_bytes] >= num) {
- num_of_bits++;
- num *= 2;
- }
- return num_of_bits + num_of_bytes * 8;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | Multiplication with fixed (specified maximum ) sizes is used to get
- | to one big, multibyte integer. Allthough the routine could be
- | modified to handle sizes bigger than 16777216, or more than just
- | a few integers, this is not done, because the gain in compression
- | isn't worth the effort. Note that overflowing the multiplication
- | or the byte buffer (32 bytes) is unchecked and causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const int num_of_bits,
- unsigned int sizes[], unsigned int nums[]) {
-
- int i;
- unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
-
- tmp = nums[0];
- num_of_bytes = 0;
- do {
- bytes[num_of_bytes++] = tmp & 0xff;
- tmp >>= 8;
- } while (tmp != 0);
-
- for (i = 1; i < num_of_ints; i++) {
- if (nums[i] >= sizes[i]) {
- fprintf(stderr,"major breakdown in sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* use one step multiply */
- tmp = nums[i];
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- if (num_of_bits >= num_of_bytes * 8) {
- for (i = 0; i < num_of_bytes; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - decode number from buf using specified number of bits
- |
- | extract the number of bits from the array buf and construct an integer
- | from it. Return that value.
- |
-*/
-
-static int receivebits(int buf[], int num_of_bits) {
-
- int cnt, num;
- unsigned int lastbits, lastbyte;
- unsigned char * cbuf;
- int mask = (1 << num_of_bits) -1;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
-
- num = 0;
- while (num_of_bits >= 8) {
- lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
- num |= (lastbyte >> lastbits) << (num_of_bits - 8);
- num_of_bits -=8;
- }
- if (num_of_bits > 0) {
- if (lastbits < num_of_bits) {
- lastbits += 8;
- lastbyte = (lastbyte << 8) | cbuf[cnt++];
- }
- lastbits -= num_of_bits;
- num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
- }
- num &= mask;
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- return num;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() and decodes the small integers
- | written to buf by calculating the remainder and doing divisions with
- | the given sizes[]. You need to specify the total number of bits to be
- | used from buf in num_of_bits.
- |
-*/
-
-static void receiveints(int buf[], const int num_of_ints, int num_of_bits,
- unsigned int sizes[], int nums[]) {
- int bytes[32];
- int i, j, num_of_bytes, p, num;
-
- bytes[1] = bytes[2] = bytes[3] = 0;
- num_of_bytes = 0;
- while (num_of_bits > 8) {
- bytes[num_of_bytes++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(buf, num_of_bits);
- }
- for (i = num_of_ints-1; i > 0; i--) {
- num = 0;
- for (j = num_of_bytes-1; j >=0; j--) {
- num = (num << 8) | bytes[j];
- p = num / sizes[i];
- bytes[j] = p;
- num = num - p * sizes[i];
- }
- nums[i] = num;
- }
- nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- int minint[3], maxint[3], mindiff, *lip, diff;
- int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
- int minidx, maxidx;
- unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
- int flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 3, bitsize, sizeint, thiscoord);
- }
-
- i++;
- thiscoord[0] += minint[0];
- thiscoord[1] += minint[1];
- thiscoord[2] += minint[2];
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
-
- flag = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- if (k == 0) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
- prevcoord[0] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
- prevcoord[1] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
- prevcoord[2] = tmp;
- *lfp++ = prevcoord[0] * inv_precision;
- *lfp++ = prevcoord[1] * inv_precision;
- *lfp++ = prevcoord[2] * inv_precision;
- } else {
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- }
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- } else {
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static int magicints[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
- 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
- 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
- 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
- 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
- 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
- 8388607, 10568983, 13316085, 16777216 };
-
-#define FIRSTIDX 9
-/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - encode num into buf using the specified number of bits
- |
- | This routines appends the value of num to the bits already present in
- | the array buf. You need to give it the number of bits to use and you
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(int buf[], int num_of_bits, int num) {
-
- unsigned int cnt, lastbyte;
- int lastbits;
- unsigned char * cbuf;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
- lastbits = buf[1];
- lastbyte =(unsigned int) buf[2];
- while (num_of_bits >= 8) {
- lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
- cbuf[cnt++] = lastbyte >> lastbits;
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- lastbyte = (lastbyte << num_of_bits) | num;
- lastbits += num_of_bits;
- if (lastbits >= 8) {
- lastbits -= 8;
- cbuf[cnt++] = lastbyte >> lastbits;
- }
- }
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- if (lastbits>0) {
- cbuf[cnt] = lastbyte << (8 - lastbits);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const int size) {
- unsigned int num = 1;
- int num_of_bits = 0;
-
- while (size >= num && num_of_bits < 32) {
- num_of_bits++;
- num <<= 1;
- }
- return num_of_bits;
-}
-
-/*___________________________________________________________________________
- |
- | sizeofints - calculate 'bitsize' of compressed ints
- |
- | given the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
- int i, num;
- unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
- num_of_bytes = 1;
- bytes[0] = 1;
- num_of_bits = 0;
- for (i=0; i < num_of_ints; i++) {
- tmp = 0;
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- num = 1;
- num_of_bytes--;
- while (bytes[num_of_bytes] >= num) {
- num_of_bits++;
- num *= 2;
- }
- return num_of_bits + num_of_bytes * 8;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | Multiplication with fixed (specified maximum ) sizes is used to get
- | to one big, multibyte integer. Allthough the routine could be
- | modified to handle sizes bigger than 16777216, or more than just
- | a few integers, this is not done, because the gain in compression
- | isn't worth the effort. Note that overflowing the multiplication
- | or the byte buffer (32 bytes) is unchecked and causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const int num_of_bits,
- unsigned int sizes[], unsigned int nums[]) {
-
- int i;
- unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
-
- tmp = nums[0];
- num_of_bytes = 0;
- do {
- bytes[num_of_bytes++] = tmp & 0xff;
- tmp >>= 8;
- } while (tmp != 0);
-
- for (i = 1; i < num_of_ints; i++) {
- if (nums[i] >= sizes[i]) {
- fprintf(stderr,"major breakdown in sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* use one step multiply */
- tmp = nums[i];
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- if (num_of_bits >= num_of_bytes * 8) {
- for (i = 0; i < num_of_bytes; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - decode number from buf using specified number of bits
- |
- | extract the number of bits from the array buf and construct an integer
- | from it. Return that value.
- |
-*/
-
-static int receivebits(int buf[], int num_of_bits) {
-
- int cnt, num;
- unsigned int lastbits, lastbyte;
- unsigned char * cbuf;
- int mask = (1 << num_of_bits) -1;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
-
- num = 0;
- while (num_of_bits >= 8) {
- lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
- num |= (lastbyte >> lastbits) << (num_of_bits - 8);
- num_of_bits -=8;
- }
- if (num_of_bits > 0) {
- if (lastbits < num_of_bits) {
- lastbits += 8;
- lastbyte = (lastbyte << 8) | cbuf[cnt++];
- }
- lastbits -= num_of_bits;
- num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
- }
- num &= mask;
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- return num;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() and decodes the small integers
- | written to buf by calculating the remainder and doing divisions with
- | the given sizes[]. You need to specify the total number of bits to be
- | used from buf in num_of_bits.
- |
-*/
-
-static void receiveints(int buf[], const int num_of_ints, int num_of_bits,
- unsigned int sizes[], int nums[]) {
- int bytes[32];
- int i, j, num_of_bytes, p, num;
-
- bytes[1] = bytes[2] = bytes[3] = 0;
- num_of_bytes = 0;
- while (num_of_bits > 8) {
- bytes[num_of_bytes++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(buf, num_of_bits);
- }
- for (i = num_of_ints-1; i > 0; i--) {
- num = 0;
- for (j = num_of_bytes-1; j >=0; j--) {
- num = (num << 8) | bytes[j];
- p = num / sizes[i];
- bytes[j] = p;
- num = num - p * sizes[i];
- }
- nums[i] = num;
- }
- nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- int minint[3], maxint[3], mindiff, *lip, diff;
- int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
- int minidx, maxidx;
- unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
- int flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 3, bitsize, sizeint, thiscoord);
- }
-
- i++;
- thiscoord[0] += minint[0];
- thiscoord[1] += minint[1];
- thiscoord[2] += minint[2];
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
-
- flag = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- if (k == 0) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
- prevcoord[0] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
- prevcoord[1] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
- prevcoord[2] = tmp;
- *lfp++ = prevcoord[0] * inv_precision;
- *lfp++ = prevcoord[1] * inv_precision;
- *lfp++ = prevcoord[2] * inv_precision;
- } else {
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- }
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- } else {
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static int magicints[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
- 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
- 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
- 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
- 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
- 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
- 8388607, 10568983, 13316085, 16777216 };
-
-#define FIRSTIDX 9
-/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - encode num into buf using the specified number of bits
- |
- | This routines appends the value of num to the bits already present in
- | the array buf. You need to give it the number of bits to use and you
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(int buf[], int num_of_bits, int num) {
-
- unsigned int cnt, lastbyte;
- int lastbits;
- unsigned char * cbuf;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
- lastbits = buf[1];
- lastbyte =(unsigned int) buf[2];
- while (num_of_bits >= 8) {
- lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
- cbuf[cnt++] = lastbyte >> lastbits;
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- lastbyte = (lastbyte << num_of_bits) | num;
- lastbits += num_of_bits;
- if (lastbits >= 8) {
- lastbits -= 8;
- cbuf[cnt++] = lastbyte >> lastbits;
- }
- }
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- if (lastbits>0) {
- cbuf[cnt] = lastbyte << (8 - lastbits);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const int size) {
- unsigned int num = 1;
- int num_of_bits = 0;
-
- while (size >= num && num_of_bits < 32) {
- num_of_bits++;
- num <<= 1;
- }
- return num_of_bits;
-}
-
-/*___________________________________________________________________________
- |
- | sizeofints - calculate 'bitsize' of compressed ints
- |
- | given the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
- int i, num;
- unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
- num_of_bytes = 1;
- bytes[0] = 1;
- num_of_bits = 0;
- for (i=0; i < num_of_ints; i++) {
- tmp = 0;
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- num = 1;
- num_of_bytes--;
- while (bytes[num_of_bytes] >= num) {
- num_of_bits++;
- num *= 2;
- }
- return num_of_bits + num_of_bytes * 8;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | Multiplication with fixed (specified maximum ) sizes is used to get
- | to one big, multibyte integer. Allthough the routine could be
- | modified to handle sizes bigger than 16777216, or more than just
- | a few integers, this is not done, because the gain in compression
- | isn't worth the effort. Note that overflowing the multiplication
- | or the byte buffer (32 bytes) is unchecked and causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const int num_of_bits,
- unsigned int sizes[], unsigned int nums[]) {
-
- int i;
- unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
-
- tmp = nums[0];
- num_of_bytes = 0;
- do {
- bytes[num_of_bytes++] = tmp & 0xff;
- tmp >>= 8;
- } while (tmp != 0);
-
- for (i = 1; i < num_of_ints; i++) {
- if (nums[i] >= sizes[i]) {
- fprintf(stderr,"major breakdown in sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* use one step multiply */
- tmp = nums[i];
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- if (num_of_bits >= num_of_bytes * 8) {
- for (i = 0; i < num_of_bytes; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - decode number from buf using specified number of bits
- |
- | extract the number of bits from the array buf and construct an integer
- | from it. Return that value.
- |
-*/
-
-static int receivebits(int buf[], int num_of_bits) {
-
- int cnt, num;
- unsigned int lastbits, lastbyte;
- unsigned char * cbuf;
- int mask = (1 << num_of_bits) -1;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
-
- num = 0;
- while (num_of_bits >= 8) {
- lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
- num |= (lastbyte >> lastbits) << (num_of_bits - 8);
- num_of_bits -=8;
- }
- if (num_of_bits > 0) {
- if (lastbits < num_of_bits) {
- lastbits += 8;
- lastbyte = (lastbyte << 8) | cbuf[cnt++];
- }
- lastbits -= num_of_bits;
- num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
- }
- num &= mask;
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- return num;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() and decodes the small integers
- | written to buf by calculating the remainder and doing divisions with
- | the given sizes[]. You need to specify the total number of bits to be
- | used from buf in num_of_bits.
- |
-*/
-
-static void receiveints(int buf[], const int num_of_ints, int num_of_bits,
- unsigned int sizes[], int nums[]) {
- int bytes[32];
- int i, j, num_of_bytes, p, num;
-
- bytes[1] = bytes[2] = bytes[3] = 0;
- num_of_bytes = 0;
- while (num_of_bits > 8) {
- bytes[num_of_bytes++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(buf, num_of_bits);
- }
- for (i = num_of_ints-1; i > 0; i--) {
- num = 0;
- for (j = num_of_bytes-1; j >=0; j--) {
- num = (num << 8) | bytes[j];
- p = num / sizes[i];
- bytes[j] = p;
- num = num - p * sizes[i];
- }
- nums[i] = num;
- }
- nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- int minint[3], maxint[3], mindiff, *lip, diff;
- int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
- int minidx, maxidx;
- unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
- int flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 3, bitsize, sizeint, thiscoord);
- }
-
- i++;
- thiscoord[0] += minint[0];
- thiscoord[1] += minint[1];
- thiscoord[2] += minint[2];
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
-
- flag = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- if (k == 0) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
- prevcoord[0] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
- prevcoord[1] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
- prevcoord[2] = tmp;
- *lfp++ = prevcoord[0] * inv_precision;
- *lfp++ = prevcoord[1] * inv_precision;
- *lfp++ = prevcoord[2] * inv_precision;
- } else {
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- }
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- } else {
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-/* fixincludes should not add extern "C" to this file */
-/*
- * Rpc additions to <sys/types.h>
- */
-#ifndef _RPC_TYPES_H
-#define _RPC_TYPES_H 1
-
-typedef int bool_t;
-typedef int enum_t;
-/* This needs to be changed to uint32_t in the future */
-typedef unsigned long rpcprog_t;
-typedef unsigned long rpcvers_t;
-typedef unsigned long rpcproc_t;
-typedef unsigned long rpcprot_t;
-typedef unsigned long rpcport_t;
-
-#define __dontcare__ -1
-
-#ifndef FALSE
-# define FALSE (0)
-#endif
-
-#ifndef TRUE
-# define TRUE (1)
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
-
-#include <stdlib.h> /* For malloc decl. */
-#define mem_alloc(bsize) malloc(bsize)
-/*
- * XXX: This must not use the second argument, or code in xdr_array.c needs
- * to be modified.
- */
-#define mem_free(ptr, bsize) free(ptr)
-
-#ifndef makedev /* ie, we haven't already included it */
-#include <sys/types.h>
-#endif
-
-#ifndef __u_char_defined
-typedef __u_char u_char;
-typedef __u_short u_short;
-typedef __u_int u_int;
-typedef __u_long u_long;
-typedef __quad_t quad_t;
-typedef __u_quad_t u_quad_t;
-typedef __fsid_t fsid_t;
-# define __u_char_defined
-#endif
-#ifndef __daddr_t_defined
-typedef __daddr_t daddr_t;
-typedef __caddr_t caddr_t;
-# define __daddr_t_defined
-#endif
-
-#include <sys/time.h>
-#include <sys/param.h>
-
-#include <netinet/in.h>
-
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK (u_long)0x7F000001
-#endif
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif
-
-#endif /* rpc/types.h */
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
-#endif
-
-/*
- * xdr.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1986, Sun Microsystems, Inc.
- *
- * These are the "generic" xdr routines used to serialize and de-serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <libintl.h>
-
-#include "types.h"
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-/*
- * constants specific to the xdr "protocol"
- */
-#define XDR_FALSE ((long) 0)
-#define XDR_TRUE ((long) 1)
-#define LASTUNSIGNED ((u_int) 0-1)
-
-/*
- * for unit alignment
- */
-static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
-
-/*
- * Free a data structure using XDR
- * Not a filter, but a convenient utility nonetheless
- */
-void
-xdr_free (xdrproc_t proc, char *objp)
-{
- XDR x;
-
- x.x_op = XDR_FREE;
- (*proc) (&x, objp);
-}
-
-/*
- * XDR nothing
- */
-bool_t
-xdr_void (void)
-{
- return TRUE;
-}
-INTDEF(xdr_void)
-
-/*
- * XDR integers
- */
-bool_t
-xdr_int (XDR *xdrs, int *ip)
-{
-
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *ip;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ip = (int) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif INT_MAX == LONG_MAX
- return INTUSE(xdr_long) (xdrs, (long *) ip);
-#elif INT_MAX == SHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) ip);
-#else
-#error unexpected integer sizes in_xdr_int()
-#endif
-}
-INTDEF(xdr_int)
-
-/*
- * XDR unsigned integers
- */
-bool_t
-xdr_u_int (XDR *xdrs, u_int *up)
-{
-#if UINT_MAX < ULONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * up;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *up = (u_int) (u_long) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif UINT_MAX == ULONG_MAX
- return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
-#elif UINT_MAX == USHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) up);
-#else
-#error unexpected integer sizes in_xdr_u_int()
-#endif
-}
-INTDEF(xdr_u_int)
-
-/*
- * XDR long integers
- * The definition of xdr_long() is kept for backward
- * compatibility. Instead xdr_int() should be used.
- */
-bool_t
-xdr_long (XDR *xdrs, long *lp)
-{
-
- if (xdrs->x_op == XDR_ENCODE
- && (sizeof (int32_t) == sizeof (long)
- || (int32_t) *lp == *lp))
- return XDR_PUTLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_DECODE)
- return XDR_GETLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_long)
-
-/*
- * XDR unsigned long integers
- * The definition of xdr_u_long() is kept for backward
- * compatibility. Instead xdr_u_int() should be used.
- */
-bool_t
-xdr_u_long (XDR *xdrs, u_long *ulp)
-{
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- {
- long int tmp;
-
- if (XDR_GETLONG (xdrs, &tmp) == FALSE)
- return FALSE;
-
- *ulp = (uint32_t) tmp;
- return TRUE;
- }
-
- case XDR_ENCODE:
- if (sizeof (uint32_t) != sizeof (u_long)
- && (uint32_t) *ulp != *ulp)
- return FALSE;
-
- return XDR_PUTLONG (xdrs, (long *) ulp);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_long)
-
-/*
- * XDR hyper integers
- * same as xdr_u_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_hyper (XDR *xdrs, quad_t *llp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (long) ((*llp) >> 32);
- t2 = (long) (*llp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *llp = ((quad_t) t1) << 32;
- *llp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_hyper)
-
-
-/*
- * XDR hyper integers
- * same as xdr_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (unsigned long) ((*ullp) >> 32);
- t2 = (unsigned long) (*ullp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *ullp = ((u_quad_t) t1) << 32;
- *ullp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_u_hyper)
-
-bool_t
-xdr_longlong_t (XDR *xdrs, quad_t *llp)
-{
- return INTUSE(xdr_hyper) (xdrs, llp);
-}
-
-bool_t
-xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
-{
- return INTUSE(xdr_u_hyper) (xdrs, ullp);
-}
-
-/*
- * XDR short integers
- */
-bool_t
-xdr_short (XDR *xdrs, short *sp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *sp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *sp = (short) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_short)
-
-/*
- * XDR unsigned short integers
- */
-bool_t
-xdr_u_short (XDR *xdrs, u_short *usp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * usp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *usp = (u_short) (u_long) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_short)
-
-
-/*
- * XDR a char
- */
-bool_t
-xdr_char (XDR *xdrs, char *cp)
-{
- int i;
-
- i = (*cp);
- if (!INTUSE(xdr_int) (xdrs, &i))
- {
- return FALSE;
- }
- *cp = i;
- return TRUE;
-}
-
-/*
- * XDR an unsigned char
- */
-bool_t
-xdr_u_char (XDR *xdrs, u_char *cp)
-{
- u_int u;
-
- u = (*cp);
- if (!INTUSE(xdr_u_int) (xdrs, &u))
- {
- return FALSE;
- }
- *cp = u;
- return TRUE;
-}
-
-/*
- * XDR booleans
- */
-bool_t
-xdr_bool (XDR *xdrs, bool_t *bp)
-{
- long lb;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- lb = *bp ? XDR_TRUE : XDR_FALSE;
- return XDR_PUTLONG (xdrs, &lb);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &lb))
- {
- return FALSE;
- }
- *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bool)
-
-/*
- * XDR enumerations
- */
-bool_t
-xdr_enum (XDR *xdrs, enum_t *ep)
-{
- enum sizecheck
- {
- SIZEVAL
- }; /* used to find the size of an enum */
-
- /*
- * enums are treated as ints
- */
- if (sizeof (enum sizecheck) == 4)
- {
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = *ep;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ep = l;
- case XDR_FREE:
- return TRUE;
-
- }
- return FALSE;
-#else
- return INTUSE(xdr_long) (xdrs, (long *) ep);
-#endif
- }
- else if (sizeof (enum sizecheck) == sizeof (short))
- {
- return INTUSE(xdr_short) (xdrs, (short *) ep);
- }
- else
- {
- return FALSE;
- }
-}
-INTDEF(xdr_enum)
-
-/*
- * XDR opaque data
- * Allows the specification of a fixed size sequence of opaque bytes.
- * cp points to the opaque object and cnt gives the byte length.
- */
-bool_t
-xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
-{
- u_int rndup;
- static char crud[BYTES_PER_XDR_UNIT];
-
- /*
- * if no data we are done
- */
- if (cnt == 0)
- return TRUE;
-
- /*
- * round byte count to full xdr units
- */
- rndup = cnt % BYTES_PER_XDR_UNIT;
- if (rndup > 0)
- rndup = BYTES_PER_XDR_UNIT - rndup;
-
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (!XDR_GETBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
-
- case XDR_ENCODE:
- if (!XDR_PUTBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_opaque)
-
-/*
- * XDR counted bytes
- * *cpp is a pointer to the bytes, *sizep is the count.
- * If *cpp is NULL maxsize bytes are allocated
- */
-bool_t
-xdr_bytes (xdrs, cpp, sizep, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int *sizep;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int nodesize;
-
- /*
- * first deal with the length since xdr bytes are counted
- */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- nodesize = *sizep;
- if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (nodesize == 0)
- {
- return TRUE;
- }
- if (sp == NULL)
- {
- *cpp = sp = (char *) mem_alloc (nodesize);
- }
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
- return FALSE;
- }
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
-
- case XDR_FREE:
- if (sp != NULL)
- {
- mem_free (sp, nodesize);
- *cpp = NULL;
- }
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bytes)
-
-/*
- * Implemented here due to commonality of the object.
- */
-bool_t
-xdr_netobj (xdrs, np)
- XDR *xdrs;
- struct netobj *np;
-{
-
- return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
-}
-INTDEF(xdr_netobj)
-
-/*
- * XDR a discriminated union
- * Support routine for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * an entry with a null procedure pointer. The routine gets
- * the discriminant value and then searches the array of xdrdiscrims
- * looking for that value. It calls the procedure given in the xdrdiscrim
- * to handle the discriminant. If there is no specific routine a default
- * routine may be called.
- * If there is no specific or default routine an error is returned.
- */
-bool_t
-xdr_union (xdrs, dscmp, unp, choices, dfault)
- XDR *xdrs;
- enum_t *dscmp; /* enum to decide which arm to work on */
- char *unp; /* the union itself */
- const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
- xdrproc_t dfault; /* default xdr routine */
-{
- enum_t dscm;
-
- /*
- * we deal with the discriminator; it's an enum
- */
- if (!INTUSE(xdr_enum) (xdrs, dscmp))
- {
- return FALSE;
- }
- dscm = *dscmp;
-
- /*
- * search choices for a value that matches the discriminator.
- * if we find one, execute the xdr routine for that value.
- */
- for (; choices->proc != NULL_xdrproc_t; choices++)
- {
- if (choices->value == dscm)
- return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
- }
-
- /*
- * no match - execute the default xdr routine if there is one
- */
- return ((dfault == NULL_xdrproc_t) ? FALSE :
- (*dfault) (xdrs, unp, LASTUNSIGNED));
-}
-INTDEF(xdr_union)
-
-
-/*
- * Non-portable xdr primitives.
- * Care should be taken when moving these routines to new architectures.
- */
-
-
-/*
- * XDR null terminated ASCII strings
- * xdr_string deals with "C strings" - arrays of bytes that are
- * terminated by a NULL character. The parameter cpp references a
- * pointer to storage; If the pointer is null, then the necessary
- * storage is allocated. The last parameter is the max allowed length
- * of the string as specified by a protocol.
- */
-bool_t
-xdr_string (xdrs, cpp, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int size;
- u_int nodesize;
-
- /*
- * first deal with the length since xdr strings are counted-strings
- */
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- if (sp == NULL)
- {
- return TRUE; /* already free */
- }
- /* fall through... */
- case XDR_ENCODE:
- if (sp == NULL)
- return FALSE;
- size = strlen (sp);
- break;
- case XDR_DECODE:
- break;
- }
- if (!INTUSE(xdr_u_int) (xdrs, &size))
- {
- return FALSE;
- }
- if (size > maxsize)
- {
- return FALSE;
- }
- nodesize = size + 1;
- if (nodesize == 0)
- {
- /* This means an overflow. It a bug in the caller which
- provided a too large maxsize but nevertheless catch it
- here. */
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (sp == NULL)
- *cpp = sp = (char *) mem_alloc (nodesize);
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_string: out of memory\n");
- return FALSE;
- }
- sp[size] = 0;
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, size);
-
- case XDR_FREE:
- mem_free (sp, nodesize);
- *cpp = NULL;
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_string)
-
-/*
- * Wrapper for xdr_string that can be called directly from
- * routines like clnt_call
- */
-bool_t
-xdr_wrapstring (xdrs, cpp)
- XDR *xdrs;
- char **cpp;
-{
- if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
- {
- return TRUE;
- }
- return FALSE;
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr.h, External Data Representation Serialization Routines.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- */
-
-#ifndef _RPC_XDR_H
-#define _RPC_XDR_H 1
-
-#include <features.h>
-#include <sys/types.h>
-#include "types.h"
-
-/* We need FILE. */
-#include <stdio.h>
-
-__BEGIN_DECLS
-
-/*
- * XDR provides a conventional way for converting between C data
- * types and an external bit-string representation. Library supplied
- * routines provide for the conversion on built-in C data types. These
- * routines and utility routines defined here are used to help implement
- * a type encode/decode routine for each user-defined type.
- *
- * Each data type provides a single procedure which takes two arguments:
- *
- * bool_t
- * xdrproc(xdrs, argresp)
- * XDR *xdrs;
- * <type> *argresp;
- *
- * xdrs is an instance of a XDR handle, to which or from which the data
- * type is to be converted. argresp is a pointer to the structure to be
- * converted. The XDR handle contains an operation field which indicates
- * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
- *
- * XDR_DECODE may allocate space if the pointer argresp is null. This
- * data can be freed with the XDR_FREE operation.
- *
- * We write only one procedure per data type to make it easy
- * to keep the encode and decode procedures for a data type consistent.
- * In many cases the same code performs all operations on a user defined type,
- * because all the hard work is done in the component type routines.
- * decode as a series of calls on the nested data types.
- */
-
-/*
- * Xdr operations. XDR_ENCODE causes the type to be encoded into the
- * stream. XDR_DECODE causes the type to be extracted from the stream.
- * XDR_FREE can be used to release the space allocated by an XDR_DECODE
- * request.
- */
-enum xdr_op {
- XDR_ENCODE = 0,
- XDR_DECODE = 1,
- XDR_FREE = 2
-};
-
-/*
- * This is the number of bytes per unit of external data.
- */
-#define BYTES_PER_XDR_UNIT (4)
-/*
- * This only works if the above is a power of 2. But it's defined to be
- * 4 by the appropriate RFCs. So it will work. And it's normally quicker
- * than the old routine.
- */
-#if 1
-#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
-#else /* this is the old routine */
-#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
- * BYTES_PER_XDR_UNIT)
-#endif
-
-/*
- * The XDR handle.
- * Contains operation which is being applied to the stream,
- * an operations vector for the particular implementation (e.g. see xdr_mem.c),
- * and two private fields for the use of the particular implementation.
- */
-typedef struct XDR XDR;
-struct XDR
- {
- enum xdr_op x_op; /* operation; fast additional param */
- struct xdr_ops
- {
- bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
- /* get a long from underlying stream */
- bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
- /* put a long to " */
- bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
- /* get some bytes from " */
- bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
- /* put some bytes to " */
- u_int (*x_getpostn) (__const XDR *__xdrs);
- /* returns bytes off from beginning */
- bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
- /* lets you reposition the stream */
- int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
- /* buf quick ptr to buffered data */
- void (*x_destroy) (XDR *__xdrs);
- /* free privates of this xdr_stream */
- bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
- /* get a int from underlying stream */
- bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
- /* put a int to " */
- }
- *x_ops;
- caddr_t x_public; /* users' data */
- caddr_t x_private; /* pointer to private data */
- caddr_t x_base; /* private used for position info */
- u_int x_handy; /* extra private word */
- };
-
-/*
- * A xdrproc_t exists for each data type which is to be encoded or decoded.
- *
- * The second argument to the xdrproc_t is a pointer to an opaque pointer.
- * The opaque pointer generally points to a structure of the data type
- * to be decoded. If this pointer is 0, then the type routines should
- * allocate dynamic storage of the appropriate size and return it.
- * bool_t (*xdrproc_t)(XDR *, caddr_t *);
- */
-typedef bool_t (*xdrproc_t) (XDR *, void *,...);
-
-
-/*
- * Operations defined on a XDR handle
- *
- * XDR *xdrs;
- * int32_t *int32p;
- * long *longp;
- * caddr_t addr;
- * u_int len;
- * u_int pos;
- */
-#define XDR_GETINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-#define xdr_getint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-
-#define XDR_PUTINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-#define xdr_putint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-
-#define XDR_GETLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-#define xdr_getlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-
-#define XDR_PUTLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-#define xdr_putlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-
-#define XDR_GETBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-#define xdr_getbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-
-#define XDR_PUTBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-#define xdr_putbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-
-#define XDR_GETPOS(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-#define xdr_getpos(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-
-#define XDR_SETPOS(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-#define xdr_setpos(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-
-#define XDR_INLINE(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-#define xdr_inline(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-
-#define XDR_DESTROY(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-#define xdr_destroy(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-
-/*
- * Support struct for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * a entry with a null procedure pointer. The xdr_union routine gets
- * the discriminant value and then searches the array of structures
- * for a matching value. If a match is found the associated xdr routine
- * is called to handle that part of the union. If there is
- * no match, then a default routine may be called.
- * If there is no match and no default routine it is an error.
- */
-#define NULL_xdrproc_t ((xdrproc_t)0)
-struct xdr_discrim
-{
- int value;
- xdrproc_t proc;
-};
-
-/*
- * Inline routines for fast encode/decode of primitive data types.
- * Caveat emptor: these use single memory cycles to get the
- * data from the underlying buffer, and will fail to operate
- * properly if the data is not aligned. The standard way to use these
- * is to say:
- * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
- * return (FALSE);
- * <<< macro calls >>>
- * where ``count'' is the number of bytes of data occupied
- * by the primitive data types.
- *
- * N.B. and frozen for all time: each data type here uses 4 bytes
- * of external representation.
- */
-
-#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
-#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
-#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
-#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
-
-/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
- * and shouldn't be used any longer. Code which use this defines or longs
- * in the RPC code will not work on 64bit Solaris platforms !
- */
-#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
-#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
-#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
-#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-
-#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
-#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
-#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
-#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
-
-#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-/*
- * These are the "generic" xdr routines.
- * None of these can have const applied because it's not possible to
- * know whether the call is a read or a write to the passed parameter
- * also, the XDR structure is always updated by some of these calls.
- */
-extern bool_t xdr_void (void) __THROW;
-extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
-extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
-extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
-extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
-extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
-extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
-extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
-extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
-extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
-extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
-extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
-extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
-extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
-extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
-extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
-extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
-extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
-extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
-extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
- u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
- __THROW;
-extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
- u_int __maxsize) __THROW;
-extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
-extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
-extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
- __const struct xdr_discrim *__choices,
- xdrproc_t dfault) __THROW;
-extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
-extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
-extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
- u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
-extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
-extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
-extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
- xdrproc_t __proc) __THROW;
-extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
- u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
-extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
-extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
-
-/*
- * Common opaque bytes objects used by many rpc protocols;
- * declared here due to commonality.
- */
-#define MAX_NETOBJ_SZ 1024
-struct netobj
-{
- u_int n_len;
- char *n_bytes;
-};
-typedef struct netobj netobj;
-extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
-
-/*
- * These are the public routines for the various implementations of
- * xdr streams.
- */
-
-/* XDR using memory buffers */
-extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
- u_int __size, enum xdr_op __xop) __THROW;
-
-/* XDR using stdio library */
-extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
- __THROW;
-
-/* XDR pseudo records for tcp */
-extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
- u_int __recvsize, caddr_t __tcp_handle,
- int (*__readit) (char *, char *, int),
- int (*__writeit) (char *, char *, int)) __THROW;
-
-/* make end of xdr record */
-extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
-
-/* move to beginning of next record */
-extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
-
-/* true if no more input */
-extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
-
-/* free memory buffers for xdr */
-extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
-
-__END_DECLS
-
-#endif /* rpc/xdr.h */
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_array.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "non-trivial" xdr primitives used to serialize and de-serialize
- * arrays. See xdr.h for more info on the interface to xdr.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include "types.h"
-#include "xdr.h"
-#include <libintl.h>
-#include <limits.h>
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-#define LASTUNSIGNED ((u_int)0-1)
-
-
-/*
- * XDR an array of arbitrary elements
- * *addrp is a pointer to the array, *sizep is the number of elements.
- * If addrp is NULL (*sizep * elsize) bytes are allocated.
- * elsize is the size (in bytes) of each element, and elproc is the
- * xdr procedure to call to handle each element of the array.
- */
-bool_t
-xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
- XDR *xdrs;
- caddr_t *addrp; /* array pointer */
- u_int *sizep; /* number of elements */
- u_int maxsize; /* max numberof elements */
- u_int elsize; /* size in bytes of each element */
- xdrproc_t elproc; /* xdr routine to handle each element */
-{
- u_int i;
- caddr_t target = *addrp;
- u_int c; /* the actual element count */
- bool_t stat = TRUE;
- u_int nodesize;
-
- /* like strings, arrays are really counted arrays */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- c = *sizep;
- /*
- * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
- * doesn't actually use its second argument anyway.
- */
- if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
- nodesize = c * elsize;
-
- /*
- * if we are deserializing, we may need to allocate an array.
- * We also save time by checking for a null array if we are freeing.
- */
- if (target == NULL)
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (c == 0)
- return TRUE;
- *addrp = target = mem_alloc (nodesize);
- if (target == NULL)
- {
- fprintf (stderr, "%s", "xdr_array: out of memory\n");
- return FALSE;
- }
- __bzero (target, nodesize);
- break;
-
- case XDR_FREE:
- return TRUE;
- default:
- break;
- }
-
- /*
- * now we xdr each element of array
- */
- for (i = 0; (i < c) && stat; i++)
- {
- stat = (*elproc) (xdrs, target, LASTUNSIGNED);
- target += elsize;
- }
-
- /*
- * the array may need freeing
- */
- if (xdrs->x_op == XDR_FREE)
- {
- mem_free (*addrp, nodesize);
- *addrp = NULL;
- }
- return stat;
-}
-INTDEF(xdr_array)
-
-/*
- * xdr_vector():
- *
- * XDR a fixed length array. Unlike variable-length arrays,
- * the storage of fixed length arrays is static and unfreeable.
- * > basep: base of the array
- * > size: size of the array
- * > elemsize: size of each element
- * > xdr_elem: routine to XDR each element
- */
-bool_t
-xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
- XDR *xdrs;
- char *basep;
- u_int nelem;
- u_int elemsize;
- xdrproc_t xdr_elem;
-{
- u_int i;
- char *elptr;
-
- elptr = basep;
- for (i = 0; i < nelem; i++)
- {
- if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
- {
- return FALSE;
- }
- elptr += elemsize;
- }
- return TRUE;
-}
+++ /dev/null
-/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_float.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "floating point" xdr routines used to (de)serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <endian.h>
-
-#include "types.h"
-#include "xdr.h"
-
-/*
- * NB: Not portable.
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
-
-#ifdef vax
-
-/* What IEEE single precision floating point looks like on a Vax */
-struct ieee_single {
- unsigned int mantissa: 23;
- unsigned int exp : 8;
- unsigned int sign : 1;
-};
-
-/* Vax single precision floating point */
-struct vax_single {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
-};
-
-#define VAX_SNG_BIAS 0x81
-#define IEEE_SNG_BIAS 0x7f
-
-static struct sgl_limits {
- struct vax_single s;
- struct ieee_single ieee;
-} sgl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
- { 0x0, 0xff, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
- { 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-#endif /* vax */
-
-bool_t
-xdr_float(xdrs, fp)
- XDR *xdrs;
- float *fp;
-{
-#ifdef vax
- struct ieee_single is;
- struct vax_single vs, *vsp;
- struct sgl_limits *lim;
- int i;
-#endif
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vs = *((struct vax_single *)fp);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((vs.mantissa2 == lim->s.mantissa2) &&
- (vs.exp == lim->s.exp) &&
- (vs.mantissa1 == lim->s.mantissa1)) {
- is = lim->ieee;
- goto shipit;
- }
- }
- is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
- is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
- shipit:
- is.sign = vs.sign;
- return (XDR_PUTLONG(xdrs, (long *)&is));
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_PUTLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp = *(int *)fp;
- return (XDR_PUTLONG(xdrs, &tmp));
- }
- break;
-#endif
-
- case XDR_DECODE:
-#ifdef vax
- vsp = (struct vax_single *)fp;
- if (!XDR_GETLONG(xdrs, (long *)&is))
- return (FALSE);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((is.exp == lim->ieee.exp) &&
- (is.mantissa == lim->ieee.mantissa)) {
- *vsp = lim->s;
- goto doneit;
- }
- }
- vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
- vsp->mantissa2 = is.mantissa;
- vsp->mantissa1 = (is.mantissa >> 16);
- doneit:
- vsp->sign = is.sign;
- return (TRUE);
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_GETLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp;
- if (XDR_GETLONG(xdrs, &tmp)) {
- *(int *)fp = tmp;
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
-
-/*
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#ifdef vax
-/* What IEEE double precision floating point looks like on a Vax */
-struct ieee_double {
- unsigned int mantissa1 : 20;
- unsigned int exp : 11;
- unsigned int sign : 1;
- unsigned int mantissa2 : 32;
-};
-
-/* Vax double precision floating point */
-struct vax_double {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
- unsigned int mantissa3 : 16;
- unsigned int mantissa4 : 16;
-};
-
-#define VAX_DBL_BIAS 0x81
-#define IEEE_DBL_BIAS 0x3ff
-#define MASK(nbits) ((1 << nbits) - 1)
-
-static struct dbl_limits {
- struct vax_double d;
- struct ieee_double ieee;
-} dbl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
- { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
- { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-
-#endif /* vax */
-
-
-bool_t
-xdr_double(xdrs, dp)
- XDR *xdrs;
- double *dp;
-{
-#ifdef vax
- struct ieee_double id;
- struct vax_double vd;
- register struct dbl_limits *lim;
- int i;
-#endif
-
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vd = *((struct vax_double *)dp);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((vd.mantissa4 == lim->d.mantissa4) &&
- (vd.mantissa3 == lim->d.mantissa3) &&
- (vd.mantissa2 == lim->d.mantissa2) &&
- (vd.mantissa1 == lim->d.mantissa1) &&
- (vd.exp == lim->d.exp)) {
- id = lim->ieee;
- goto shipit;
- }
- }
- id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
- id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
- id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
- (vd.mantissa3 << 13) |
- ((vd.mantissa4 >> 3) & MASK(13));
- shipit:
- id.sign = vd.sign;
- dp = (double *)&id;
-#endif
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_PUTLONG(xdrs, lp+!LSW) &&
- XDR_PUTLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- tmp[0] = ip[!LSW];
- tmp[1] = ip[LSW];
- return (XDR_PUTLONG(xdrs, tmp) &&
- XDR_PUTLONG(xdrs, tmp+1));
- }
- break;
-
- case XDR_DECODE:
-#ifdef vax
- lp = (long *)&id;
- if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
- return (FALSE);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((id.mantissa2 == lim->ieee.mantissa2) &&
- (id.mantissa1 == lim->ieee.mantissa1) &&
- (id.exp == lim->ieee.exp)) {
- vd = lim->d;
- goto doneit;
- }
- }
- vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
- vd.mantissa1 = (id.mantissa1 >> 13);
- vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
- (id.mantissa2 >> 29);
- vd.mantissa3 = (id.mantissa2 >> 13);
- vd.mantissa4 = (id.mantissa2 << 3);
- doneit:
- vd.sign = id.sign;
- *dp = *((double *)&vd);
- return (TRUE);
-#else
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_GETLONG(xdrs, lp+!LSW) &&
- XDR_GETLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- if (XDR_GETLONG(xdrs, tmp+!LSW) &&
- XDR_GETLONG(xdrs, tmp+LSW)) {
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr_stdio.c, XDR implementation on standard i/o file.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * This set of routines implements a XDR on a stdio stream.
- * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
- * from the stream.
- */
-
-#include "types.h"
-#include <stdio.h>
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define fflush(s) INTUSE(_IO_fflush) (s)
-# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
-# define ftell(s) INTUSE(_IO_ftell) (s)
-# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
-#endif
-
-static bool_t xdrstdio_getlong (XDR *, long *);
-static bool_t xdrstdio_putlong (XDR *, const long *);
-static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
-static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
-static u_int xdrstdio_getpos (const XDR *);
-static bool_t xdrstdio_setpos (XDR *, u_int);
-static int32_t *xdrstdio_inline (XDR *, u_int);
-static void xdrstdio_destroy (XDR *);
-static bool_t xdrstdio_getint32 (XDR *, int32_t *);
-static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
-
-/*
- * Ops vector for stdio type XDR
- */
-static const struct xdr_ops xdrstdio_ops =
-{
- xdrstdio_getlong, /* deserialize a long int */
- xdrstdio_putlong, /* serialize a long int */
- xdrstdio_getbytes, /* deserialize counted bytes */
- xdrstdio_putbytes, /* serialize counted bytes */
- xdrstdio_getpos, /* get offset in the stream */
- xdrstdio_setpos, /* set offset in the stream */
- xdrstdio_inline, /* prime stream for inline macros */
- xdrstdio_destroy, /* destroy stream */
- xdrstdio_getint32, /* deserialize a int */
- xdrstdio_putint32 /* serialize a int */
-};
-
-/*
- * Initialize a stdio xdr stream.
- * Sets the xdr stream handle xdrs for use on the stream file.
- * Operation flag is set to op.
- */
-void
-xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
-{
- xdrs->x_op = op;
- /* We have to add the const since the `struct xdr_ops' in `struct XDR'
- is not `const'. */
- xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
- xdrs->x_private = (caddr_t) file;
- xdrs->x_handy = 0;
- xdrs->x_base = 0;
-}
-
-/*
- * Destroy a stdio xdr stream.
- * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
- */
-static void
-xdrstdio_destroy (XDR *xdrs)
-{
- (void) fflush ((FILE *) xdrs->x_private);
- /* xx should we close the file ?? */
-};
-
-static bool_t
-xdrstdio_getlong (XDR *xdrs, long *lp)
-{
- u_int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *lp = (long) ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putlong (XDR *xdrs, const long *lp)
-{
- int32_t mycopy = htonl ((u_int32_t) *lp);
-
- if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
-{
- if ((len != 0) && (fread (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
-{
- if ((len != 0) && (fwrite (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static u_int
-xdrstdio_getpos (const XDR *xdrs)
-{
- return (u_int) ftell ((FILE *) xdrs->x_private);
-}
-
-static bool_t
-xdrstdio_setpos (XDR *xdrs, u_int pos)
-{
- return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
-}
-
-static int32_t *
-xdrstdio_inline (XDR *xdrs, u_int len)
-{
- /*
- * Must do some work to implement this: must insure
- * enough data in the underlying stdio buffer,
- * that the buffer is aligned so that we can indirect through a
- * long *, and stuff this pointer in xdrs->x_buf. Doing
- * a fread or fwrite to a scratch buffer would defeat
- * most of the gains to be had here and require storage
- * management on this buffer, so we don't do this.
- */
- return NULL;
-}
-
-static bool_t
-xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
-{
- int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *ip = ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
-{
- int32_t mycopy = htonl (*ip);
-
- ip = &mycopy;
- if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-/* libc_hidden_def (xdrstdio_create) */
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrfo ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static int magicints[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
- 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
- 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
- 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
- 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
- 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
- 8388607, 10568983, 13316085, 16777216 };
-
-#define FIRSTIDX 9
-/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - encode num into buf using the specified number of bits
- |
- | This routines appends the value of num to the bits already present in
- | the array buf. You need to give it the number of bits to use and you
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(int buf[], int num_of_bits, int num) {
-
- unsigned int cnt, lastbyte;
- int lastbits;
- unsigned char * cbuf;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
- lastbits = buf[1];
- lastbyte =(unsigned int) buf[2];
- while (num_of_bits >= 8) {
- lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
- cbuf[cnt++] = lastbyte >> lastbits;
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- lastbyte = (lastbyte << num_of_bits) | num;
- lastbits += num_of_bits;
- if (lastbits >= 8) {
- lastbits -= 8;
- cbuf[cnt++] = lastbyte >> lastbits;
- }
- }
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- if (lastbits>0) {
- cbuf[cnt] = lastbyte << (8 - lastbits);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const int size) {
- unsigned int num = 1;
- int num_of_bits = 0;
-
- while (size >= num && num_of_bits < 32) {
- num_of_bits++;
- num <<= 1;
- }
- return num_of_bits;
-}
-
-/*___________________________________________________________________________
- |
- | sizeofints - calculate 'bitsize' of compressed ints
- |
- | given the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
- int i, num;
- unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
- num_of_bytes = 1;
- bytes[0] = 1;
- num_of_bits = 0;
- for (i=0; i < num_of_ints; i++) {
- tmp = 0;
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- num = 1;
- num_of_bytes--;
- while (bytes[num_of_bytes] >= num) {
- num_of_bits++;
- num *= 2;
- }
- return num_of_bits + num_of_bytes * 8;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | Multiplication with fixed (specified maximum ) sizes is used to get
- | to one big, multibyte integer. Allthough the routine could be
- | modified to handle sizes bigger than 16777216, or more than just
- | a few integers, this is not done, because the gain in compression
- | isn't worth the effort. Note that overflowing the multiplication
- | or the byte buffer (32 bytes) is unchecked and causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const int num_of_bits,
- unsigned int sizes[], unsigned int nums[]) {
-
- int i;
- unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
-
- tmp = nums[0];
- num_of_bytes = 0;
- do {
- bytes[num_of_bytes++] = tmp & 0xff;
- tmp >>= 8;
- } while (tmp != 0);
-
- for (i = 1; i < num_of_ints; i++) {
- if (nums[i] >= sizes[i]) {
- fprintf(stderr,"major breakdown in sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* use one step multiply */
- tmp = nums[i];
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- if (num_of_bits >= num_of_bytes * 8) {
- for (i = 0; i < num_of_bytes; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - decode number from buf using specified number of bits
- |
- | extract the number of bits from the array buf and construct an integer
- | from it. Return that value.
- |
-*/
-
-static int receivebits(int buf[], int num_of_bits) {
-
- int cnt, num;
- unsigned int lastbits, lastbyte;
- unsigned char * cbuf;
- int mask = (1 << num_of_bits) -1;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
-
- num = 0;
- while (num_of_bits >= 8) {
- lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
- num |= (lastbyte >> lastbits) << (num_of_bits - 8);
- num_of_bits -=8;
- }
- if (num_of_bits > 0) {
- if (lastbits < num_of_bits) {
- lastbits += 8;
- lastbyte = (lastbyte << 8) | cbuf[cnt++];
- }
- lastbits -= num_of_bits;
- num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
- }
- num &= mask;
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- return num;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() and decodes the small integers
- | written to buf by calculating the remainder and doing divisions with
- | the given sizes[]. You need to specify the total number of bits to be
- | used from buf in num_of_bits.
- |
-*/
-
-static void receiveints(int buf[], const int num_of_ints, int num_of_bits,
- unsigned int sizes[], int nums[]) {
- int bytes[32];
- int i, j, num_of_bytes, p, num;
-
- bytes[1] = bytes[2] = bytes[3] = 0;
- num_of_bytes = 0;
- while (num_of_bits > 8) {
- bytes[num_of_bytes++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(buf, num_of_bits);
- }
- for (i = num_of_ints-1; i > 0; i--) {
- num = 0;
- for (j = num_of_bytes-1; j >=0; j--) {
- num = (num << 8) | bytes[j];
- p = num / sizes[i];
- bytes[j] = p;
- num = num - p * sizes[i];
- }
- nums[i] = num;
- }
- nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- int minint[3], maxint[3], mindiff, *lip, diff;
- int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
- int minidx, maxidx;
- unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
- int flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 3, bitsize, sizeint, thiscoord);
- }
-
- i++;
- thiscoord[0] += minint[0];
- thiscoord[1] += minint[1];
- thiscoord[2] += minint[2];
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
-
- flag = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- if (k == 0) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
- prevcoord[0] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
- prevcoord[1] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
- prevcoord[2] = tmp;
- *lfp++ = prevcoord[0] * inv_precision;
- *lfp++ = prevcoord[1] * inv_precision;
- *lfp++ = prevcoord[2] * inv_precision;
- } else {
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- }
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- } else {
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static int magicints[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
- 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
- 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
- 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
- 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
- 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
- 8388607, 10568983, 13316085, 16777216 };
-
-#define FIRSTIDX 9
-/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - encode num into buf using the specified number of bits
- |
- | This routines appends the value of num to the bits already present in
- | the array buf. You need to give it the number of bits to use and you
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(int buf[], int num_of_bits, int num) {
-
- unsigned int cnt, lastbyte;
- int lastbits;
- unsigned char * cbuf;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
- lastbits = buf[1];
- lastbyte =(unsigned int) buf[2];
- while (num_of_bits >= 8) {
- lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
- cbuf[cnt++] = lastbyte >> lastbits;
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- lastbyte = (lastbyte << num_of_bits) | num;
- lastbits += num_of_bits;
- if (lastbits >= 8) {
- lastbits -= 8;
- cbuf[cnt++] = lastbyte >> lastbits;
- }
- }
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- if (lastbits>0) {
- cbuf[cnt] = lastbyte << (8 - lastbits);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const int size) {
- unsigned int num = 1;
- int num_of_bits = 0;
-
- while (size >= num && num_of_bits < 32) {
- num_of_bits++;
- num <<= 1;
- }
- return num_of_bits;
-}
-
-/*___________________________________________________________________________
- |
- | sizeofints - calculate 'bitsize' of compressed ints
- |
- | given the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
- int i, num;
- unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
- num_of_bytes = 1;
- bytes[0] = 1;
- num_of_bits = 0;
- for (i=0; i < num_of_ints; i++) {
- tmp = 0;
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- num = 1;
- num_of_bytes--;
- while (bytes[num_of_bytes] >= num) {
- num_of_bits++;
- num *= 2;
- }
- return num_of_bits + num_of_bytes * 8;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | Multiplication with fixed (specified maximum ) sizes is used to get
- | to one big, multibyte integer. Allthough the routine could be
- | modified to handle sizes bigger than 16777216, or more than just
- | a few integers, this is not done, because the gain in compression
- | isn't worth the effort. Note that overflowing the multiplication
- | or the byte buffer (32 bytes) is unchecked and causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const int num_of_bits,
- unsigned int sizes[], unsigned int nums[]) {
-
- int i;
- unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
-
- tmp = nums[0];
- num_of_bytes = 0;
- do {
- bytes[num_of_bytes++] = tmp & 0xff;
- tmp >>= 8;
- } while (tmp != 0);
-
- for (i = 1; i < num_of_ints; i++) {
- if (nums[i] >= sizes[i]) {
- fprintf(stderr,"major breakdown in sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* use one step multiply */
- tmp = nums[i];
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- if (num_of_bits >= num_of_bytes * 8) {
- for (i = 0; i < num_of_bytes; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - decode number from buf using specified number of bits
- |
- | extract the number of bits from the array buf and construct an integer
- | from it. Return that value.
- |
-*/
-
-static int receivebits(int buf[], int num_of_bits) {
-
- int cnt, num;
- unsigned int lastbits, lastbyte;
- unsigned char * cbuf;
- int mask = (1 << num_of_bits) -1;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
-
- num = 0;
- while (num_of_bits >= 8) {
- lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
- num |= (lastbyte >> lastbits) << (num_of_bits - 8);
- num_of_bits -=8;
- }
- if (num_of_bits > 0) {
- if (lastbits < num_of_bits) {
- lastbits += 8;
- lastbyte = (lastbyte << 8) | cbuf[cnt++];
- }
- lastbits -= num_of_bits;
- num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
- }
- num &= mask;
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- return num;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() and decodes the small integers
- | written to buf by calculating the remainder and doing divisions with
- | the given sizes[]. You need to specify the total number of bits to be
- | used from buf in num_of_bits.
- |
-*/
-
-static void receiveints(int buf[], const int num_of_ints, int num_of_bits,
- unsigned int sizes[], int nums[]) {
- int bytes[32];
- int i, j, num_of_bytes, p, num;
-
- bytes[1] = bytes[2] = bytes[3] = 0;
- num_of_bytes = 0;
- while (num_of_bits > 8) {
- bytes[num_of_bytes++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(buf, num_of_bits);
- }
- for (i = num_of_ints-1; i > 0; i--) {
- num = 0;
- for (j = num_of_bytes-1; j >=0; j--) {
- num = (num << 8) | bytes[j];
- p = num / sizes[i];
- bytes[j] = p;
- num = num - p * sizes[i];
- }
- nums[i] = num;
- }
- nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- int minint[3], maxint[3], mindiff, *lip, diff;
- int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
- int minidx, maxidx;
- unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
- int flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 3, bitsize, sizeint, thiscoord);
- }
-
- i++;
- thiscoord[0] += minint[0];
- thiscoord[1] += minint[1];
- thiscoord[2] += minint[2];
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
-
- flag = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- if (k == 0) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
- prevcoord[0] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
- prevcoord[1] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
- prevcoord[2] = tmp;
- *lfp++ = prevcoord[0] * inv_precision;
- *lfp++ = prevcoord[1] * inv_precision;
- *lfp++ = prevcoord[2] * inv_precision;
- } else {
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- }
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- } else {
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static int magicints[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
- 80, 101, 128, 161, 203, 256, 322, 406, 512, 645,
- 812, 1024, 1290, 1625, 2048, 2580, 3250, 4096, 5060, 6501,
- 8192, 10321, 13003, 16384, 20642, 26007, 32768, 41285, 52015, 65536,
- 82570, 104031, 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
- 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, 4194304, 5284491, 6658042,
- 8388607, 10568983, 13316085, 16777216 };
-
-#define FIRSTIDX 9
-/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - encode num into buf using the specified number of bits
- |
- | This routines appends the value of num to the bits already present in
- | the array buf. You need to give it the number of bits to use and you
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(int buf[], int num_of_bits, int num) {
-
- unsigned int cnt, lastbyte;
- int lastbits;
- unsigned char * cbuf;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
- lastbits = buf[1];
- lastbyte =(unsigned int) buf[2];
- while (num_of_bits >= 8) {
- lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
- cbuf[cnt++] = lastbyte >> lastbits;
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- lastbyte = (lastbyte << num_of_bits) | num;
- lastbits += num_of_bits;
- if (lastbits >= 8) {
- lastbits -= 8;
- cbuf[cnt++] = lastbyte >> lastbits;
- }
- }
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- if (lastbits>0) {
- cbuf[cnt] = lastbyte << (8 - lastbits);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const int size) {
- unsigned int num = 1;
- int num_of_bits = 0;
-
- while (size >= num && num_of_bits < 32) {
- num_of_bits++;
- num <<= 1;
- }
- return num_of_bits;
-}
-
-/*___________________________________________________________________________
- |
- | sizeofints - calculate 'bitsize' of compressed ints
- |
- | given the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const int num_of_ints, unsigned int sizes[]) {
- int i, num;
- unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
- num_of_bytes = 1;
- bytes[0] = 1;
- num_of_bits = 0;
- for (i=0; i < num_of_ints; i++) {
- tmp = 0;
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- num = 1;
- num_of_bytes--;
- while (bytes[num_of_bytes] >= num) {
- num_of_bits++;
- num *= 2;
- }
- return num_of_bits + num_of_bytes * 8;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | Multiplication with fixed (specified maximum ) sizes is used to get
- | to one big, multibyte integer. Allthough the routine could be
- | modified to handle sizes bigger than 16777216, or more than just
- | a few integers, this is not done, because the gain in compression
- | isn't worth the effort. Note that overflowing the multiplication
- | or the byte buffer (32 bytes) is unchecked and causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const int num_of_bits,
- unsigned int sizes[], unsigned int nums[]) {
-
- int i;
- unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
-
- tmp = nums[0];
- num_of_bytes = 0;
- do {
- bytes[num_of_bytes++] = tmp & 0xff;
- tmp >>= 8;
- } while (tmp != 0);
-
- for (i = 1; i < num_of_ints; i++) {
- if (nums[i] >= sizes[i]) {
- fprintf(stderr,"major breakdown in sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* use one step multiply */
- tmp = nums[i];
- for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) {
- tmp = bytes[bytecnt] * sizes[i] + tmp;
- bytes[bytecnt] = tmp & 0xff;
- tmp >>= 8;
- }
- while (tmp != 0) {
- bytes[bytecnt++] = tmp & 0xff;
- tmp >>= 8;
- }
- num_of_bytes = bytecnt;
- }
- if (num_of_bits >= num_of_bytes * 8) {
- for (i = 0; i < num_of_bytes; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - decode number from buf using specified number of bits
- |
- | extract the number of bits from the array buf and construct an integer
- | from it. Return that value.
- |
-*/
-
-static int receivebits(int buf[], int num_of_bits) {
-
- int cnt, num;
- unsigned int lastbits, lastbyte;
- unsigned char * cbuf;
- int mask = (1 << num_of_bits) -1;
-
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
-
- num = 0;
- while (num_of_bits >= 8) {
- lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
- num |= (lastbyte >> lastbits) << (num_of_bits - 8);
- num_of_bits -=8;
- }
- if (num_of_bits > 0) {
- if (lastbits < num_of_bits) {
- lastbits += 8;
- lastbyte = (lastbyte << 8) | cbuf[cnt++];
- }
- lastbits -= num_of_bits;
- num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
- }
- num &= mask;
- buf[0] = cnt;
- buf[1] = lastbits;
- buf[2] = lastbyte;
- return num;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() and decodes the small integers
- | written to buf by calculating the remainder and doing divisions with
- | the given sizes[]. You need to specify the total number of bits to be
- | used from buf in num_of_bits.
- |
-*/
-
-static void receiveints(int buf[], const int num_of_ints, int num_of_bits,
- unsigned int sizes[], int nums[]) {
- int bytes[32];
- int i, j, num_of_bytes, p, num;
-
- bytes[1] = bytes[2] = bytes[3] = 0;
- num_of_bytes = 0;
- while (num_of_bits > 8) {
- bytes[num_of_bytes++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(buf, num_of_bits);
- }
- for (i = num_of_ints-1; i > 0; i--) {
- num = 0;
- for (j = num_of_bytes-1; j >=0; j--) {
- num = (num << 8) | bytes[j];
- p = num / sizes[i];
- bytes[j] = p;
- num = num - p * sizes[i];
- }
- nums[i] = num;
- }
- nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- int minint[3], maxint[3], mindiff, *lip, diff;
- int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
- int minidx, maxidx;
- unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
- int flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- sizeint[0] = maxint[0] - minint[0]+1;
- sizeint[1] = maxint[1] - minint[1]+1;
- sizeint[2] = maxint[2] - minint[2]+1;
-
- /* check if one of the sizes is to big to be multiplied */
- if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) {
- bitsizeint[0] = sizeofint(sizeint[0]);
- bitsizeint[1] = sizeofint(sizeint[1]);
- bitsizeint[2] = sizeofint(sizeint[2]);
- bitsize = 0; /* flag the use of large sizes */
- } else {
- bitsize = sizeofints(3, sizeint);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 3, bitsize, sizeint, thiscoord);
- }
-
- i++;
- thiscoord[0] += minint[0];
- thiscoord[1] += minint[1];
- thiscoord[2] += minint[2];
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
-
- flag = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- if (k == 0) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
- prevcoord[0] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
- prevcoord[1] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
- prevcoord[2] = tmp;
- *lfp++ = prevcoord[0] * inv_precision;
- *lfp++ = prevcoord[1] * inv_precision;
- *lfp++ = prevcoord[2] * inv_precision;
- } else {
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- }
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- } else {
- *lfp++ = thiscoord[0] * inv_precision;
- *lfp++ = thiscoord[1] * inv_precision;
- *lfp++ = thiscoord[2] * inv_precision;
- }
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-