Gammu internals  1.38.0
backvcf.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 "backvcf.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 SaveVCard(const char *FileName, GSM_Backup *backup)
23 {
24  int i;
25  size_t Length = 0;
26  char Buffer[100000];
27  FILE *file;
28  GSM_Error error;
29 
30  file = fopen(FileName, "wb");
31  if (file == NULL) return ERR_CANTOPENFILE;
32 
33  i=0;
34  while (backup->PhonePhonebook[i]!=NULL) {
35  Length = 0;
36  error = GSM_EncodeVCARD(NULL, Buffer, sizeof(Buffer), &Length,backup->PhonePhonebook[i],TRUE,Nokia_VCard21);
37  if (error != ERR_NONE) {
38  fclose(file);
39  return error;
40  }
41  chk_fwrite(Buffer,1,Length,file);
42  sprintf(Buffer, "%c%c",13,10);
43  chk_fwrite(Buffer,1,2,file);
44  i++;
45  }
46 
47  i=0;
48  while (backup->SIMPhonebook[i]!=NULL) {
49  Length = 0;
50  error = GSM_EncodeVCARD(NULL, Buffer, sizeof(Buffer), &Length,backup->SIMPhonebook[i],TRUE,Nokia_VCard21);
51  if (error != ERR_NONE) {
52  fclose(file);
53  return error;
54  }
55  chk_fwrite(Buffer,1,Length,file);
56  sprintf(Buffer, "%c%c",13,10);
57  chk_fwrite(Buffer,1,2,file);
58  i++;
59  }
60 
61  fclose(file);
62  return ERR_NONE;
63 fail:
64  fclose(file);
65  return ERR_WRITING_FILE;
66 }
67 
68 GSM_Error LoadVCard(const char *FileName, GSM_Backup *backup)
69 {
70  GSM_File File;
71  GSM_Error error;
72  GSM_MemoryEntry Pbk;
73  int numPbk = 0, numSIM = 0;
74  size_t Pos = 0;
75 
76  File.Buffer = NULL;
77  error = GSM_ReadFile(FileName, &File);
78  if (error != ERR_NONE) return error;
79 
80  while (1) {
81  error = GSM_DecodeVCARD(NULL, File.Buffer, &Pos, &Pbk, Nokia_VCard21);
82  if (error == ERR_EMPTY) {
83  error = ERR_NONE;
84  break;
85  }
86  if (error != ERR_NONE) break;
87  if (Pbk.MemoryType == MEM_SM) {
88  if (numSIM < GSM_BACKUP_MAX_SIMPHONEBOOK) {
89  backup->SIMPhonebook[numSIM] = (GSM_MemoryEntry *)malloc(sizeof(GSM_MemoryEntry));
90  if (backup->SIMPhonebook[numSIM] == NULL) {
91  error = ERR_MOREMEMORY;
92  break;
93  }
94  backup->SIMPhonebook[numSIM + 1] = NULL;
95  } else {
96  dbgprintf(NULL, "Increase GSM_BACKUP_MAX_SIMPHONEBOOK\n");
97  error = ERR_MOREMEMORY;
98  break;
99  }
100  memcpy(backup->SIMPhonebook[numSIM],&Pbk,sizeof(GSM_MemoryEntry));
101  if (backup->SIMPhonebook[numSIM]->Location == 0) {
102  backup->SIMPhonebook[numSIM]->Location = numSIM + 1;
103  }
104  numSIM++;
105  } else {
106  if (numPbk < GSM_BACKUP_MAX_PHONEPHONEBOOK) {
107  backup->PhonePhonebook[numPbk] = (GSM_MemoryEntry *)malloc(sizeof(GSM_MemoryEntry));
108  if (backup->PhonePhonebook[numPbk] == NULL) {
109  error = ERR_MOREMEMORY;
110  break;
111  }
112  backup->PhonePhonebook[numPbk + 1] = NULL;
113  } else {
114  dbgprintf(NULL, "Increase GSM_BACKUP_MAX_PHONEPHONEBOOK\n");
115  error = ERR_MOREMEMORY;
116  break;
117  }
118  memcpy(backup->PhonePhonebook[numPbk],&Pbk,sizeof(GSM_MemoryEntry));
119  if (backup->PhonePhonebook[numPbk]->Location == 0) {
120  backup->PhonePhonebook[numPbk]->Location = numPbk + 1;
121  }
122  backup->PhonePhonebook[numPbk]->MemoryType = MEM_ME;
123  numPbk++;
124  }
125  }
126 
127  free(File.Buffer);
128  return error;
129 }
130 
131 #endif
132 
133 /* How should editor hadle tabs in this file? Add editor commands here.
134  * vim: noexpandtab sw=8 ts=8 sts=8:
135  */
GSM_Error GSM_EncodeVCARD(GSM_Debug_Info *di, char *Buffer, const size_t buff_len, size_t *Length, GSM_MemoryEntry *pbk, const gboolean header, const GSM_VCardVersion Version)
Definition: gsmpbk.c:126
GSM_MemoryType MemoryType
Definition: gammu-memory.h:415
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
GSM_Error GSM_DecodeVCARD(GSM_Debug_Info *di, char *Buffer, size_t *Pos, GSM_MemoryEntry *Pbk, GSM_VCardVersion Version)
Definition: gsmpbk.c:677
GSM_Error GSM_ReadFile(const char *FileName, GSM_File *File)
Definition: gsmmisc.c:74
#define dbgprintf
Definition: debug.h:72
#define TRUE
Definition: gammu-types.h:28