Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / thesaurus / thesaurus.cxx
blob07bd605cb964de3eb9d7633256a4862c26989fc0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * OpenOffice.org - a multi-platform office productivity suite
6 * The Contents of this file are made available subject to
7 * the terms of GNU Lesser General Public License Version 3.
10 * GNU Lesser General Public License Version 3
11 * =============================================
12 * Copyright 2005 by Sun Microsystems, Inc.
13 * 901 San Antonio Road, Palo Alto, CA 94303, USA
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License version 2.1, as published by the Free Software Foundation.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
29 ************************************************************************/
33 #define WIN32_LEAN_AND_MEAN
34 #define _WIN32_WINNT 0x0500
35 #undef WINVER
36 #define WINVER 0x0500
38 #include <windows.h>
39 #include <msiquery.h>
40 #include <malloc.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include <systools/win32/uwinapi.h>
47 #include <io.h>
48 #include <iostream>
49 #include <fstream>
50 #include <string>
51 #include <map>
52 #include <vector>
53 #include <stdlib.h>
54 #include <string.h>
55 using namespace std;
56 namespace
59 string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
61 string result;
62 TCHAR szDummy[1] = TEXT("");
63 DWORD nChars = 0;
65 if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
67 DWORD nBytes = ++nChars * sizeof(TCHAR);
68 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
69 ZeroMemory( buffer, nBytes );
70 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
71 result = buffer;
73 return result;
76 inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
78 return (GetMsiProperty(handle, sProperty).length() > 0);
81 inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
83 MsiSetProperty(handle, sProperty.c_str(), NULL);
86 inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string&)
88 MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
91 static const int MAXLINE = 1024*64;
94 void generateIndex(const char *inputFile, const char *outputFile)
97 ifstream in(inputFile);
98 char inputBuffer[MAXLINE];
99 map<string, size_t> entries;
100 map<string,size_t>::iterator ret(entries.begin());
102 int line(1);
103 in.getline(inputBuffer, MAXLINE);
104 const string encoding(inputBuffer);
105 size_t currentOffset(encoding.size()+1);
106 while (true)
108 // Extract the next word, but not the entry count
109 in.getline(inputBuffer, MAXLINE, '|');
111 if (in.eof()) break;
113 string word(inputBuffer);
114 ret = entries.insert(ret, pair<string, size_t>(word, currentOffset));
115 currentOffset += word.size() + 1;
116 // Next is the entry count
117 in.getline(inputBuffer, MAXLINE);
118 if (!in.good())
120 return;
123 currentOffset += strlen(inputBuffer)+1;
124 int entryCount(strtol(inputBuffer, NULL, 10));
125 for (int i(0); i < entryCount; ++i)
127 in.getline(inputBuffer, MAXLINE);
128 currentOffset += strlen(inputBuffer)+1;
129 ++line;
133 // Use binary mode to prevent any translation of LF to CRLF on Windows
134 ofstream outputStream(outputFile, ios_base::binary| ios_base::trunc|ios_base::out);
135 if (!outputStream.is_open())
137 //cerr << "Unable to open output file " << outputFile << endl;
138 return;
141 cerr << "Doing it now" << outputFile << endl;
142 outputStream << encoding << '\n' << entries.size() << '\n';
144 for (map<string, size_t>::const_iterator ii(entries.begin());
145 ii != entries.end();
146 ++ii
149 outputStream << ii->first << '|' << ii->second << '\n';
151 outputStream.close();
154 void generateIndex(const string &datFile)
156 string idxFile(datFile.substr(0, datFile.size()-3)+"idx");
157 generateIndex(datFile.c_str(), idxFile.c_str());
159 void createIndexesForThesaurusFiles(const string & sOfficeInstallPath)
161 vector<string> thesaurusPaths;
163 string sExtensionsPath = sOfficeInstallPath + "\\share\\extensions\\";
165 string dictionariesWildcard = sExtensionsPath + "dict-*";
167 struct _finddatai64_t dictFind;
168 long h = _findfirsti64(dictionariesWildcard.c_str(),&dictFind);
169 if (h > 0)
173 if (dictFind.attrib & _A_SUBDIR)
175 struct _finddatai64_t datFind;
176 // Find any .dat files in the subdirectory
177 string dictPath = sExtensionsPath + dictFind.name + "\\";
178 string datWildCard = dictPath + "*.dat";
179 long h2 = _findfirsti64(datWildCard.c_str(), &datFind);
180 if (h2 > 0)
184 thesaurusPaths.push_back(dictPath + datFind.name);
186 } while (_findnexti64(h2,&datFind) == 0);
190 } while (_findnexti64(h,&dictFind) == 0);
192 for (vector<string>::const_iterator ii(thesaurusPaths.begin());
193 ii != thesaurusPaths.end();
194 ++ii
197 generateIndex(*ii);
201 } // namespace
204 // Creates the thesaurus .idx files for all installed .dat
205 // thesaurus files
206 extern "C" UINT __stdcall CreateIndexes( MSIHANDLE handle )
209 string sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
210 createIndexesForThesaurusFiles(sOfficeInstallPath);
211 return ERROR_SUCCESS;
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */