aboutsummaryrefslogtreecommitdiff
path: root/lib/util.c
blob: 595db44a6b98aae3a01909b99b8187b84bf40938 (plain)
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
/* This file is part of GNU Dico
   Copyright (C) 2008, 2010, 2012 Sergey Poznyakoff
  
   GNU Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <dico.h>
#include <xalloc.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

size_t
dico_string_trim(char *buf, size_t len, int (*pred)(int c))
{
    while (len > 0 && pred(buf[len - 1]))
	buf[--len] = 0;
    return len;
}

static int
_is_nl(int c)
{
    return c == '\r' || c == '\n';
}

size_t
dico_trim_nl(char *buf)
{
    return dico_string_trim(buf, strlen(buf), _is_nl);
}

static int
_is_ws(int c)
{
    return isspace(c);
}

size_t
dico_trim_ws(char *buf)
{
    return dico_string_trim(buf, strlen(buf), _is_ws);
}

char *
dico_full_file_name(const char *dir, const char *file)
{
    size_t dirlen = strlen(dir);
    int need_slash = !(dirlen && dir[dirlen - 1] == '/');
    size_t size = dirlen + need_slash + 1 + strlen(file) + 1;
    char *buf = malloc(size);

    if (!buf)
	return NULL;
    
    strcpy(buf, dir);
    if (need_slash)
	strcpy(buf + dirlen++, "/");
    else {
	while (dirlen > 0 && buf[dirlen-1] == '/')
	    dirlen--;
	dirlen++;
    }
    while (*file == '/')
	file++;
    strcpy(buf + dirlen, file);
    return buf;
}

Return to:

Send suggestions and report system problems to the System administrator.