<commit_before>/*
  wordhist.c

  CPSC 223C
  Spring 2015
  CSU Fullerton

  Lab 6: word frequencies in O(n log n) time

  Authors: TODO: replace this with your names and CSUF email addresses

*/ 

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wordhist.h"

/* You can assume every word has at most MAX_WORD_LENGTH
   characters.

   This is defined in the .c file, not the .h file, since this is an
   implementation detail that should be hidden from users. */
#define MAX_WORD_LENGTH 200

WordHist* word_hist_parse(char* filename) {
  WordHist *hist; /* object to create and return */
  FILE *f;
  char **tokens;  /* array of strings corresponding to all the tokens in f */
  int n;          /* number of tokens */

  /* TODO: You will probably need more local variables. */

  assert(filename != NULL);

  /* Try to open the file. */
  f = fopen(filename, "r");
  if (f == NULL) {
    return NULL; /* file not found */
  }

  /* TODO: Initialize n with a loop that reads one word, increments n,
     and continues until EOF is reached. Essentially you need to count
     the words one by one. Hint: see how word tokens are read with
     fscanf in the Lab 5 solution code. */

  /* TODO: Now that you know how many tokens there are, initialize the
     tokens array by allocating an array of n char* pointers. Hint:
     use calloc. Remeber that, if the allocation fails, you have to
     close f and return NULL. */

  /* TODO: Now go back and load the contents of each token into
     memory. Rewind f back to the beginning. Then use a loop that
     reads one word into a stack-allocated string, copies that string
     into a new heap-allocated string, and stores that pointer in the
     tokens array. Hint: use rewind() and strdup(). Remember that,
     again, if any allocation fails you need to close f, free
     everything, and return NULL. */

  /* We are done with f. */
  fclose(f);

  /* TODO: sort tokens with qsort(). */

  /* TODO: Initialize hist by allocating memory for it with
     malloc(). If this fails you need to free all the words and return
     NULL. */
<commit_msg>End of class, working on qsort comparison.<commit_after>/*
  wordhist.c

  CPSC 223C
  Spring 2015
  CSU Fullerton

  Lab 6: word frequencies in O(n log n) time

  Authors: <NAME> (<EMAIL>)
*/ 

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wordhist.h"

/* You can assume every word has at most MAX_WORD_LENGTH
   characters.

   This is defined in the .c file, not the .h file, since this is an
   implementation detail that should be hidden from users. */
#define MAX_WORD_LENGTH 200

WordHist* word_hist_parse(char* filename) {
  WordHist *hist; /* object to create and return */
  FILE *f;
  char **tokens;  /* array of strings corresponding to all the tokens in f */
  int n;          /* number of tokens */
  char str[MAX_WORD_LENGTH];
  int count = 0;

  /* TODO: You will probably need more local variables. */

  assert(filename != NULL);

  /* Try to open the file. */
  f = fopen(filename, "r");
  if (f == NULL) {
    return NULL; /* file not found */
  }

  /* TODO: Initialize n with a loop that reads one word, increments n,
     and continues until EOF is reached. Essentially you need to count
     the words one by one. Hint: see how word tokens are read with
     fscanf in the Lab 5 solution code. */
  n = 0;
  while(fscanf(f, "%s", str) != EOF) {
    n++;
  }

  /* TODO: Now that you know how many tokens there are, initialize the
     tokens array by allocating an array of n char* pointers. Hint:
     use calloc. Remeber that, if the allocation fails, you have to
     close f and return NULL. */
  if(!(tokens = calloc(n, sizeof(char*)))) {
    fprintf(stderr, "\nError: failed allocation of tokens");
    fclose(f);
    return NULL;
  }
  
  /* TODO: Now go back and load the contents of each token into
     memory. Rewind f back to the beginning. Then use a loop that
     reads one word into a stack-allocated string, copies that string
     into a new heap-allocated string, and stores that pointer in the
     tokens array. Hint: use rewind() and strdup(). Remember that,
     again, if any allocation fails you need to close f, free
     everything, and return NULL. */
  rewind(f);

  while(fscanf(f, "%s", str) != EOF) {
    char  *heapString;
    
    heapString = malloc(sizeof(str));

    if(heapString == NULL) {
      return NULL;
    }

    heapString = strdup(str);

    tokens[count] = heapString;
    count++;
  }
  
  /* We are done with f. */
  fclose(f);

  /* TODO: sort tokens with qsort(). */

  qsort(
  /* TODO: Initialize hist by allocating memory for it with
     malloc(). If this fails you need to free all the words and return
     NULL. */

{'fraction_non_alphanumeric': 0.09385451505016723, 'fraction_numerical': 0.005434782608695652, 'mean_word_length': 2.767716535433071, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '164280', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1413, 'n_words': 669}