Fixed some more issues with streams (stdin/pipe based)
substr didn't work properly, nor did seek. Just use the same principle as stream_next
This commit is contained in:
26
stream.c
26
stream.c
@@ -188,19 +188,19 @@ bool stream_seek(stream_t *stream, i64 offset)
|
|||||||
|
|
||||||
bool stream_seek_forward(stream_t *stream, u64 offset)
|
bool stream_seek_forward(stream_t *stream, u64 offset)
|
||||||
{
|
{
|
||||||
if (stream_eos(stream))
|
if (stream_eos(stream) && stream_eoc(stream))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (stream->type)
|
switch (stream->type)
|
||||||
{
|
{
|
||||||
case STREAM_TYPE_STRING:
|
case STREAM_TYPE_STRING:
|
||||||
{
|
{
|
||||||
if (stream->position + offset < stream->string.size)
|
if (stream->position + offset >= stream->string.size)
|
||||||
{
|
return false;
|
||||||
|
|
||||||
stream->position += offset;
|
stream->position += offset;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
case STREAM_TYPE_FILE:
|
case STREAM_TYPE_FILE:
|
||||||
{
|
{
|
||||||
// Similar principle as stream_peek really...
|
// Similar principle as stream_peek really...
|
||||||
@@ -219,7 +219,7 @@ bool stream_seek_forward(stream_t *stream, u64 offset)
|
|||||||
read_chunk = stream_chunk(stream))
|
read_chunk = stream_chunk(stream))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Same principle as the stream_eos(stream) check.
|
// Same principle as the stream_eoc(stream) check.
|
||||||
if (stream->position + offset >= stream->pipe.cache.size)
|
if (stream->position + offset >= stream->pipe.cache.size)
|
||||||
return false;
|
return false;
|
||||||
stream->position += offset;
|
stream->position += offset;
|
||||||
@@ -242,12 +242,15 @@ bool stream_seek_backward(stream_t *stream, u64 offset)
|
|||||||
|
|
||||||
sv_t stream_substr(stream_t *stream, u64 size)
|
sv_t stream_substr(stream_t *stream, u64 size)
|
||||||
{
|
{
|
||||||
if (stream_eos(stream))
|
if (stream_eos(stream) && stream_eoc(stream))
|
||||||
return SV(NULL, 0);
|
return SV(NULL, 0);
|
||||||
|
|
||||||
|
// TODO: this is kinda disgusting, any better way of doing this
|
||||||
u64 current_position = stream->position;
|
u64 current_position = stream->position;
|
||||||
bool successful = stream_seek_forward(stream, size);
|
bool successful = stream_seek_forward(stream, size);
|
||||||
// In case we did happen to move forward
|
// Reset the position in either situation
|
||||||
stream->position = current_position;
|
stream->position = current_position;
|
||||||
|
|
||||||
if (!successful)
|
if (!successful)
|
||||||
return SV(NULL, 0);
|
return SV(NULL, 0);
|
||||||
|
|
||||||
@@ -270,16 +273,15 @@ sv_t stream_substr(stream_t *stream, u64 size)
|
|||||||
|
|
||||||
sv_t stream_substr_abs(stream_t *stream, u64 index, u64 size)
|
sv_t stream_substr_abs(stream_t *stream, u64 index, u64 size)
|
||||||
{
|
{
|
||||||
|
|
||||||
switch (stream->type)
|
switch (stream->type)
|
||||||
{
|
{
|
||||||
case STREAM_TYPE_STRING:
|
case STREAM_TYPE_STRING:
|
||||||
if (index + size < stream_size(stream))
|
if (index + size <= stream_size(stream))
|
||||||
return SV(stream->string.data + index, size);
|
return SV(stream->string.data + index, size);
|
||||||
return SV(NULL, 0);
|
return SV(NULL, 0);
|
||||||
case STREAM_TYPE_FILE:
|
case STREAM_TYPE_FILE:
|
||||||
{
|
{
|
||||||
if (index + size < stream_size(stream))
|
if (index + size <= stream_size(stream))
|
||||||
return SV(vec_data(&stream->pipe.cache) + index, size);
|
return SV(vec_data(&stream->pipe.cache) + index, size);
|
||||||
// stream_size(stream) <= index + size => try reading chunks
|
// stream_size(stream) <= index + size => try reading chunks
|
||||||
for (bool read_chunk = stream_chunk(stream);
|
for (bool read_chunk = stream_chunk(stream);
|
||||||
@@ -287,7 +289,7 @@ sv_t stream_substr_abs(stream_t *stream, u64 index, u64 size)
|
|||||||
read_chunk = stream_chunk(stream))
|
read_chunk = stream_chunk(stream))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (index + size >= stream_size(stream))
|
if (index + size > stream_size(stream))
|
||||||
return SV(NULL, 0);
|
return SV(NULL, 0);
|
||||||
return SV(vec_data(&stream->pipe.cache) + index, size);
|
return SV(vec_data(&stream->pipe.cache) + index, size);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user