]>
Commit | Line | Data |
---|---|---|
1992b825 | 1 | /* $Header$ |
2 | * | |
3 | * Generic Queue Routines | |
babbc197 | 4 | * |
5 | * (c) Copyright 1988 by the Massachusetts Institute of Technology. | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
1992b825 | 8 | */ |
9 | ||
babbc197 | 10 | #include <mit-copyright.h> |
8defc06b | 11 | #include <moira.h> |
8fd777cf | 12 | #include <string.h> |
a43ce477 | 13 | #include <stdlib.h> |
24582af9 | 14 | |
5eaef520 | 15 | struct save_queue *sq_create(void) |
1992b825 | 16 | { |
44d12d58 | 17 | struct save_queue *sq; |
5eaef520 | 18 | |
19 | sq = malloc(sizeof(struct save_queue)); | |
20 | if (!sq) | |
21 | return sq; | |
22 | sq->q_next = sq; | |
23 | sq->q_prev = sq; | |
24 | sq->q_lastget = 0; | |
25 | return sq; | |
1992b825 | 26 | } |
27 | ||
44d12d58 | 28 | int sq_save_data(struct save_queue *sq, char *data) |
1992b825 | 29 | { |
44d12d58 | 30 | struct save_queue *q; |
5eaef520 | 31 | |
32 | q = malloc(sizeof(struct save_queue)); | |
33 | if (!q) | |
34 | return 0; | |
35 | q->q_next = sq; | |
36 | q->q_prev = sq->q_prev; | |
37 | sq->q_prev->q_next = q; | |
38 | sq->q_prev = q; | |
39 | q->q_data = data; | |
40 | return 1; | |
1992b825 | 41 | } |
42 | ||
44d12d58 | 43 | int sq_save_args(int argc, char *argv[], struct save_queue *sq) |
1992b825 | 44 | { |
44d12d58 | 45 | char **argv_copy; |
46 | int i, n; | |
5eaef520 | 47 | |
48 | argv_copy = malloc(argc * sizeof(char *)); | |
49 | if (!argv_copy) | |
50 | return 0; | |
51 | for (i = 0; i < argc; i++) | |
52 | { | |
53 | n = strlen(argv[i]) + 1; | |
54 | argv_copy[i] = malloc(n); | |
55 | if (!argv_copy[i]) | |
56 | { | |
57 | for (i--; i >= 0; i--) | |
58 | free(argv_copy[i]); | |
59 | free(argv_copy); | |
9d627547 | 60 | return 0; |
61 | } | |
5eaef520 | 62 | memcpy(argv_copy[i], argv[i], n); |
1992b825 | 63 | } |
64 | ||
5eaef520 | 65 | return sq_save_data(sq, (char *)argv_copy); |
1992b825 | 66 | } |
67 | ||
44d12d58 | 68 | int sq_save_unique_data(struct save_queue *sq, char *data) |
1992b825 | 69 | { |
44d12d58 | 70 | struct save_queue *q; |
1992b825 | 71 | |
5eaef520 | 72 | for (q = sq->q_next; q != sq; q = q->q_next) |
73 | { | |
74 | if (q->q_data == data) | |
75 | return 1; | |
76 | } | |
1992b825 | 77 | |
5eaef520 | 78 | return sq_save_data(sq, data); |
1992b825 | 79 | } |
80 | ||
44d12d58 | 81 | int sq_save_unique_string(struct save_queue *sq, char *data) |
1992b825 | 82 | { |
44d12d58 | 83 | struct save_queue *q; |
1992b825 | 84 | |
5eaef520 | 85 | for (q = sq->q_next; q != sq; q = q->q_next) |
86 | { | |
87 | if (!strcmp(q->q_data, data)) | |
88 | return 1; | |
89 | } | |
1992b825 | 90 | |
5eaef520 | 91 | return sq_save_data(sq, data); |
1992b825 | 92 | } |
93 | ||
44d12d58 | 94 | int sq_get_data(struct save_queue *sq, char **data) |
1992b825 | 95 | { |
5eaef520 | 96 | if (sq->q_lastget == (struct save_queue *)0) |
97 | sq->q_lastget = sq->q_next; | |
98 | else | |
99 | sq->q_lastget = sq->q_lastget->q_next; | |
100 | ||
101 | if (sq->q_lastget == sq) | |
102 | return 0; | |
103 | *data = sq->q_lastget->q_data; | |
104 | return 1; | |
1992b825 | 105 | } |
106 | ||
44d12d58 | 107 | int sq_remove_data(struct save_queue *sq, char **data) |
bfa8d940 | 108 | { |
5eaef520 | 109 | if (sq->q_next != sq) |
110 | { | |
111 | *data = sq->q_next->q_data; | |
112 | sq->q_next = sq->q_next->q_next; | |
113 | free(sq->q_next->q_prev); | |
114 | sq->q_next->q_prev = sq; | |
115 | return 1; | |
bfa8d940 | 116 | } |
5eaef520 | 117 | return 0; |
bfa8d940 | 118 | } |
119 | ||
44d12d58 | 120 | int sq_empty(struct save_queue *sq) |
bfa8d940 | 121 | { |
5eaef520 | 122 | if (sq->q_next == sq) |
123 | return 1; | |
124 | else | |
125 | return 0; | |
bfa8d940 | 126 | } |
127 | ||
44d12d58 | 128 | sq_destroy(struct save_queue *sq) |
1992b825 | 129 | { |
44d12d58 | 130 | struct save_queue *q; |
1992b825 | 131 | |
5eaef520 | 132 | for (q = sq->q_next; q != sq; q = sq->q_next) |
133 | { | |
134 | sq->q_next = q->q_next; | |
135 | free(q); | |
1992b825 | 136 | } |
5eaef520 | 137 | free(sq); |
1992b825 | 138 | } |
139 |