Пример #1
0
/* Gets the specified number of characters from the stream. */
MVMString * MVM_io_syncstream_read_chars(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 chars) {
    MVMIOSyncStreamData *data = (MVMIOSyncStreamData *)h->body.data;
    MVMString *result;
    ensure_decode_stream(tc, data);

    /* Do we already have the chars available? */
    result = MVM_string_decodestream_get_chars(tc, data->ds, chars);
    if (result) {
        return result;
    }
    else {
        /* No; read and try again. */
        read_to_buffer(tc, data, CHUNK_SIZE);
        result = MVM_string_decodestream_get_chars(tc, data->ds, chars);
        if (result != NULL)
            return result;
    }

    /* Fetched all we immediately can, so just take what we have. */
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Пример #2
0
/* Gets the specified number of characters from the file. */
static MVMString * read_chars(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 chars) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    ensure_decode_stream(tc, data);

    /* Pull data until we can read the chars we want. */
    do {
        MVMString *result = MVM_string_decodestream_get_chars(tc, data->ds, chars);
        if (result != NULL)
            return result;
    } while (read_to_buffer(tc, data, CHUNK_SIZE) > 0);

    /* Reached end of file, so just take what we have. */
    return MVM_string_decodestream_get_all(tc, data->ds);
}