Debian Codegraph @

texk/xdvik/string_list.c

raw
1 #include "xdvi-config.h"
2 #include "xdvi.h"
3 #include "util.h"
4 #include "string_list.h"
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10
11 /*
12 * Helper functions for string lists
13 */
14 char *
15 string_list_to_str(char **list, const char *sep)
16 {
17 char *result = xstrdup("");
18 int i;
19
20 for (i = 0; list[i] != NULL; i++) {
21 result = xstrcat(result, list[i]);
22 result = xstrcat(result, sep);
23 }
24 return result;
25 }
26
27
28 void
29 string_list_print(char **list)
30 {
31 int i;
32
33 for (i = 0; list != NULL && list[i] != NULL; i++) {
34 fprintf(stderr, "List %d: |%s|\n", i, list[i]);
35 }
36 }
37
38 /*
39 * Reorder the list so that str (which is supposed to occur in the list somewhere)
40 * is the first element.
41 */
42 char **
43 string_list_reorder(char **list, char *str)
44 {
45 size_t i;
46 char *tmp;
47 Boolean found = False;
48
49 for (i = 0; list[i] != NULL; i++) {
50 if (strcmp(list[i], str) == 0) {
51 found = True;
52 break;
53 }
54 }
55
56 if (!found) {
57 XDVI_ERROR((stderr, "Item `%s' not found in list!\n", str));
58 return list;
59 }
60
61 tmp = list[0];
62 list[0] = str;
63 list[i] = tmp;
64 return list;
65 }
66
67 /*
68 * Move the item at position idx to the start of the list,
69 * by iteratively swapping it with its predecessor.
70 */
71 char **
72 string_list_move_to_start(char **list, size_t idx)
73 {
74 size_t i;
75
76 #if 0
77 for (i = 0; list[i] != NULL; i++) {
78 fprintf(stderr, "list before: %d: |%s|\n", i, list[i]);
79 }
80 #endif
81
82 for (i = idx; i > 0; i--) {
83 char *tmp = list[i];
84 list[i] = list[i - 1];
85 list[i - 1] = tmp;
86 }
87
88 #if 0
89 for (i = 0; list[i] != NULL; i++) {
90 fprintf(stderr, "list after: %d: |%s|\n", i, list[i]);
91 }
92 #endif
93
94 return list;
95 }
96
97 /*
98 * Rotate the list so that the first element is shifted to the end,
99 * and all next elements are moved down by 1; e.g.:
100 * before rotate: a b c d e f
101 * after rotate: b c d e f a
102 */
103 char **
104 string_list_rotate_down(char **list)
105 {
106 size_t i;
107 char *tmp;
108
109 tmp = list[0];
110 if (tmp == NULL) {
111 return list;
112 }
113 for (i = 1; list[i] != NULL; i++) {
114 list[i - 1] = list[i];
115 }
116 list[i - 1] = tmp;
117
118 return list;
119 }
120
121 /*
122 * Rotate the list so that the last element is shifted to the beginning,
123 * and all next elements are moved up by 1; e.g.:
124 * before rotate: a b c d e f
125 * after rotate: f a b c d e
126 */
127 char **
128 string_list_rotate_up(char **list)
129 {
130 size_t i;
131 char *tmp = NULL;
132
133 if (list[0] == NULL) {
134 return list;
135 }
136 /* go to end */
137 for (i = 0; list[i] != NULL; i++) { ; }
138 i--; /* get last index */
139
140 tmp = list[i];
141 for (; i > 0; i--) {
142 list[i] = list[i - 1];
143 }
144 list[0] = tmp;
145
146 return list;
147 }
148
149 char **
150 string_list_prepend(char **list, const char *str)
151 {
152 int i, k;
153 /* reallocate with larger capacity */
154 for (i = 0; list[i] != NULL; i++) { ; }
155 i++;
156 list = xrealloc(list, (i + 1) * sizeof *list);
157
158 /* shift old contents down */
159 for (k = i; k > 0; k--) {
160 list[k] = list[k - 1];
161 }
162 /* add new element at beginning */
163 list[0] = xstrdup(str);
164
165 return list;
166 }