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