Added option not to use configured logging in SMSD inject and monitor (bug #1539).
[gammu.git] / tests / cal-read.c
bloba8fb0365ad940315abd84230434b421c4191e07e
1 /**
2 * [iv]Calendar parser testing.
4 * First parameter is location of [iv]Calendar, second location of Gammu backup
5 * how it should be parsed.
7 * Optional third parameter can be used to generate template backup
8 * file.
9 */
10 #include <gammu.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
15 #include "common.h"
17 int main(int argc, char **argv)
19 size_t pos = 0;
20 GSM_ToDoEntry todo;
21 GSM_CalendarEntry cal;
22 GSM_Error error;
23 char buffer[65536];
24 FILE *f;
25 size_t len;
26 gboolean generate = FALSE;
27 GSM_Backup backup;
28 int i, j;
29 GSM_Debug_Info *debug_info;
30 gboolean skipcal, skiptodo;
31 gboolean check_map[MAX(GSM_CALENDAR_ENTRIES, GSM_TODO_ENTRIES)];
33 /* Configure debugging */
34 debug_info = GSM_GetGlobalDebug();
35 GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
36 GSM_SetDebugLevel("textall", debug_info);
38 /* Check parameters */
39 if (argc != 3 && argc != 4) {
40 printf("Not enough parameters!\nUsage: cal-read file.[vi]cs file.backup\n");
41 return 1;
44 /* Check for generating option */
45 if (argc == 4 && strcmp(argv[3], "generate") == 0) {
46 generate = TRUE;
49 /* Open file */
50 f = fopen(argv[1], "r");
51 test_result(f != NULL);
53 /* Read data */
54 len = fread(buffer, 1, sizeof(buffer) - 1, f);
55 test_result(feof(f));
57 /* Zero terminate string */
58 buffer[len] = 0;
60 /* We don't need file any more */
61 fclose(f);
63 todo.Type = 0;
64 cal.Type = 0;
65 todo.Location = 0;
66 cal.Location = 0;
68 /* Parse [iv]Calendar */
69 if (strstr(argv[1], ".ics") != NULL) {
70 error = GSM_DecodeVCALENDAR_VTODO(NULL, buffer, &pos, &cal, &todo, Mozilla_iCalendar, Mozilla_VToDo);
71 } else {
72 error = GSM_DecodeVCALENDAR_VTODO(NULL, buffer, &pos, &cal, &todo, SonyEricsson_VCalendar, SonyEricsson_VToDo);
74 gammu_test_result(error, "GSM_DecodeVCALENDAR_VTODO");
76 /* Generate file if we should */
77 if (generate) {
78 GSM_ClearBackup(&backup);
79 strcpy(backup.Creator, "[iv]Calendar tester");
80 if (cal.EntriesNum != 0) {
81 backup.Calendar[0] = &cal;
82 backup.Calendar[1] = NULL;
83 } else {
84 backup.Calendar[0] = NULL;
86 if (todo.EntriesNum != 0) {
87 backup.ToDo[0] = &todo;
88 backup.ToDo[1] = NULL;
89 } else {
90 backup.ToDo[0] = NULL;
92 error = GSM_SaveBackupFile(argv[2], &backup, TRUE);
93 gammu_test_result(error, "GSM_SaveBackupFile");
96 /* Read file content */
97 GSM_ClearBackup(&backup);
98 error = GSM_ReadBackupFile(argv[2], &backup, GSM_Backup_GammuUCS2);
99 gammu_test_result(error, "GSM_ReadBackupFile");
101 /* Did we read something? */
102 skipcal = FALSE;
103 if (cal.EntriesNum == 0 && backup.Calendar[0] == NULL) {
104 skipcal = TRUE;
106 skiptodo = FALSE;
107 if (todo.EntriesNum == 0 && backup.ToDo[0] == NULL) {
108 skiptodo = TRUE;
111 /* Compare size */
112 if (!skipcal && cal.EntriesNum != backup.Calendar[0]->EntriesNum) {
113 printf("Different number of entries!\n");
114 return 1;
116 if (!skiptodo && todo.EntriesNum != backup.ToDo[0]->EntriesNum) {
117 printf("Different number of entries!\n");
118 return 1;
121 for (i = 0; i < (int)(sizeof(check_map) / sizeof(gboolean)); i++) {
122 check_map[i] = FALSE;
125 /* Compare content */
126 if (!skipcal) {
127 for (i = 0; i < cal.EntriesNum; i++) {
128 printf("field:%d\nn", cal.Entries[i].EntryType);
129 for (j = 0; j < backup.Calendar[0]->EntriesNum; j++) {
130 if (check_map[j] == FALSE && cal.Entries[i].EntryType == backup.Calendar[0]->Entries[j].EntryType) {
131 check_map[j] = TRUE;
132 break;
135 test_result(j < backup.Calendar[0]->EntriesNum);
136 test_result(cal.Entries[i].EntryType == backup.Calendar[0]->Entries[j].EntryType);
137 switch (cal.Entries[i].EntryType) {
138 case CAL_TEXT:
139 case CAL_DESCRIPTION:
140 case CAL_LOCATION:
141 case CAL_PHONE:
142 case CAL_LUID:
143 if (!mywstrncmp(cal.Entries[i].Text, backup.Calendar[0]->Entries[j].Text, 0)) {
144 printf("Calendar field %d is not the same!\n", i);
145 printf("vc: '%s'\n", DecodeUnicodeConsole(cal.Entries[i].Text));
146 printf("bak: '%s'\n", DecodeUnicodeConsole(backup.Calendar[0]->Entries[j].Text));
147 return 1;
149 break;
150 case CAL_START_DATETIME:
151 case CAL_END_DATETIME:
152 case CAL_TONE_ALARM_DATETIME:
153 case CAL_SILENT_ALARM_DATETIME:
154 case CAL_PRIVATE:
155 case CAL_CONTACTID:
156 case CAL_REPEAT_DAYOFWEEK:
157 case CAL_REPEAT_DAY:
158 case CAL_REPEAT_DAYOFYEAR:
159 case CAL_REPEAT_WEEKOFMONTH:
160 case CAL_REPEAT_MONTH:
161 case CAL_REPEAT_FREQUENCY:
162 case CAL_REPEAT_STARTDATE:
163 case CAL_REPEAT_STOPDATE:
164 case CAL_REPEAT_COUNT:
165 case CAL_LAST_MODIFIED:
166 break;
170 if (!skiptodo) {
171 for (i = 0; i < todo.EntriesNum; i++) {
172 printf("%d,%d\n", todo.Entries[i].EntryType, backup.ToDo[0]->Entries[i].EntryType);
173 switch (todo.Entries[i].EntryType) {
174 case TODO_TEXT:
175 case TODO_DESCRIPTION:
176 case TODO_LOCATION:
177 case TODO_PHONE:
178 case TODO_LUID:
179 if (!mywstrncmp(todo.Entries[i].Text, backup.ToDo[0]->Entries[i].Text, 0)) {
180 printf("Todo field %d is not the same!\n", i);
181 return 1;
183 break;
184 case TODO_END_DATETIME:
185 case TODO_START_DATETIME:
186 case TODO_COMPLETED_DATETIME:
187 case TODO_ALARM_DATETIME:
188 case TODO_SILENT_ALARM_DATETIME:
189 case TODO_PRIVATE:
190 case TODO_CONTACTID:
191 case TODO_COMPLETED:
192 case TODO_CATEGORY:
193 case TODO_LAST_MODIFIED:
194 break;
199 /* Free data */
200 GSM_FreeBackup(&backup);
202 /* We're done */
203 return 0;
206 /* Editor configuration
207 * vim: noexpandtab sw=8 ts=8 sts=8 tw=72: