It can sometimes be useful to write information temporarily to file, to read back later. These functions allow you to do so. The dump file importer would use this to save a postimage from delta application until the length is known and fast-import is ready to read it. Signed-off-by: Jonathan Nieder <jrnieder@xxxxxxxxx> --- vcs-svn/line_buffer.c | 24 ++++++++++++++++++++++++ vcs-svn/line_buffer.h | 7 ++++++- vcs-svn/line_buffer.txt | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletions(-) diff --git a/vcs-svn/line_buffer.c b/vcs-svn/line_buffer.c index c54031b..f583623 100644 --- a/vcs-svn/line_buffer.c +++ b/vcs-svn/line_buffer.c @@ -42,6 +42,30 @@ int buffer_at_eof(struct line_buffer *buf) return 0; } +int buffer_tmpfile_init(struct line_buffer *buf) +{ + buf->infile = tmpfile(); + if (!buf->infile) + return error("cannot open temporary file: %s", strerror(errno)); + return 0; +} + +FILE *buffer_tmpfile_rewind(struct line_buffer *buf) +{ + rewind(buf->infile); + return buf->infile; +} + +long buffer_tmpfile_prepare_to_read(struct line_buffer *buf) +{ + long pos = ftell(buf->infile); + if (pos < 0) + return error("ftell error: %s", strerror(errno)); + if (fseek(buf->infile, 0, SEEK_SET)) + return error("seek error: %s", strerror(errno)); + return pos; +} + int buffer_read_char(struct line_buffer *buf) { return fgetc(buf->infile); diff --git a/vcs-svn/line_buffer.h b/vcs-svn/line_buffer.h index 2375ee1..1db7434 100644 --- a/vcs-svn/line_buffer.h +++ b/vcs-svn/line_buffer.h @@ -16,12 +16,17 @@ int buffer_init(struct line_buffer *buf, const char *filename); int buffer_deinit(struct line_buffer *buf); int buffer_ferror(struct line_buffer *buf); int buffer_at_eof(struct line_buffer *buf); +void buffer_reset(struct line_buffer *buf); + +int buffer_tmpfile_init(struct line_buffer *buf); +FILE *buffer_tmpfile_rewind(struct line_buffer *buf); /* prepare to write. */ +long buffer_tmpfile_prepare_to_read(struct line_buffer *buf); + char *buffer_read_line(struct line_buffer *buf); char *buffer_read_string(struct line_buffer *buf, uint32_t len); int buffer_read_char(struct line_buffer *buf); void buffer_read_binary(struct strbuf *sb, uint32_t len, struct line_buffer *f); void buffer_copy_bytes(struct line_buffer *buf, off_t len); off_t buffer_skip_bytes(struct line_buffer *buf, off_t len); -void buffer_reset(struct line_buffer *buf); #endif diff --git a/vcs-svn/line_buffer.txt b/vcs-svn/line_buffer.txt index d06db24..344efc3 100644 --- a/vcs-svn/line_buffer.txt +++ b/vcs-svn/line_buffer.txt @@ -24,6 +24,28 @@ The calling program: When finished, the caller can use `buffer_reset` to deallocate resources. +Using temporary files +--------------------- + +Sometimes a file is just a convenient place to stash data for +later. A program + + - initializes a `struct line_buffer` to LINE_BUFFER_INIT + - allocates a temporary file with `buffer_tmpfile_init` + - acquires an output handle by calling `buffer_tmpfile_rewind` + - uses standard I/O functions like `fprintf` and `fwrite` to fill + the temporary file + - declares writing is over with `buffer_tmpfile_prepare_to_read` + - can re-read what was written with `buffer_read_line`, + `buffer_read_string`, and so on + - can reuse the temporary file by calling `buffer_tmpfile_rewind` + again + - cleans up the temporary file with `buffer_deinit`, perhaps to + reuse the line_buffer for some other file. + +When finished, the program should use `buffer_reset` to deallocate +resources. + Functions --------- -- 1.7.2.3 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html