Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added the output file open check unit test and code.
[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"
21 ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, 
22         wxString InputFilename, wxString OutputFilename){
24         std::vector<wxString> ValidFormats = { 
25                 wxT("vCard3"),
26                 wxT("vCard4") 
27         };
29         bool ValidInput = false;
30         bool ValidOutput = false;
32         // Check that the input format is valid.
34         for (long l = 0; l < ValidFormats.size(); l++){
35         
36                 if (InputFormat == ValidFormats[l]){
37                         ValidInput = true;
38                         break;
39                 }
40         
41         }
42         
43         if (ValidInput == false){
44         
45                 return CONVERTRESULT_INVALIDINPUTFORMAT;
46         
47         }
49         // Check that the output format is valid.
51         for (long l = 0; l < ValidFormats.size(); l++){
52         
53                 if (OutputFormat == ValidFormats[l]){
54                         ValidOutput = true;
55                         break;
56                 }
57         
58         }
59         
60         if (ValidOutput == false){
61         
62                 return CONVERTRESULT_INVALIDOUTPUTFORMAT;
63         
64         }
66         // Check that the input and output formats
67         // are not the same.
69         if (InputFormat == OutputFormat){
70         
71                 return CONVERTRESULT_FORMATSSAME;
72         
73         }
74         
75         // Check that the input file given exists.
76         
77         if (!wxFileExists(InputFilename)){
78                 
79                 return CONVERTRESULT_INPUTFILEMISSING;
80                 
81         }
82         
83         wxFile InputFile;
84         
85         if (!InputFile.Open(InputFilename, wxFile::read, wxS_DEFAULT)){
86                 
87                 int InputFileErrNo = InputFile.GetLastError();
88                 
89                 return CONVERTRESULT_INPUTFILEERROR;
90                 
91         }
92         
93         // Check that the output file can be opened.
94         
95         wxFile OutputFile;
96         
97         if (!OutputFilename.IsEmpty()){
98         
99                 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
100                         return CONVERTRESULT_OUTPUTFILEERROR;
101                 }
102         
103         }
104         
105         return CONVERTRESULT_OK;
106         
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