Gammu internals  1.38.0
backvcs.c
Go to the documentation of this file.
1 /* (c) 2003 by Marcin Wiacek */
2 
3 #include <string.h>
4 #include <ctype.h>
5 
6 #include <gammu-config.h>
7 
8 #include "../../misc/coding/coding.h"
9 #include "../../debug.h"
10 #include "../gsmlogo.h"
11 #include "../gsmmisc.h"
12 #include "backvcs.h"
13 
14 #ifdef GSM_ENABLE_BACKUP
15 
19 #define chk_fwrite(data, size, count, file) \
20  if (fwrite(data, size, count, file) != count) goto fail;
21 
22 GSM_Error SaveVCalendar(const char *FileName, GSM_Backup *backup)
23 {
24  int i;
25  size_t Length = 0;
26  unsigned char Buffer[1000];
27  FILE *file;
28  GSM_Error error;
29 
30  file = fopen(FileName, "wb");
31  if (file == NULL) return ERR_CANTOPENFILE;
32 
33  Length=sprintf(Buffer, "BEGIN:VCALENDAR%c%c",13,10);
34  Length+=sprintf(Buffer+Length, "VERSION:1.0%c%c",13,10);
35  chk_fwrite(Buffer,1,Length,file);
36 
37  i=0;
38  while (backup->Calendar[i]!=NULL) {
39  sprintf(Buffer, "%c%c",13,10);
40  chk_fwrite(Buffer,1,2,file);
41  Length = 0;
42  error = GSM_EncodeVCALENDAR(Buffer, sizeof(Buffer),&Length,backup->Calendar[i],FALSE,Nokia_VCalendar);
43  if (error != ERR_NONE) {
44  fclose(file);
45  return error;
46  }
47  chk_fwrite(Buffer,1,Length,file);
48  i++;
49  }
50  i=0;
51  while (backup->ToDo[i]!=NULL) {
52  sprintf(Buffer, "%c%c",13,10);
53  chk_fwrite(Buffer,1,2,file);
54  Length = 0;
55  error = GSM_EncodeVTODO(Buffer, sizeof(Buffer),&Length,backup->ToDo[i],FALSE,Nokia_VToDo);
56  if (error != ERR_NONE) {
57  fclose(file);
58  return error;
59  }
60  chk_fwrite(Buffer,1,Length,file);
61  i++;
62  }
63 
64  Length=sprintf(Buffer, "%c%cEND:VCALENDAR%c%c",13,10,13,10);
65  chk_fwrite(Buffer,1,Length,file);
66 
67  fclose(file);
68  return ERR_NONE;
69 fail:
70  fclose(file);
71  return ERR_WRITING_FILE;
72 }
73 
74 GSM_Error LoadVCalendarPrivate(const char *FileName, GSM_Backup *backup, GSM_VCalendarVersion CalVer, GSM_VToDoVersion ToDoVer)
75 {
76  GSM_File File;
77  GSM_Error error;
78  GSM_CalendarEntry Calendar;
79  GSM_ToDoEntry ToDo;
80  int numCal = 0, numToDo = 0;
81  size_t Pos = 0;
82 
83  File.Buffer = NULL;
84  error = GSM_ReadFile(FileName, &File);
85  if (error != ERR_NONE) return error;
86 
87  while (1) {
88  error = GSM_DecodeVCALENDAR_VTODO(NULL, File.Buffer, &Pos, &Calendar, &ToDo, CalVer, ToDoVer);
89  if (error == ERR_EMPTY) {
90  error = ERR_NONE;
91  break;
92  }
93  if (error != ERR_NONE) break;
94  if (Calendar.EntriesNum != 0) {
95  if (numCal < GSM_MAXCALENDARTODONOTES) {
96  backup->Calendar[numCal] = (GSM_CalendarEntry *)malloc(sizeof(GSM_CalendarEntry));
97  if (backup->Calendar[numCal] == NULL) {
98  error = ERR_MOREMEMORY;
99  break;
100  }
101  backup->Calendar[numCal + 1] = NULL;
102  } else {
103  dbgprintf(NULL, "Increase GSM_MAXCALENDARTODONOTES\n");
104  error = ERR_MOREMEMORY;
105  break;
106  }
107  memcpy(backup->Calendar[numCal],&Calendar,sizeof(GSM_CalendarEntry));
108  backup->Calendar[numCal]->Location = numCal + 1;
109  numCal++;
110  }
111  if (ToDo.EntriesNum != 0) {
112  if (numToDo < GSM_MAXCALENDARTODONOTES) {
113  backup->ToDo[numToDo] = (GSM_ToDoEntry *)malloc(sizeof(GSM_ToDoEntry));
114  if (backup->ToDo[numToDo] == NULL) {
115  error = ERR_MOREMEMORY;
116  break;
117  }
118  backup->ToDo[numToDo + 1] = NULL;
119  } else {
120  dbgprintf(NULL, "Increase GSM_MAXCALENDARTODONOTES\n");
121  error = ERR_MOREMEMORY;
122  break;
123  }
124  memcpy(backup->ToDo[numToDo], &ToDo, sizeof(GSM_ToDoEntry));
125  backup->ToDo[numToDo]->Location = numToDo + 1;
126  numToDo++;
127  }
128  }
129 
130  free(File.Buffer);
131  File.Buffer=NULL;
132  return error;
133 }
134 
135 GSM_Error LoadVCalendar(const char *FileName, GSM_Backup *backup)
136 {
137  return LoadVCalendarPrivate(FileName, backup, Nokia_VCalendar, Nokia_VToDo);
138 }
139 
140 #endif
141 
142 /* How should editor hadle tabs in this file? Add editor commands here.
143  * vim: noexpandtab sw=8 ts=8 sts=8:
144  */
GSM_Error GSM_EncodeVCALENDAR(char *Buffer, const size_t buff_len, size_t *Length, GSM_CalendarEntry *note, const gboolean header, const GSM_VCalendarVersion Version)
Definition: gsmcal.c:814
GSM_VCalendarVersion
GSM_VToDoVersion
GSM_Error
Definition: gammu-error.h:23
unsigned char * Buffer
Definition: gammu-file.h:94
#define chk_fwrite(data, size, count, file)
Definition: gsmlogo.c:20
#define FALSE
Definition: gammu-types.h:25
GSM_Error GSM_EncodeVTODO(char *Buffer, const size_t buff_len, size_t *Length, const GSM_ToDoEntry *note, const gboolean header, const GSM_VToDoVersion Version)
Definition: gsmcal.c:1038
#define GSM_MAXCALENDARTODONOTES
Definition: gammu-limits.h:84
GSM_Error GSM_DecodeVCALENDAR_VTODO(GSM_Debug_Info *di, char *Buffer, size_t *Pos, GSM_CalendarEntry *Calendar, GSM_ToDoEntry *ToDo, GSM_VCalendarVersion CalVer, GSM_VToDoVersion ToDoVer)
Definition: gsmcal.c:1673
GSM_Error GSM_ReadFile(const char *FileName, GSM_File *File)
Definition: gsmmisc.c:74
#define dbgprintf
Definition: debug.h:72