<commit_before>#include "manager.h"

#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

typedef struct {
    Fd_Data_Cb cb;
    Fd_Data data;
} Fd_Register;

#define MAX_BUFFER_SIZE 2048

static int fd_number = 0;
static Fd_Register *cbs = NULL; //array of length fd_number

static int stop = 0;
static int changes = 0;
static int readout = 0;

void
manager_init(void) {

}

static void
_error_handling(result) {
    perror("manager failed, reason :");
}

static void
_read_available(Fd_Register *reg, int fd)
{
    //call callback!
    reg->cb(&reg->data, fd);
}

static void
_error_available(Fd_Register *reg, int fd)
{
   printf("Error on %d\n", reg->data.fd);
   manager_unregister_fd(reg->data.fd);
}

static void
_fd_set_check(Fd_Register *reg, fd_set *set, void (*handle)(Fd_Register *reg, int fd), int i)
{
   //check if set
   if (FD_ISSET(reg->data.fd, set)) {
      handle(reg, reg->data.fd);
   }
}

static void
_clear_objects(void)
{
   int result = 0;
   int mode = 0;
   for (int i = 0; i < fd_number; i++)
     {
        if (mode == 0 && !cbs[i].cb)
          {
             result = 1;
             mode = 1;
          }
        else if (mode == 1)
          {
             cbs[i - 1] = cbs[i];
          }
     }
   if (result)
     {
        fd_number --;
        cbs = realloc(cbs, fd_number*sizeof(Fd_Register));
     }
}

int
manager_run(void) {
    while (!stop) {
        int result;
        int max_fd = 0;
        struct timeval timeout;
        fd_set fds_read, fds_error;

        //null out the fd sets
        FD_ZERO(&fds_read);
        FD_ZERO(&fds_error);

        //init sets with new fds
        for(int i = 0; i < fd_number; i ++) {
            if (cbs[i].data.fd > max_fd) max_fd = cbs[i].data.fd;
            FD_SET(cbs[i].data.fd, &fds_read);
            FD_SET(cbs[i].data.fd, &fds_error);
        }
        //set timeout to one second
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;

        //read the fds
        result = select(max_fd + 1, &fds_read, NULL, &fds_error, &timeout);

        readout = 1;

        //check for error
        if (result == -1) {
            _error_handling(result);
            readout = 0;
            return 0;
        } else if (result >= 0) {
            //look for errors fd´s
            for(int i = 0; i < fd_number; i ++) {
               _fd_set_check(&cbs[i], &fds_error, _error_available, i);
            }

            //look for read fd´s
            for(int i = 0; i < fd_number; i ++) {
               _fd_set_check(&cbs[i], &fds_read, _read_available, i);
            }
       }
       readout = 0;

       //flush out the changes
       for(; changes > 0; changes --)
         {
            _clear_objects();
         }
    }
    return 1;
}

void
manager_stop(void) {<commit_msg>manager: ignore sigint<commit_after>#include "manager.h"

#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

typedef struct {
    Fd_Data_Cb cb;
    Fd_Data data;
} Fd_Register;

#define MAX_BUFFER_SIZE 2048

static int fd_number = 0;
static Fd_Register *cbs = NULL; //array of length fd_number

static int stop = 0;
static int changes = 0;
static int readout = 0;

void
manager_init(void) {

}

static void
_read_available(Fd_Register *reg, int fd)
{
    //call callback!
    reg->cb(&reg->data, fd);
}

static void
_error_available(Fd_Register *reg, int fd)
{
   printf("Error on %d\n", reg->data.fd);
   manager_unregister_fd(reg->data.fd);
}

static void
_fd_set_check(Fd_Register *reg, fd_set *set, void (*handle)(Fd_Register *reg, int fd), int i)
{
   //check if set
   if (FD_ISSET(reg->data.fd, set)) {
      handle(reg, reg->data.fd);
   }
}

static void
_clear_objects(void)
{
   int result = 0;
   int mode = 0;
   for (int i = 0; i < fd_number; i++)
     {
        if (mode == 0 && !cbs[i].cb)
          {
             result = 1;
             mode = 1;
          }
        else if (mode == 1)
          {
             cbs[i - 1] = cbs[i];
          }
     }
   if (result)
     {
        fd_number --;
        cbs = realloc(cbs, fd_number*sizeof(Fd_Register));
     }
}

static int
_errno_ignore(void) {
   if (errno == EINTR) return 1;
   return 0;
}

int
manager_run(void) {
    while (!stop) {
        int result;
        int max_fd = 0;
        struct timeval timeout;
        fd_set fds_read, fds_error;

        //null out the fd sets
        FD_ZERO(&fds_read);
        FD_ZERO(&fds_error);

        //init sets with new fds
        for(int i = 0; i < fd_number; i ++) {
            if (cbs[i].data.fd > max_fd) max_fd = cbs[i].data.fd;
            FD_SET(cbs[i].data.fd, &fds_read);
            FD_SET(cbs[i].data.fd, &fds_error);
        }
        //set timeout to one second
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;

        //read the fds
        result = select(max_fd + 1, &fds_read, NULL, &fds_error, &timeout);

        readout = 1;

        //check for error
        if (result == -1 && !_errno_ignore()) {
            perror("Manager failed, reason");
            readout = 0;
            return 0;
        } else if (result >= 0) {
            //look for errors fd´s
            for(int i = 0; i < fd_number; i ++) {
               _fd_set_check(&cbs[i], &fds_error, _error_available, i);
            }

            //look for read fd´s
            for(int i = 0; i < fd_number; i ++) {
               _fd_set_check(&cbs[i], &fds_read, _read_available, i);
            }
       }
       readout = 0;

       //flush out the changes
       for(; changes > 0; changes --)
         {
            _clear_objects();
         }
    }
    return 1;
}

void
manager_stop(void) {
{'fraction_non_alphanumeric': 0.13461203138622493, 'fraction_numerical': 0.011159546643417612, 'mean_word_length': 1.8608478802992519, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 33, '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': '74167', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2075, 'n_words': 521}