Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

fortread.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2006 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: fortread.h,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.7 $       $Date: 2006/10/11 23:18:13 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Unformatted and formatted fortran file reading routines for
00019  *   use in various plugins to simplify dealing with fortran i/o quirks.
00020  ***************************************************************************/
00021 
00022 #ifndef FORTREAD_H
00023 #define FORTREAD_H
00024 
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 
00028 #include "endianswap.h"
00029 
00030 
00031 /*  Unformatted reads.
00032  *
00033  *   Each function reads the next record from the file (provided it contains
00034  *   no more than n elements), optionally swapping its contents before
00035  *   writing it into dest. 
00036  *   Returns the number of elements on success, 0 on failure.
00037  *
00038  *   TODO: These should perhaps rewind the file to the beginning of the
00039  *   record on failure.
00040  */
00041 
00042 /* Only works with aligned four-byte quantities, swap4_aligned() will */
00043 /* cause a bus error on sume platforms if used on unaligned data      */
00044 static int fortread_4(void *dest, int n, int swap, FILE *fd) {
00045   int dataBegin, dataEnd, count;
00046 
00047   if (fread(&dataBegin, sizeof(int), 1, fd) != 1) return 0;
00048   if (swap) swap4_aligned(&dataBegin, 1);
00049   if ((dataBegin <= 0) || (n < dataBegin/4)) return 0;
00050 
00051   count = fread(dest, 4, dataBegin/4, fd);
00052   if (count != dataBegin/4) return 0;
00053   if (swap) swap4_aligned(dest, dataBegin/4);
00054 
00055   if (fread(&dataEnd, sizeof(int), 1, fd) != 1) return 0;
00056   if (swap) swap4_aligned(&dataBegin, 1);
00057   if (dataEnd != dataBegin) return 0;
00058 
00059   return count;
00060 }
00061 
00062 /* Formatted reads:
00063  *
00064  * copy at most 'len' characters from source to target. 
00065  *
00066  * leading white space is skipped over but counts towards 'len'.
00067  * the copy stops at first whitspace or a '\0'.
00068  * unlike strncpy(3) the result will always be \0 terminated.
00069  *
00070  * intended for copying (short) strings from formatted fortran  
00071  * i/o files that must not contain whitespace (e.g. residue names, 
00072  * atom name/types etc. in .pdb, .psf and alike.). 
00073  */
00074 static void strnwscpy(char *target, const char *source, const int len) {
00075   int i, c;
00076 
00077   for (i=0, c=0; i<len; ++i) {
00078     if (*source == '\0' || (c > 0 && *source == ' ')) {
00079       break;
00080     }
00081 
00082     if (*source == ' ') { 
00083       source++;
00084     } else {
00085       *target++ = *source++;
00086       c++;
00087     }
00088   }
00089   *target = '\0';
00090 }
00091 
00092 #endif
00093 

Generated on Fri Sep 5 01:38:28 2008 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002