Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
fgetsany.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file fgetsany.c
5 
6  \version V1.1
7  \date 07.07.14
8  \brief Like fgets(), but allocates memory and returns pointer
9  to memory block
10 
11  \copyright (c) UCL / Dr. Andrew C. R. Martin 1995-2014
12  \author Dr. Andrew C. R. Martin
13  \par
14  Institute of Structural & Molecular Biology,
15  University College London,
16  Gower Street,
17  London.
18  WC1E 6BT.
19  \par
20  andrew@bioinf.org.uk
21  andrew.martin@ucl.ac.uk
22 
23 **************************************************************************
24 
25  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
26  according to the conditions laid out in the accompanying file
27  COPYING.DOC.
28 
29  The code may be modified as required, but any modifications must be
30  documented so that the person responsible can be identified.
31 
32  The code may not be sold commercially or included as part of a
33  commercial product except as described in the file COPYING.DOC.
34 
35 **************************************************************************
36 
37  Description:
38  ============
39 
40 
41  blFgetsany() provides a routine like fgets() for reading strings from
42  a file, but does not require you to impose a limit on the length of
43  string which may be read. fgetsany() allocates memory to store a
44  string of any length and returns a pointer to that allocated memory.
45  After use, this memory must be freed by the calling routine.
46 
47 **************************************************************************
48 
49  Usage:
50  ======
51 
52  blFgetsany() returns NULL on end-of-file and if memory allocation
53  failed. It uses the global `errno' variable to indicate a memory
54  allocation failure (errno==ENOMEM).
55 
56  Typical usage is as follows:
57 
58 \code
59 
60  #include "bioplib/general.h"
61 
62  main()
63  {
64  char *ptr;
65  FILE *fp;
66 
67  fp = fopen("test.txt","r");
68 
69  while((ptr=blFgetsany(fp))!=NULL)
70  {
71  printf("%s",ptr);
72  free(ptr);
73  }
74 
75  if(errno)
76  perror("SYSTEM ERROR");
77  }
78 
79 \endcode
80 
81 **************************************************************************
82 
83  Revision History:
84  =================
85 
86 - V1.0 11.09.95 Original By: ACRM
87 - V1.1 07.07.14 Use bl prefix for functions By: CTP
88 
89 *************************************************************************/
90 /* Doxygen
91  -------
92  #GROUP General Programming
93  #SUBGROUP File IO
94  #FUNCTION blFgetsany()
95  blFgetsany() provides a routine like fgets() for reading strings from
96  a file, but does not require you to impose a limit on the length of
97  string which may be read.
98 */
99 /************************************************************************/
100 /* Includes
101 */
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <errno.h>
105 
106 /************************************************************************/
107 /* Defines and macros
108 */
109 #define MAXBUFF 80 /* Memory allocated in chunks of this size */
110 
111 /************************************************************************/
112 /* Globals
113 */
114 
115 extern int errno;
116 
117 /************************************************************************/
118 /* Prototypes
119 */
120 
121 /************************************************************************/
122 /*>char *blFgetsany(FILE *fp)
123  --------------------------
124 *//**
125 
126  \param[in] *fp File pointer open for reading
127  \return Allocated string or NULL on EOF and
128  no memory (errno==ENOMEM)
129 
130  blFgetsany() provides a routine like fgets() for reading strings from
131  a file, but does not require you to impose a limit on the length of
132  string which may be read. blFgetsany() allocates memory to store a
133  string of any length and returns a pointer to that allocated memory.
134  After use, this memory must be freed by the calling routine.
135 
136  blFgetsany() returns NULL on end-of-file and if memory allocation
137  failed. It uses the global `errno' variable to indicate a memory
138  allocation failure (errno==ENOMEM).
139 
140 - 11.09.95 Original By: ACRM
141 - 07.07.14 Use bl prefix for functions By: CTP
142 */
143 char *blFgetsany(FILE *fp)
144 {
145  int bufflen = MAXBUFF,
146  buffpos = 0,
147  ch;
148  char *buffer = NULL;
149 
150  /* Get a character and test for end of file */
151  if((ch = fgetc(fp))==EOF)
152  return(NULL);
153 
154  /* If it wasn't EOF, put the character back in the input stream */
155  ungetc(ch, fp);
156 
157  /* OK, wasn't EOF, so allocate initial buffer space */
158  if((buffer = (char *)malloc(bufflen * sizeof(char)))==NULL)
159  {
160  errno = ENOMEM;
161  return(NULL);
162  }
163 
164  /* Read characters from file */
165  do
166  {
167  ch = fgetc(fp);
168 
169  /* If it's not end of file, store the character */
170  if(ch != EOF)
171  {
172  buffer[buffpos] = ch;
173 
174  /* Increment buffer position and test for overflow */
175  if(++buffpos == bufflen)
176  {
177  bufflen += MAXBUFF;
178 
179  /* Allocate additional space on overflow */
180  if((buffer = realloc(buffer, bufflen))==NULL)
181  {
182  errno = ENOMEM;
183  return(NULL);
184  }
185  }
186  }
187  } while((ch != EOF) && (ch != '\n'));
188 
189  /* Terminate the string */
190  buffer[buffpos] = '\0';
191 
192  /* If haven't used all the buffer, shrink it down to used size */
193  if(++buffpos < bufflen)
194  {
195  if((buffer = realloc(buffer, bufflen))==NULL)
196  {
197  errno = ENOMEM;
198  return(NULL);
199  }
200  }
201 
202  return(buffer);
203 }
#define MAXBUFF
Definition: fgetsany.c:109
#define NULL
Definition: array2.c:99
int errno
char * blFgetsany(FILE *fp)
Definition: fgetsany.c:143