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
129
130
131
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
165
166
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xdvi-config.h"
#include "xdvi.h"
#include "util.h"
#include "string_list.h"
/*
* Helper functions for string lists
*/
char *
string_list_to_str(char **list, const char *sep)
{
char *result = xstrdup("");
int i;
for (i = 0; list[i] != NULL; i++) {
result = xstrcat(result, list[i]);
result = xstrcat(result, sep);
}
return result;
}
void
string_list_print(char **list)
{
int i;
for (i = 0; list != NULL && list[i] != NULL; i++) {
fprintf(stderr, "List %d: |%s|\n", i, list[i]);
}
}
/*
* Reorder the list so that str (which is supposed to occur in the list somewhere)
* is the first element.
*/
char **
string_list_reorder(char **list, char *str)
{
size_t i;
char *tmp;
Boolean found = False;
for (i = 0; list[i] != NULL; i++) {
if (strcmp(list[i], str) == 0) {
found = True;
break;
}
}
if (!found) {
XDVI_ERROR((stderr, "Item `%s' not found in list!\n", str));
return list;
}
tmp = list[0];
list[0] = str;
list[i] = tmp;
return list;
}
/*
* Move the item at position idx to the start of the list,
* by iteratively swapping it with its predecessor.
*/
char **
string_list_move_to_start(char **list, size_t idx)
{
size_t i;
#if 0
for (i = 0; list[i] != NULL; i++) {
fprintf(stderr, "list before: %d: |%s|\n", i, list[i]);
}
#endif
for (i = idx; i > 0; i--) {
char *tmp = list[i];
list[i] = list[i - 1];
list[i - 1] = tmp;
}
#if 0
for (i = 0; list[i] != NULL; i++) {
fprintf(stderr, "list after: %d: |%s|\n", i, list[i]);
}
#endif
return list;
}
/*
* Rotate the list so that the first element is shifted to the end,
* and all next elements are moved down by 1; e.g.:
* before rotate: a b c d e f
* after rotate: b c d e f a
*/
char **
string_list_rotate_down(char **list)
{
size_t i;
char *tmp;
tmp = list[0];
if (tmp == NULL) {
return list;
}
for (i = 1; list[i] != NULL; i++) {
list[i - 1] = list[i];
}
list[i - 1] = tmp;
return list;
}
/*
* Rotate the list so that the last element is shifted to the beginning,
* and all next elements are moved up by 1; e.g.:
* before rotate: a b c d e f
* after rotate: f a b c d e
*/
char **
string_list_rotate_up(char **list)
{
size_t i;
char *tmp = NULL;
if (list[0] == NULL) {
return list;
}
/* go to end */
for (i = 0; list[i] != NULL; i++) { ; }
i--; /* get last index */
tmp = list[i];
for (; i > 0; i--) {
list[i] = list[i - 1];
}
list[0] = tmp;
return list;
}
char **
string_list_prepend(char **list, const char *str)
{
int i, k;
/* reallocate with larger capacity */
for (i = 0; list[i] != NULL; i++) { ; }
i++;
list = xrealloc(list, (i + 1) * sizeof *list);
/* shift old contents down */
for (k = i; k > 0; k--) {
list[k] = list[k - 1];
}
/* add new element at beginning */
list[0] = xstrdup(str);
return list;
}
|