PostgreSQL Source Code  git master
pg_get_line.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_get_line.c
4  * fgets() with an expansible result buffer
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/common/pg_get_line.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #ifndef FRONTEND
16 #include "postgres.h"
17 #else
18 #include "postgres_fe.h"
19 #endif
20 
21 #include "common/string.h"
22 #include "lib/stringinfo.h"
23 
24 
25 /*
26  * pg_get_line()
27  *
28  * This is meant to be equivalent to fgets(), except that instead of
29  * reading into a caller-supplied, fixed-size buffer, it reads into
30  * a palloc'd (in frontend, really malloc'd) string, which is resized
31  * as needed to handle indefinitely long input lines. The caller is
32  * responsible for pfree'ing the result string when appropriate.
33  *
34  * As with fgets(), returns NULL if there is a read error or if no
35  * characters are available before EOF. The caller can distinguish
36  * these cases by checking ferror(stream).
37  *
38  * Since this is meant to be equivalent to fgets(), the trailing newline
39  * (if any) is not stripped. Callers may wish to apply pg_strip_crlf().
40  *
41  * Note that while I/O errors are reflected back to the caller to be
42  * dealt with, an OOM condition for the palloc'd buffer will not be;
43  * there'll be an ereport(ERROR) or exit(1) inside stringinfo.c.
44  *
45  * Also note that the palloc'd buffer is usually a lot longer than
46  * strictly necessary, so it may be inadvisable to use this function
47  * to collect lots of long-lived data. A less memory-hungry option
48  * is to use pg_get_line_buf() or pg_get_line_append() in a loop,
49  * then pstrdup() each line.
50  */
51 char *
52 pg_get_line(FILE *stream)
53 {
55 
56  initStringInfo(&buf);
57 
58  if (!pg_get_line_append(stream, &buf))
59  {
60  /* ensure that free() doesn't mess up errno */
61  int save_errno = errno;
62 
63  pfree(buf.data);
64  errno = save_errno;
65  return NULL;
66  }
67 
68  return buf.data;
69 }
70 
71 /*
72  * pg_get_line_buf()
73  *
74  * This has similar behavior to pg_get_line(), and thence to fgets(),
75  * except that the collected data is returned in a caller-supplied
76  * StringInfo buffer. This is a convenient API for code that just
77  * wants to read and process one line at a time, without any artificial
78  * limit on line length.
79  *
80  * Returns true if a line was successfully collected (including the
81  * case of a non-newline-terminated line at EOF). Returns false if
82  * there was an I/O error or no data was available before EOF.
83  * (Check ferror(stream) to distinguish these cases.)
84  *
85  * In the false-result case, buf is reset to empty.
86  */
87 bool
89 {
90  /* We just need to drop any data from the previous call */
91  resetStringInfo(buf);
92  return pg_get_line_append(stream, buf);
93 }
94 
95 /*
96  * pg_get_line_append()
97  *
98  * This has similar behavior to pg_get_line(), and thence to fgets(),
99  * except that the collected data is appended to whatever is in *buf.
100  * This is useful in preference to pg_get_line_buf() if the caller wants
101  * to merge some lines together, e.g. to implement backslash continuation.
102  *
103  * Returns true if a line was successfully collected (including the
104  * case of a non-newline-terminated line at EOF). Returns false if
105  * there was an I/O error or no data was available before EOF.
106  * (Check ferror(stream) to distinguish these cases.)
107  *
108  * In the false-result case, the contents of *buf are logically unmodified,
109  * though it's possible that the buffer has been resized.
110  */
111 bool
113 {
114  int orig_len = buf->len;
115 
116  /* Read some data, appending it to whatever we already have */
117  while (fgets(buf->data + buf->len, buf->maxlen - buf->len, stream) != NULL)
118  {
119  buf->len += strlen(buf->data + buf->len);
120 
121  /* Done if we have collected a newline */
122  if (buf->len > orig_len && buf->data[buf->len - 1] == '\n')
123  return true;
124 
125  /* Make some more room in the buffer, and loop to read more data */
126  enlargeStringInfo(buf, 128);
127  }
128 
129  /* Check for I/O errors and EOF */
130  if (ferror(stream) || buf->len == orig_len)
131  {
132  /* Discard any data we collected before detecting error */
133  buf->len = orig_len;
134  buf->data[orig_len] = '\0';
135  return false;
136  }
137 
138  /* No newline at EOF, but we did collect some data */
139  return true;
140 }
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:88
void pfree(void *pointer)
Definition: mcxt.c:1057
static char * buf
Definition: pg_test_fsync.c:68
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool pg_get_line_append(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:112
char * pg_get_line(FILE *stream)
Definition: pg_get_line.c:52