Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added code and unit tests to check that input file has the correct format for vCard3...
[xestiaab/.git] / source / convert.cpp
1 // convert.h - Convert subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "convert.h"
20 #include "vcard/vcard.h"
22 ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, 
23         wxString InputFilename, wxString OutputFilename){
25         std::vector<wxString> ValidFormats = { 
26                 wxT("vCard3"),
27                 wxT("vCard4") 
28         };
30         bool ValidInput = false;
31         bool ValidOutput = false;
33         // Check that the input format is valid.
35         for (long l = 0; l < ValidFormats.size(); l++){
36         
37                 if (InputFormat == ValidFormats[l]){
38                         ValidInput = true;
39                         break;
40                 }
41         
42         }
43         
44         if (ValidInput == false){
45         
46                 return CONVERTRESULT_INVALIDINPUTFORMAT;
47         
48         }
50         // Check that the output format is valid.
52         for (long l = 0; l < ValidFormats.size(); l++){
53         
54                 if (OutputFormat == ValidFormats[l]){
55                         ValidOutput = true;
56                         break;
57                 }
58         
59         }
60         
61         if (ValidOutput == false){
62         
63                 return CONVERTRESULT_INVALIDOUTPUTFORMAT;
64         
65         }
67         // Check that the input and output formats
68         // are not the same.
70         if (InputFormat == OutputFormat){
71         
72                 return CONVERTRESULT_FORMATSSAME;
73         
74         }
75         
76         // Check that the input filename is not empty.
77         
78         if (InputFilename.empty()){
79         
80                 return CONVERTRESULT_INPUTFILEEMPTY;
81         
82         }
83         
84         // Check that the input file given exists.
85         
86         if (!wxFileExists(InputFilename)){
87                 
88                 return CONVERTRESULT_INPUTFILEMISSING;
89                 
90         }
91         
92         wxFile InputFile;
93         
94         if (!InputFile.Open(InputFilename, wxFile::read, wxS_DEFAULT)){
95                 
96                 int InputFileErrNo = InputFile.GetLastError();
97                 
98                 return CONVERTRESULT_INPUTFILEERROR;
99                 
100         }
101         
102         // Check that the output file can be opened.
103         
104         bool OutputPipe = false;
105         wxFile OutputFile;
106         
107         if (!OutputFilename.IsEmpty()){
108         
109                 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
110                         return CONVERTRESULT_OUTPUTFILEERROR;
111                 }
112         
113         } else {
114         
115                 OutputPipe = true;
116         
117         }
118         
119         // Check that the file has the correct file format.
120         
121         if (InputFormat == "vCard4"){
122         
123                 // Read from the file.
124                 
125                 wxString InputFileData;
126                 InputFile.ReadAll(&InputFileData, wxConvAuto());
127         
128                 vCard InputvCard;
129                 
130                 InputvCard.LoadString(InputFileData);
131                 
132                 if (InputvCard.Get("VERSION") != "4.0"){
133                         return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
134                 }
135         
136         } else if (InputFormat == "vCard3"){
137         
138                 // Read from the file.
139                 
140                 wxString InputFileData;
141                 InputFile.ReadAll(&InputFileData, wxConvAuto());
142         
143                 vCard InputvCard;
144                 
145                 InputvCard.LoadString(InputFileData);
146                 
147                 if (InputvCard.Get("VERSION") != "3.0"){
148                         return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
149                 }
150         
151         }
152         
153         return CONVERTRESULT_OK;
154         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy