Gammu internals  1.38.0
backvnt.c
Go to the documentation of this file.
1 
4 /* Copyright (c) 2009 Michal Cihar <[email protected]> */
5 /* Licensend under GNU GPL 2 */
6 
7 #include <string.h>
8 #include <ctype.h>
9 
10 #include <gammu-config.h>
11 
12 #include "../../misc/coding/coding.h"
13 #include "../../debug.h"
14 #include "../gsmlogo.h"
15 #include "../gsmmisc.h"
16 #include "backics.h"
17 #include "backvcs.h"
18 
19 #ifdef GSM_ENABLE_BACKUP
20 
24 #define chk_fwrite(data, size, count, file) \
25  if (fwrite(data, size, count, file) != count) goto fail;
26 
27 GSM_Error SaveVNT(char *FileName, GSM_Backup *backup)
28 {
29  int i;
30  size_t Length = 0;
31  unsigned char Buffer[1000];
32  FILE *file;
33  GSM_Error error;
34 
35  file = fopen(FileName, "wb");
36  if (file == NULL) return ERR_CANTOPENFILE;
37 
38  i=0;
39  while (backup->Note[i]!=NULL) {
40  Length = 0;
41  error = GSM_EncodeVNTFile(Buffer, sizeof(Buffer), &Length, backup->Note[i]);
42  if (error != ERR_NONE) {
43  fclose(file);
44  return error;
45  }
46  chk_fwrite(Buffer,1,Length,file);
47  i++;
48  sprintf(Buffer, "%c%c",13,10);
49  chk_fwrite(Buffer,1,2,file);
50  }
51 
52  fclose(file);
53  return ERR_NONE;
54 fail:
55  fclose(file);
56  return ERR_WRITING_FILE;
57 }
58 
59 GSM_Error LoadVNT(const char *FileName, GSM_Backup *backup)
60 {
61  GSM_File File;
62  GSM_Error error;
63  GSM_NoteEntry Note;
64  int num = 0;
65  size_t Pos = 0;
66 
67  File.Buffer = NULL;
68  error = GSM_ReadFile(FileName, &File);
69  if (error != ERR_NONE) return error;
70 
71  while (1) {
72  error = GSM_DecodeVNOTE(File.Buffer, &Pos, &Note);
73  if (error == ERR_EMPTY) {
74  error = ERR_NONE;
75  break;
76  }
77  if (error != ERR_NONE) break;
78  if (num < GSM_BACKUP_MAX_NOTE) {
79  backup->Note[num] = (GSM_NoteEntry *)malloc(sizeof(GSM_NoteEntry));
80  if (backup->Note[num] == NULL) {
81  error = ERR_MOREMEMORY;
82  break;
83  }
84  backup->Note[num + 1] = NULL;
85  } else {
86  dbgprintf(NULL, "Increase GSM_BACKUP_MAX_NOTE\n");
87  error = ERR_MOREMEMORY;
88  break;
89  }
90  memcpy(backup->Note[num], &Note, sizeof(GSM_NoteEntry));
91  backup->Note[num]->Location = num + 1;
92  num++;
93  }
94 
95  free(File.Buffer);
96  File.Buffer=NULL;
97  return error;
98 }
99 
100 #endif
101 
102 /* How should editor hadle tabs in this file? Add editor commands here.
103  * vim: noexpandtab sw=8 ts=8 sts=8:
104  */
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_EncodeVNTFile(char *Buffer, const size_t buff_len, size_t *Length, GSM_NoteEntry *Note)
Definition: gsmcal.c:2066
GSM_Error GSM_DecodeVNOTE(char *Buffer, size_t *Pos, GSM_NoteEntry *Note)
Definition: gsmcal.c:2030
GSM_Error GSM_ReadFile(const char *FileName, GSM_File *File)
Definition: gsmmisc.c:74
#define dbgprintf
Definition: debug.h:72