summaryrefslogtreecommitdiff
path: root/usual/string.h
blob: 21a738b92498f52ac50eef9e8d2dd7d0262da6ad (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2007-2009  Marko Kreen, Skype Technologies OÜ
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/**
 * \file
 * Theme include for strings.
 */

#ifndef _USUAL_STRING_H_
#define _USUAL_STRING_H_

#include <usual/cxalloc.h>

#include <string.h>

/**
 * @name  List of strings.
 * @{
 */

/** Callback signature */
typedef bool (*str_cb)(void *arg, const char *s);

struct StrList;
/** Allocate new string list */
struct StrList *strlist_new(CxMem *ca);
/** Free string string */
void strlist_free(struct StrList *slist);
/** Check if empty */
bool strlist_empty(struct StrList *slist);
/** Append copy of string. */
bool strlist_append(struct StrList *slist, const char *str);
/** Append reference, strlist now owns it. */
bool strlist_append_ref(struct StrList *slist, const char *str);
/** Call function on each element */
bool strlist_foreach(const struct StrList *slist, str_cb cb_func, void *cb_arg);
/** Remove and return first element */
const char *strlist_pop(struct StrList *slist);
/* @} */

/** Parse comma-separated elements from string and launch callback for each of them. */
bool parse_word_list(const char *s, str_cb cb_func, void *cb_arg);

#ifndef HAVE_STRLCPY
#define strlcpy(a,b,c) usual_strlcpy(a,b,c)
/** Compat: Safely copy string to fixed-length buffer.  */
size_t strlcpy(char *dst, const char *src, size_t n);
#endif

#ifndef HAVE_STRLCAT
#define strlcat(a,b,c) usual_strlcat(a,b,c)
/** Compat: Safely append string to fixed-length buffer. */
size_t strlcat(char *dst, const char *src, size_t n);
#endif

#ifndef HAVE_MEMRCHR
#define memrchr(a,b,c) usual_memrchr(a,b,c)
/** Compat: find byte in reverse direction */
void *memrchr(const void *s, int c, size_t n);
#endif

#ifndef HAVE_MEMMEM
#define memmem(a,b,c,d) usual_memmem(a,b,c,d)
/** Compat: find memory area */
void *memmem(const void *s, size_t slen, const void *q, size_t qlen);
#endif

/** Return position to first byte that is in 'find'. */
void *mempbrk(const void *data, size_t dlen, const void *find, size_t flen);

/** Return number of bytes where none are in reject. */
size_t memcspn(const void *data, size_t dlen, const void *reject, size_t rlen);

/** Return number of bytes where all are in accept. */
size_t memspn(const void *data, size_t dlen, const void *accept, size_t alen);

#ifndef HAVE_BASENAME
#undef basename
#define basename(a) usual_basename(a)
/** Compat: Return pointer to last non-path element.
    Never modifies path, returns either pointer inside path or static buffer.  */
const char *basename(const char *path);
#endif

#ifndef HAVE_DIRNAME
#undef dirname
#define dirname(a) usual_dirname(a)
/** Compat: Return directory part of pathname.
    Never modifies path, returns either pointer inside path or static buffer.  */
const char *dirname(const char *path);
#endif

/*
 * strerror, strerror_r
 */

#ifdef WIN32
const char *win32_strerror(int e);
/** Compat: strerror() for win32 */
#define strerror(x) win32_strerror(x)
#endif

const char *usual_strerror_r(int e, char *dst, size_t dstlen);
/** Compat: Provide GNU-style API: const char *strerror_r(int e, char *dst, size_t dstlen)  */
#define strerror_r(a,b,c) usual_strerror_r(a,b,c)

/** strtod() that uses '.' as decimal separator */
double strtod_dot(const char *s, char **tokend);

/** Convert double to string with '.' as decimal separator */
ssize_t dtostr_dot(char *buf, size_t buflen, double val);


#endif