1 // convert.h - Convert subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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/>
20 #include "vcard/vcard.h"
22 ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat,
23 wxString InputFilename, wxString OutputFilename){
25 std::vector<wxString> ValidFormats = {
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++){
37 if (InputFormat == ValidFormats[l]){
44 if (ValidInput == false){
46 return CONVERTRESULT_INVALIDINPUTFORMAT;
50 // Check that the output format is valid.
52 for (long l = 0; l < ValidFormats.size(); l++){
54 if (OutputFormat == ValidFormats[l]){
61 if (ValidOutput == false){
63 return CONVERTRESULT_INVALIDOUTPUTFORMAT;
67 // Check that the input and output formats
70 if (InputFormat == OutputFormat){
72 return CONVERTRESULT_FORMATSSAME;
76 // Check that the input filename is not empty.
78 if (InputFilename.empty()){
80 return CONVERTRESULT_INPUTFILEEMPTY;
84 // Check that the input file given exists.
86 if (!wxFileExists(InputFilename)){
88 return CONVERTRESULT_INPUTFILEMISSING;
94 if (!InputFile.Open(InputFilename, wxFile::read, wxS_DEFAULT)){
96 int InputFileErrNo = InputFile.GetLastError();
98 return CONVERTRESULT_INPUTFILEERROR;
102 // Check that the output file can be opened.
104 bool OutputPipe = false;
107 if (!OutputFilename.IsEmpty()){
109 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
110 return CONVERTRESULT_OUTPUTFILEERROR;
119 // Check that the file has the correct file format.
121 if (InputFormat == "vCard4"){
123 // Read from the file.
125 wxString InputFileData;
126 InputFile.ReadAll(&InputFileData, wxConvAuto());
130 InputvCard.LoadString(InputFileData);
132 if (InputvCard.Get("VERSION") != "4.0"){
133 return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
136 } else if (InputFormat == "vCard3"){
138 // Read from the file.
140 wxString InputFileData;
141 InputFile.ReadAll(&InputFileData, wxConvAuto());
145 InputvCard.LoadString(InputFileData);
147 if (InputvCard.Get("VERSION") != "3.0"){
148 return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
153 return CONVERTRESULT_OK;