<commit_before>/*
 *  Copyright (c) 2010 <NAME>
 *  Copyright (c) 2010 <NAME>
 *
 *  This is free software; see gpl-3.0.txt
 */
#include <sys/time.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

#include <chunk.h>

#include "input.h"
#include "input-stream.h"
#include "dbg.h"

struct input_desc {
  struct input_stream *s;
  int id;
  int interframe;
  uint64_t start_time;
  uint64_t first_ts;
};

struct input_desc *input_open(const char *fname, uint16_t flags)
{
  struct input_desc *res;
  struct timeval tv;

  res = malloc(sizeof(struct input_desc));
  if (res == NULL) {
    return NULL;
  }
  gettimeofday(&tv, NULL);
  res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
  res->first_ts = 0;
  res->s = input_stream_open(fname, &res->interframe, flags);
  if (res->s == NULL) {
    free(res);
    return NULL;
  }
  res->id = (res->start_time / res->interframe) % INT_MAX; //TODO: verify 32/64 bit

  return res;
}

void input_close(struct input_desc *s)
{
  input_stream_close(s->s);
  free(s);
}

int input_get(struct input_desc *s, struct chunk *c)
{
  struct timeval now;
  int64_t delta;

  c->data = chunkise(s->s, s->id, &c->size, &c->timestamp);
  if (c->size == -1) {
    return -1;
  }
  if (c->data) {
    c->id = s->id++;
  }
  c->attributes_size = 0;
  c->attributes = NULL;
  if (s->first_ts == 0) {
    s->first_ts = c->timestamp;
  }
  delta = c->timestamp - s->first_ts + s->interframe;
  gettimeofday(&now, NULL);
  delta = delta + s->start_time - now.tv_sec * 1000000ULL - now.tv_usec;
  dprintf("Delta: %lld\n", delta);
  dprintf("Generate Chunk[%d] (TS: %llu)\n", c->id, c->timestamp);

  c->timestamp = now.tv_sec * 1000000ULL + now.tv_usec;

  return delta > 0 ? delta : 0;
}
<commit_msg>fix return value of input_open<commit_after>/*
 *  Copyright (c) 2010 <NAME>
 *  Copyright (c) 2010 <NAME>
 *
 *  This is free software; see gpl-3.0.txt
 */
#include <sys/time.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

#include <chunk.h>

#include "input.h"
#include "input-stream.h"
#include "dbg.h"

struct input_desc {
  struct input_stream *s;
  int id;
  int interframe;
  uint64_t start_time;
  uint64_t first_ts;
};

struct input_desc *input_open(const char *fname, uint16_t flags)
{
  struct input_desc *res;
  struct timeval tv;

  res = malloc(sizeof(struct input_desc));
  if (res == NULL) {
    return NULL;
  }
  gettimeofday(&tv, NULL);
  res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
  res->first_ts = 0;
  res->s = input_stream_open(fname, &res->interframe, flags);
  if (res->s == NULL) {
    free(res);
    res = NULL;
    return res;
  }
  res->id = (res->start_time / res->interframe) % INT_MAX; //TODO: verify 32/64 bit

  return res;
}

void input_close(struct input_desc *s)
{
  input_stream_close(s->s);
  free(s);
}

int input_get(struct input_desc *s, struct chunk *c)
{
  struct timeval now;
  int64_t delta;

  c->data = chunkise(s->s, s->id, &c->size, &c->timestamp);
  if (c->size == -1) {
    return -1;
  }
  if (c->data) {
    c->id = s->id++;
  }
  c->attributes_size = 0;
  c->attributes = NULL;
  if (s->first_ts == 0) {
    s->first_ts = c->timestamp;
  }
  delta = c->timestamp - s->first_ts + s->interframe;
  gettimeofday(&now, NULL);
  delta = delta + s->start_time - now.tv_sec * 1000000ULL - now.tv_usec;
  dprintf("Delta: %lld\n", delta);
  dprintf("Generate Chunk[%d] (TS: %llu)\n", c->id, c->timestamp);

  c->timestamp = now.tv_sec * 1000000ULL + now.tv_usec;

  return delta > 0 ? delta : 0;
}

{'fraction_non_alphanumeric': 0.17158774373259053, 'fraction_numerical': 0.027855153203342618, 'mean_word_length': 3.358009708737864, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 83, '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': '5332436', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1446, 'n_words': 342}