<commit_before>/*
 * AVSystem Commons Library
 *
 * Copyright (C) 2016 AVSystem <http://www.avsystem.com/>
 *
 * This code is free and open source software licensed under the MIT License.
 * See the LICENSE file for details.
 */

#ifdef AVS_UNIT_TESTING
#define _BSD_SOURCE /* for mkstemp */
#endif

#include <config.h>

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>

#include <limits.h>

#include <avsystem/commons/stream/stream_file.h>
#include <avsystem/commons/stream_v_table.h>

#define MODULE_NAME avs_stream
#include <x_log_config.h>

#ifdef HAVE_VISIBILITY
#pragma GCC visibility push(hidden)
#endif

struct avs_file_stream_struct {
    const void *const vtable;
    uint8_t mode;
    int error_code;
    FILE *fp;
};

int avs_stream_file_length(avs_stream_abstract_t *stream,
                           avs_off_t *out_length) {
    const avs_stream_v_table_extension_file_t *ext =
            (const avs_stream_v_table_extension_file_t *)
            avs_stream_v_table_find_extension(stream,
                                              AVS_STREAM_V_TABLE_EXTENSION_FILE);
    if (ext) {
        return ext->length(stream, out_length);
    }
    return -1;
}

int avs_stream_file_offset(avs_stream_abstract_t *stream,
                           avs_off_t *out_offset) {
    const avs_stream_v_table_extension_file_t *ext =
            (const avs_stream_v_table_extension_file_t *)
            avs_stream_v_table_find_extension(stream,
                                              AVS_STREAM_V_TABLE_EXTENSION_FILE);
    if (ext) {
        return ext->offset(stream, out_offset);
    }
    return -1;
}

int avs_stream_file_seek(avs_stream_abstract_t *stream,
                         avs_off_t offset_from_start) {
    const avs_stream_v_table_extension_file_t *ext =
            (const avs_stream_v_table_extension_file_t *)
            avs_stream_v_table_find_extension(stream,
                                              AVS_STREAM_V_TABLE_EXTENSION_FILE);
    if (ext) {
        return ext->seek(stream, offset_from_start);
    }
    return -1;
}

static int stream_file_write(avs_stream_abstract_t *stream_,
                             const void *buffer,
                             size_t buffer_length) {
    avs_stream_file_t *file = (avs_stream_file_t *) stream_;
    size_t written;
    if ((file->mode & AVS_STREAM_FILE_WRITE) == 0) {
        file->error_code = EBADF;
        return -1;
    }
    written = fwrite(buffer, 1, buffer_length, file->fp);
    if (ferror(file->fp) || written != buffer_length) {
        file->error_code = EIO;
        return -1;
    }
    file->error_code = 0;
    return 0;
}

static int stream_file_read(avs_stream_abstract_t *stream_,
                            size_t *out_bytes_read,
                            char *out_message_finished,
                            void *buffer,
                            size_t buffer_length) {
    avs_stream_file_t *file = (avs_stream_file_t *) stream_;
    if ((file->mode & AVS_STREAM_FILE_READ) == 0) {
        file->error_code = EBADF;
        return -1;
    }
    *out_bytes_read = fread(buffer, 1, buffer_length, file->fp);
    if (ferror(file->fp)) {
        file->error_code = EIO;
        return -1;
    } else if (feof(file->fp)) {
        *out_message_finished = 1;
    } else {
        *out_message_finished = 0;
    }
    file->error_code = 0;
    return 0;<commit_msg>Make stream_file_read accept NULL out_message_finished<commit_after>/*
 * AVSystem Commons Library
 *
 * Copyright (C) 2016 AVSystem <http://www.avsystem.com/>
 *
 * This code is free and open source software licensed under the MIT License.
 * See the LICENSE file for details.
 */

#ifdef AVS_UNIT_TESTING
#define _BSD_SOURCE /* for mkstemp */
#endif

#include <config.h>

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>

#include <limits.h>

#include <avsystem/commons/stream/stream_file.h>
#include <avsystem/commons/stream_v_table.h>

#define MODULE_NAME avs_stream
#include <x_log_config.h>

#ifdef HAVE_VISIBILITY
#pragma GCC visibility push(hidden)
#endif

struct avs_file_stream_struct {
    const void *const vtable;
    uint8_t mode;
    int error_code;
    FILE *fp;
};

int avs_stream_file_length(avs_stream_abstract_t *stream,
                           avs_off_t *out_length) {
    const avs_stream_v_table_extension_file_t *ext =
            (const avs_stream_v_table_extension_file_t *)
            avs_stream_v_table_find_extension(stream,
                                              AVS_STREAM_V_TABLE_EXTENSION_FILE);
    if (ext) {
        return ext->length(stream, out_length);
    }
    return -1;
}

int avs_stream_file_offset(avs_stream_abstract_t *stream,
                           avs_off_t *out_offset) {
    const avs_stream_v_table_extension_file_t *ext =
            (const avs_stream_v_table_extension_file_t *)
            avs_stream_v_table_find_extension(stream,
                                              AVS_STREAM_V_TABLE_EXTENSION_FILE);
    if (ext) {
        return ext->offset(stream, out_offset);
    }
    return -1;
}

int avs_stream_file_seek(avs_stream_abstract_t *stream,
                         avs_off_t offset_from_start) {
    const avs_stream_v_table_extension_file_t *ext =
            (const avs_stream_v_table_extension_file_t *)
            avs_stream_v_table_find_extension(stream,
                                              AVS_STREAM_V_TABLE_EXTENSION_FILE);
    if (ext) {
        return ext->seek(stream, offset_from_start);
    }
    return -1;
}

static int stream_file_write(avs_stream_abstract_t *stream_,
                             const void *buffer,
                             size_t buffer_length) {
    avs_stream_file_t *file = (avs_stream_file_t *) stream_;
    size_t written;
    if ((file->mode & AVS_STREAM_FILE_WRITE) == 0) {
        file->error_code = EBADF;
        return -1;
    }
    written = fwrite(buffer, 1, buffer_length, file->fp);
    if (ferror(file->fp) || written != buffer_length) {
        file->error_code = EIO;
        return -1;
    }
    file->error_code = 0;
    return 0;
}

static int stream_file_read(avs_stream_abstract_t *stream_,
                            size_t *out_bytes_read,
                            char *out_message_finished,
                            void *buffer,
                            size_t buffer_length) {
    avs_stream_file_t *file = (avs_stream_file_t *) stream_;
    if ((file->mode & AVS_STREAM_FILE_READ) == 0) {
        file->error_code = EBADF;
        return -1;
    }
    *out_bytes_read = fread(buffer, 1, buffer_length, file->fp);
    if (ferror(file->fp)) {
        file->error_code = EIO;
        return -1;
    }
    if (out_message_finished) {
        *out_message_finished = !!feof(file->fp);
    }
    file->error_code = 0;
    return 0;
{'fraction_non_alphanumeric': 0.0912009338975631, 'fraction_numerical': 0.006128702757916241, 'mean_word_length': 2.270038167938931, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '629489', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2247, 'n_words': 437}