PostgreSQL Source Code  git master
pg_get_line.c File Reference
#include "postgres.h"
#include "common/string.h"
#include "lib/stringinfo.h"
Include dependency graph for pg_get_line.c:

Go to the source code of this file.

Functions

char * pg_get_line (FILE *stream)
 
bool pg_get_line_buf (FILE *stream, StringInfo buf)
 
bool pg_get_line_append (FILE *stream, StringInfo buf)
 

Function Documentation

◆ pg_get_line()

char* pg_get_line ( FILE *  stream)

Definition at line 52 of file pg_get_line.c.

References buf, StringInfoData::data, initStringInfo(), pfree(), and pg_get_line_append().

Referenced by get_su_pwd(), and simple_prompt().

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 }
void pfree(void *pointer)
Definition: mcxt.c:1057
static char * buf
Definition: pg_test_fsync.c:68
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool pg_get_line_append(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:112

◆ pg_get_line_append()

bool pg_get_line_append ( FILE *  stream,
StringInfo  buf 
)

Definition at line 112 of file pg_get_line.c.

References StringInfoData::data, enlargeStringInfo(), StringInfoData::len, and StringInfoData::maxlen.

Referenced by pg_get_line(), pg_get_line_buf(), and tokenize_file().

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 }
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283

◆ pg_get_line_buf()

bool pg_get_line_buf ( FILE *  stream,
StringInfo  buf 
)

Definition at line 88 of file pg_get_line.c.

References pg_get_line_append(), and resetStringInfo().

Referenced by convert_sourcefiles_in(), ecpg_filter(), parseServiceFile(), readfile(), SortTocFromFile(), and tsearch_readline().

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 }
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
bool pg_get_line_append(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:112