37ec56e5be46e88bcd9a2f41d70170a642297b9f
   1/*
   2 * Licensed under a two-clause BSD-style license.
   3 * See LICENSE for details.
   4 */
   5
   6#include "git-compat-util.h"
   7#include "line_buffer.h"
   8#include "strbuf.h"
   9
  10#define COPY_BUFFER_LEN 4096
  11
  12int buffer_init(struct line_buffer *buf, const char *filename)
  13{
  14        buf->infile = filename ? fopen(filename, "r") : stdin;
  15        if (!buf->infile)
  16                return -1;
  17        return 0;
  18}
  19
  20int buffer_deinit(struct line_buffer *buf)
  21{
  22        int err;
  23        if (buf->infile == stdin)
  24                return ferror(buf->infile);
  25        err = ferror(buf->infile);
  26        err |= fclose(buf->infile);
  27        return err;
  28}
  29
  30int buffer_read_char(struct line_buffer *buf)
  31{
  32        return fgetc(buf->infile);
  33}
  34
  35/* Read a line without trailing newline. */
  36char *buffer_read_line(struct line_buffer *buf)
  37{
  38        char *end;
  39        if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
  40                /* Error or data exhausted. */
  41                return NULL;
  42        end = buf->line_buffer + strlen(buf->line_buffer);
  43        if (end[-1] == '\n')
  44                end[-1] = '\0';
  45        else if (feof(buf->infile))
  46                ; /* No newline at end of file.  That's fine. */
  47        else
  48                /*
  49                 * Line was too long.
  50                 * There is probably a saner way to deal with this,
  51                 * but for now let's return an error.
  52                 */
  53                return NULL;
  54        return buf->line_buffer;
  55}
  56
  57char *buffer_read_string(struct line_buffer *buf, uint32_t len)
  58{
  59        strbuf_reset(&buf->blob_buffer);
  60        strbuf_fread(&buf->blob_buffer, len, buf->infile);
  61        return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
  62}
  63
  64void buffer_read_binary(struct line_buffer *buf,
  65                                struct strbuf *sb, uint32_t size)
  66{
  67        strbuf_fread(sb, size, buf->infile);
  68}
  69
  70void buffer_copy_bytes(struct line_buffer *buf, uint32_t len)
  71{
  72        char byte_buffer[COPY_BUFFER_LEN];
  73        uint32_t in;
  74        while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
  75                in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
  76                in = fread(byte_buffer, 1, in, buf->infile);
  77                len -= in;
  78                fwrite(byte_buffer, 1, in, stdout);
  79                if (ferror(stdout)) {
  80                        buffer_skip_bytes(buf, len);
  81                        return;
  82                }
  83        }
  84}
  85
  86void buffer_skip_bytes(struct line_buffer *buf, uint32_t len)
  87{
  88        char byte_buffer[COPY_BUFFER_LEN];
  89        uint32_t in;
  90        while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
  91                in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
  92                in = fread(byte_buffer, 1, in, buf->infile);
  93                len -= in;
  94        }
  95}
  96
  97void buffer_reset(struct line_buffer *buf)
  98{
  99        strbuf_release(&buf->blob_buffer);
 100}