Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
BuffInp.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file BuffInp.c
5 
6  \version V1.2
7  \date 07.07.14
8  \brief Read from a file a line at a time, allowing one to probe
9  ahead and look at the contants of the next line without
10  removing it from the input stream.
11 
12  \copyright (c) UCL / Dr. Andrew C. R. Martin 1994-2014
13  \author Dr. Andrew C. R. Martin
14  \par
15  Institute of Structural & Molecular Biology,
16  University College London,
17  Gower Street,
18  London.
19  WC1E 6BT.
20  \par
21  andrew@bioinf.org.uk
22  andrew.martin@ucl.ac.uk
23 
24 **************************************************************************
25 
26  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
27  according to the conditions laid out in the accompanying file
28  COPYING.DOC.
29 
30  The code may be modified as required, but any modifications must be
31  documented so that the person responsible can be identified.
32 
33  The code may not be sold commercially or included as part of a
34  commercial product except as described in the file COPYING.DOC.
35 
36 **************************************************************************
37 
38  Description:
39  ============
40 
41 
42 **************************************************************************
43 
44  Usage:
45  ======
46 
47 **************************************************************************
48 
49  Revision History:
50  =================
51 - V1.0 08.03.94 Original
52 - V1.1 11.03.94 Changes to ReadBufferedFile() and ProbeBufferedFile()
53  to RePrompt() if reading from stdin when we get a
54  blank line
55 - V1.2 07.07.14 Use bl prefix for functions By: CTP
56 
57 *************************************************************************/
58 /* Doxygen
59  -------
60  #GROUP General Programming
61  #SUBGROUP File IO
62  #FUNCTION blOpenBufferedFile()
63  Open a file for buffered input. This allows probe-ahead to look at the
64  contents of the next line without removing it from the input stream.
65 
66  #FUNCTION blReadBufferedFile()
67  Reads a line from a buffered file (like fgets()).
68  Blank lines in the file will be skipped.
69 
70  #FUNCTION blProbeBufferedFile()
71  Read the next line from a buffered file without removing it from
72  the input stream. Repeated calls will thus return the same string.
73  The next call to ReadBufferedFile will also output the same string,
74  but will remove the line from the input stream.
75  Blank lines in the file will be skipped.
76 */
77 /************************************************************************/
78 /* Includes
79 */
80 #include <stdlib.h>
81 #include <string.h>
82 
83 #include "macros.h"
84 #include "BuffInp.h"
85 #include "WindIO.h"
86 
87 /************************************************************************/
88 /* Defines and macros
89 */
90 
91 /************************************************************************/
92 /* Globals
93 */
94 
95 /************************************************************************/
96 /* Prototypes
97 */
98 
99 /************************************************************************/
100 /*>INBUFFER blOpenBufferedFile(char *filename, int maxstr)
101  -------------------------------------------------------
102 *//**
103 
104  \param[in] *filename File name
105  \param[in] maxstr Max string length in file for buffering
106  \return Pointer to a buffered file
107 
108  Open a file for buffered input. This allows probe-ahead to look at the
109  contents of the next line without removing it from the input stream.
110 
111 - 28.02.94 Original By: ACRM
112 - 03.03.94 If filename is NULL, make file stdin
113 - 07.07.14 Use bl prefix for functions By: CTP
114 */
115 INBUFFER *blOpenBufferedFile(char *filename, int maxstr)
116 {
117  FILE *fp;
118  INBUFFER *BuffStruc = NULL;
119 
120  if(filename != NULL)
121  fp=fopen(filename,"r");
122  else
123  fp=stdin;
124 
125  if(fp!=NULL)
126  {
127  if((BuffStruc = (INBUFFER *)malloc(sizeof(INBUFFER)))!=NULL)
128  {
129  BuffStruc->fp = fp;
130  BuffStruc->nlines = 0;
131  BuffStruc->maxstr = maxstr;
132  if((BuffStruc->buffer =
133  (char *)malloc(maxstr * sizeof(char))) != NULL)
134  return(BuffStruc);
135  }
136  fclose(fp);
137  }
138  return(NULL);
139 }
140 
141 /************************************************************************/
142 /*>BOOL blReadBufferedFile(INBUFFER *bfp, char *string, int length)
143  ----------------------------------------------------------------
144 *//**
145 
146  \param[in] *bfp Pointer to a buffered file structure
147  \param[in] length Size of output string
148  \param[out] *string Output string read from file
149  \return TRUE: Successful read
150  FALSE: End of file (or error)
151 
152  Reads a line from a buffered file (like fgets()).
153  Blank lines in the file will be skipped.
154 
155 - 28.02.94 Original By: ACRM
156 - 07.03.94 Added code to skip blank lines
157 - 10.03.94 Added call to RePrompt() if we're reading from stdin and
158  we get a blank line
159 - 07.07.14 Use bl prefix for functions By: CTP
160 */
161 BOOL blReadBufferedFile(INBUFFER *bfp, char *string, int length)
162 {
163  int bufflen = 0;
164 
165  if(bfp == NULL)
166  return(FALSE);
167 
168  if(bfp->nlines == 0)
169  {
170  while(bufflen==0)
171  {
172  if(fgets(string,length,bfp->fp))
173  {
174  TERMINATE(string);
175  bufflen=strlen(string);
176  }
177  else
178  {
179  return(FALSE);
180  }
181 
182  if(!bufflen && bfp->fp == stdin)
183  blRePrompt();
184  }
185  }
186  else
187  {
188  strncpy(string,bfp->buffer,length);
189  (bfp->nlines)--;
190  }
191 
192  return(TRUE);
193 }
194 
195 /************************************************************************/
196 /*>BOOL blProbeBufferedFile(INBUFFER *bfp, char *string, int length)
197  -----------------------------------------------------------------
198 *//**
199 
200  \param[in] *bfp Pointer to a buffered file structure
201  \param[in] length Size of output string
202  \param[out] *string Output string read from file
203  \return TRUE: Successful read
204  FALSE: End of file (or error)
205 
206  Read the next line from a buffered file without removing it from
207  the input stream. Repeated calls will thus return the same string.
208  The next call to ReadBufferedFile will also output the same string,
209  but will remove the line from the input stream.
210  Blank lines in the file will be skipped.
211 
212 - 28.02.94 Original By: ACRM
213 - 07.03.94 Added code to skip blank lines
214 - 10.03.94 Added call to RePrompt() after blank line
215 - 07.07.14 Use bl prefix for functions By: CTP
216 */
217 BOOL blProbeBufferedFile(INBUFFER *bfp, char *string, int length)
218 {
219  int bufflen = 0;
220 
221  if(bfp==NULL)
222  return(FALSE);
223 
224  while(bufflen==0)
225  {
226  if(bfp->nlines == 0)
227  {
228  if(fgets(bfp->buffer,bfp->maxstr,bfp->fp))
229  {
230  TERMINATE(bfp->buffer);
231 
232  bufflen = strlen(bfp->buffer);
233  if(bufflen) (bfp->nlines)++;
234  }
235  else
236  {
237  return(FALSE);
238  }
239 
240  if(!bufflen && bfp->fp == stdin)
241  blRePrompt();
242  }
243  }
244 
245  strncpy(string,bfp->buffer,length);
246 
247  return(TRUE);
248 }
249 
FILE * fp
Definition: BuffInp.h:71
int maxstr
Definition: BuffInp.h:73
void blRePrompt(void)
Definition: WindIO.c:241
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
Header for window/normal interface routines.
#define FALSE
Definition: macros.h:223
BOOL blReadBufferedFile(INBUFFER *bfp, char *string, int length)
Definition: BuffInp.c:161
INBUFFER * blOpenBufferedFile(char *filename, int maxstr)
Definition: BuffInp.c:115
Useful macros.
#define TERMINATE(x)
Definition: macros.h:366
char * buffer
Definition: BuffInp.h:72
#define TRUE
Definition: macros.h:219
int nlines
Definition: BuffInp.h:73
Header file for BuffInp.c.
BOOL blProbeBufferedFile(INBUFFER *bfp, char *string, int length)
Definition: BuffInp.c:217