<commit_before>/*
    This file is a part of saldl.

    Copyright (C) 2014-2015 <NAME> <CE.Mohammad.AlSaleh <EMAIL>>
    https://saldl.github.io

    saldl is free software: you can redistribute it and/or modify
    it under the terms of the Affero GNU General Public License as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Affero GNU General Public License for more details.

    You should have received a copy of the Affero GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "events.h"
#include "ctrl.h"

void ctrl_cleanup_info(ctrl_info_s *ctrl) {
  if (ctrl->chunks_progress_str) {
    free(ctrl->chunks_progress_str);
  }
  ctrl = NULL;
}

int ctrl_get_info(char *ctrl_filename, ctrl_info_s *ctrl) {

  if (access(ctrl_filename,F_OK)) {
    warn_msg(FN, "%s does not exist.\n", ctrl_filename);
    return 1;
  }

  FILE *f_ctrl = fopen(ctrl_filename, "rb");

  if (! fsize(f_ctrl) ) { /* ctrl file is empty */
    fatal(FN, "ctrl file is empty.\n");
  } else {
    char *p;
    char ctrl_file_size_str[s_num_digits(OFF_T_MAX)];
    char ctrl_chunk_size_str[u_num_digits(SIZE_MAX)];
    char ctrl_rem_size_str[u_num_digits(SIZE_MAX)];

    ctrl->chunks_progress_str = saldl_calloc( fsize(f_ctrl), sizeof(char) ); /* fsize() guarantees allocating enough bytes */

    char *s1 = fgets(ctrl_file_size_str, s_num_digits(OFF_T_MAX), f_ctrl);
    char *s2 = fgets(ctrl_chunk_size_str, u_num_digits(SIZE_MAX), f_ctrl);
    char *s3 = fgets(ctrl_rem_size_str, u_num_digits(SIZE_MAX), f_ctrl);
    char *s4 = fgets(ctrl->chunks_progress_str, fsize(f_ctrl), f_ctrl);

    if (!s1 || !s2 || !s3 || !s4) {
      fatal(FN, "Reading the ctrl file failed. Are you sure it's not corrupt!\n");
    }



    if ( fgetc(f_ctrl) != (char)EOF ) {
      fatal(FN, "ctrl file should have ended here.\n");
    }

    if (! ( strchr(ctrl_file_size_str, '\n') && strchr(ctrl_chunk_size_str, '\n') && strchr(ctrl_rem_size_str, '\n') && strchr(ctrl->chunks_progress_str, '\n') ) ) {
      fatal(FN, "Parsing ctrl file failed.\n");
    }

    /* Needed for strtoimax()/strtoumax() in parse_num_o()/parse_num_z()  */
    p = strchr(ctrl_file_size_str, '\n');
    *p = '\0';
    p = strchr(ctrl_chunk_size_str, '\n');
    *p = '\0';
    p = strchr(ctrl_rem_size_str, '\n');
    *p = '\0';<commit_msg>Use more meaningful var names<commit_after>/*
    This file is a part of saldl.

    Copyright (C) 2014-2015 <NAME> <CE.Mohammad.AlSaleh at gmail.com>
    https://saldl.github.io

    saldl is free software: you can redistribute it and/or modify
    it under the terms of the Affero GNU General Public License as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Affero GNU General Public License for more details.

    You should have received a copy of the Affero GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "events.h"
#include "ctrl.h"

void ctrl_cleanup_info(ctrl_info_s *ctrl) {
  if (ctrl->chunks_progress_str) {
    free(ctrl->chunks_progress_str);
  }
  ctrl = NULL;
}

int ctrl_get_info(char *ctrl_filename, ctrl_info_s *ctrl) {

  if (access(ctrl_filename,F_OK)) {
    warn_msg(FN, "%s does not exist.\n", ctrl_filename);
    return 1;
  }

  FILE *f_ctrl = fopen(ctrl_filename, "rb");

  if (! fsize(f_ctrl) ) { /* ctrl file is empty */
    fatal(FN, "ctrl file is empty.\n");
  } else {
    char *p;
    char ctrl_file_size_str[s_num_digits(OFF_T_MAX)];
    char ctrl_chunk_size_str[u_num_digits(SIZE_MAX)];
    char ctrl_rem_size_str[u_num_digits(SIZE_MAX)];

    ctrl->chunks_progress_str = saldl_calloc( fsize(f_ctrl), sizeof(char) ); /* fsize() guarantees allocating enough bytes */

    char *ret_fgets1 = fgets(ctrl_file_size_str, s_num_digits(OFF_T_MAX), f_ctrl);
    char *ret_fgets2 = fgets(ctrl_chunk_size_str, u_num_digits(SIZE_MAX), f_ctrl);
    char *ret_fgets3 = fgets(ctrl_rem_size_str, u_num_digits(SIZE_MAX), f_ctrl);
    char *ret_fgets4 = fgets(ctrl->chunks_progress_str, fsize(f_ctrl), f_ctrl);

    if (!ret_fgets1 || !ret_fgets2 || !ret_fgets3 || !ret_fgets4) {
      fatal(FN, "Reading the ctrl file failed. Are you sure it's not corrupt!\n");
    }



    if ( fgetc(f_ctrl) != (char)EOF ) {
      fatal(FN, "ctrl file should have ended here.\n");
    }

    if (! ( strchr(ctrl_file_size_str, '\n') && strchr(ctrl_chunk_size_str, '\n') && strchr(ctrl_rem_size_str, '\n') && strchr(ctrl->chunks_progress_str, '\n') ) ) {
      fatal(FN, "Parsing ctrl file failed.\n");
    }

    /* Needed for strtoimax()/strtoumax() in parse_num_o()/parse_num_z()  */
    p = strchr(ctrl_file_size_str, '\n');
    *p = '\0';
    p = strchr(ctrl_chunk_size_str, '\n');
    *p = '\0';
    p = strchr(ctrl_rem_size_str, '\n');
    *p = '\0';
{'fraction_non_alphanumeric': 0.1303340413207183, 'fraction_numerical': 0.007723498744931454, 'mean_word_length': 3.6044444444444443, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3116937', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1858, 'n_words': 492}