aboutsummaryrefslogtreecommitdiff
path: root/iceys-utils.c
blob: 2331322ab6de640dc8d7929faf1bcf96a55afa4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef iceys_utils_c
#define iceys_utils_c
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include <stdio.h>

char *strremove(char *str, const char *sub) {
    size_t len = strlen(sub);
    if (len > 0) {
        char *p = str;
        size_t size = 0;
        while ((p = strstr(p, sub)) != NULL) {
            size = (size == 0) ? (p - str) + strlen(p + len) + 1 : size - len;
            memmove(p, p + len, size - (p - str));
        }
    }
    return str;
}

float map(float value, float start1, float stop1, float start2, float stop2){
	return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}

//i made this one :)
void str_first(char *str, char *str2, char eraser, int found_count){
    int i=0;
    int found=0;

    while(str[i]!='\0'){
        if(str[i]==eraser){
            if(found>=found_count){
                break;
            } else { found++; }
        }
        i++;
    }

    memcpy(str2,str,sizeof(char)*i);
}

regex_t regex;
int reti;

int is_number(char *number){
    if(!number){
        return -1;
    }

    /* Compile regular expression */
    reti = regcomp(&regex, "^[0-9]*$", 0);
    if (reti) {
        fprintf(stderr, "Could not compile regex\n");
        exit(1);
    }


    reti = regexec(&regex, number, 0, NULL, 0);
    if (reti == REG_NOMATCH) {
        return -2;
    }

    regfree(&regex);

    return 1;
}

int execret(char *return_buffer, char *command){
    // Open a process by creating a pipe
    FILE *fp = popen(command, "r");
    if (fp == NULL) {
        perror("popen");
        return 1;
    }

    // Read the output from the command
    char buffer[128];
    while (fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
        strcat(return_buffer,buffer);
    }

    // Close the pipe
    if (pclose(fp) == -1) {
        perror("pclose");
        return 1;
    }

    return 0;
}

int execretarr(char **return_arr, char *command){
    // Open a process by creating a pipe
    FILE *fp = popen(command, "r");
    int i=0;
    if (fp == NULL) {
        perror("popen");
        return 1;
    }

    // Read the output from the command
    char buffer[128];
    while (fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
        for( int i=0;i<sizeof(buffer);i++){
            if(buffer[i]=='\n')
                buffer[i]=' ';
        }
        memcpy(return_arr[i],buffer,sizeof(buffer));
        i++;
    }

    // Close the pipe
    if (pclose(fp) == -1) {
        perror("pclose");
        return 1;
    }

    return 0;
}
#endif