1 /* wmbday
2 * Copyright (C) 2003-2005 astratis <steffen@x-berg.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public Licensse as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "ring.h"
20 #include "date.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26
27 static struct ring* start = NULL; /* first element in ring, so first birthday in year */
28 static struct ring* end = NULL; /* last element in ring*/
29 static struct ring* current = NULL; /* points to person, whose birthday is next */
30 static int count = 0; /* number of elements in ring */
31
32
33 /* adds a new birthday to the ring */
ring_add(char * name,int day,int month,int year)34 void ring_add(char* name, int day, int month, int year) {
35 struct ring* newelement = NULL;
36 struct ring* temp = NULL;
37 struct ring* temp_old = NULL;
38 int i;
39
40 if((newelement = malloc(sizeof(struct ring))) == NULL) {
41 fprintf(stderr, "Couldn't allocate memory\n");
42 exit(1);
43 }
44
45 memset(newelement->name, '\0', sizeof(newelement->name));
46 strncpy(newelement->name, name, sizeof(newelement->name)-1);
47 newelement->day = day;
48 newelement->month = month;
49 newelement->year = year;
50
51
52 /* there is no ring x-isting */
53 if(count == 0) {
54 start = newelement;
55 end = newelement;
56 newelement->next = NULL;
57 ++count;
58 return;
59 }
60
61 /* there is already a ring, so we need to find the right place */
62 i = 0;
63 temp = start;
64
65 while(i != count && compare_dates(temp->day, temp->month, day, month) == -1) {
66 temp_old = temp;
67 temp = temp->next;
68 ++i;
69 }
70
71 /* new start */
72 if(i == 0) {
73 if(start->next == NULL) {
74 start->next = newelement;
75 }
76 newelement->next = start;
77 start = newelement;
78 end->next = start;
79 ++count;
80 return;
81 }
82
83 /* new end */
84 if(i == count) {
85 temp_old->next = newelement;
86 newelement->next = start;
87 end = newelement;
88 ++count;
89 return;
90 }
91
92 /* put it somewhere in the middle */
93 temp_old->next = newelement;
94 newelement->next = temp;
95 ++count;
96
97 return;
98 }
99
100
101 /* makes "current" point to the person, whose birthday is next */
ring_goto_date(int day,int month)102 void ring_goto_date(int day, int month) {
103 int i = 0;
104
105 current = start;
106
107 while(i < count && compare_dates(current->day, current->month, day, month) == -1) {
108 if(current->next == NULL)
109 break;
110
111 current = current->next;
112
113 ++i;
114 }
115
116 return;
117 }
118
119
ring_check_bday(int day,int month)120 int ring_check_bday(int day, int month) {
121 int i = 0;
122 int bday_count = 0;
123 struct ring* temp;
124
125 if(current == NULL)
126 return -1;
127
128 temp = current;
129
130 while(i < count && compare_dates(temp->day, temp->month, day, month) == 0) {
131 ++bday_count;
132 temp = temp->next;
133 }
134
135 return bday_count;
136 }
137
138
139 /* puts next "count" birthdays into "target" */
ring_get_next(struct ring * target[],int number)140 void ring_get_next(struct ring* target[], int number) {
141 struct ring* temp;
142 int i = 0;
143
144 temp = current;
145
146 while(i<number && i<count) {
147 target[i] = temp;
148 ++i;
149 if(temp->next == NULL)
150 break;
151
152 temp = temp->next;
153 }
154
155 while(i<number) {
156 target[i] = NULL;
157 ++i;
158 }
159 return;
160 }
161
162
163 /* clean up whole list */
ring_clear(void)164 void ring_clear(void) {
165 struct ring* temp;
166
167 temp = start;
168
169 while(count < 0) {
170 free(temp);
171 --count;
172 }
173
174 start = end = current = NULL;
175 count = 0;
176
177 return;
178 }
179