result = (char **) pg_malloc(maxlines * sizeof(char *));
n = 0;
- while (pg_get_line_append(infile, &line))
+ while (pg_get_line_buf(infile, &line))
{
/* make sure there will be room for a trailing NULL pointer */
if (n >= maxlines - 1)
}
result[n++] = pg_strdup(line.data);
-
- resetStringInfo(&line);
}
result[n] = NULL;
* Also note that the palloc'd buffer is usually a lot longer than
* strictly necessary, so it may be inadvisable to use this function
* to collect lots of long-lived data. A less memory-hungry option
- * is to use pg_get_line_append() in a loop, then pstrdup() each line.
+ * is to use pg_get_line_buf() or pg_get_line_append() in a loop,
+ * then pstrdup() each line.
*/
char *
pg_get_line(FILE *stream)
return buf.data;
}
+/*
+ * pg_get_line_buf()
+ *
+ * This has similar behavior to pg_get_line(), and thence to fgets(),
+ * except that the collected data is returned in a caller-supplied
+ * StringInfo buffer. This is a convenient API for code that just
+ * wants to read and process one line at a time, without any artificial
+ * limit on line length.
+ *
+ * Returns true if a line was successfully collected (including the
+ * case of a non-newline-terminated line at EOF). Returns false if
+ * there was an I/O error or no data was available before EOF.
+ * (Check ferror(stream) to distinguish these cases.)
+ *
+ * In the false-result case, buf is reset to empty.
+ */
+bool
+pg_get_line_buf(FILE *stream, StringInfo buf)
+{
+ /* We just need to drop any data from the previous call */
+ resetStringInfo(buf);
+ return pg_get_line_append(stream, buf);
+}
+
/*
* pg_get_line_append()
*
* This has similar behavior to pg_get_line(), and thence to fgets(),
* except that the collected data is appended to whatever is in *buf.
+ * This is useful in preference to pg_get_line_buf() if the caller wants
+ * to merge some lines together, e.g. to implement backslash continuation.
*
* Returns true if a line was successfully collected (including the
* case of a non-newline-terminated line at EOF). Returns false if
/* functions in src/common/pg_get_line.c */
extern char *pg_get_line(FILE *stream);
+extern bool pg_get_line_buf(FILE *stream, struct StringInfoData *buf);
extern bool pg_get_line_append(FILE *stream, struct StringInfoData *buf);
/* functions in src/common/sprompt.c */
initStringInfo(&linebuf);
- while (pg_get_line_append(s, &linebuf))
+ while (pg_get_line_buf(s, &linebuf))
{
/* check for "#line " in the beginning */
if (strstr(linebuf.data, "#line ") == linebuf.data)
}
}
fputs(linebuf.data, t);
- resetStringInfo(&linebuf);
}
pfree(linebuf.data);
initStringInfo(&line);
- while (pg_get_line_append(infile, &line))
+ while (pg_get_line_buf(infile, &line))
{
replace_string(&line, "@abs_srcdir@", inputdir);
replace_string(&line, "@abs_builddir@", outputdir);
replace_string(&line, "@libdir@", dlpath);
replace_string(&line, "@DLSUFFIX@", DLSUFFIX);
fputs(line.data, outfile);
- resetStringInfo(&line);
}
pfree(line.data);