1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2008 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
19 #include "shlwapi.h"
\r
21 #include "codecvt.h"
\r
23 #include "ResModule.h"
\r
24 #include ".\pofile.h"
\r
26 #define MYERROR {CUtils::Error(); return FALSE;}
\r
32 CPOFile::~CPOFile(void)
\r
36 BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting /* = TRUE */)
\r
38 if (!PathFileExists(szPath))
\r
42 _ftprintf(stdout, _T("parsing file %s...\n"), szPath);
\r
46 int nTranslated = 0;
\r
47 //since stream classes still expect the filepath in char and not wchar_t
\r
48 //we need to convert the filepath to multibyte
\r
49 char filepath[MAX_PATH+1];
\r
50 SecureZeroMemory(filepath, sizeof(filepath));
\r
51 WideCharToMultiByte(CP_ACP, NULL, szPath, -1, filepath, MAX_PATH, NULL, NULL);
\r
53 std::wifstream File;
\r
54 File.imbue(std::locale(std::locale(), new utf8_conversion()));
\r
55 File.open(filepath);
\r
58 _ftprintf(stderr, _T("can't open input file %s\n"), szPath);
\r
61 TCHAR line[2*MAX_STRING_LENGTH];
\r
62 std::vector<std::wstring> entry;
\r
65 File.getline(line, sizeof(line)/sizeof(TCHAR));
\r
68 //empty line means end of entry!
\r
69 RESOURCEENTRY resEntry;
\r
72 for (std::vector<std::wstring>::iterator I = entry.begin(); I != entry.end(); ++I)
\r
74 if (_tcsncmp(I->c_str(), _T("# "), 2)==0)
\r
77 resEntry.translatorcomments.push_back(I->c_str());
\r
80 if (_tcsncmp(I->c_str(), _T("#."), 2)==0)
\r
82 //automatic comments
\r
83 resEntry.automaticcomments.push_back(I->c_str());
\r
86 if (_tcsncmp(I->c_str(), _T("#,"), 2)==0)
\r
89 resEntry.flag = I->c_str();
\r
92 if (_tcsncmp(I->c_str(), _T("msgid"), 5)==0)
\r
96 msgid = std::wstring(msgid.substr(7, msgid.size() - 8));
\r
100 if (_tcsncmp(I->c_str(), _T("msgstr"), 6)==0)
\r
103 resEntry.msgstr = I->c_str();
\r
104 resEntry.msgstr = resEntry.msgstr.substr(8, resEntry.msgstr.length() - 9);
\r
105 if (resEntry.msgstr.size()>0)
\r
109 if (_tcsncmp(I->c_str(), _T("\""), 1)==0)
\r
113 std::wstring temp = I->c_str();
\r
114 temp = temp.substr(1, temp.length()-2);
\r
119 if (resEntry.msgstr.size() == 0)
\r
121 std::wstring temp = I->c_str();
\r
122 temp = temp.substr(1, temp.length()-2);
\r
123 resEntry.msgstr += temp;
\r
128 if ((bUpdateExisting)&&(this->count(msgid) == 0))
\r
131 (*this)[msgid] = resEntry;
\r
136 entry.push_back(line);
\r
138 } while (File.gcount() > 0);
\r
139 printf(File.getloc().name().c_str());
\r
141 RESOURCEENTRY emptyentry;
\r
142 (*this)[std::wstring(_T(""))] = emptyentry;
\r
144 _ftprintf(stdout, _T("%d Entries found, %d were already translated and %d got deleted\n"), nEntries, nTranslated, nDeleted);
\r
148 BOOL CPOFile::SaveFile(LPCTSTR szPath)
\r
150 //since stream classes still expect the filepath in char and not wchar_t
\r
151 //we need to convert the filepath to multibyte
\r
152 char filepath[MAX_PATH+1];
\r
154 SecureZeroMemory(filepath, sizeof(filepath));
\r
155 WideCharToMultiByte(CP_ACP, NULL, szPath, -1, filepath, MAX_PATH, NULL, NULL);
\r
157 std::wofstream File;
\r
158 // File.open(filepath, std::ios_base::binary);
\r
159 // File << _T("\xEF\xBB\xBF");
\r
161 File.imbue(std::locale(std::locale(), new utf8_conversion()));
\r
162 File.open(filepath, std::ios_base::binary);
\r
163 File << _T("# SOME DESCRIPTIVE TITLE.\n");
\r
164 File << _T("# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER\n");
\r
165 File << _T("# This file is distributed under the same license as the PACKAGE package.\n");
\r
166 File << _T("# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n");
\r
168 File << _T("#, fuzzy\n");
\r
169 File << _T("msgid \"\"\n");
\r
170 File << _T("msgstr \"\"\n");
\r
171 File << _T("\"Project-Id-Version: PACKAGE VERSION\\n\"\n");
\r
172 File << _T("\"Report-Msgid-Bugs-To: \\n\"\n");
\r
173 File << _T("\"POT-Creation-Date: 1900-01-01 00:00+0000\\n\"\n");
\r
174 File << _T("\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n");
\r
175 File << _T("\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n");
\r
176 File << _T("\"Language-Team: LANGUAGE <LL@li.org>\\n\"\n");
\r
177 File << _T("\"MIME-Version: 1.0\\n\"\n");
\r
178 File << _T("\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
\r
179 File << _T("\"Content-Transfer-Encoding: 8bit\\n\"\n\n");
\r
181 File << _T("# msgid/msgstr fields for Accelerator keys\n");
\r
182 File << _T("# Format is: \"ID:xxxxxx:VACS+X\" where:\n");
\r
183 File << _T("# ID:xxxxx = the menu ID corresponding to the accelerator\n");
\r
184 File << _T("# V = Virtual key (or blank if not used) - nearly always set!\n");
\r
185 File << _T("# A = Alt key (or blank if not used)\n");
\r
186 File << _T("# C = Ctrl key (or blank if not used)\n");
\r
187 File << _T("# S = Shift key (or blank if not used)\n");
\r
188 File << _T("# X = upper case character\n");
\r
189 File << _T("# e.g. \"V CS+Q\" == Ctrl + Shift + 'Q'\n");
\r
191 File << _T("# ONLY Accelerator Keys with corresponding alphanumeric characters can be\n");
\r
192 File << _T("# updated i.e. function keys (F2), special keys (Delete, HoMe) etc. will not.\n");
\r
194 File << _T("# ONLY change the msgstr field. Do NOT change any other.\n");
\r
195 File << _T("# If you do not want to change an Accelerator Key, copy msgid to msgstr\n");
\r
198 for (std::map<std::wstring, RESOURCEENTRY>::iterator I = this->begin(); I != this->end(); ++I)
\r
200 if (I->first.size() == 0)
\r
202 RESOURCEENTRY entry = I->second;
\r
203 for (std::vector<std::wstring>::iterator II = entry.automaticcomments.begin(); II != entry.automaticcomments.end(); ++II)
\r
205 File << II->c_str() << _T("\n");
\r
207 for (std::vector<std::wstring>::iterator II = entry.translatorcomments.begin(); II != entry.translatorcomments.end(); ++II)
\r
209 File << II->c_str() << _T("\n");
\r
211 if (I->second.resourceIDs.size() > 0)
\r
213 File << _T("#. Resource IDs: (");
\r
215 std::set<DWORD>::const_iterator II = I->second.resourceIDs.begin();
\r
218 while (II != I->second.resourceIDs.end())
\r
226 if (I->second.flag.length() > 0)
\r
227 File << (I->second.flag.c_str()) << _T("\n");
\r
228 File << (_T("msgid \"")) << (I->first.c_str()) << _T("\"\n");
\r
229 File << (_T("msgstr \"")) << (I->second.msgstr.c_str()) << _T("\"\n\n");
\r
234 _ftprintf(stdout, _T("File %s saved, containing %d entries\n"), szPath, nEntries);
\r