aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c222
1 files changed, 113 insertions, 109 deletions
diff --git a/src/utils.c b/src/utils.c
index 56cca93..16f7589 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -9,127 +9,131 @@
* 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;
+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;
}
// input two char pointer
// output: one char pointer
// misc: caller needs to free the memory
-char *concat(const char *s1, const char *s2) {
- const size_t len1 = strlen(s1);
- const size_t len2 = strlen(s2);
- char *result = malloc(len1 + len2 + 1); // +1 for '\0'
-
- if (result == NULL) {
- perror("allocating memory for strings failed");
- return NULL;
- }
-
- memcpy(result, s1, len1);
- memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator
- return result; //caller needs to free memory
+char *concat(const char *s1, const char *s2)
+{
+ const size_t len1 = strlen(s1);
+ const size_t len2 = strlen(s2);
+ char *result = malloc(len1 + len2 + 1); // +1 for '\0'
+
+ if (result == NULL) {
+ perror("allocating memory for strings failed");
+ return NULL;
+ }
+
+ memcpy(result, s1, len1);
+ memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator
+ return result; //caller needs to free memory
}
+// input: pointer to string
+// output: pointer to string with leading and tailing whitespaces removed
char *trim_whitespace(char* str)
{
- char *end;
+ char *end;
+
+ // Trim leading spaces
+ while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') {
+ str++;
+ }
+
+ if (*str == '\0') return str; // all spaces
- // Trim leading spaces
- while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') {
- str++;
- }
+ // Trim trailing space
+ end = str + strlen(str) - 1;
- if (*str == '\0') return str; // all spaces
-
- // Trim trailing space
- end = str + strlen(str) - 1;
+ while (end > str && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) end--;
- while (end > str && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) end--;
-
- // Write new null terminator character
- end[1] = '\0';
+ // Write new null terminator character
+ end[1] = '\0';
- return str;
+ return str;
}