toolbox

Check-in [93148cea01]
Login

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

Overview
Comment:lib: redesign jtlst
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 93148cea014b199ef3f6848dcca46625d869176533748b9523ed1213d960365a
User & Date: jef 2020-07-17 18:59:48
Context
2020-07-17
19:00
retab: adapt to new jtlst structure check-in: 80511aebc8 user: jef tags: trunk
18:59
lib: redesign jtlst check-in: 93148cea01 user: jef tags: trunk
2020-07-14
12:07
radios-check: release resources acquired by curl_global_init() check-in: 33c82f54d0 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/include/jt.h.

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
jtbuf jtbuf_trim_right(jtbuf buffer);
jtbuf jtbuf_trim(jtbuf buffer);

/*
** jtlst
*/

typedef struct jtlstnode {
	void *data;
	TAILQ_ENTRY(jtlstnode) next;
} jtlstnode;

TAILQ_HEAD(jt_tailq_list, jtlstnode);

typedef struct jtlst {
	size_t size;
	struct jt_tailq_list list;
} jtlst;

jtlst *jtlst_new();
jtlst *jtlst_clone(jtlst *arr);
void jtlst_free(jtlst *arr);
void jtlst_free_all(jtlst *arr);

void jtlst_prepend(jtlst *arr, void *data);
void jtlst_append(jtlst *arr, void *data);

int jtlst_pop(jtlst *arr, void **data);
jtlstnode *jtlst_iter(jtlst *arr);
int jtlst_next(jtlstnode **node, void **data);

size_t jtlst_size(jtlst *arr);

// scary macro to walk a jtlst list in a
// convenient manner from the user perspective
#define __jtlstnode JTGENSYM()
#define JTLST_FOREACH(value, arr) \
         struct jtlstnode * __jtlstnode; \
         for (__jtlstnode = TAILQ_FIRST((&arr->list)); \
                 (void) (__jtlstnode && ((value) = __jtlstnode->data)), __jtlstnode; \
                 __jtlstnode = TAILQ_NEXT(__jtlstnode, next))


/*
** str
*/ 






char *str_del_prefix(char *str, const char *prefix);
char *str_del_suffix(char *str, const char *suffix);

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_cpy(char *dst, const char *src, const char *end);
char *str_repeat(char *pattern, size_t n);

char *str_join(const char *items[], size_t size, const char *sep);
jtlst *str_split(const char *src, const char *sep);

#endif







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>




>
>
>
>
>













|


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
jtbuf jtbuf_trim_right(jtbuf buffer);
jtbuf jtbuf_trim(jtbuf buffer);

/*
** jtlst
*/

struct jtlst_node {
	struct jtlst_node *prev;
	struct jtlst_node *next;
};

struct jtlst {
	size_t offset;
	size_t size;
	struct jtlst_node head;
};

#define jtlst_init(list, type, member) \
	jtlst_init_offset(list, offsetof(type, member))

struct jtlst *jtlst_new();
void jtlst_init_offset(struct jtlst *list, size_t offset);
void jtlst_prepend(struct jtlst *list, struct jtlst_node *node);
void jtlst_append(struct jtlst *list, struct jtlst_node *node);
void *jtlst_pop(struct jtlst *list);
size_t jtlst_size(struct jtlst *list);
void jtlst_free(struct jtlst *list);

