00001 /* B U _ F G E T S 00002 * BRL-CAD 00003 * 00004 * Copyright (c) 1995-2006 United States Government as represented by 00005 * the U.S. Army Research Laboratory. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation; either version 2 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this file; see the file named COPYING for more 00019 * information. 00020 */ 00021 00022 /** \addtogroup bu_log */ 00023 /*@{*/ 00024 /** @file bu_fgets.c 00025 *@brief 00026 * fgets replacement function that also handles CR as an EOL marker 00027 * 00028 * @Author John Anderson 00029 * 00030 * @par Source 00031 * The U. S. Army Research Laboratory @n 00032 * Aberdeen Proving Ground, Maryland 21005-5068 USA 00033 */ 00034 00035 #ifndef lint 00036 static const char libbu_fgets_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/bu_fgets.c,v 14.3 2006/09/03 15:14:07 lbutler Exp $ (ARL)"; 00037 #endif 00038 00039 #include "common.h" 00040 00041 #include <stdlib.h> 00042 #include <stdio.h> 00043 #include <math.h> 00044 #if HAVE_STRING_H 00045 # include <string.h> 00046 #endif 00047 00048 #include "machine.h" 00049 #include "bu.h" 00050 00051 00052 /** 00053 * B U _ F G E T S 00054 * 00055 * Reads in at most one less than size characters from stream and 00056 * stores them into the buffer pointed to by s. Reading stops after an 00057 * EOF, CR, LF, or a CR/LF combination. If a LF or CR is read, 00058 * it is stored into the buffer. If a CR/LF is read, just a CR is 00059 * stored into the buffer. A '\\0' is stored after the last 00060 * character in the buffer. Returns s on success, and NULL on error or 00061 * when end of file occurs while no characters have been read. 00062 */ 00063 00064 char * 00065 bu_fgets(char *s, int size, FILE *stream) 00066 { 00067 int totBytesRead = 0; 00068 int isEOF = 0; 00069 00070 /* if we are not asked to read anything, just return */ 00071 if(size < 1) { 00072 return s; 00073 } 00074 00075 /* if the buffer size is one, we have no space (we add a null) 00076 * so just return 00077 */ 00078 if(size == 1) { 00079 *s = '\0'; 00080 return s; 00081 } 00082 00083 /* check for EOF or error */ 00084 if(feof(stream) || ferror(stream)) { 00085 *s = '\0'; 00086 return (char *)NULL; 00087 } 00088 00089 /* actually do some reading */ 00090 while(totBytesRead < size - 1) { 00091 int c; 00092 00093 c = fgetc(stream); 00094 if(c == EOF) { 00095 isEOF = 1; 00096 break; 00097 } 00098 00099 s[totBytesRead++] = c; 00100 00101 /* check for newline */ 00102 if(c == '\n') { 00103 break; 00104 } 00105 00106 /* chech for CR */ 00107 if(c == '\r') { 00108 00109 /* check for CR/LF combination */ 00110 c = fgetc(stream); 00111 if(c != '\n') { 00112 /* not a CR/LF, so unget the last char */ 00113 ungetc(c, stream); 00114 } 00115 00116 break; 00117 } 00118 } 00119 00120 /* add our null */ 00121 s[totBytesRead] = '\0'; 00122 00123 if(isEOF && totBytesRead == 0) 00124 return (char *)NULL; 00125 else 00126 return s; 00127 } 00128 /*@}*/ 00129 /* 00130 * Local Variables: 00131 * mode: C 00132 * tab-width: 8 00133 * c-basic-offset: 4 00134 * indent-tabs-mode: t 00135 * End: 00136 * ex: shiftwidth=4 tabstop=8 00137 */