aboutsummaryrefslogblamecommitdiff
path: root/src/util.c
blob: 1f86b03f11462ee5701eab98b83479d9d14827d9 (plain) (tree)





































                                                                                        
















                                                                   
/*
 * =====================================================================================
 *
 *       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;
}