<commit_before>#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

#include "stream.h"
#include "params.h"
#include "ringbuffer.h"

/* #define DEBUG */

#define CONTENT_TYPE "video/mp4"

static void *
stream_writer (struct stream * stream)
{
        size_t available;
        ssize_t n;
        fd_set rfds;
        struct timeval tv;
        int retval;

        stream->active = 1;

        while (stream->active) {
                FD_ZERO (&rfds);
                FD_SET (stream->input_fd, &rfds);

                tv.tv_sec = 5;
                tv.tv_usec = 0;
                retval = select (1, &rfds, NULL, NULL, &tv);
                if (retval == -1)
                        perror ("select");
                else if (retval) {
                        n = ringbuffer_readfd (stream->input_fd, &stream->rb);
#ifdef DEBUG
                        if (n!=0) printf ("stream_writer: read %ld bytes\n", n);
#endif
                }
        }

        return NULL;
}

struct stream *
stream_open (int fd)
{
        struct stream * stream;
	pthread_t child;
        unsigned char * data;
        size_t len = 4096*16*32;

        if ((stream = malloc (sizeof(*stream))) == NULL)
                return NULL;

        if ((data = malloc (len)) == NULL) {
                free (stream);
                return NULL;
        }

        ringbuffer_init (&stream->rb, data, len);
	pthread_create(&child, 0, stream_writer, stream);
	pthread_detach(child);

        return stream;
}

void
stream_close (struct stream * stream)
{
        stream->active = 0;
        free (stream->rb.data);

        free (stream);
}

params_t *
stream_append_headers (params_t * response_headers, struct stream * stream)<commit_msg>set input fd in stream_open()<commit_after>#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

#include "stream.h"
#include "params.h"
#include "ringbuffer.h"

/* #define DEBUG */

#define CONTENT_TYPE "video/mp4"

static void *
stream_writer (struct stream * stream)
{
        size_t available;
        ssize_t n;
        fd_set rfds;
        struct timeval tv;
        int retval;

        stream->active = 1;

        while (stream->active) {
                FD_ZERO (&rfds);
                FD_SET (stream->input_fd, &rfds);

                tv.tv_sec = 5;
                tv.tv_usec = 0;
                retval = select (1, &rfds, NULL, NULL, &tv);
                if (retval == -1)
                        perror ("select");
                else if (retval) {
                        n = ringbuffer_readfd (stream->input_fd, &stream->rb);
#ifdef DEBUG
                        if (n!=0) printf ("stream_writer: read %ld bytes\n", n);
#endif
                }
        }

        return NULL;
}

struct stream *
stream_open (int fd)
{
        struct stream * stream;
	pthread_t child;
        unsigned char * data;
        size_t len = 4096*16*32;

        if ((stream = malloc (sizeof(*stream))) == NULL)
                return NULL;

        if ((data = malloc (len)) == NULL) {
                free (stream);
                return NULL;
        }

	stream->input_fd = fd;

        ringbuffer_init (&stream->rb, data, len);
	pthread_create(&child, 0, stream_writer, stream);
	pthread_detach(child);

        return stream;
}

void
stream_close (struct stream * stream)
{
        stream->active = 0;
        free (stream->rb.data);

        free (stream);
}

params_t *
stream_append_headers (params_t * response_headers, struct stream * stream)
{'fraction_non_alphanumeric': 0.12565734846388044, 'fraction_numerical': 0.00941046221976197, 'mean_word_length': 1.8212334113973458, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4770812', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1116, 'n_words': 297}