File: icalmerge.c

package info (click to toggle)
ical2html 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 440 kB
  • ctags: 104
  • sloc: sh: 1,048; ansic: 670; makefile: 32
file content (315 lines) | stat: -rw-r--r-- 8,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * icalfilter -- filter an iCalendar file
 *
 * Author: Bert Bos <bert@w3.org>
 * Created: 30 Sep 2002
 */

#include "config.h"
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
#include <ctype.h>
#include <libical/icalcomponent.h>
#include <libical/icalparser.h>
#include <libical/icalset.h>
#include <libical/icalfileset.h>


#define PRODID "-//W3C//NONSGML icalmerge " VERSION "//EN"

#define ERR_OUT_OF_MEM 1	/* Program exit codes */
#define ERR_USAGE 2
#define ERR_DATE 3
#define ERR_PARSE 4
#define ERR_FILEIO 5
#define ERR_ICAL_ERR 6		/* Other error */
#define ERR_HASH 7		/* Hash failure */

#define USAGE "Usage: ical2html input [input...] output\n\
  inputs and output are iCalendar files\n"

/* Long command line options */
static struct option options[] = {
  {0, 0, 0, 0}
};

#define OPTIONS ""



/* fatal -- print error message and exit with errcode */
static void fatal(int errcode, const char *message,...)
{
  va_list args;
  va_start(args, message);
  vfprintf(stderr, message, args);
  va_end(args);
  exit(errcode);
}


/* debug -- print message on stderr */
static void debug(const char *message,...)
{
#ifdef DEBUG
  va_list args;
  va_start(args, message);
  vfprintf(stderr, message, args);
  va_end(args);
#endif /* DEBUG */
}


#ifdef HAVE_SEARCH_H
#  include <search.h>
#else
/*
 * hsearch() on Mac OS X 10.1.2 appears to be broken: there is no
 * search.h, there is a search() in the C library, but it doesn't work
 * properly. We include some hash functions here, protected by
 * HAVE_SEARCH_H. Hopefully when search.h appears in Mac OS X,
 * hsearch() will be fixed at the same time...
 */

typedef struct entry {char *key; void *data;} ENTRY;
typedef enum {FIND, ENTER} ACTION;

static ENTRY *htab;
static int *htab_index1, *htab_index2;
static unsigned int htab_size = 0;
static unsigned int htab_inited;


/* isprime -- test if n is a prime number */
static int isprime(unsigned int n)
{
  /* Simplistic algorithm, probably good enough for now */
  unsigned int i;
  assert(n % 2);				/* n not even */
  for (i = 3; i * i < n; i += 2) if (n % i == 0) return 0;
  return 1;
}


/* hcreate -- create a hash table for at least nel entries */
static int hcreate(size_t nel)
{
  /* Change nel to next higher prime */
  for (nel |= 1; !isprime(nel); nel += 2) ;

  /* Allocate hash table and array to keep track of initialized entries */
  if (! (htab = malloc(nel * sizeof(*htab)))) return 0;
  if (! (htab_index1 = malloc(nel * sizeof(*htab_index1)))) return 0;
  if (! (htab_index2 = malloc(nel * sizeof(*htab_index2)))) return 0;
  htab_inited = 0;
  htab_size = nel;

  return 1;
}


#if 0
/* hdestroy -- deallocate hash table */
static void hdestroy(void)
{
  assert(htab_size);
  free(htab_index1);
  free(htab_index2);
  free(htab);
  htab_size = 0;
}
#endif


/* hsearch -- search for and/or insert an entry in the hash table */
static ENTRY *hsearch(ENTRY item, ACTION action)
{
  unsigned int hval, i;
  char *p;

  assert(htab_size);				/* There must be a hash table */

  /* Compute a hash value */
#if 1
  /* This function suggested by Dan Bernstein */
  for (hval = 5381, p = item.key; *p; p++) hval = (hval * 33) ^ *p;
#else
  i = hval = strlen(item.key);
  do {i--; hval = (hval << 1) + item.key[i];} while (i > 0);
#endif
  hval %= htab_size;
  /* if (action == ENTER) debug("%d\n", hval); */

  /* Look for either an empty slot or an entry with the wanted key */
  i = hval;
  while (htab_index1[i] < htab_inited
	 && htab_index2[htab_index1[i]] == i
	 && strcmp(htab[i].key, item.key) != 0) {
    i = (i + 1) % htab_size;			/* "Open" hash method */
    if (i == hval) return NULL;			/* Made full round */
  }
  /* Now we either have an empty slot or an entry with the same key */
  if (action == ENTER) {
    htab[i].key = item.key;			/* Put the item in this slot */
    htab[i].data = item.data;
    if (htab_index1[i] >= htab_inited || htab_index2[htab_index1[i]] != i) {
      /* Item was not yet used, mark it as used */
      htab_index1[i] = htab_inited;
      htab_index2[htab_inited] = i;
      htab_inited++;
    }
    return &htab[i];
  } else if (htab_index1[i] < htab_inited && htab_index2[htab_index1[i]] == i)
    return &htab[i];				/* action == FIND, found key */

  return NULL;					/* Found empty slot */
}

