blob: 1f86b03f11462ee5701eab98b83479d9d14827d9 (
plain) (
blame)
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
|
/*
* =====================================================================================
*
* Filename: util.c
*
* Description:
*
* Version: 1.0
* Created: 01/28/2022 03:47:07 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char* trimwhitespace(char* str) {
char* end;
// Trim leading spaces
while(isspace((unsigned char) *str)) str++;
if (*str == 0) // All spaces?
return str;
// trailing spaces
end = str + strlen(str) -1;
while (end > str && isspace((unsigned char)* end)) end--;
end[1] = '\0';
return str;
}
/*
* Remove given section from string. Negative len means remove
* everything up to the end.
*
* based on SO answer https://stackoverflow.com/a/20346241
*/
int cut_str(char *str, int begin, int len)
{
int l = strlen(str);
if (len < 0) len = l - begin;
if (begin + len > l) len = l - begin;
memmove(str + begin, str + begin + len, l - len + 1);
return len;
}
|