Use CAutoGeneralHandle
[TortoiseGit.git] / src / ResText / POFile.cpp
blob2ab447f0b5605bd3b0ac6a1479da2b3d1dde9b78
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012 - TortoiseGit
4 // Copyright (C) 2003-2008, 2011-2012 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program 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
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "shlwapi.h"
21 #include <fstream>
22 #include "codecvt.h"
23 #include "Utils.h"
24 #include "ResModule.h"
25 #include ".\pofile.h"
27 #define MYERROR {CUtils::Error(); return FALSE;}
29 CPOFile::CPOFile()
33 CPOFile::~CPOFile(void)
37 BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting, bool bAdjustEOLs)
39 if (!PathFileExists(szPath))
40 return FALSE;
42 m_bAdjustEOLs = bAdjustEOLs;
44 if (!m_bQuiet)
45 _ftprintf(stdout, _T("parsing file %s...\n"), szPath);
47 int nEntries = 0;
48 int nDeleted = 0;
49 int nTranslated = 0;
50 //since stream classes still expect the filepath in char and not wchar_t
51 //we need to convert the filepath to multibyte
52 char filepath[MAX_PATH+1];
53 SecureZeroMemory(filepath, sizeof(filepath));
54 WideCharToMultiByte(CP_ACP, NULL, szPath, -1, filepath, _countof(filepath)-1, NULL, NULL);
56 std::wifstream File;
57 File.imbue(std::locale(std::locale(), new utf8_conversion()));
58 File.open(filepath);
59 if (!File.good())
61 _ftprintf(stderr, _T("can't open input file %s\n"), szPath);
62 return FALSE;
64 TCHAR line[2*MAX_STRING_LENGTH];
65 std::vector<std::wstring> entry;
68 File.getline(line, _countof(line));
69 if (line[0]==0)
71 //empty line means end of entry!
72 RESOURCEENTRY resEntry;
73 std::wstring msgid;
74 int type = 0;
75 for (std::vector<std::wstring>::iterator I = entry.begin(); I != entry.end(); ++I)
77 if (_tcsncmp(I->c_str(), _T("# "), 2)==0)
79 //user comment
80 resEntry.translatorcomments.push_back(I->c_str());
81 type = 0;
83 if (_tcsncmp(I->c_str(), _T("#."), 2)==0)
85 //automatic comments
86 resEntry.automaticcomments.push_back(I->c_str());
87 type = 0;
89 if (_tcsncmp(I->c_str(), _T("#,"), 2)==0)
91 //flag
92 resEntry.flag = I->c_str();
93 type = 0;
95 if (_tcsncmp(I->c_str(), _T("msgid"), 5)==0)
97 //message id
98 msgid = I->c_str();
99 msgid = std::wstring(msgid.substr(7, msgid.size() - 8));
101 bool foundNonSpace = false;
102 for (std::wstring::iterator it = msgid.begin(); it < msgid.end(); it++)
104 if (*it != _T(' ') && *it != _T('\n') && *it != _T('\r'))
106 foundNonSpace = true;
107 break;
110 if (foundNonSpace)
111 nEntries++;
113 type = 1;
115 if (_tcsncmp(I->c_str(), _T("msgstr"), 6)==0)
117 //message string
118 resEntry.msgstr = I->c_str();
119 resEntry.msgstr = resEntry.msgstr.substr(8, resEntry.msgstr.length() - 9);
120 if (resEntry.msgstr.size()>0)
121 nTranslated++;
122 type = 2;
124 if (_tcsncmp(I->c_str(), _T("\""), 1)==0)
126 if (type == 1)
128 std::wstring temp = I->c_str();
129 temp = temp.substr(1, temp.length()-2);
130 msgid += temp;
132 if (type == 2)
134 if (resEntry.msgstr.size() == 0)
135 nTranslated++;
136 std::wstring temp = I->c_str();
137 temp = temp.substr(1, temp.length()-2);
138 resEntry.msgstr += temp;
142 entry.clear();
143 if ((bUpdateExisting)&&(this->count(msgid) == 0))
144 nDeleted++;
145 else
147 if ((m_bAdjustEOLs)&&(msgid.find(L"\\r\\n") != std::string::npos))
149 AdjustEOLs(resEntry.msgstr);
151 (*this)[msgid] = resEntry;
153 msgid.clear();
155 else
157 entry.push_back(line);
159 } while (File.gcount() > 0);
160 printf(File.getloc().name().c_str());
161 File.close();
162 RESOURCEENTRY emptyentry;
163 (*this)[std::wstring(_T(""))] = emptyentry;
164 if (!m_bQuiet)
165 _ftprintf(stdout, _T("%d Entries found, %d were already translated and %d got deleted\n"), nEntries, nTranslated, nDeleted);
166 return TRUE;
169 BOOL CPOFile::SaveFile(LPCTSTR szPath, LPCTSTR lpszHeaderFile)
171 //since stream classes still expect the filepath in char and not wchar_t
172 //we need to convert the filepath to multibyte
173 char filepath[MAX_PATH+1];
174 int nEntries = 0;
175 SecureZeroMemory(filepath, sizeof(filepath));
176 WideCharToMultiByte(CP_ACP, NULL, szPath, -1, filepath, _countof(filepath)-1, NULL, NULL);
178 std::wofstream File;
179 File.imbue(std::locale(std::locale(), new utf8_conversion()));
180 File.open(filepath, std::ios_base::binary);
182 if ((lpszHeaderFile)&&(lpszHeaderFile[0])&&(PathFileExists(lpszHeaderFile)))
184 // read the header file and save it to the top of the pot file
185 std::wifstream inFile;
186 inFile.imbue(std::locale(std::locale(), new utf8_conversion()));
187 inFile.open(lpszHeaderFile, std::ios_base::binary);
189 wchar_t ch;
190 while(inFile && inFile.get(ch))
191 File.put(ch);
192 inFile.close();
194 else
196 File << _T("# SOME DESCRIPTIVE TITLE.\n");
197 File << _T("# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER\n");
198 File << _T("# This file is distributed under the same license as the PACKAGE package.\n");
199 File << _T("# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n");
200 File << _T("#\n");
201 File << _T("#, fuzzy\n");
202 File << _T("msgid \"\"\n");
203 File << _T("msgstr \"\"\n");
204 File << _T("\"Project-Id-Version: PACKAGE VERSION\\n\"\n");
205 File << _T("\"Report-Msgid-Bugs-To: \\n\"\n");
206 File << _T("\"POT-Creation-Date: 1900-01-01 00:00+0000\\n\"\n");
207 File << _T("\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n");
208 File << _T("\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n");
209 File << _T("\"Language-Team: LANGUAGE <LL@li.org>\\n\"\n");
210 File << _T("\"MIME-Version: 1.0\\n\"\n");
211 File << _T("\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
212 File << _T("\"Content-Transfer-Encoding: 8bit\\n\"\n");
214 File << _T("\n");
215 File << _T("# msgid/msgstr fields for Accelerator keys\n");
216 File << _T("# Format is: \"ID:xxxxxx:VACS+X\" where:\n");
217 File << _T("# ID:xxxxx = the menu ID corresponding to the accelerator\n");
218 File << _T("# V = Virtual key (or blank if not used) - nearly always set!\n");
219 File << _T("# A = Alt key (or blank if not used)\n");
220 File << _T("# C = Ctrl key (or blank if not used)\n");
221 File << _T("# S = Shift key (or blank if not used)\n");
222 File << _T("# X = upper case character\n");
223 File << _T("# e.g. \"V CS+Q\" == Ctrl + Shift + 'Q'\n");
224 File << _T("\n");
225 File << _T("# ONLY Accelerator Keys with corresponding alphanumeric characters can be\n");
226 File << _T("# updated i.e. function keys (F2), special keys (Delete, HoMe) etc. will not.\n");
227 File << _T("\n");
228 File << _T("# ONLY change the msgstr field. Do NOT change any other.\n");
229 File << _T("# If you do not want to change an Accelerator Key, copy msgid to msgstr\n");
230 File << _T("\n");
232 for (std::map<std::wstring, RESOURCEENTRY>::iterator I = this->begin(); I != this->end(); ++I)
234 if (I->first.size() == 0)
235 continue;
237 std::wstring s = I->first;
238 bool foundNonSpace = false;
239 for (std::wstring::iterator it = s.begin(); it < s.end(); it++)
241 if (*it != _T(' ') && *it != _T('\n') && *it != _T('\r'))
243 foundNonSpace = true;
244 break;
248 if (!foundNonSpace)
249 continue;
251 RESOURCEENTRY entry = I->second;
252 for (std::vector<std::wstring>::iterator II = entry.automaticcomments.begin(); II != entry.automaticcomments.end(); ++II)
254 File << II->c_str() << _T("\n");
256 for (std::vector<std::wstring>::iterator II = entry.translatorcomments.begin(); II != entry.translatorcomments.end(); ++II)
258 File << II->c_str() << _T("\n");
260 if (I->second.resourceIDs.size() > 0)
262 File << _T("#. Resource IDs: (");
264 std::set<DWORD>::const_iterator II = I->second.resourceIDs.begin();
265 File << (*II);
266 ++II;
267 while (II != I->second.resourceIDs.end())
269 File << _T(", ");
270 File << (*II);
271 ++II;
273 File << _T(")\n");
275 if (I->second.flag.length() > 0)
276 File << (I->second.flag.c_str()) << _T("\n");
277 File << (_T("msgid \"")) << (I->first.c_str()) << _T("\"\n");
278 File << (_T("msgstr \"")) << (I->second.msgstr.c_str()) << _T("\"\n\n");
279 nEntries++;
281 File.close();
282 if (!m_bQuiet)
283 _ftprintf(stdout, _T("File %s saved, containing %d entries\n"), szPath, nEntries);
284 return TRUE;
287 void CPOFile::AdjustEOLs(std::wstring& str)
289 std::wstring result;
290 std::wstring::size_type pos = 0;
291 for ( ; ; ) // while (true)
293 std::wstring::size_type next = str.find(L"\\r\\n", pos);
294 result.append(str, pos, next-pos);
295 if( next != std::string::npos )
297 result.append(L"\\n");
298 pos = next + 4; // 4 = sizeof("\\r\\n")
300 else
302 break; // exit loop
305 str.swap(result);
306 result.clear();
307 pos = 0;
309 for ( ; ; ) // while (true)
311 std::wstring::size_type next = str.find(L"\\n", pos);
312 result.append(str, pos, next-pos);
313 if( next != std::string::npos )
315 result.append(L"\\r\\n");
316 pos = next + 2; // 2 = sizeof("\\n")
318 else
320 break; // exit loop
323 str.swap(result);