diff options
| author | Anser <rostgans@tutamail.com> | 2026-07-23 01:49:45 +0200 |
|---|---|---|
| committer | Anser <rostgans@tutamail.com> | 2026-07-31 11:12:41 +0200 |
| commit | 856376d623535d2fd6734592f7018efeaf9bab7a (patch) | |
| tree | c25b510ab5e975d71a3a9f67928ef01dc63d278f /src/modules | |
| parent | 4d4d1ff11c26ac277d8015f4a4e55dbc6da3b25c (diff) | |
created utils module
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/cpu/cpu.c | 94 | ||||
| -rw-r--r-- | src/modules/utils/utils.c | 95 | ||||
| -rw-r--r-- | src/modules/utils/utils.h | 14 |
3 files changed, 110 insertions, 93 deletions
diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index b02a68f..a6dac93 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -2,99 +2,7 @@ #include <string.h> #include <stdlib.h> #include "cpu.h" - -/* function: char *string_format(char* string) - * input: pointer to a null-terminated string (must not be NULL) - * return: pointer to a newly allocated string containing all space-separated words, - * joined by single spaces; leading/trailing/multiple spaces are ignored. - * on-error: returns NULL if memory allocation fails - * misc: the returned pointer must be freed by the caller using free() - * limits: processes at most 10 words, each up to 49 characters (last '\0') - */ -char *string_format(char* string) { - // Wörterbuch array [Wort][Zeichenkette] - char woerter[10][50] = { 0 }; - int wort_index = 0; - int char_index = 0; - - while (*string != '\0') { - // add '\0' stringterminator to end of word - if (*string == ' ' && char_index > 0) { - // safty check for array length - if (wort_index > 9 || char_index > 49) { - break; - } - - // next slot is char - // add '\0' to end string - woerter[wort_index][char_index] = '\0'; - wort_index++; - char_index = 0; - string++; - continue; - } - // skip extra spaces - else if (*string == ' ' && char_index == 0) { - string++; - continue; - } - - // safty check for array length - if (char_index >= 49) { - break; - } - - woerter[wort_index][char_index] = *string; - char_index++; - string++; - } - - // terminate last word with '\0' - if (char_index > 0 && wort_index < 10 && char_index < 49) { - woerter[wort_index][char_index] = '\0'; - wort_index++; - } - - /* - * return - */ - char *ptr = NULL; - - // get length of woerter array - if (wort_index == 0) { - ptr = NULL; - } else { - int sum = 0; - - for (int i = 0; i < wort_index; i++) { - sum += strlen(woerter[i]); - } - sum += wort_index - 1; // for spaces - sum += 1; // for Nulltermination - - - ptr = (char *)malloc(sum); - - // check for NULL pointer - if (ptr == NULL) { - return NULL; - } - - //strcpy - char *ende = ptr; - - for (int i = 0; i < wort_index; i++) { - ende = stpcpy(ende, woerter[i]); - - if (i != wort_index - 1) { - *(ende++) = ' '; - } - } - } - - return ptr; -} - +#include "../utils/utils.h" int cpu(void) { FILE* file = fopen("/proc/cpuinfo", "r"); diff --git a/src/modules/utils/utils.c b/src/modules/utils/utils.c new file mode 100644 index 0000000..d5a3a96 --- /dev/null +++ b/src/modules/utils/utils.c @@ -0,0 +1,95 @@ +#include <string.h> +#include <stdlib.h> + + +/* function: char *string_format(char* string) + * input: pointer to a null-terminated string (must not be NULL) + * return: pointer to a newly allocated string containing all space-separated words, + * joined by single spaces; leading/trailing/multiple spaces are ignored. + * on-error: returns NULL if memory allocation fails + * misc: the returned pointer must be freed by the caller using free() + * limits: processes at most 10 words, each up to 49 characters (last '\0') + */ +char *string_format(char* string) { + // Wörterbuch array [Wort][Zeichenkette] + char woerter[10][50] = { 0 }; + int wort_index = 0; + int char_index = 0; + + while (*string != '\0') { + // add '\0' stringterminator to end of word + if (*string == ' ' && char_index > 0) { + // safty check for array length + if (wort_index > 9 || char_index > 49) { + break; + } + + // next slot is char + // add '\0' to end string + woerter[wort_index][char_index] = '\0'; + wort_index++; + char_index = 0; + string++; + continue; + } + // skip extra spaces + else if (*string == ' ' && char_index == 0) { + string++; + continue; + } + + // safty check for array length + if (char_index >= 49) { + break; + } + + woerter[wort_index][char_index] = *string; + char_index++; + string++; + } + + // terminate last word with '\0' + if (char_index > 0 && wort_index < 10 && char_index < 49) { + woerter[wort_index][char_index] = '\0'; + wort_index++; + } + + /* + * return + */ + char *ptr = NULL; + + // get length of woerter array + if (wort_index == 0) { + ptr = NULL; + } else { + int sum = 0; + + for (int i = 0; i < wort_index; i++) { + sum += strlen(woerter[i]); + } + sum += wort_index - 1; // for spaces + sum += 1; // for Nulltermination + + + ptr = (char *)malloc(sum); + + // check for NULL pointer + if (ptr == NULL) { + return NULL; + } + + //strcpy + char *ende = ptr; + + for (int i = 0; i < wort_index; i++) { + ende = stpcpy(ende, woerter[i]); + + if (i != wort_index - 1) { + *(ende++) = ' '; + } + } + } + + return ptr; +} diff --git a/src/modules/utils/utils.h b/src/modules/utils/utils.h new file mode 100644 index 0000000..1dfbb51 --- /dev/null +++ b/src/modules/utils/utils.h @@ -0,0 +1,14 @@ +#ifndef UTILS_H +#define UTILS_H + +/* function: char *string_format(char* string) + * input: pointer to a null-terminated string (must not be NULL) + * return: pointer to a newly allocated string containing all space-separated words, + * joined by single spaces; leading/trailing/multiple spaces are ignored. + * on-error: returns NULL if memory allocation fails + * misc: the returned pointer must be freed by the caller using free() + * limits: processes at most 10 words, each up to 49 characters (last '\0') + */ +char *string_format(char* string); + +#endif |
