<commit_before>/** Copyright 2015-2017 <NAME>.
* See LICENSE file for license details.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <limits.h>

#include "status_bar.h"
#include "sys_file_monitor.h"

void* sys_file_init_config(gunichar icon, GArray* temp_filenames,
    int(*convert)(int),
    GString* bar_text, GMutex* mutex, GKeyFile* configs) {
  struct sys_file_monitor* m = malloc(sizeof(struct sys_file_monitor));

  m->bar_text = bar_text;
  m->mutex = mutex;

  m->icon = icon;
  m->temp_filenames = temp_filenames;
  m->convert = convert;
  m->str = g_string_new(NULL);

  return m;
}

void append_filename(GArray* filenames, char* filename) {
  GString* filename_str = g_string_new(filename);
  g_array_append_val(filenames, filename_str);
}

gboolean sys_file_update_text(void* ptr) {
  struct sys_file_monitor* m = (struct sys_file_monitor*)ptr;
  monitor_null_check(m, "sys_file_monitor", "update");

  int n_temps = m->temp_filenames->len;

  int min_temp = INT_MAX;
  int max_temp = INT_MIN;
  int n_read = 0;
  for (int i_file = 0; i_file < n_temps; i_file++) {
    char* temp_file_path = g_array_index(m->temp_filenames, GString*, i_file)->str;
    FILE* temp_file = fopen(temp_file_path, "r");
    if (temp_file == NULL) {
      fprintf(stderr, "Can't open temp file %s!\n", temp_file_path);
    } else {
      int temp;
      fscanf(temp_file, "%d", &temp);
      min_temp = temp < min_temp ? temp : min_temp;
      max_temp = temp > max_temp ? temp : max_temp;
      n_read++;
      fclose(temp_file);
    }
  }

  if (n_read == n_temps) {
    if (n_temps == 1) {
      g_string_printf(m->str, "U+%04"G_GINT32_FORMAT"%d", m->icon, m->convert(min_temp));
    } else {
      g_string_printf(m->str, "U+%04"G_GINT32_FORMAT"%d\\%d", m->icon, m->convert(min_temp), m->convert(max_temp));
    }

  } else {
    g_string_printf(m->str, "U+%04"G_GINT32_FORMAT"X!", m->icon);
  }

  g_mutex_lock(m->mutex);
  m->bar_text = g_string_assign(m->bar_text, m->str->str);
  g_mutex_unlock(m->mutex);

  return TRUE;
}<commit_msg>Fix printing in sys_file_monitor.<commit_after>/** Copyright 2015-2017 <NAME>.
* See LICENSE file for license details.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <limits.h>

#include "status_bar.h"
#include "sys_file_monitor.h"

void* sys_file_init_config(gunichar icon, GArray* temp_filenames,
    int(*convert)(int),
    GString* bar_text, GMutex* mutex, GKeyFile* configs) {
  struct sys_file_monitor* m = malloc(sizeof(struct sys_file_monitor));

  m->bar_text = bar_text;
  m->mutex = mutex;

  m->icon = icon;
  m->temp_filenames = temp_filenames;
  m->convert = convert;
  m->str = g_string_new(NULL);

  return m;
}

void append_filename(GArray* filenames, char* filename) {
  GString* filename_str = g_string_new(filename);
  g_array_append_val(filenames, filename_str);
}

gboolean sys_file_update_text(void* ptr) {
  struct sys_file_monitor* m = (struct sys_file_monitor*)ptr;
  monitor_null_check(m, "sys_file_monitor", "update");

  int n_temps = m->temp_filenames->len;

  int min_temp = INT_MAX;
  int max_temp = INT_MIN;
  int n_read = 0;
  for (int i_file = 0; i_file < n_temps; i_file++) {
    char* temp_file_path = g_array_index(m->temp_filenames, GString*, i_file)->str;
    FILE* temp_file = fopen(temp_file_path, "r");
    if (temp_file == NULL) {
      fprintf(stderr, "Can't open temp file %s!\n", temp_file_path);
    } else {
      int temp;
      fscanf(temp_file, "%d", &temp);
      min_temp = temp < min_temp ? temp : min_temp;
      max_temp = temp > max_temp ? temp : max_temp;
      n_read++;
      fclose(temp_file);
    }
  }

  m->str = g_string_set_size(m->str, 0);
  g_string_append_unichar(m->str, m->icon);
  if (n_read == n_temps) {
    if (n_temps == 1) {
      g_string_append_printf(m->str, "%d", m->convert(min_temp));
    } else {
      g_string_append_printf(m->str, "%d\\%d", m->convert(min_temp), m->convert(max_temp));
    }

  } else {
    g_string_append_printf(m->str, "X!");
  }

  g_mutex_lock(m->mutex);
  m->bar_text = g_string_assign(m->bar_text, m->str->str);
  g_mutex_unlock(m->mutex);

  return TRUE;
}
{'fraction_non_alphanumeric': 0.15390121689334288, 'fraction_numerical': 0.008351228823669768, 'mean_word_length': 4.020359281437126, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 68, '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': '2189549', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1658, 'n_words': 323}