diff options
author | rcombs <rcombs@rcombs.me> | 2024-01-28 14:12:55 -0800 |
---|---|---|
committer | rcombs <rcombs@rcombs.me> | 2024-02-11 17:01:07 -0800 |
commit | 98eeef44aad8e5bd09c4765db852d6155e790d72 (patch) | |
tree | d836dc49ad27cc6373cd6d77bdc56361a9bef021 /libavformat | |
parent | 3be80ce299d0073118ae42f5d99c14f912751d93 (diff) | |
download | ffmpeg-98eeef44aad8e5bd09c4765db852d6155e790d72.tar.gz |
lavf/avio_internal: add ffio_write_lines for line ending normalization
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/avio_internal.h | 11 | ||||
-rw-r--r-- | libavformat/aviobuf.c | 31 |
2 files changed, 42 insertions, 0 deletions
diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h index f2e4ff30cb..16cf6ce016 100644 --- a/libavformat/avio_internal.h +++ b/libavformat/avio_internal.h @@ -157,6 +157,17 @@ unsigned int ffio_read_leb(AVIOContext *s); void ffio_write_leb(AVIOContext *s, unsigned val); /** + * Write a sequence of text lines, converting line endings. + * All input line endings (LF, CRLF, CR) are converted to the configured line ending. + * @param s The AVIOContext to write to + * @param buf The buffer to write + * @param size The size of the buffer, or <0 to use the full length of a null-terminated string + * @param ending The line ending sequence to convert to, or NULL for \n + */ +void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size, + const unsigned char *ending); + +/** * Read size bytes from AVIOContext into buf. * Check that exactly size bytes have been read. * @return number of bytes read or AVERROR diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 5a329ce465..76780bc852 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1004,6 +1004,37 @@ void ffio_write_leb(AVIOContext *s, unsigned val) } } +void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size, + const unsigned char *ending) +{ + int ending_len = ending ? strlen(ending) : 1; + if (!ending) + ending = "\n"; + if (size < 0) + size = strlen(buf); + + while (size > 0) { + size_t len = 0; + char last = 0; + for (; len < size; len++) { + last = buf[len]; + if (last == '\r' || last == '\n') + break; + } + + avio_write(s, buf, len); + avio_write(s, ending, ending_len); + + buf += len + 1; + size -= len + 1; + + if (size > 0 && last == '\r' && buf[0] == '\n') { + buf++; + size--; + } + } +} + int ffio_fdopen(AVIOContext **s, URLContext *h) { uint8_t *buffer = NULL; |