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.5 $ $Date: 2006/01/05 00:05:52 $ 00015 * 00016 *************************************************************************** 00017 * DESCRIPTION: 00018 * Formatted fortran file reading routines used in various plugins. 00019 * Each function reads the next record from the file (provided it contains 00020 * no more than n elements), optionally swapping its contents before 00021 * writing it into dest. 00022 * Returns the number of elements on success, 0 on failure. 00023 * 00024 * TODO: These should perhaps rewind the file to the beginning of the 00025 * record on failure. 00026 * 00027 ***************************************************************************/ 00028 00029 #ifndef FORTREAD_H 00030 #define FORTREAD_H 00031 00032 #include <stdlib.h> 00033 #include <stdio.h> 00034 00035 #include "endianswap.h" 00036 00037 /* Only works with aligned four-byte quantities, swap4_aligned() will */ 00038 /* cause a bus error on sume platforms if used on unaligned data */ 00039 static int fortread_4(void *dest, int n, int swap, FILE *fd) { 00040 int dataBegin, dataEnd, count; 00041 00042 if (fread(&dataBegin, sizeof(int), 1, fd) != 1) return 0; 00043 if (swap) swap4_aligned(&dataBegin, 1); 00044 if ((dataBegin <= 0) || (n < dataBegin/4)) return 0; 00045 00046 count = fread(dest, 4, dataBegin/4, fd); 00047 if (count != dataBegin/4) return 0; 00048 if (swap) swap4_aligned(dest, dataBegin/4); 00049 00050 if (fread(&dataEnd, sizeof(int), 1, fd) != 1) return 0; 00051 if (swap) swap4_aligned(&dataBegin, 1); 00052 if (dataEnd != dataBegin) return 0; 00053 00054 return count; 00055 } 00056 00057 #endif 00058