]> andersk Git - moira.git/blob - lib/sq.c
eliminate use of the `register' keyword: let the compiler decide
[moira.git] / lib / sq.c
1 /* $Header$
2  *
3  * Generic Queue Routines
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>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12 #include <string.h>
13 #include <stdlib.h>
14
15 struct save_queue *sq_create(void)
16 {
17   struct save_queue *sq;
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;
26 }
27
28 int sq_save_data(struct save_queue *sq, char *data)
29 {
30   struct save_queue *q;
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;
41 }
42
43 int sq_save_args(int argc, char *argv[], struct save_queue *sq)
44 {
45   char **argv_copy;
46   int i, n;
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);
60           return 0;
61         }
62       memcpy(argv_copy[i], argv[i], n);
63     }
64
65   return sq_save_data(sq, (char *)argv_copy);
66 }
67
68 int sq_save_unique_data(struct save_queue *sq, char *data)
69 {
70   struct save_queue *q;
71
72   for (q = sq->q_next; q != sq; q = q->q_next)
73     {
74       if (q->q_data == data)
75         return 1;
76     }
77
78   return sq_save_data(sq, data);
79 }
80
81 int sq_save_unique_string(struct save_queue *sq, char *data)
82 {
83   struct save_queue *q;
84
85   for (q = sq->q_next; q != sq; q = q->q_next)
86     {
87       if (!strcmp(q->q_data, data))
88         return 1;
89     }
90
91   return sq_save_data(sq, data);
92 }
93
94 int sq_get_data(struct save_queue *sq, char **data)
95 {
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;
105 }
106
107 int sq_remove_data(struct save_queue *sq, char **data)
108 {
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;
116     }
117   return 0;
118 }
119
120 int sq_empty(struct save_queue *sq)
121 {
122   if (sq->q_next == sq)
123     return 1;
124   else
125     return 0;
126 }
127
128 sq_destroy(struct save_queue *sq)
129 {
130   struct save_queue *q;
131
132   for (q = sq->q_next; q != sq; q = sq->q_next)
133     {
134       sq->q_next = q->q_next;
135       free(q);
136     }
137   free(sq);
138 }
139
This page took 0.051282 seconds and 5 git commands to generate.