<commit_before>#include "highscore.h"

// calculate the highscore out of the points and the time needed
int calculate_score(int points, long time_sec) {
  // 10 to highscore for every point you make -2 for every second you need
  return points * 10 - time_sec * 2;
}


// read the highscore file
HIGHSCORE *read_highscore(int *num) {
  // static pointer to an array of highscore records
  static HIGHSCORE *ptr = NULL;
  // the highscore file handle
  FILE *hs_file;
  int x, swapped;
  // the current record
  HIGHSCORE current;

  // if a NULL pointer is given free all the allocated memory and exit
  if(num == NULL) {
    free(ptr);
    return NULL;
  }

  // open the highscore file
  if((hs_file = fopen(HIGHSCORE_FILE, "r")) == NULL) {
    return NULL;
  }

  // free the allocated memory of calls before
  if(ptr != NULL) {
    free(ptr);
  }

  // initialize the ptr and num
  *num = 0;
  ptr = malloc(*num);

  // read the records into the `ptr`-array
  while(fread(&current, sizeof(HIGHSCORE), 1, hs_file) != 0) {
    // next record
    (*num)++;
    // allocate memory for the new record
    ptr = realloc(ptr, sizeof(HIGHSCORE) * *num);
    // save the new record in the array
    ptr[*num - 1] = current;
  }

  // sort the whole thing using a simple bubble sort algorithm
  do {
    // if this variable contains 0 at the end of the following for loop the
    // array is properly sorted
    swapped = 0;
    // iterate througth each record except the last
    for(x = 0; x < *num - 1; x++) {
      // check if the next record's value is greater than the value of the current one
      if(ptr[x].highscore < ptr[x + 1].highscore) {
        // swap the position of the two elements
        current = ptr[x];
        ptr[x] = ptr[x + 1];
        ptr[x + 1] = current;
        // the array isn't properly sorted 
        swapped = 1;
      }
    }
  } while (swapped);

  // return the array
  return ptr;
}

// add an entry to the highscore file
int add_highscore(char *name, int points, int time_sec) {
  FILE *hs_file;
  HIGHSCORE highscore;
<commit_msg>initialized file pointer<commit_after>#include "highscore.h"

// calculate the highscore out of the points and the time needed
int calculate_score(int points, long time_sec) {
  // 10 to highscore for every point you make -2 for every second you need
  return points * 10 - time_sec * 2;
}


// read the highscore file
HIGHSCORE *read_highscore(int *num) {
  // static pointer to an array of highscore records
  static HIGHSCORE *ptr = NULL;
  // the highscore file handle
  FILE *hs_file;
  int x, swapped;
  // the current record
  HIGHSCORE current;

  // if a NULL pointer is given free all the allocated memory and exit
  if(num == NULL) {
    free(ptr);
    return NULL;
  }

  // open the highscore file
  if((hs_file = fopen(HIGHSCORE_FILE, "r")) == NULL) {
    return NULL;
  }

  // free the allocated memory of calls before
  if(ptr != NULL) {
    free(ptr);
  }

  // initialize the ptr and num
  *num = 0;
  ptr = malloc(*num);

  // read the records into the `ptr`-array
  while(fread(&current, sizeof(HIGHSCORE), 1, hs_file) != 0) {
    // next record
    (*num)++;
    // allocate memory for the new record
    ptr = realloc(ptr, sizeof(HIGHSCORE) * *num);
    // save the new record in the array
    ptr[*num - 1] = current;
  }

  // sort the whole thing using a simple bubble sort algorithm
  do {
    // if this variable contains 0 at the end of the following for loop the
    // array is properly sorted
    swapped = 0;
    // iterate througth each record except the last
    for(x = 0; x < *num - 1; x++) {
      // check if the next record's value is greater than the value of the current one
      if(ptr[x].highscore < ptr[x + 1].highscore) {
        // swap the position of the two elements
        current = ptr[x];
        ptr[x] = ptr[x + 1];
        ptr[x + 1] = current;
        // the array isn't properly sorted 
        swapped = 1;
      }
    }
  } while (swapped);

  // return the array
  return ptr;
}

// add an entry to the highscore file
int add_highscore(char *name, int points, int time_sec) {
  FILE *hs_file = NULL;
  HIGHSCORE highscore;

{'fraction_non_alphanumeric': 0.1051998074145402, 'fraction_numerical': 0.008666345690900338, 'mean_word_length': 2.524173027989822, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '3898288', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1309, 'n_words': 523}