http://marcin-wiacek.fkn.pl/english/zips/mygnokii.tar.gz
[gnokii.git] / common / vcal.lx
1 %{
2 #include <string.h>
3
4 #include "gsm-common.h"
5 #include "gsm-filetypes.h"
6
7 #define YY_DECL int yylex(char *type, char *text, char* time, char *alarm, int lexnumber)
8 int veventcounter=0;
9
10 %}
11
12 %x vcalendar
13 %x vevent
14 %x category
15 %x summary
16 %x ttime
17 %x aalarm
18 %option noyywrap
19 %option nounput
20
21 %%
22
23 BEGIN:VCALENDAR                         BEGIN(vcalendar);
24
25 <vcalendar>BEGIN:VEVENT                 ++veventcounter; BEGIN(vevent);
26
27 <vevent>CATEGORIES:                     BEGIN(category);
28
29 <category>[[:alnum:] ]+                 {
30                                                 if(veventcounter==lexnumber)
31                                                         strncpy(type, yytext, 21);
32                                                 BEGIN(vevent);
33                                         }
34
35 <vevent>SUMMARY:                        BEGIN(summary);
36
37 <summary>[[:alnum:] ]+                  {
38                                                 if(veventcounter==lexnumber)
39                                                         strncpy(text, yytext, 21);
40                                                 BEGIN(vevent);
41                                         }
42
43 <vevent>DTSTART:                        BEGIN(ttime);
44
45 <ttime>[0-9]{8}T[0-9]{6}                {
46                                                 if(veventcounter==lexnumber)
47                                                         strncpy(time, yytext, 16);
48                                                 BEGIN(vevent);
49                                         }
50
51 <vevent>AALARM:                         BEGIN(aalarm);
52
53 <aalarm>[0-9]{8}T[0-9]{6}               {
54                                                 if(veventcounter==lexnumber)
55                                                         strncpy(alarm, yytext, 16);
56                                                 BEGIN(vevent);
57                                         }
58
59
60 <vevent>END:VEVENT                      BEGIN(vcalendar);
61
62 <vcalendar>END:VCALENDAR                BEGIN(0);
63 <*>.|\n
64 <*><<EOF>>                              {
65                                                 if((lexnumber<1) | (lexnumber>veventcounter))
66                                                 {
67                                                         fprintf(stderr, "Error: Invalid calendar note number!\n");
68                                                         return -1;
69                                                 }
70                                                 return 0;
71                                         }
72
73 %%
74
75 /**
76 * GetvCalTime
77 *
78 * Fills vCalendar time string into GSM_DateTime structure
79 *
80 * in:
81 *   dt:  datetime structure
82 *   time:  string in format yyyymmddThhmmss
83 * out:
84 *   <>0 if error
85 */
86 int GetvCalTime(GSM_DateTime *dt, char *time)
87 {
88   char year[5]="", month[3]="", day[3]="", hour[3]="", minute[3]="", second[3]="";
89   dt->Year=dt->Month=dt->Day=dt->Hour=dt->Minute=dt->Second=dt->Timezone=0;
90
91   strncpy(year, time, 4);
92   strncpy(month, time+4, 2);
93   strncpy(day, time+6, 2);
94   strncpy(hour, time+9, 2);
95   strncpy(minute, time+11, 2);
96   strncpy(second, time+13, 2);
97
98 /* FIXME: Should check ranges... */
99   dt->Year=atoi(year);
100   dt->Month=atoi(month);
101   dt->Day=atoi(day);
102   dt->Hour=atoi(hour);
103   dt->Minute=atoi(minute);
104   dt->Second=atoi(second);
105 /* FIXME */
106   dt->Timezone=0;
107
108   return 0;
109 }
110
111 /**
112 * FillCalendarNote
113 *
114 * Fills calendar data from strings into calendar note
115 *
116 * in:
117 *   note:  calendar note structure
118 *   type:  type of calendar note
119 *   text:  text or phonenumber
120 *   time:  string in format yyyymmddThhmmss
121 *   alarm: dito
122 * out:
123 *   <>0 if error
124 */
125 int FillCalendarNote(GSM_CalendarNote *note, char *type,
126                        char *text, char *time, char *alarm)
127 {
128
129   GetvCalTime(&note->Time, time);
130   GetvCalTime(&note->Alarm, alarm);
131
132   note->Location=0; 
133
134   strncpy(note->Text, text, 20);
135   strcpy(note->Phone, ""); /* correct in most cases */
136
137 /* FIXME: Handle additional strings, maybee from configuration file */
138
139   if(!strcmp(type, "PHONE CALL"))
140   {
141     strncpy(note->Phone, text, 20);
142     note->Type=GCN_CALL;
143   }
144   else if(!strcmp(type, "MEETING"))
145       note->Type=GCN_MEETING;
146   else if(!strcmp(type, "SPECIAL OCASSION"))
147       note->Type=GCN_BIRTHDAY;
148   else
149       note->Type=GCN_REMINDER;
150
151   return 0;
152 }
153
154 /**
155 * GSM_ReadVCalendarFile
156 *
157 * Reads vCalendar file
158 *
159 * in:
160 *   FileName: name of vCalendar file 
161 *   cnote:  pointer to calendar note
162 *   number:  number in file of calendar note to read
163 * out:
164 *   <>0 if error
165 */
166 int GSM_ReadVCalendarFile(char *FileName, GSM_CalendarNote *cnote, int number)
167 {
168   FILE *file;
169   char type[21]="", text[21]="", time[16]="", alarm[16]="";
170
171   file=fopen(FileName, "r");
172   if (!file) {
173     fprintf(stderr, _("File cannot be opened!\n"));
174     return -1;
175   }
176
177   yyin=file;
178
179   if(yylex(type, text, time, alarm, number))
180   {
181     fprintf(stderr, _("Error parsing vCalendar file!\n"));
182     return -1;
183   }
184   FillCalendarNote(cnote, type, text, time, alarm);  
185
186   fclose(file);
187   return 0;
188 }