00001 /* $Id: plstdio.c 9612 2009-02-25 12:56:05Z smekal $ 00002 00003 Standardized I/O handler for PLplot. 00004 00005 Copyright (C) 2006 Jim Dishaw 00006 Copyright (C) 2006 Hazen Babcock 00007 00008 This file is part of PLplot. 00009 00010 PLplot is free software; you can redistribute it and/or modify 00011 it under the terms of the GNU Library General Public License as published 00012 by the Free Software Foundation; either version 2 of the License, or 00013 (at your option) any later version. 00014 00015 PLplot is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Library General Public License for more details. 00019 00020 You should have received a copy of the GNU Library General Public License 00021 along with PLplot; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00023 00024 */ 00025 00026 #define NEED_PLDEBUG 00027 #include "plplotP.h" 00028 00029 /* 00030 * plio_write() 00031 * 00032 * Writes the contents of buf to stream. Handles any I/O error conditions 00033 * so that the caller can "fire and forget." 00034 */ 00035 00036 void 00037 plio_fwrite(void *buf, size_t size, size_t nmemb, FILE *stream) 00038 { 00039 size_t bytes; 00040 00041 dbug_enter("plio_fwrite"); 00042 00043 /* Exit if there is nothing to write */ 00044 if(size == 0 || nmemb == 0) return; 00045 00046 /* Clear the error flag for this steam */ 00047 clearerr(stream); 00048 00049 bytes = fwrite(buf, size, nmemb, stream); 00050 00051 if(ferror(stream)) { 00052 /* Perhaps we can add a flag (global or per output stream) 00053 in order to decide if we should abort or warn. I think 00054 I/O errors should generate an abort */ 00055 plabort("Error writing to file"); 00056 } 00057 } 00058 00059 /* 00060 * plio_read() 00061 * 00062 * Read from stream into buf. Like plio_write(), this function will 00063 * handle any I/O error conditions. 00064 */ 00065 00066 void 00067 plio_fread(void *buf, size_t size, size_t nmemb, FILE *stream) 00068 { 00069 size_t bytes; 00070 00071 dbug_enter("plio_fread"); 00072 00073 /* If the buffer has a size of zero, we should complain */ 00074 if(size == 0 || nmemb == 0) { 00075 plwarn("Zero length buffer size in plio_read, returning"); 00076 return; 00077 } 00078 00079 /* Clear the error flag for this steam */ 00080 clearerr(stream); 00081 00082 bytes = fread(buf, size, nmemb, stream); 00083 00084 if(ferror(stream)) { 00085 /* The read resulted in an error */ 00086 plabort("Error reading from file"); 00087 } 00088 } 00089 00090 /* 00091 * plio_fgets() 00092 * 00093 * Read from stream into buf. This version of fgets is designed for the occasions 00094 * where the caller wants to ignore the return value. 00095 * 00096 * NOTE: If one is reading from a file until an EOF condition, fgets() is better suited 00097 * than this function, i.e. 00098 * 00099 * while(fgets(buf, size, fp) != NULL) { ... do some stuff ... } 00100 * 00101 * rather than 00102 * 00103 * while(!feof(fp)) { plio_fgets(buf, size, fp); ... do some stuff ... } 00104 * 00105 * which would require checking for an empty buffer. 00106 */ 00107 00108 void 00109 plio_fgets(char *buf, int size, FILE *stream) 00110 { 00111 char *s; 00112 00113 dbug_enter("plio_fgets"); 00114 00115 /* If the buffer has a size of zero, we should complain */ 00116 if(size == 0) { 00117 plwarn("Zero length buffer size in plio_fgets, returning"); 00118 return; 00119 } 00120 00121 /* Clear the error flag for this steam */ 00122 clearerr(stream); 00123 00124 s = fgets(buf, size, stream); 00125 00126 if(s == NULL && ferror(stream)) { 00127 /* The read resulted in an error */ 00128 plabort("Error reading from file"); 00129 } 00130 }
1.6.1