toolbox

Check-in [33ccecc090]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:lib: add useful function in jtbuf & jtstr
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 33ccecc090f8e93384fdaf6182c9c0850dbe0ef5f7bfb7ec180ca6c130c096cb
User & Date: jef 2020-04-27 18:36:52
Context
2020-04-28
10:01
rrdreel: initial import check-in: 677c67fdd7 user: jef tags: trunk
2020-04-27
18:36
lib: add useful function in jtbuf & jtstr check-in: 33ccecc090 user: jef tags: trunk
2020-04-26
12:12
soundmachine: improve Makefile check-in: 8cfc651746 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/include/jt.h.

1
2
3
4
5






6
7
8
9
10
11
12
..
26
27
28
29
30
31
32

33
34
35
36
37
38
39
..
41
42
43
44
45
46
47

48
49

50
51
52
53
54
55
#ifndef _JT_H
#define _JT_H

#include <stddef.h>







/*
** messages
*/ 

void warning(const char *fmt, ...);
void die(const char *fmt, ...);

................................................................................
*/ 

char *str_trim_left(char *str);
char *str_trim_right(char *str);
char *str_trim(char *str);
char *str_replace(const char *str, const char *pattern, const char *repl);
char *str_join(const char *items[], size_t size, const char *sep);


/*
** jtbuf
*/ 

typedef char *jtbuf;

................................................................................
jtbuf jtbuf_reset(jtbuf buffer);
void jtbuf_free(jtbuf buffer);

size_t jtbuf_size(jtbuf buffer);

jtbuf jtbuf_prepend(jtbuf buffer, void *data, size_t size);
jtbuf jtbuf_prepend_str(jtbuf buffer, const char *str);

jtbuf jtbuf_append(jtbuf buffer, void *data, size_t size);
jtbuf jtbuf_append_str(jtbuf buffer, const char *str);


jtbuf jtbuf_trim_left(jtbuf buffer);
jtbuf jtbuf_trim_right(jtbuf buffer);
jtbuf jtbuf_trim(jtbuf buffer);

#endif





>
>
>
>
>
>







 







>







 







>


>






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
..
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef _JT_H
#define _JT_H

#include <stddef.h>

/*
** macros
*/

#define ARRAY_SIZE(a) (sizeof((a))/sizeof(*(a)))

/*
** messages
*/ 

void warning(const char *fmt, ...);
void die(const char *fmt, ...);

................................................................................
*/ 

char *str_trim_left(char *str);
char *str_trim_right(char *str);
char *str_trim(char *str);
char *str_replace(const char *str, const char *pattern, const char *repl);
char *str_join(const char *items[], size_t size, const char *sep);
char *str_cpy(char *dst, const char *src, const char *end);

/*
** jtbuf
*/ 

typedef char *jtbuf;

................................................................................
jtbuf jtbuf_reset(jtbuf buffer);
void jtbuf_free(jtbuf buffer);

size_t jtbuf_size(jtbuf buffer);

jtbuf jtbuf_prepend(jtbuf buffer, void *data, size_t size);
jtbuf jtbuf_prepend_str(jtbuf buffer, const char *str);
jtbuf jtbuf_prepend_sprintf(jtbuf buffer, const char *format, ...);
jtbuf jtbuf_append(jtbuf buffer, void *data, size_t size);
jtbuf jtbuf_append_str(jtbuf buffer, const char *str);
jtbuf jtbuf_append_sprintf(jtbuf buffer, const char *format, ...);

jtbuf jtbuf_trim_left(jtbuf buffer);
jtbuf jtbuf_trim_right(jtbuf buffer);
jtbuf jtbuf_trim(jtbuf buffer);

#endif

Changes to src/lib/jtbuf.c.


1

2
3

4
5
6
7
8
9
10
..
88
89
90
91
92
93
94



















95
96
97
98
99
100
101
...
110
111
112
113
114
115
116



















117
118
119
120
121
122
123

#include <ctype.h>

#include <stdlib.h>
#include <string.h>

#include "jt.h"

struct jtbuf_head {
	size_t size;
	size_t cap;
};

................................................................................
}

jtbuf
jtbuf_append_str(jtbuf buffer, const char *str)
{
	return jtbuf_append(buffer, (void *) str, strlen(str));
}




















jtbuf
jtbuf_prepend(jtbuf buffer, void *data, size_t size)
{
	size_t avail;

	avail = jtbuf_avail(buffer);
................................................................................
}

jtbuf
jtbuf_prepend_str(jtbuf buffer, const char *str)
{
	return jtbuf_prepend(buffer, (void *) str, strlen(str));
}




















jtbuf
jtbuf_trim_left(jtbuf buffer)
{
	char *pos = buffer;

	while (isspace(*pos))
>

>


>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
..
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
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#define _GNU_SOURCE  /* for vasprintf under linux */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "jt.h"

struct jtbuf_head {
	size_t size;
	size_t cap;
};

................................................................................
}

jtbuf
jtbuf_append_str(jtbuf buffer, const char *str)
{
	return jtbuf_append(buffer, (void *) str, strlen(str));
}

jtbuf
jtbuf_append_sprintf(jtbuf buffer, const char *format, ...)
{
	va_list argp;
	char *str = NULL;
	int res;

	va_start(argp, format);
	res = vasprintf(&str, format, argp);
	if (res == -1)
		die("vasprintf error:");
	va_end(argp);

	buffer = jtbuf_append_str(buffer, str);
	xfree(str);

	return buffer;
}

jtbuf
jtbuf_prepend(jtbuf buffer, void *data, size_t size)
{
	size_t avail;

	avail = jtbuf_avail(buffer);
................................................................................
}

jtbuf
jtbuf_prepend_str(jtbuf buffer, const char *str)
{
	return jtbuf_prepend(buffer, (void *) str, strlen(str));
}

jtbuf
jtbuf_prepend_sprintf(jtbuf buffer, const char *format, ...)
{
	va_list argp;
	char *str = NULL;
	int res;

	va_start(argp, format);
	res = vasprintf(&str, format, argp);
	if (res == -1)
		die("vasprintf error:");
	va_end(argp);

	buffer = jtbuf_prepend_str(buffer, str);
	xfree(str);

	return buffer;
}

jtbuf
jtbuf_trim_left(jtbuf buffer)
{
	char *pos = buffer;

	while (isspace(*pos))

Changes to src/lib/jtstr.c.

79
80
81
82
83
84
85













		pos += strlen(sep);
		memcpy(pos, items[i], strlen(items[i]));
		pos += strlen(items[i]);
	}

	return str;
}




















>
>
>
>
>
>
>
>
>
>
>
>
>
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		pos += strlen(sep);
		memcpy(pos, items[i], strlen(items[i]));
		pos += strlen(items[i]);
	}

	return str;
}

char *
str_cpy(char *dst, const char *src, const char *end)
{
	if (dst >= end)
		return dst;

	while (*src && dst < end - 1)
		*dst++ = *src++;
	*dst = '\0';

	return dst;
}