<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "loader.h"

void load_help(char *help) {
  int ch, ch_count = 0;
  char ch_str[2];
  FILE *f = fopen(FILE_HELP, "r");
  while ((ch = fgetc(f)) != EOF && ch_count < MAX_HELP_TEXT_CHARS) {
    snprintf(ch_str, 2, "%c", ch);
    strcat(help, ch_str);
    ch_count++;
  }
  fclose(f);
}

void load_meta(struct meta *data) {
  int linenum = 0, run = 1;
  char line[1024];

  FILE *f = fopen(FILE_META, "r");
  do {
    strncpy(line, "", sizeof(line));
    if (fscanf(f, "%[^\n]\n", &line[0]) && strlen(line) > 0) {
      if (linenum == 0)
        strncpy(data->title, line, sizeof(data->title));
      else if (linenum == 1)
        strncpy(data->version, line, sizeof(data->version));
      else if (linenum == 2)
        strncpy(data->author, line, sizeof(data->author));
      else if (linenum == 3)
        data->cyear = atoi(line);
      linenum++;
    } else {
      run = 0;
    }
  } while (run == 1);
  fclose(f);
}

void load_phrases(struct phrases *data) {
  FILE *f = fopen(FILE_PHRASES, "r");
  int ch, linenum = 0;
  char line[1024] = "", chstr[2];

  while ((ch = fgetc(f)) != EOF) {
    if (ch != '\n') {
      snprintf(chstr, 2, "%c", ch);
      strcat(line, chstr);
    } else {
      switch (linenum) {
        case 0:
          strncpy(data->inv_title, line, sizeof(data->inv_title));
          break;
        case 1:
          strncpy(data->no_inv_items, line, sizeof(data->no_inv_items));
          break;
        case 2:
          strncpy(data->items_comb, line, sizeof(data->items_comb));
          break;
        case 3:
          strncpy(data->items_comb_failure, line, sizeof(data->items_comb_failure));
          break;
        case 4:
          strncpy(data->item_usage_failure, line, sizeof(data->item_usage_failure));
          break;
      }
      strncpy(line, "", sizeof(line));
      linenum++;
    }
  }
  fclose(f);
}

int load_commands(struct command *data, int lmax) {
  FILE *f = fopen(FILE_COMMANDS, "r");
  int ch, entryidx = 0;<commit_msg>use fscanf in phrases loader<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "loader.h"

void load_help(char *help) {
  int ch, ch_count = 0;
  char ch_str[2];
  FILE *f = fopen(FILE_HELP, "r");
  while ((ch = fgetc(f)) != EOF && ch_count < MAX_HELP_TEXT_CHARS) {
    snprintf(ch_str, 2, "%c", ch);
    strcat(help, ch_str);
    ch_count++;
  }
  fclose(f);
}

void load_meta(struct meta *data) {
  int linenum = 0, run = 1;
  char line[1024];
  FILE *f = fopen(FILE_META, "r");
  do {
    strncpy(line, "", sizeof(line));
    if (fscanf(f, "%[^\n]\n", &line[0]) && strlen(line) > 0) {
      if (linenum == 0)
        strncpy(data->title, line, sizeof(data->title));
      else if (linenum == 1)
        strncpy(data->version, line, sizeof(data->version));
      else if (linenum == 2)
        strncpy(data->author, line, sizeof(data->author));
      else if (linenum == 3)
        data->cyear = atoi(line);
      linenum++;
    } else {
      run = 0;
    }
  } while (run == 1);
  fclose(f);
}

void load_phrases(struct phrases *data) {
  int linenum = 0, run = 1;
  char line[1024];
  FILE *f = fopen(FILE_PHRASES, "r");
  do {
    strncpy(line, "", sizeof(line));
    if (fscanf(f, "%[^\n]\n", &line[0]) && strlen(line) > 0) {
      switch (linenum) {
        case 0:
          strncpy(data->inv_title, line, sizeof(data->inv_title));
          break;
        case 1:
          strncpy(data->no_inv_items, line, sizeof(data->no_inv_items));
          break;
        case 2:
          strncpy(data->items_comb, line, sizeof(data->items_comb));
          break;
        case 3:
          strncpy(data->items_comb_failure, line, sizeof(data->items_comb_failure));
          break;
        case 4:
          strncpy(data->item_usage_failure, line, sizeof(data->item_usage_failure));
          break;
      }
      linenum++;
    } else {
      run = 0;
    }
  } while (run == 1);
  fclose(f);
}

int load_commands(struct command *data, int lmax) {
  FILE *f = fopen(FILE_COMMANDS, "r");
  int ch, entryidx = 0;
{'fraction_non_alphanumeric': 0.17262192177691937, 'fraction_numerical': 0.01521004345726702, 'mean_word_length': 2.587012987012987, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 48, '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': '3654357', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1559, 'n_words': 350}