-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
72 lines (66 loc) · 3.12 KB
/
main.c
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
#include <CoreFoundation/CoreFoundation.h>
#include <libical/ical.h>
#include <sqlite3.h>
int main (int argc, const char * argv[]) {
if (argc > 2) {
// Initialize new calendar
icalcomponent *calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
sqlite3 *database;
sqlite3_stmt *statement;
char *sql;
int row = 0;
// Open database and select all from events
sqlite3_open(argv[1], &database);
sql = "SELECT summary, description, start_date, end_date, start_tz, url, location FROM Event";
sqlite3_prepare_v2(database, sql, strlen(sql)+1, &statement, NULL);
while (1) {
int s;
// Step through each event
s = sqlite3_step(statement);
if (s == SQLITE_ROW) {
// Assemble an ical event from each entry - this implementation only supports name, description, start date, end date, time zone, URL, and location, but it would be easy to add more
icalcomponent *event = icalcomponent_new(ICAL_VEVENT_COMPONENT);
icalproperty *nameProperty = icalproperty_new_summary((const char *)sqlite3_column_text(statement, 0));
icalproperty *descriptionProperty = icalproperty_new_description((const char *)sqlite3_column_text(statement, 1));
icalproperty *startDate = icalproperty_new_dtstart(icaltime_from_timet(978292800+atoi((const char *)sqlite3_column_text(statement, 2)), 0));
icalproperty *endDate = icalproperty_new_dtend(icaltime_from_timet(978292800+atoi((const char *)sqlite3_column_text(statement, 3)), 0));
icalproperty *timezoneProperty = icalproperty_new_tzid((const char *)sqlite3_column_text(statement, 4));
icalproperty *URLProperty = icalproperty_new_url((const char *)sqlite3_column_text(statement, 5));
icalproperty *locationProperty = icalproperty_new_location((const char *)sqlite3_column_text(statement, 6));
icalcomponent_add_property(event, nameProperty);
icalcomponent_add_property(event, descriptionProperty);
icalcomponent_add_property(event, startDate);
icalcomponent_add_property(event, endDate);
icalcomponent_add_property(event, timezoneProperty);
icalcomponent_add_property(event, URLProperty);
icalcomponent_add_property(event, locationProperty);
// Add ical event to calendar
icalcomponent_add_component(calendar, event);
row++;
}
else if (s == SQLITE_DONE) {
printf("Finished reading sqlite database.\n");
break;
}
else {
fprintf(stderr, "Failed to open sqlite database.\n");
exit(1);
}
}
// Write ical calendar to file
FILE *file = fopen(argv[2], "w");
if (file == NULL) {
fprintf(stderr, "Failed to open output file.\n");
} else {
fprintf(file, "%s", icalcomponent_as_ical_string(calendar));
fclose(file);
printf("Successfully wrote ical calendar!\n");
}
return 0;
} else if (argc == 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) {
printf("sqlite2ical converts iPhone sqlite format calendars to iCal/vCal/ICS format\n\nsqlite2ical <input.sqlitedb> <output.ics>\n\nCopyright (c) 2011 Ari Weinstein/Squish Software\n");
} else {
printf("sqlite2ical\n\nIncorrect arguments. Try sqlite2ical -h.\n\nCopyright (c) 2011 Ari Weinstein/Squish Software\n");
}
return 0;
}