Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Replaced string processing in URL with ProcessStringValue.
[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"
21 #include "vcard/vcard34conv.h"
23 ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, 
24         wxString InputFilename, wxString OutputFilename){
26         std::vector<wxString> ValidFormats = { 
27                 wxT("vCard3"),
28                 wxT("vCard4") 
29         };
31         bool ValidInput = false;
32         bool ValidOutput = false;
34         // Check that the input format is valid.
36         for (long l = 0; l < ValidFormats.size(); l++){
37         
38                 if (InputFormat == ValidFormats[l]){
39                         ValidInput = true;
40                         break;
41                 }
42         
43         }
44         
45         if (ValidInput == false){
46         
47                 return CONVERTRESULT_INVALIDINPUTFORMAT;
48         
49         }
51         // Check that the output format is valid.
53         for (long l = 0; l < ValidFormats.size(); l++){
54         
55                 if (OutputFormat == ValidFormats[l]){
56                         ValidOutput = true;
57                         break;
58                 }
59         
60         }
61         
62         if (ValidOutput == false){
63         
64                 return CONVERTRESULT_INVALIDOUTPUTFORMAT;
65         
66         }
68         // Check that the input and output formats
69         // are not the same.
71         if (InputFormat == OutputFormat){
72         
73                 return CONVERTRESULT_FORMATSSAME;
74         
75         }
76         
77         // Check that the input filename is not empty.
78         
79         if (InputFilename.empty()){
80         
81                 return CONVERTRESULT_INPUTFILEEMPTY;
82         
83         }
84         
85         // Check that the input file given exists.
86         
87         if (!wxFileExists(InputFilename)){
88                 
89                 return CONVERTRESULT_INPUTFILEMISSING;
90                 
91         }
92         
93         wxFile InputFile;
94         
95         if (!InputFile.Open(InputFilename, wxFile::read, wxS_DEFAULT)){
96                 
97                 int InputFileErrNo = InputFile.GetLastError();
98                 
99                 return CONVERTRESULT_INPUTFILEERROR;
100                 
101         }
102         
103         // Check that the output file can be opened.
104         
105         bool OutputPipe = false;
106         wxFile OutputFile;
107         
108         if (!OutputFilename.IsEmpty()){
109         
110                 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
111                         return CONVERTRESULT_OUTPUTFILEERROR;
112                 }
113         
114         } else {
115         
116                 OutputPipe = true;
117         
118         }
119         
120         // Check that the file has the correct file format.
121         
122         vCard vCard4Format;
123         wxString FinalData;
124         
125         if (InputFormat == "vCard4"){
126         
127                 // Read from the file.
128                 
129                 wxString InputFileData;
130                 InputFile.ReadAll(&InputFileData, wxConvAuto());
131         
132                 vCard vCard4Format;
133                 
134                 vCard4Format.LoadString(InputFileData);
135                 
136                 if (vCard4Format.Get("VERSION") != "4.0"){
137                         return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
138                 }
139         
140         } else if (InputFormat == "vCard3"){
141         
142                 // Read from the file.
143                 
144                 wxString InputFileData;
145                 InputFile.ReadAll(&InputFileData, wxConvAuto());
146         
147                 vCard InputvCard;
148                 
149                 InputvCard.LoadString(InputFileData);
150                 
151                 if (InputvCard.Get("VERSION") != "3.0"){
152                         return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
153                 }
155                 vCard34Conv vCard34ConvObj;
156                 
157                 vCard vCard4Format;
158                 
159                 vCard34ConvObj.ConvertToV4(&InputFileData, &vCard4Format);
160                 
161                 FinalData = vCard4Format.WriteString();
162                 
163         }
164         
165         // Convert the vCard into the required format and
166         // get the string value.
167         
168         if (OutputFormat == "vCard4"){
169         
170                 // Do nothing as the vCard is already in the vCard 4.0
171                 // format.
172         
173         } else if (OutputFormat == "vCard3"){
174         
175                 // Convert the vCard to vCard 3.0.
176                 
177                 vCard34Conv vCard34ConvObj;
178                 
179                 vCard vCard3Format;
180                 
181                 vCard34ConvObj.ConvertToV3(InputFilename, &FinalData);
182         
183         }
184         
185         // Check if we are outputting to console or to file.
186         
187         if (OutputPipe == false){
188         
189                 // We are outputting to a file.
190                 
191                 OutputFile.Write(FinalData, wxConvUTF8);
192         
193         } else {
194         
195                 // Write out the data to the console.
196                 
197                 FinalData.Trim();
198                 std::cout << FinalData << std::endl;
199         
200         }
201         
202         return CONVERTRESULT_OK;
203         
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