#endif /* HAVE_SEARCH_H */


/* merge -- add b to a, keeping only newer entries in case of duplicates */
static void merge(icalcomponent **a, icalcomponent *b)
{
  static int first_time = 1;
  icalcomponent *h, *next;
  icalproperty *mod_a, *mod_b;
  struct icaltimetype modif_a, modif_b;
  icalproperty *uid;
  ENTRY *e, e1;

  /* Create the hash table */
  if (first_time) {
    if (! hcreate(10000)) fatal(ERR_HASH, "%s\n", strerror(errno));
    first_time = 0;
  }

  /* Iterate over VEVENTs */
  for (h = icalcomponent_get_first_component(b, ICAL_VEVENT_COMPONENT);
       h; h = next) {

    next = icalcomponent_get_next_component(b, ICAL_VEVENT_COMPONENT);

    uid = icalcomponent_get_first_property(h, ICAL_UID_PROPERTY);
    /*debug("%s", uid ? icalproperty_get_uid(uid) : "NO UID!?");*/
    if (!uid) continue;				/* Error in iCalendar file */
    e1.key = strdup(icalproperty_get_uid(uid));
    if (! (e = hsearch(e1, FIND))) {

      /* New UID, add the VEVENT to the hash and to the set a */
      icalcomponent_remove_component(b, h); /* Move from b... */
      icalcomponent_add_component(*a, h); /* ... to a */
      e1.data = h;
      if (! (e = hsearch(e1, ENTER)))
	fatal(ERR_OUT_OF_MEM, "No room in hash table\n");
      /*debug(" (added %lx)\n", h);*/

    } else {

      /* Already an entry with this UID, compare modified dates */
      /*debug(" (found %lx", e->data);*/
      mod_a = icalcomponent_get_first_property((icalcomponent*)e->data,
					       ICAL_LASTMODIFIED_PROPERTY);
      if (!mod_a) continue;	/* Hmmm... */
      modif_a = icalproperty_get_lastmodified(mod_a);

      mod_b = icalcomponent_get_first_property(h, ICAL_LASTMODIFIED_PROPERTY);
      if (!mod_b) continue;	/* Hmmm... */
      modif_b = icalproperty_get_lastmodified(mod_b);

      if (icaltime_compare(modif_a, modif_b) == -1) {
	/* a is older than b, so replace it */
	icalcomponent_remove_component(*a, (icalcomponent*)e->data);
	icalcomponent_remove_component(b, h); /* Move from b... */
	icalcomponent_add_component(*a, h); /* ... to a */
	e->data = h;
	/*debug(" replaced)\n");*/
      } else {
	/*debug(" ignored)\n");*/
      }      
      free(e1.key);
    }
  }
}


/* read_stream -- read size bytes into s from stream d */
static char* read_stream(char *s, size_t size, void *d)  
{  
  return fgets(s, size, (FILE*)d); 
} 


/* main */
int main(int argc, char *argv[])
{
  FILE* stream;
  icalcomponent *comp;
  icalparser *parser;
  char c;
  icalset *out;
  icalcomponent *newset;

  /* We handle errors ourselves */
  icalerror_errors_are_fatal = 0;
  icalerrno = 0;

  /* Read commandline */
  while ((c = getopt_long(argc, argv, OPTIONS, options, NULL)) != -1) {
    switch (c) {
    default: fatal(ERR_USAGE, USAGE);
    }
  }

  /* Initialize a new, empty icalendar */
  newset = icalcomponent_vanew(ICAL_VCALENDAR_COMPONENT,
			       icalproperty_new_version("2.0"),
			       icalproperty_new_prodid(PRODID),
			       0);

  /* Loop over remaining file arguments, except the last */
  if (optind >= argc - 1) fatal(ERR_USAGE, USAGE);
  while (optind != argc - 1) {

    /* Open the file */
    stream = fopen(argv[optind], "r");
    if (!stream) fatal(ERR_FILEIO, "%s: %s\n", argv[optind], strerror(errno));

    /* Create a new parser object */
    /* Tell the parser what input stream it should use */
    /* Let the parser read the file and return all components */
    parser = icalparser_new();
    icalparser_set_gen_data(parser, stream);
    if (! (comp = icalparser_parse(parser, read_stream)))
      fatal(ERR_PARSE, "Parse error: %s\n", icalerror_strerror(icalerrno));
    icalparser_free(parser);
    if (fclose(stream) != 0)
      fatal(ERR_FILEIO, "%s: %s\n", argv[optind], strerror(errno));

    /* Add all found components to a hash table, removing duplicates */
    merge(&newset, comp);
    
    optind++;
  }

  /* Get output file name */
  (void) unlink(argv[optind]);	/* Remove output file if it already exists */
  out = icalfileset_new(argv[optind]);
  if (!out) fatal(ERR_FILEIO, "%s: %s\n", argv[optind], strerror(errno));

  /* Put new icalendar in output file, flush to disk and close file */
  icalfileset_add_component(out, newset);
  icalfileset_free(out);

  /* Clean up */
  /* icalcomponent_free(newset); */

  return 0;
}