<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "acpi.h"
#include "config.h"
#include "result.h"

Result *battery_status(char *str) {
    Result *res;
    res = init_res();
    FILE *f;
    char file_location[128];
    int val;

    file_location[0] = 0;
    strcat(file_location, batpath);
    strcat(file_location, str);
    strcat(file_location, "/");
    strcat(file_location, "status");

    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }

    val = fscanf(f, "%s", res->string);
    fclose(f);

    if(val == EOF) {
        res->error=1;
        return res;
    }

    return res;
}

Result *battery_capacity(char *str) {
    Result *res;
    res = init_res();
    FILE *f;
    int capacity = 0;
    char file_location[128];
    int val;
    file_location[0] = 0;
    strcat(file_location, batpath);
    strcat(file_location, str);
    strcat(file_location, "/");
    strcat(file_location, "capacity");

    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }

    val = fscanf(f, "%i", &capacity);
    fclose(f);

    if(val == EOF) {
        res->error=1;
        return res;
    }

    res->max=100;
    res->value=(int)capacity;

    sprintf(res->string, "%i%%", capacity);

    return res;
}

Result *battery_time(char *str) {
    Result *res;
    res = init_res();

    FILE *f;
    int voltage = 0;
    int power = 0;
    int energy = 0;
    int capacity = 0;
    int rate = 0;
    char file_location[128];
    int val;

    sprintf(file_location, "%s%s/voltage_now", batpath, str);
    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }
    val = fscanf(f, "%i", &voltage);
    fclose(f);
    if(val == EOF) {
        res->error=1;
        return res;
    }

    sprintf(file_location, "%s%s/power_now", batpath, str);
    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }
    val = fscanf(f, "%i", &power);
    fclose(f);
    if(val == EOF) {
        res->error=1;
        return res;
    }

    sprintf(file_location, "%s%s/energy_now", batpath, str);
    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }
    val = fscanf(f, "%i", &energy);
    fclose(f);
    if(val == EOF) {
        res->error=1;
        return res;
    }

    voltage /= 1000;
    power /= 1000;
    energy /= 1000;

    capacity = energy * 1000 / voltage;
    rate = power * 1000 / voltage;
    res->value = 3600 * capacity / rate;

    return res;
}

int bat_islow(char *str) {
    FILE *f;
    int capacity = 0;
    char file_location[128];
    int val;<commit_msg>fixed crashing when there is no battery consumption<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "acpi.h"
#include "config.h"
#include "result.h"

Result *battery_status(char *str) {
    Result *res;
    res = init_res();
    FILE *f;
    char file_location[128];
    int val;

    file_location[0] = 0;
    strcat(file_location, batpath);
    strcat(file_location, str);
    strcat(file_location, "/");
    strcat(file_location, "status");

    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }

    val = fscanf(f, "%s", res->string);
    fclose(f);

    if(val == EOF) {
        res->error=1;
        return res;
    }

    return res;
}

Result *battery_capacity(char *str) {
    Result *res;
    res = init_res();
    FILE *f;
    int capacity = 0;
    char file_location[128];
    int val;
    file_location[0] = 0;
    strcat(file_location, batpath);
    strcat(file_location, str);
    strcat(file_location, "/");
    strcat(file_location, "capacity");

    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }

    val = fscanf(f, "%i", &capacity);
    fclose(f);

    if(val == EOF) {
        res->error=1;
        return res;
    }

    res->max=100;
    res->value=(int)capacity;

    sprintf(res->string, "%i%%", capacity);

    return res;
}

Result *battery_time(char *str) {
    Result *res;
    res = init_res();

    FILE *f;
    int voltage = 0;
    int power = 0;
    int energy = 0;
    int capacity = 0;
    int rate = 0;
    char file_location[128];
    int val;

    sprintf(file_location, "%s%s/voltage_now", batpath, str);
    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }
    val = fscanf(f, "%i", &voltage);
    fclose(f);
    if(val == EOF) {
        res->error=1;
        return res;
    }

    sprintf(file_location, "%s%s/power_now", batpath, str);
    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }
    val = fscanf(f, "%i", &power);
    fclose(f);
    if(val == EOF) {
        res->error=1;
        return res;
    }

    sprintf(file_location, "%s%s/energy_now", batpath, str);
    f = fopen(file_location, "r");

    if(f == NULL) {
        res->error=1;
        return res;
    }
    val = fscanf(f, "%i", &energy);
    fclose(f);
    if(val == EOF) {
        res->error=1;
        return res;
    }

    voltage /= 1000;
    power /= 1000;
    energy /= 1000;

    capacity = energy * 1000 / voltage;
    rate = power * 1000 / voltage;
    if(rate <= 0.01) {
        res->value = 0;
    } else {
        res->value = 3600 * capacity / rate;
    }
    return res;
}

int bat_islow(char *str) {
    FILE *f;
    int capacity = 0;
    char file_location[128];
    int val;
{'fraction_non_alphanumeric': 0.16349012065046337, 'fraction_numerical': 0.021682112257387656, 'mean_word_length': 2.195530726256983, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 46, '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': '2579523', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2123, 'n_words': 483}