#define jtlst_foreach(elem, list) \
	for (struct jtlst_node *jtlst_it_##elem = (list)->head.next; \
			jtlst_it_##elem != &(list)->head \
			&& ((elem) = ((void *) ((char *) (jtlst_it_##elem) - (list)->offset))); \
			jtlst_it_##elem = (jtlst_it_##elem)->next)

struct jtlst_iter {
	struct jtlst *list;
	struct jtlst_node *current;
};

void jtlst_iter(struct jtlst *list, struct jtlst_iter *iter);
void * jtlst_iter_next(struct jtlst_iter *iter);

/*
** str
*/ 

struct jtlst_str {
	struct jtlst_node node;
	char *str;
};

char *str_del_prefix(char *str, const char *prefix);
char *str_del_suffix(char *str, const char *suffix);

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_cpy(char *dst, const char *src, const char *end);
char *str_repeat(char *pattern, size_t n);

char *str_join(const char *items[], size_t size, const char *sep);
void str_split(const char *src, const char *sep, struct jtlst *head);

#endif

Changes to src/lib/jtlst.c.

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
#include "jt.h"

jtlst *

jtlst_new()
{
	jtlst *arr;


	arr = xcalloc(1, sizeof(jtlst));
	TAILQ_INIT(&arr->list);

	return arr;
}



jtlst *
jtlst_clone(jtlst *arr)





{
	struct jtlst *clone;
	void *data;


	clone = jtlst_new();
	JTLST_FOREACH(data, arr)
		jtlst_append(clone, data);






	return clone;
}

void
jtlst_free(jtlst *arr)

{
	struct jtlstnode *e;

	if (arr == NULL)

		return;

	while (! TAILQ_EMPTY(&arr->list)) {
		e = TAILQ_FIRST(&arr->list);
		TAILQ_REMOVE(&arr->list, e, next);
		xfree(e);



	}
	xfree(arr);







}

void
jtlst_free_all(jtlst *arr)
{
	struct jtlstnode *e;

	if (arr == NULL)
		return;

	while (! TAILQ_EMPTY(&arr->list)) {
		e = TAILQ_FIRST(&arr->list);
		TAILQ_REMOVE(&arr->list, e, next);
		xfree(e->data);
		xfree(e);
	}
	xfree(arr);
}

void
jtlst_prepend(jtlst *arr, void *data)
{


	struct jtlstnode *e;

	e = xcalloc(1, sizeof(struct jtlstnode));
	e->data = data;
	TAILQ_INSERT_HEAD(&arr->list, e, next);
	arr->size++;
}

void
jtlst_append(jtlst *arr, void *data)
{
	struct jtlstnode *e;

	e = xcalloc(1, sizeof(struct jtlstnode));
	e->data = data;
	TAILQ_INSERT_TAIL(&arr->list, e, next);
	arr->size++;
}

int
jtlst_pop(jtlst *arr, void **data)
{
	jtlstnode *e;

	if (TAILQ_EMPTY(&arr->list))

		return 0;

	e = TAILQ_FIRST(&arr->list);
	*data = e->data;
	TAILQ_REMOVE(&arr->list, e, next);
	xfree(e);
	arr->size--;


	return 1;

}

size_t
jtlst_size(jtlst *arr)
{
	return arr->size;
}

jtlstnode *
jtlst_iter(jtlst *arr)
{
	return TAILQ_FIRST(&arr->list);
}

int
jtlst_next(jtlstnode **node, void **data)
{
	if (*node == NULL)
		return 0;
	*data = (*node)->data;
	*node = TAILQ_NEXT(*node, next);

	return 1;
}


<
>
|

|
<
>
|
|
|
<
|
>
>
|
<
|
>
>
>
>
>
|
<
|
>
|
<
<
<
>
>
>
>
>
|
<
|
<
|
<
>

|

<
>
|

<
<
<
<
>
>
>
|
<
>
>
>
>
>
>
>



|

|

|
<
<
<
<
<
<
|
<
|



|

<
>
|
|
<
<
<
<
|
<
|
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
>
|

<
<
<
<
<
>

<
>

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
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























#include "jt.h"


void
jtlst_init_offset(struct jtlst *list, size_t offset)
{
	list->size = 0;

	list->offset = offset;
	list->head.next = &list->head;
	list->head.prev = &list->head;
}


void
jtlst_prepend(struct jtlst *list, struct jtlst_node *node)
{

	list->head.next->prev = node;
	node->prev = &list->head;
	node->next = list->head.next;
	list->head.next = node;
	list->size++;
}


void
jtlst_append(struct jtlst *list, struct jtlst_node *node)
{



	list->head.prev->next = node;
	node->prev = list->head.prev;
	node->next = &list->head;
	list->head.prev = node;
	list->size++;
}



void *

jtlst_pop(struct jtlst *list)
{
	struct jtlst_node *next;


	if (&list->head == list->head.next)
		return NULL;





	next = list->head.next;
	list->head.next = list->head.next->next;
	list->size--;


	return (void *) ((char *)next - list->offset);
}

size_t
jtlst_size(struct jtlst *list)
{
	return list->size;
}

void
jtlst_free(struct jtlst *list)
{
	void *elem;

	while ((elem = jtlst_pop(list)))






		xfree(elem);

	xfree(list);
}

void
jtlst_iter(struct jtlst *list, struct jtlst_iter *iter)
{

	iter->list = list;
	iter->current = &list->head;
}






void *


jtlst_iter_next(struct jtlst_iter *iter)
{












	if (iter->current->next == &iter->list->head)
		return NULL;






	iter->current = iter->current->next;


	return (void *) ((char *) iter->current - iter->list->offset);
}























Changes to src/lib/jtstr.c.

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
		memcpy(p, pattern, len);
		p += len;
	}

	return res;
}

jtlst *

str_split(const char *str, const char *sep)
{
	jtlst *arr;
	char *field;
	const char *p = str;

	arr = jtlst_new();
	while ((p = strstr(str, sep)) != NULL) {

		field = xstrndup(str, p - str);
		jtlst_append(arr, field);
		str = p + strlen(sep);
	}

	jtlst_append(arr, xstrdup(str));


	return arr;
}








<
>
|

<
|


<

>
|
|


>
|
>
|
<
|
<
137
138
139
140
141
142
143

144
145
146

147
148
149

150
151
152
153
154
155
156
157
158
159

160

		memcpy(p, pattern, len);
		p += len;
	}

	return res;
}


void
str_split(const char *str, const char *sep, struct jtlst *head)
{

	struct jtlst_str *field;
	const char *p = str;


	while ((p = strstr(str, sep)) != NULL) {
		field = xcalloc(1, sizeof(struct jtlst_str));
		field->str = xstrndup(str, p - str);
		jtlst_append(head, &field->node);
		str = p + strlen(sep);
	}
	field = xcalloc(1, sizeof(struct jtlst_str));
	field->str = xstrdup(str);
	jtlst_append(head, &field->node);
}