File: utils.c

package info (click to toggle)
dunst 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 484 kB
  • ctags: 519
  • sloc: ansic: 3,699; sh: 170; makefile: 76
file content (110 lines) | stat: -rw-r--r-- 2,569 bytes parent folder | download | duplicates (2)
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
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#define _GNU_SOURCE

#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>

#include "utils.h"
#include "dunst.h"

char *string_replace_char(char needle, char replacement, char *haystack) {
    char *current = haystack;
    while ((current = strchr (current, needle)) != NULL)
        *current++ = replacement;
    return haystack;
}

char *string_replace_all(const char *needle, const char *replacement,
                         char *haystack)
{
        char *start;
        start = strstr(haystack, needle);
        while (start != NULL) {
                haystack = string_replace(needle, replacement, haystack);
                start = strstr(haystack, needle);
        }
        return haystack;
}

char *string_replace(const char *needle, const char *replacement,
                     char *haystack)
{
        char *tmp, *start;
        int size;
        start = strstr(haystack, needle);
        if (start == NULL) {
                return haystack;
        }

        size = (strlen(haystack) - strlen(needle)) + strlen(replacement) + 1;
        tmp = calloc(sizeof(char), size);
        memset(tmp, '\0', size);

        strncpy(tmp, haystack, start - haystack);
        tmp[start - haystack] = '\0';

        sprintf(tmp + strlen(tmp), "%s%s", replacement, start + strlen(needle));
        free(haystack);

        return tmp;
}

char *string_append(char *a, const char *b, const char *sep)
{
        if (!a)
                return g_strdup(b);

        char *new;
        if (!sep)
                new = g_strconcat(a, b, NULL);
        else
                new = g_strconcat(a, sep, b, NULL);
        free(a);

        return new;

}

char **string_to_argv(const char *s)
{
        char *str = strdup(s);
        char **argv = NULL;
        char *p = strtok (str, " ");
        int n_spaces = 0;

        while (p) {
                argv = realloc (argv, sizeof (char*) * ++n_spaces);
                argv[n_spaces-1] = g_strdup(p);
                p = strtok (NULL, " ");
        }
        argv = realloc (argv, sizeof (char*) * (n_spaces+1));
        argv[n_spaces] = NULL;

        free(str);

        return argv;
}

int digit_count(int i)
{
        i = ABS(i);
        int len = 1;

        while (i > 0) {
                len++;
                i /= 10;
        }

        return len;
}

void die(char *text, int exit_value)
{
        fputs(text, stderr);
        exit(exit_value);
}

/* vim: set ts=8 sw=8 tw=0: */