xref: /aoo42x/main/sal/osl/w32/profile.cxx (revision ba392698)
187d2adbcSAndrew Rist /**************************************************************
287d2adbcSAndrew Rist  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1087d2adbcSAndrew Rist  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1287d2adbcSAndrew Rist  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
1987d2adbcSAndrew Rist  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "file_url.h"
27cdf0e10cSrcweir #include "path_helper.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir #include <osl/profile.h>
31cdf0e10cSrcweir #include <osl/process.h>
32cdf0e10cSrcweir #include <osl/file.h>
33cdf0e10cSrcweir #include <osl/util.h>
34cdf0e10cSrcweir #include <rtl/alloc.h>
35cdf0e10cSrcweir #include <algorithm>
36cdf0e10cSrcweir using std::min;
copy_ustr_n(void * dest,const void * source,size_t length)37cdf0e10cSrcweir static inline void copy_ustr_n( void *dest, const void *source, size_t length ) { rtl_copyMemory(dest, source, length*sizeof(sal_Unicode)); }
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #define LINES_INI       32
40cdf0e10cSrcweir #define LINES_ADD       10
41cdf0e10cSrcweir #define SECTIONS_INI    5
42cdf0e10cSrcweir #define SECTIONS_ADD    3
43cdf0e10cSrcweir #define ENTRIES_INI     5
44cdf0e10cSrcweir #define ENTRIES_ADD     3
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #define STR_INI_EXTENSION   L".ini"
48cdf0e10cSrcweir #define STR_INI_METAHOME	"?~"
49cdf0e10cSrcweir #define STR_INI_METASYS		"?$"
50cdf0e10cSrcweir #define STR_INI_METACFG		"?^"
51cdf0e10cSrcweir #define STR_INI_METAINS		"?#"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #define STR_INI_BOOLYES     "yes"
54cdf0e10cSrcweir #define STR_INI_BOOLON      "on"
55cdf0e10cSrcweir #define STR_INI_BOOLONE     "1"
56cdf0e10cSrcweir #define STR_INI_BOOLNO      "no"
57cdf0e10cSrcweir #define STR_INI_BOOLOFF     "off"
58cdf0e10cSrcweir #define STR_INI_BOOLZERO    "0"
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #define FLG_USER            0x00FF
61cdf0e10cSrcweir #define FLG_AUTOOPEN        0x0100
62cdf0e10cSrcweir #define FLG_MODIFIED        0x0200
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #define SVERSION_LOCATION   STR_INI_METACFG
65cdf0e10cSrcweir #define SVERSION_FALLBACK   STR_INI_METASYS
66cdf0e10cSrcweir #define SVERSION_NAME   	"sversion"
67cdf0e10cSrcweir #define SVERSION_SECTION    "Versions"
68cdf0e10cSrcweir #define SVERSION_SOFFICE    "StarOffice"
69cdf0e10cSrcweir #define SVERSION_PROFILE    "soffice.ini"
70cdf0e10cSrcweir #define SVERSION_OPTION     "userid:"
71cdf0e10cSrcweir #define SVERSION_DIRS		{ "bin", "program" }
72cdf0e10cSrcweir #define SVERSION_USER       "user"
73cdf0e10cSrcweir 
74cdf0e10cSrcweir #define DEFAULT_PMODE   (_S_IREAD | _S_IWRITE)
75cdf0e10cSrcweir 
76cdf0e10cSrcweir #define _BUILD_STR_(n)	# n
77cdf0e10cSrcweir #define BUILD_STR(n)	_BUILD_STR_(n)
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 
80cdf0e10cSrcweir /*#define DEBUG_OSL_PROFILE 1*/
81cdf0e10cSrcweir /*#define TRACE_OSL_PROFILE 1*/
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 
84cdf0e10cSrcweir /*****************************************************************************/
85cdf0e10cSrcweir /* Data Type Definition */
86cdf0e10cSrcweir /*****************************************************************************/
87cdf0e10cSrcweir 
88cdf0e10cSrcweir typedef FILETIME osl_TStamp;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir typedef enum _osl_TLockMode
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	un_lock, read_lock, write_lock
93cdf0e10cSrcweir } osl_TLockMode;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir typedef struct _osl_TFile
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	HANDLE  m_Handle;
98cdf0e10cSrcweir 	sal_Char*   m_pReadPtr;
99cdf0e10cSrcweir 	sal_Char    m_ReadBuf[512];
100cdf0e10cSrcweir /*  	sal_Char*   m_pWritePtr; */
101cdf0e10cSrcweir /*  	sal_Char    m_WriteBuf[512]; */
102cdf0e10cSrcweir     sal_Char*   m_pWriteBuf;
103cdf0e10cSrcweir     sal_uInt32  m_nWriteBufLen;
104cdf0e10cSrcweir     sal_uInt32  m_nWriteBufFree;
105cdf0e10cSrcweir } osl_TFile;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir typedef struct _osl_TProfileEntry
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	sal_uInt32   	m_Line;
110cdf0e10cSrcweir 	sal_uInt32    	m_Offset;
111cdf0e10cSrcweir 	sal_uInt32      m_Len;
112cdf0e10cSrcweir } osl_TProfileEntry;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir typedef struct _osl_TProfileSection
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 	sal_uInt32          m_Line;
117cdf0e10cSrcweir 	sal_uInt32          m_Offset;
118cdf0e10cSrcweir 	sal_uInt32          m_Len;
119cdf0e10cSrcweir 	sal_uInt32          m_NoEntries;
120cdf0e10cSrcweir 	sal_uInt32          m_MaxEntries;
121cdf0e10cSrcweir 	osl_TProfileEntry*  m_Entries;
122cdf0e10cSrcweir } osl_TProfileSection;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 
125cdf0e10cSrcweir /*
126cdf0e10cSrcweir 	Profile-data structure hidden behind oslProfile:
127cdf0e10cSrcweir */
128cdf0e10cSrcweir typedef struct _osl_TProfileImpl
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	sal_uInt32  m_Flags;
131cdf0e10cSrcweir 	osl_TFile*  m_pFile;
132cdf0e10cSrcweir 	osl_TStamp  m_Stamp;
133cdf0e10cSrcweir 	sal_uInt32  m_NoLines;
134cdf0e10cSrcweir 	sal_uInt32  m_MaxLines;
135cdf0e10cSrcweir 	sal_uInt32  m_NoSections;
136cdf0e10cSrcweir 	sal_uInt32  m_MaxSections;
137cdf0e10cSrcweir 	sal_Char**  m_Lines;
138cdf0e10cSrcweir 	rtl_uString *m_strFileName;
139cdf0e10cSrcweir 	osl_TProfileSection* m_Sections;
140cdf0e10cSrcweir } osl_TProfileImpl;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir /*****************************************************************************/
144cdf0e10cSrcweir /* Static Module Function Declarations */
145cdf0e10cSrcweir /*****************************************************************************/
146cdf0e10cSrcweir 
147cdf0e10cSrcweir static osl_TFile*           openFileImpl(rtl_uString * strFileName, oslProfileOption ProfileFlags  );
148cdf0e10cSrcweir static osl_TStamp           closeFileImpl(osl_TFile* pFile);
149cdf0e10cSrcweir static sal_Bool             lockFile(const osl_TFile* pFile, osl_TLockMode eMode);
150cdf0e10cSrcweir static sal_Bool             rewindFile(osl_TFile* pFile, sal_Bool bTruncate);
151cdf0e10cSrcweir static osl_TStamp           getFileStamp(osl_TFile* pFile);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir static sal_Bool             getLine(osl_TFile* pFile, const sal_Char *pszLine, int MaxLen);
154cdf0e10cSrcweir static sal_Bool             putLine(osl_TFile* pFile, const sal_Char *pszLine);
155cdf0e10cSrcweir static const sal_Char*      stripBlanks(const sal_Char* String, sal_uInt32* pLen);
156cdf0e10cSrcweir static const sal_Char*      addLine(osl_TProfileImpl* pProfile, const sal_Char* Line);
157cdf0e10cSrcweir static const sal_Char*      insertLine(osl_TProfileImpl* pProfile, const sal_Char* Line, sal_uInt32 LineNo);
158cdf0e10cSrcweir static void                 removeLine(osl_TProfileImpl* pProfile, sal_uInt32 LineNo);
159cdf0e10cSrcweir static void                 setEntry(osl_TProfileImpl* pProfile, osl_TProfileSection* pSection,
160cdf0e10cSrcweir                                      sal_uInt32 NoEntry, sal_uInt32 Line,
161cdf0e10cSrcweir                                      const sal_Char* Entry, sal_uInt32 Len);
162cdf0e10cSrcweir static sal_Bool             addEntry(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection,
163cdf0e10cSrcweir                                      int Line, const sal_Char* Entry, sal_uInt32 Len);
164cdf0e10cSrcweir static void                 removeEntry(osl_TProfileSection *pSection, sal_uInt32 NoEntry);
165cdf0e10cSrcweir static sal_Bool             addSection(osl_TProfileImpl* pProfile, int Line, const sal_Char* Section, sal_uInt32 Len);
166cdf0e10cSrcweir static void                 removeSection(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection);
167cdf0e10cSrcweir static osl_TProfileSection* findEntry(osl_TProfileImpl* pProfile, const sal_Char* Section,
168cdf0e10cSrcweir                                       const sal_Char* Entry, sal_uInt32 *pNoEntry);
169cdf0e10cSrcweir static sal_Bool             loadProfile(osl_TFile* pFile, osl_TProfileImpl* pProfile);
170cdf0e10cSrcweir static sal_Bool             storeProfile(osl_TProfileImpl* pProfile, sal_Bool bCleanup);
171cdf0e10cSrcweir static osl_TProfileImpl*    acquireProfile(oslProfile Profile, sal_Bool bWriteable);
172cdf0e10cSrcweir static sal_Bool             releaseProfile(osl_TProfileImpl* pProfile);
173cdf0e10cSrcweir static sal_Bool             lookupProfile(const sal_Unicode *strPath, const sal_Unicode *strFile, sal_Unicode *strProfile);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir static sal_Bool writeProfileImpl (osl_TFile* pFile);
176cdf0e10cSrcweir static osl_TFile* osl_openTmpProfileImpl(osl_TProfileImpl*);
177cdf0e10cSrcweir static sal_Bool osl_ProfileSwapProfileNames(osl_TProfileImpl*);
178cdf0e10cSrcweir static rtl_uString* osl_ProfileGenerateExtension(rtl_uString* ustrFileName, rtl_uString* ustrExtension);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir static sal_Bool SAL_CALL osl_getProfileName(rtl_uString* strPath, rtl_uString* strName, rtl_uString** strProfileName);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir /*****************************************************************************/
183cdf0e10cSrcweir /* Exported Module Functions */
184cdf0e10cSrcweir /*****************************************************************************/
185cdf0e10cSrcweir 
osl_openProfile(rtl_uString * strProfileName,sal_uInt32 Flags)186cdf0e10cSrcweir oslProfile SAL_CALL osl_openProfile(rtl_uString *strProfileName, sal_uInt32 Flags)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	osl_TFile*        pFile = NULL;
189cdf0e10cSrcweir 	osl_TProfileImpl* pProfile;
190cdf0e10cSrcweir 	rtl_uString		  *FileName=NULL;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
193cdf0e10cSrcweir     OSL_TRACE("In  osl_openProfile\n");
194cdf0e10cSrcweir #endif
195cdf0e10cSrcweir 	OSL_VERIFY(strProfileName);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	if (rtl_uString_getLength(strProfileName) == 0 )
198cdf0e10cSrcweir 	{
199cdf0e10cSrcweir 		OSL_VERIFY(osl_getProfileName(NULL, NULL, &FileName));
200cdf0e10cSrcweir 	}
201cdf0e10cSrcweir 	else
202cdf0e10cSrcweir 	{
203cdf0e10cSrcweir 		rtl_uString_assign(&FileName, strProfileName);
204cdf0e10cSrcweir 	}
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	osl_getSystemPathFromFileURL(FileName, &FileName);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 
210cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
211cdf0e10cSrcweir     Flags=osl_Profile_FLUSHWRITE;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     // OSL_TRACE("opening '%s'\n",FileName);
214cdf0e10cSrcweir     if ( Flags == osl_Profile_DEFAULT )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         OSL_TRACE("with osl_Profile_DEFAULT \n");
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     if ( Flags & osl_Profile_SYSTEM )
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         OSL_TRACE("with osl_Profile_SYSTEM \n");
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir     if ( Flags & osl_Profile_READLOCK )
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         OSL_TRACE("with osl_Profile_READLOCK \n");
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir     if ( Flags & osl_Profile_WRITELOCK )
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         OSL_TRACE("with osl_Profile_WRITELOCK \n");
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir /*      if ( Flags & osl_Profile_READWRITE ) */
231cdf0e10cSrcweir /*      { */
232cdf0e10cSrcweir /*          OSL_TRACE("with osl_Profile_READWRITE \n"); */
233cdf0e10cSrcweir /*      } */
234cdf0e10cSrcweir     if ( Flags & osl_Profile_FLUSHWRITE )
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         OSL_TRACE("with osl_Profile_FLUSHWRITE \n");
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir #endif
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     if ( (! (Flags & osl_Profile_SYSTEM)) && ( (pFile = openFileImpl(FileName, Flags) ) == NULL ) )
241cdf0e10cSrcweir     {
242cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
243cdf0e10cSrcweir 	    OSL_TRACE("Out osl_openProfile [not opened]\n");
244cdf0e10cSrcweir #endif
245cdf0e10cSrcweir 		if( FileName)
246cdf0e10cSrcweir 			rtl_uString_release( FileName);
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         return (NULL);
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	pProfile = (osl_TProfileImpl*)calloc(1, sizeof(osl_TProfileImpl));
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	pProfile->m_Flags = Flags & FLG_USER;
256cdf0e10cSrcweir 	osl_getSystemPathFromFileURL(strProfileName, &pProfile->m_strFileName);
257cdf0e10cSrcweir //	rtl_uString_assign(&pProfile->m_strFileName, strProfileName);
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	if (Flags & (osl_Profile_READLOCK | osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE ))
260cdf0e10cSrcweir 		pProfile->m_pFile = pFile;
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	pProfile->m_Stamp = getFileStamp(pFile);
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	loadProfile(pFile, pProfile);
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 	if (pProfile->m_pFile == NULL)
267cdf0e10cSrcweir 		closeFileImpl(pFile);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
270cdf0e10cSrcweir     OSL_TRACE("Out osl_openProfile [ok]\n");
271cdf0e10cSrcweir #endif
272cdf0e10cSrcweir 	if( FileName)
273cdf0e10cSrcweir 		rtl_uString_release( FileName);
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	return (pProfile);
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
osl_closeProfile(oslProfile Profile)278cdf0e10cSrcweir sal_Bool SAL_CALL osl_closeProfile(oslProfile Profile)
279cdf0e10cSrcweir {
280cdf0e10cSrcweir 	osl_TProfileImpl* pProfile = (osl_TProfileImpl*)Profile;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
283cdf0e10cSrcweir     OSL_TRACE("In  osl_closeProfile\n");
284cdf0e10cSrcweir #endif
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     if ( Profile == 0 )
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
289cdf0e10cSrcweir         OSL_TRACE("Out osl_closeProfile [profile==0]\n");
290cdf0e10cSrcweir #endif
291cdf0e10cSrcweir         return sal_False;
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
295cdf0e10cSrcweir 	{
296cdf0e10cSrcweir         pProfile = acquireProfile(Profile,sal_True);
297cdf0e10cSrcweir 
298cdf0e10cSrcweir         if ( pProfile != 0 )
299cdf0e10cSrcweir         {
300cdf0e10cSrcweir 			if ( !( pProfile->m_Flags & osl_Profile_READLOCK )  && ( pProfile->m_Flags & FLG_MODIFIED ) )
301cdf0e10cSrcweir 			{
302cdf0e10cSrcweir /*  				if (pProfile->m_pFile == NULL) */
303cdf0e10cSrcweir /*  					pProfile->m_pFile = openFileImpl(pProfile->m_Filename, sal_True); */
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 				storeProfile(pProfile, sal_False);
306cdf0e10cSrcweir 			}
307cdf0e10cSrcweir 		}
308cdf0e10cSrcweir 		else
309cdf0e10cSrcweir 		{
310cdf0e10cSrcweir 			pProfile = acquireProfile(Profile,sal_False);
311cdf0e10cSrcweir 		}
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 		if ( pProfile == 0 )
314cdf0e10cSrcweir 		{
315cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
316cdf0e10cSrcweir 			OSL_TRACE("Out osl_closeProfile [pProfile==0]\n");
317cdf0e10cSrcweir #endif
318cdf0e10cSrcweir 			return sal_False;
319cdf0e10cSrcweir 		}
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 		if (pProfile->m_pFile != NULL)
322cdf0e10cSrcweir 			closeFileImpl(pProfile->m_pFile);
323cdf0e10cSrcweir 	}
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 	pProfile->m_pFile = NULL;
326cdf0e10cSrcweir 	rtl_uString_release(pProfile->m_strFileName);
327cdf0e10cSrcweir 	pProfile->m_strFileName = NULL;
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 	/* release whole profile data types memory */
330cdf0e10cSrcweir 	if ( pProfile->m_NoLines > 0)
331cdf0e10cSrcweir 	{
332cdf0e10cSrcweir 		unsigned int index=0;
333cdf0e10cSrcweir 		if ( pProfile->m_Lines != 0 )
334cdf0e10cSrcweir 		{
335cdf0e10cSrcweir 			for ( index = 0 ; index < pProfile->m_NoLines ; ++index)
336cdf0e10cSrcweir 			{
337cdf0e10cSrcweir 				if ( pProfile->m_Lines[index] != 0 )
338cdf0e10cSrcweir 				{
339cdf0e10cSrcweir 					free(pProfile->m_Lines[index]);
340cdf0e10cSrcweir 				}
341cdf0e10cSrcweir 			}
342cdf0e10cSrcweir 			free(pProfile->m_Lines);
343cdf0e10cSrcweir 		}
344cdf0e10cSrcweir 		if ( pProfile->m_Sections != 0 )
345cdf0e10cSrcweir 		{
346cdf0e10cSrcweir 			/*osl_TProfileSection* pSections=pProfile->m_Sections;*/
347cdf0e10cSrcweir 			for ( index = 0 ; index < pProfile->m_NoSections ; ++index )
348cdf0e10cSrcweir 			{
349cdf0e10cSrcweir 				if ( pProfile->m_Sections[index].m_Entries != 0 )
350cdf0e10cSrcweir 				{
351cdf0e10cSrcweir 					free(pProfile->m_Sections[index].m_Entries);
352cdf0e10cSrcweir 				}
353cdf0e10cSrcweir 			}
354cdf0e10cSrcweir 			free(pProfile->m_Sections);
355cdf0e10cSrcweir 		}
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 	}
358cdf0e10cSrcweir 	free(pProfile);
359cdf0e10cSrcweir 
360cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
361cdf0e10cSrcweir     OSL_TRACE("Out osl_closeProfile [ok]\n");
362cdf0e10cSrcweir #endif
363cdf0e10cSrcweir 	return (sal_True);
364cdf0e10cSrcweir }
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 
osl_flushProfile(oslProfile Profile)367cdf0e10cSrcweir sal_Bool SAL_CALL osl_flushProfile(oslProfile Profile)
368cdf0e10cSrcweir {
369cdf0e10cSrcweir 	osl_TProfileImpl* pProfile = (osl_TProfileImpl*) Profile;
370cdf0e10cSrcweir 	osl_TFile* pFile;
371cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
372cdf0e10cSrcweir 
373cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
374cdf0e10cSrcweir     OSL_TRACE("In  osl_flushProfile()\n");
375cdf0e10cSrcweir #endif
376cdf0e10cSrcweir 
377cdf0e10cSrcweir     if ( pProfile == 0 )
378cdf0e10cSrcweir     {
379cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
380cdf0e10cSrcweir         OSL_TRACE("Out osl_flushProfile() [pProfile == 0]\n");
381cdf0e10cSrcweir #endif
382cdf0e10cSrcweir         return sal_False;
383cdf0e10cSrcweir     }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 	pFile = pProfile->m_pFile;
386cdf0e10cSrcweir     if ( !( pFile != 0 && pFile->m_Handle >= 0 ) )
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
389cdf0e10cSrcweir         OSL_TRACE("Out osl_flushProfile() [invalid file]\n");
390cdf0e10cSrcweir #endif
391cdf0e10cSrcweir         return sal_False;
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 	if ( pProfile->m_Flags & FLG_MODIFIED )
395cdf0e10cSrcweir 	{
396cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
397cdf0e10cSrcweir         OSL_TRACE("swapping to storeprofile\n");
398cdf0e10cSrcweir #endif
399cdf0e10cSrcweir 		bRet = storeProfile(pProfile,sal_False);
400cdf0e10cSrcweir 	}
401cdf0e10cSrcweir 
402cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
403cdf0e10cSrcweir     OSL_TRACE("Out osl_flushProfile() [ok]\n");
404cdf0e10cSrcweir #endif
405cdf0e10cSrcweir     return bRet;
406cdf0e10cSrcweir }
407cdf0e10cSrcweir 
writeProfileImpl(osl_TFile * pFile)408cdf0e10cSrcweir static sal_Bool writeProfileImpl(osl_TFile* pFile)
409cdf0e10cSrcweir {
410cdf0e10cSrcweir 	DWORD BytesWritten=0;
411cdf0e10cSrcweir 	BOOL bRet;
412cdf0e10cSrcweir 
413cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
414cdf0e10cSrcweir     OSL_TRACE("In  osl_writeProfileImpl()\n");
415cdf0e10cSrcweir #endif
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     if ( !( pFile != 0 && pFile->m_Handle != INVALID_HANDLE_VALUE ) || ( pFile->m_pWriteBuf == 0 ) )
418cdf0e10cSrcweir     {
419cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
420cdf0e10cSrcweir         OSL_TRACE("Out osl_writeProfileImpl() [invalid args]\n");
421cdf0e10cSrcweir #endif
422cdf0e10cSrcweir         return sal_False;
423cdf0e10cSrcweir     }
424cdf0e10cSrcweir 
425cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
426cdf0e10cSrcweir /*    OSL_TRACE("File Buffer in writeProfileImpl '%s' size == '%i' '%i'(%i)\n",
427cdf0e10cSrcweir       pFile->m_pWriteBuf,pFile->m_nWriteBufLen,strlen(pFile->m_pWriteBuf),pFile->m_nWriteBufLen - pFile->m_nWriteBufFree);*/
428cdf0e10cSrcweir #endif
429cdf0e10cSrcweir 
430cdf0e10cSrcweir     bRet=WriteFile(pFile->m_Handle, pFile->m_pWriteBuf, pFile->m_nWriteBufLen - pFile->m_nWriteBufFree,&BytesWritten,NULL);
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     if ( bRet == 0 || BytesWritten <= 0 )
433cdf0e10cSrcweir     {
434cdf0e10cSrcweir 		OSL_ENSURE(bRet,"WriteFile failed!!!");
435cdf0e10cSrcweir 
436cdf0e10cSrcweir         OSL_TRACE("write failed '%s'\n",strerror(errno));
437cdf0e10cSrcweir 
438cdf0e10cSrcweir /*        OSL_TRACE("Out osl_writeProfileImpl() [write '%s']\n",strerror(errno));*/
439cdf0e10cSrcweir         return (sal_False);
440cdf0e10cSrcweir     }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     free(pFile->m_pWriteBuf);
443cdf0e10cSrcweir     pFile->m_pWriteBuf=0;
444cdf0e10cSrcweir 	pFile->m_nWriteBufLen=0;
445cdf0e10cSrcweir 	pFile->m_nWriteBufFree=0;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
448cdf0e10cSrcweir     OSL_TRACE("Out osl_writeProfileImpl() [ok]\n");
449cdf0e10cSrcweir #endif
450cdf0e10cSrcweir     return sal_True;
451cdf0e10cSrcweir }
452cdf0e10cSrcweir 
453cdf0e10cSrcweir 
osl_readProfileString(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_Char * pszString,sal_uInt32 MaxLen,const sal_Char * pszDefault)454cdf0e10cSrcweir sal_Bool SAL_CALL osl_readProfileString(oslProfile Profile,
455cdf0e10cSrcweir 							  const sal_Char* pszSection, const sal_Char* pszEntry,
456cdf0e10cSrcweir 							  sal_Char* pszString, sal_uInt32 MaxLen,
457cdf0e10cSrcweir 							  const sal_Char* pszDefault)
458cdf0e10cSrcweir {
459cdf0e10cSrcweir 	sal_uInt32    NoEntry;
460cdf0e10cSrcweir 	const sal_Char* pStr = 0;
461cdf0e10cSrcweir 	osl_TProfileSection* pSec;
462cdf0e10cSrcweir 	osl_TProfileImpl*    pProfile = 0;
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 
465cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
466cdf0e10cSrcweir     OSL_TRACE("In  osl_readProfileString\n");
467cdf0e10cSrcweir #endif
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     pProfile = acquireProfile(Profile, sal_False);
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 	if (pProfile == NULL)
472cdf0e10cSrcweir     {
473cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
474cdf0e10cSrcweir         OSL_TRACE("Out osl_readProfileString [pProfile==0]\n");
475cdf0e10cSrcweir #endif
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 
478cdf0e10cSrcweir 		return (sal_False);
479cdf0e10cSrcweir     }
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 
482cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
483cdf0e10cSrcweir 	{
484cdf0e10cSrcweir 		if (((pSec = findEntry(pProfile, pszSection, pszEntry, &NoEntry)) != NULL) &&
485cdf0e10cSrcweir 			(NoEntry < pSec->m_NoEntries) &&
486cdf0e10cSrcweir 			((pStr = strchr(pProfile->m_Lines[pSec->m_Entries[NoEntry].m_Line],
487cdf0e10cSrcweir 							'=')) != NULL))
488cdf0e10cSrcweir 			pStr++;
489cdf0e10cSrcweir 		else
490cdf0e10cSrcweir 			pStr = pszDefault;
491cdf0e10cSrcweir 
492cdf0e10cSrcweir 		if ( pStr != 0 )
493cdf0e10cSrcweir 		{
494cdf0e10cSrcweir 			pStr = stripBlanks(pStr, NULL);
495cdf0e10cSrcweir 			MaxLen = (MaxLen - 1 < strlen(pStr)) ? (MaxLen - 1) : strlen(pStr);
496cdf0e10cSrcweir 			pStr = stripBlanks(pStr, &MaxLen);
497cdf0e10cSrcweir 			strncpy(pszString, pStr, MaxLen);
498cdf0e10cSrcweir 			pszString[MaxLen] = '\0';
499cdf0e10cSrcweir 		}
500cdf0e10cSrcweir 	}
501cdf0e10cSrcweir 	else
502cdf0e10cSrcweir 	{
503cdf0e10cSrcweir         ::osl::LongPathBuffer< sal_Char > aFileName( MAX_LONG_PATH );
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(pProfile->m_strFileName->buffer), -1, aFileName, aFileName.getBufSizeInSymbols(), NULL, NULL);
506cdf0e10cSrcweir 		GetPrivateProfileString(pszSection, pszEntry, pszDefault, pszString, MaxLen, aFileName);
507cdf0e10cSrcweir 	}
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 	releaseProfile(pProfile);
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 	if ( pStr == 0 )
512cdf0e10cSrcweir     {
513cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
514cdf0e10cSrcweir         OSL_TRACE("Out osl_readProfileString [pStr==0]\n");
515cdf0e10cSrcweir #endif
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 
518cdf0e10cSrcweir 		return (sal_False);
519cdf0e10cSrcweir     }
520cdf0e10cSrcweir 
521cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
522cdf0e10cSrcweir     OSL_TRACE("Out osl_readProfileString [ok]\n");
523cdf0e10cSrcweir #endif
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 	return (sal_True);
529cdf0e10cSrcweir }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 
osl_readProfileBool(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_Bool Default)532cdf0e10cSrcweir sal_Bool SAL_CALL osl_readProfileBool(oslProfile Profile,
533cdf0e10cSrcweir 							const sal_Char* pszSection, const sal_Char* pszEntry,
534cdf0e10cSrcweir 							sal_Bool Default)
535cdf0e10cSrcweir {
536cdf0e10cSrcweir 	sal_Char Line[32];
537cdf0e10cSrcweir 
538cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
539cdf0e10cSrcweir     OSL_TRACE("In  osl_readProfileBool\n");
540cdf0e10cSrcweir #endif
541cdf0e10cSrcweir 
542cdf0e10cSrcweir 	if (osl_readProfileString(Profile, pszSection, pszEntry, Line, sizeof(Line), ""))
543cdf0e10cSrcweir 	{
544cdf0e10cSrcweir 		if ((stricmp(Line, STR_INI_BOOLYES) == 0) ||
545cdf0e10cSrcweir 			(stricmp(Line, STR_INI_BOOLON)  == 0) ||
546cdf0e10cSrcweir 			(stricmp(Line, STR_INI_BOOLONE) == 0))
547cdf0e10cSrcweir 			Default = sal_True;
548cdf0e10cSrcweir 		else
549cdf0e10cSrcweir 			if ((stricmp(Line, STR_INI_BOOLNO)   == 0) ||
550cdf0e10cSrcweir 				(stricmp(Line, STR_INI_BOOLOFF)  == 0) ||
551cdf0e10cSrcweir 				(stricmp(Line, STR_INI_BOOLZERO) == 0))
552cdf0e10cSrcweir 				Default = sal_False;
553cdf0e10cSrcweir 	}
554cdf0e10cSrcweir 
555cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
556cdf0e10cSrcweir     OSL_TRACE("Out osl_readProfileBool [ok]\n");
557cdf0e10cSrcweir #endif
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 	return (Default);
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir 
osl_readProfileIdent(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_uInt32 FirstId,const sal_Char * Strings[],sal_uInt32 Default)563cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_readProfileIdent(oslProfile Profile,
564cdf0e10cSrcweir 							  const sal_Char* pszSection, const sal_Char* pszEntry,
565cdf0e10cSrcweir 							  sal_uInt32 FirstId, const sal_Char* Strings[],
566cdf0e10cSrcweir 							  sal_uInt32 Default)
567cdf0e10cSrcweir {
568cdf0e10cSrcweir 	sal_uInt32    i;
569cdf0e10cSrcweir 	sal_Char        Line[256];
570cdf0e10cSrcweir 
571cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
572cdf0e10cSrcweir     OSL_TRACE("In  osl_readProfileIdent\n");
573cdf0e10cSrcweir #endif
574cdf0e10cSrcweir 
575cdf0e10cSrcweir 	if (osl_readProfileString(Profile, pszSection, pszEntry, Line, sizeof(Line), ""))
576cdf0e10cSrcweir 	{
577cdf0e10cSrcweir 		i = 0;
578cdf0e10cSrcweir 		while (Strings[i] != NULL)
579cdf0e10cSrcweir 		{
580cdf0e10cSrcweir 			if (stricmp(Line, Strings[i]) == 0)
581cdf0e10cSrcweir 			{
582cdf0e10cSrcweir 				Default = i + FirstId;
583cdf0e10cSrcweir 				break;
584cdf0e10cSrcweir 			}
585cdf0e10cSrcweir 			i++;
586cdf0e10cSrcweir 		}
587cdf0e10cSrcweir 	}
588cdf0e10cSrcweir 
589cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
590cdf0e10cSrcweir     OSL_TRACE("Out osl_readProfileIdent [ok]\n");
591cdf0e10cSrcweir #endif
592cdf0e10cSrcweir 	return (Default);
593cdf0e10cSrcweir }
594cdf0e10cSrcweir 
osl_writeProfileString(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,const sal_Char * pszString)595cdf0e10cSrcweir sal_Bool SAL_CALL osl_writeProfileString(oslProfile Profile,
596cdf0e10cSrcweir 							   const sal_Char* pszSection, const sal_Char* pszEntry,
597cdf0e10cSrcweir 							   const sal_Char* pszString)
598cdf0e10cSrcweir {
599cdf0e10cSrcweir 	sal_uInt32    i;
600cdf0e10cSrcweir     sal_Bool bRet = sal_False;
601cdf0e10cSrcweir 	sal_uInt32    NoEntry;
602cdf0e10cSrcweir 	const sal_Char* pStr;
603cdf0e10cSrcweir 	sal_Char        Line[4096];
604cdf0e10cSrcweir 	osl_TProfileSection* pSec;
605cdf0e10cSrcweir 	osl_TProfileImpl*    pProfile = 0;
606cdf0e10cSrcweir 
607cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
608cdf0e10cSrcweir     OSL_TRACE("In  osl_writeProfileString\n");
609cdf0e10cSrcweir #endif
610cdf0e10cSrcweir 
611cdf0e10cSrcweir     pProfile = acquireProfile(Profile, sal_True);
612cdf0e10cSrcweir 
613cdf0e10cSrcweir 	if (pProfile == NULL)
614cdf0e10cSrcweir     {
615cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
616cdf0e10cSrcweir         OSL_TRACE("Out osl_writeProfileString [pProfile==0]\n");
617cdf0e10cSrcweir #endif
618cdf0e10cSrcweir 		return (sal_False);
619cdf0e10cSrcweir     }
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 
622cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
623cdf0e10cSrcweir 	{
624cdf0e10cSrcweir 		if ((pSec = findEntry(pProfile, pszSection, pszEntry, &NoEntry)) == NULL)
625cdf0e10cSrcweir 		{
626cdf0e10cSrcweir 			Line[0] = '\0';
627cdf0e10cSrcweir 			addLine(pProfile, Line);
628cdf0e10cSrcweir 
629cdf0e10cSrcweir 			Line[0] = '[';
630cdf0e10cSrcweir 			strcpy(&Line[1], pszSection);
631cdf0e10cSrcweir 			Line[1 + strlen(pszSection)] = ']';
632cdf0e10cSrcweir 			Line[2 + strlen(pszSection)] = '\0';
633cdf0e10cSrcweir 
634cdf0e10cSrcweir 			if (((pStr = addLine(pProfile, Line)) == NULL) ||
635cdf0e10cSrcweir 				(! addSection(pProfile, pProfile->m_NoLines - 1, &pStr[1], strlen(pszSection))))
636cdf0e10cSrcweir 			{
637cdf0e10cSrcweir 				releaseProfile(pProfile);
638cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
639cdf0e10cSrcweir                 OSL_TRACE("Out osl_writeProfileString [not added]\n");
640cdf0e10cSrcweir #endif
641cdf0e10cSrcweir 				return (sal_False);
642cdf0e10cSrcweir 			}
643cdf0e10cSrcweir 
644cdf0e10cSrcweir 			pSec = &pProfile->m_Sections[pProfile->m_NoSections - 1];
645cdf0e10cSrcweir 			NoEntry = pSec->m_NoEntries;
646cdf0e10cSrcweir 		}
647cdf0e10cSrcweir 
648cdf0e10cSrcweir 		Line[0] = '\0';
649cdf0e10cSrcweir 		strcpy(&Line[0], pszEntry);
650cdf0e10cSrcweir 		Line[0 + strlen(pszEntry)] = '=';
651cdf0e10cSrcweir 		strcpy(&Line[1 + strlen(pszEntry)], pszString);
652cdf0e10cSrcweir 
653cdf0e10cSrcweir 		if (NoEntry >= pSec->m_NoEntries)
654cdf0e10cSrcweir 		{
655cdf0e10cSrcweir 			if (pSec->m_NoEntries > 0)
656cdf0e10cSrcweir 				i = pSec->m_Entries[pSec->m_NoEntries - 1].m_Line + 1;
657cdf0e10cSrcweir 			else
658cdf0e10cSrcweir 				i = pSec->m_Line + 1;
659cdf0e10cSrcweir 
660cdf0e10cSrcweir 			if (((pStr = insertLine(pProfile, Line, i)) == NULL) ||
661cdf0e10cSrcweir 				(! addEntry(pProfile, pSec, i, pStr, strlen(pszEntry))))
662cdf0e10cSrcweir 			{
663cdf0e10cSrcweir 				releaseProfile(pProfile);
664cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
665cdf0e10cSrcweir                 OSL_TRACE("Out osl_writeProfileString [not inserted]\n");
666cdf0e10cSrcweir #endif
667cdf0e10cSrcweir 				return (sal_False);
668cdf0e10cSrcweir 			}
669cdf0e10cSrcweir 
670cdf0e10cSrcweir 			pProfile->m_Flags |= FLG_MODIFIED;
671cdf0e10cSrcweir 		}
672cdf0e10cSrcweir 		else
673cdf0e10cSrcweir 		{
674cdf0e10cSrcweir 			i = pSec->m_Entries[NoEntry].m_Line;
675cdf0e10cSrcweir 			free(pProfile->m_Lines[i]);
676cdf0e10cSrcweir 			pProfile->m_Lines[i] = strdup(Line);
677cdf0e10cSrcweir 			setEntry(pProfile, pSec, NoEntry, i, pProfile->m_Lines[i], strlen(pszEntry));
678cdf0e10cSrcweir 
679cdf0e10cSrcweir 			pProfile->m_Flags |= FLG_MODIFIED;
680cdf0e10cSrcweir 		}
681cdf0e10cSrcweir 	}
682cdf0e10cSrcweir 	else
683cdf0e10cSrcweir 	{
684cdf0e10cSrcweir         ::osl::LongPathBuffer< sal_Char > aFileName( MAX_LONG_PATH );
685cdf0e10cSrcweir 
686cdf0e10cSrcweir 		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(pProfile->m_strFileName->buffer), -1, aFileName, aFileName.getBufSizeInSymbols(), NULL, NULL);
687cdf0e10cSrcweir 		WritePrivateProfileString(pszSection, pszEntry, pszString, aFileName);
688cdf0e10cSrcweir 	}
689cdf0e10cSrcweir 
690cdf0e10cSrcweir     bRet = releaseProfile(pProfile);
691cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
692cdf0e10cSrcweir     OSL_TRACE("Out osl_writeProfileString [ok]\n");
693cdf0e10cSrcweir #endif
694cdf0e10cSrcweir 	return bRet;
695cdf0e10cSrcweir }
696cdf0e10cSrcweir 
697cdf0e10cSrcweir 
osl_writeProfileBool(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_Bool Value)698cdf0e10cSrcweir sal_Bool SAL_CALL osl_writeProfileBool(oslProfile Profile,
699cdf0e10cSrcweir 							 const sal_Char* pszSection, const sal_Char* pszEntry,
700cdf0e10cSrcweir 							 sal_Bool Value)
701cdf0e10cSrcweir {
702cdf0e10cSrcweir     sal_Bool bRet = sal_False;
703cdf0e10cSrcweir 
704cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
705cdf0e10cSrcweir     OSL_TRACE("In  osl_writeProfileBool\n");
706cdf0e10cSrcweir #endif
707cdf0e10cSrcweir 
708cdf0e10cSrcweir 	if (Value)
709cdf0e10cSrcweir 		bRet=osl_writeProfileString(Profile, pszSection, pszEntry, STR_INI_BOOLONE);
710cdf0e10cSrcweir 	else
711cdf0e10cSrcweir 		bRet=osl_writeProfileString(Profile, pszSection, pszEntry, STR_INI_BOOLZERO);
712cdf0e10cSrcweir 
713cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
714cdf0e10cSrcweir     OSL_TRACE("Out osl_writeProfileBool [ok]\n");
715cdf0e10cSrcweir #endif
716cdf0e10cSrcweir 
717cdf0e10cSrcweir     return bRet;
718cdf0e10cSrcweir }
719cdf0e10cSrcweir 
720cdf0e10cSrcweir 
osl_writeProfileIdent(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_uInt32 FirstId,const sal_Char * Strings[],sal_uInt32 Value)721cdf0e10cSrcweir sal_Bool SAL_CALL osl_writeProfileIdent(oslProfile Profile,
722cdf0e10cSrcweir 							  const sal_Char* pszSection, const sal_Char* pszEntry,
723cdf0e10cSrcweir 							  sal_uInt32 FirstId, const sal_Char* Strings[],
724cdf0e10cSrcweir 							  sal_uInt32 Value)
725cdf0e10cSrcweir {
726cdf0e10cSrcweir 	int i, n;
727cdf0e10cSrcweir     sal_Bool bRet = sal_False;
728cdf0e10cSrcweir 
729cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
730cdf0e10cSrcweir     OSL_TRACE("In  osl_writeProfileIdent\n");
731cdf0e10cSrcweir #endif
732cdf0e10cSrcweir 
733cdf0e10cSrcweir 	for (n = 0; Strings[n] != NULL; n++);
734cdf0e10cSrcweir 
735cdf0e10cSrcweir 	if ((i = Value - FirstId) >= n)
736cdf0e10cSrcweir 		bRet=sal_False;
737cdf0e10cSrcweir 	else
738cdf0e10cSrcweir 		bRet=osl_writeProfileString(Profile, pszSection, pszEntry, Strings[i]);
739cdf0e10cSrcweir 
740cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
741cdf0e10cSrcweir     OSL_TRACE("Out osl_writeProfileIdent\n");
742cdf0e10cSrcweir #endif
743cdf0e10cSrcweir     return bRet;
744cdf0e10cSrcweir }
745cdf0e10cSrcweir 
746cdf0e10cSrcweir 
osl_removeProfileEntry(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry)747cdf0e10cSrcweir sal_Bool SAL_CALL osl_removeProfileEntry(oslProfile Profile,
748cdf0e10cSrcweir 							   const sal_Char *pszSection, const sal_Char *pszEntry)
749cdf0e10cSrcweir {
750cdf0e10cSrcweir 	sal_uInt32    NoEntry;
751cdf0e10cSrcweir 	osl_TProfileSection* pSec;
752cdf0e10cSrcweir 	osl_TProfileImpl*    pProfile = 0;
753cdf0e10cSrcweir     sal_Bool bRet = sal_False;
754cdf0e10cSrcweir 
755cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
756cdf0e10cSrcweir     OSL_TRACE("In  osl_removeProfileEntry\n");
757cdf0e10cSrcweir #endif
758cdf0e10cSrcweir 
759cdf0e10cSrcweir     pProfile = acquireProfile(Profile, sal_True);
760cdf0e10cSrcweir 
761cdf0e10cSrcweir 	if (pProfile == NULL)
762cdf0e10cSrcweir     {
763cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
764cdf0e10cSrcweir         OSL_TRACE("Out osl_removeProfileEntry [pProfile==0]\n");
765cdf0e10cSrcweir #endif
766cdf0e10cSrcweir 
767cdf0e10cSrcweir 
768cdf0e10cSrcweir 		return (sal_False);
769cdf0e10cSrcweir     }
770cdf0e10cSrcweir 
771cdf0e10cSrcweir 
772cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
773cdf0e10cSrcweir 	{
774cdf0e10cSrcweir 		if (((pSec = findEntry(pProfile, pszSection, pszEntry, &NoEntry)) != NULL) &&
775cdf0e10cSrcweir 			(NoEntry < pSec->m_NoEntries))
776cdf0e10cSrcweir 		{
777cdf0e10cSrcweir 			removeLine(pProfile, pSec->m_Entries[NoEntry].m_Line);
778cdf0e10cSrcweir 			removeEntry(pSec, NoEntry);
779cdf0e10cSrcweir 			if (pSec->m_NoEntries == 0)
780cdf0e10cSrcweir 			{
781cdf0e10cSrcweir 				removeLine(pProfile, pSec->m_Line);
782cdf0e10cSrcweir 
783cdf0e10cSrcweir 				/* remove any empty separation line */
784cdf0e10cSrcweir 				if ((pSec->m_Line > 0) && (pProfile->m_Lines[pSec->m_Line - 1][0] == '\0'))
785cdf0e10cSrcweir 		            removeLine(pProfile, pSec->m_Line - 1);
786cdf0e10cSrcweir 
787cdf0e10cSrcweir 				removeSection(pProfile, pSec);
788cdf0e10cSrcweir 			}
789cdf0e10cSrcweir 
790cdf0e10cSrcweir 			pProfile->m_Flags |= FLG_MODIFIED;
791cdf0e10cSrcweir 		}
792cdf0e10cSrcweir 	}
793cdf0e10cSrcweir 	else
794cdf0e10cSrcweir 	{
795cdf0e10cSrcweir         ::osl::LongPathBuffer< sal_Char > aFileName( MAX_LONG_PATH );
796cdf0e10cSrcweir 
797cdf0e10cSrcweir 		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(pProfile->m_strFileName->buffer), -1, aFileName, aFileName.getBufSizeInSymbols(), NULL, NULL);
798cdf0e10cSrcweir 		WritePrivateProfileString(pszSection, pszEntry, NULL, aFileName);
799cdf0e10cSrcweir 	}
800cdf0e10cSrcweir 
801cdf0e10cSrcweir     bRet = releaseProfile(pProfile);
802cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
803cdf0e10cSrcweir     OSL_TRACE("Out osl_removeProfileEntry [ok]\n");
804cdf0e10cSrcweir #endif
805cdf0e10cSrcweir 	return bRet;
806cdf0e10cSrcweir }
807cdf0e10cSrcweir 
808cdf0e10cSrcweir 
osl_getProfileSectionEntries(oslProfile Profile,const sal_Char * pszSection,sal_Char * pszBuffer,sal_uInt32 MaxLen)809cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_getProfileSectionEntries(oslProfile Profile, const sal_Char *pszSection,
810cdf0e10cSrcweir 	   							    sal_Char* pszBuffer, sal_uInt32 MaxLen)
811cdf0e10cSrcweir {
812cdf0e10cSrcweir 	sal_uInt32    i, n = 0;
813cdf0e10cSrcweir 	sal_uInt32    NoEntry;
814cdf0e10cSrcweir 	osl_TProfileSection* pSec;
815cdf0e10cSrcweir 	osl_TProfileImpl*    pProfile = 0;
816cdf0e10cSrcweir 
817cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
818cdf0e10cSrcweir     OSL_TRACE("In  osl_getProfileSectionEntries\n");
819cdf0e10cSrcweir #endif
820cdf0e10cSrcweir 
821cdf0e10cSrcweir     pProfile = acquireProfile(Profile, sal_False);
822cdf0e10cSrcweir 
823cdf0e10cSrcweir     if (pProfile == NULL)
824cdf0e10cSrcweir     {
825cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
826cdf0e10cSrcweir         OSL_TRACE("Out osl_getProfileSectionEntries [pProfile=0]\n");
827cdf0e10cSrcweir #endif
828cdf0e10cSrcweir 
829cdf0e10cSrcweir 
830cdf0e10cSrcweir 		return (0);
831cdf0e10cSrcweir     }
832cdf0e10cSrcweir 
833cdf0e10cSrcweir 
834cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
835cdf0e10cSrcweir 	{
836cdf0e10cSrcweir 		if ((pSec = findEntry(pProfile, pszSection, "", &NoEntry)) != NULL)
837cdf0e10cSrcweir 		{
838cdf0e10cSrcweir 			if (MaxLen != 0)
839cdf0e10cSrcweir 			{
840cdf0e10cSrcweir 				for (i = 0; i < pSec->m_NoEntries; i++)
841cdf0e10cSrcweir 				{
842cdf0e10cSrcweir 					if ((n + pSec->m_Entries[i].m_Len + 1) < MaxLen)
843cdf0e10cSrcweir 					{
844cdf0e10cSrcweir 						strncpy(&pszBuffer[n], &pProfile->m_Lines[pSec->m_Entries[i].m_Line]
845cdf0e10cSrcweir 								[pSec->m_Entries[i].m_Offset], pSec->m_Entries[i].m_Len);
846cdf0e10cSrcweir 						n += pSec->m_Entries[i].m_Len;
847cdf0e10cSrcweir 						pszBuffer[n++] = '\0';
848cdf0e10cSrcweir 					}
849cdf0e10cSrcweir 					else
850cdf0e10cSrcweir 						break;
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 				}
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 				pszBuffer[n++] = '\0';
855cdf0e10cSrcweir 			}
856cdf0e10cSrcweir 			else
857cdf0e10cSrcweir 			{
858cdf0e10cSrcweir 				for (i = 0; i < pSec->m_NoEntries; i++)
859cdf0e10cSrcweir 					n += pSec->m_Entries[i].m_Len + 1;
860cdf0e10cSrcweir 
861cdf0e10cSrcweir 				n += 1;
862cdf0e10cSrcweir 			}
863cdf0e10cSrcweir 		}
864cdf0e10cSrcweir 		else
865cdf0e10cSrcweir 			n = 0;
866cdf0e10cSrcweir 	}
867cdf0e10cSrcweir 	else
868cdf0e10cSrcweir 	{
869cdf0e10cSrcweir         ::osl::LongPathBuffer< sal_Char > aFileName( MAX_LONG_PATH );
870cdf0e10cSrcweir 
871cdf0e10cSrcweir 		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(pProfile->m_strFileName->buffer), -1, aFileName, aFileName.getBufSizeInSymbols(), NULL, NULL);
872cdf0e10cSrcweir 		n = GetPrivateProfileString(pszSection, NULL, NULL, pszBuffer, MaxLen, aFileName);
873cdf0e10cSrcweir 	}
874cdf0e10cSrcweir 
875cdf0e10cSrcweir 	releaseProfile(pProfile);
876cdf0e10cSrcweir 
877cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
878cdf0e10cSrcweir     OSL_TRACE("Out osl_getProfileSectionEntries [ok]\n");
879cdf0e10cSrcweir #endif
880cdf0e10cSrcweir 
881cdf0e10cSrcweir 	return (n);
882cdf0e10cSrcweir }
883cdf0e10cSrcweir 
884cdf0e10cSrcweir 
osl_getProfileName(rtl_uString * strPath,rtl_uString * strName,rtl_uString ** strProfileName)885cdf0e10cSrcweir sal_Bool SAL_CALL osl_getProfileName(rtl_uString* strPath, rtl_uString* strName, rtl_uString** strProfileName)
886cdf0e10cSrcweir {
887cdf0e10cSrcweir 	sal_Bool bFailed;
888cdf0e10cSrcweir     ::osl::LongPathBuffer< sal_Unicode > aFile( MAX_LONG_PATH );
889cdf0e10cSrcweir     ::osl::LongPathBuffer< sal_Unicode > aPath( MAX_LONG_PATH );
890cdf0e10cSrcweir 	sal_uInt32  nFileLen = 0;
891cdf0e10cSrcweir 	sal_uInt32  nPathLen = 0;
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 	rtl_uString * strTmp = NULL;
894cdf0e10cSrcweir 	oslFileError nError;
895cdf0e10cSrcweir 
896cdf0e10cSrcweir 	/* build file name */
897cdf0e10cSrcweir 	if (strName && strName->length)
898cdf0e10cSrcweir 	{
899cdf0e10cSrcweir 		if( ::sal::static_int_cast< sal_uInt32 >( strName->length ) >= aFile.getBufSizeInSymbols() )
900cdf0e10cSrcweir 			return sal_False;
901cdf0e10cSrcweir 
902cdf0e10cSrcweir 		copy_ustr_n( aFile, strName->buffer, strName->length+1);
903cdf0e10cSrcweir 		nFileLen = strName->length;
904cdf0e10cSrcweir 
905cdf0e10cSrcweir 		if (rtl_ustr_indexOfChar( aFile, L'.' ) == -1)
906cdf0e10cSrcweir 		{
907cdf0e10cSrcweir 			if (nFileLen + wcslen(STR_INI_EXTENSION) >= aFile.getBufSizeInSymbols())
908cdf0e10cSrcweir 				return sal_False;
909cdf0e10cSrcweir 
910cdf0e10cSrcweir 			/* add default extension */
911cdf0e10cSrcweir 			copy_ustr_n( aFile + nFileLen, STR_INI_EXTENSION, wcslen(STR_INI_EXTENSION)+1 );
912cdf0e10cSrcweir 			nFileLen += wcslen(STR_INI_EXTENSION);
913cdf0e10cSrcweir 		}
914cdf0e10cSrcweir 	}
915cdf0e10cSrcweir 	else
916cdf0e10cSrcweir 	{
917cdf0e10cSrcweir 		rtl_uString *strProgName = NULL;
918cdf0e10cSrcweir 		sal_Unicode *pProgName;
919cdf0e10cSrcweir 		sal_Int32 nOffset = 0;
920cdf0e10cSrcweir 		sal_Int32 nLen;
921cdf0e10cSrcweir 		sal_Int32 nPos;
922cdf0e10cSrcweir 
923cdf0e10cSrcweir 		if (osl_getExecutableFile(&strProgName) != osl_Process_E_None)
924cdf0e10cSrcweir 			return sal_False;
925cdf0e10cSrcweir 
926cdf0e10cSrcweir 		/* remove path and extension from filename */
927cdf0e10cSrcweir 		pProgName = strProgName->buffer;
928cdf0e10cSrcweir 		nLen = strProgName->length ;
929cdf0e10cSrcweir 
930cdf0e10cSrcweir 		if ((nPos = rtl_ustr_lastIndexOfChar( pProgName, L'/' )) != -1)
931cdf0e10cSrcweir 			nOffset = nPos + 1;
932cdf0e10cSrcweir 		else if ((nPos = rtl_ustr_lastIndexOfChar( pProgName, L':' )) != -1)
933cdf0e10cSrcweir 			nOffset = nPos + 1;
934cdf0e10cSrcweir 
935cdf0e10cSrcweir 		if ((nPos = rtl_ustr_lastIndexOfChar( pProgName, L'.' )) != -1 )
936cdf0e10cSrcweir 			nLen -= 4;
937cdf0e10cSrcweir 
938cdf0e10cSrcweir 		if ((nFileLen = nLen - nOffset) >= aFile.getBufSizeInSymbols())
939cdf0e10cSrcweir 			return sal_False;
940cdf0e10cSrcweir 
941cdf0e10cSrcweir 		copy_ustr_n(aFile, pProgName + nOffset, nFileLen);
942cdf0e10cSrcweir 
943cdf0e10cSrcweir 		if (nFileLen + wcslen(STR_INI_EXTENSION) >= aFile.getBufSizeInSymbols())
944cdf0e10cSrcweir 			return sal_False;
945cdf0e10cSrcweir 
946cdf0e10cSrcweir 		/* add default extension */
947cdf0e10cSrcweir 		copy_ustr_n(aFile + nFileLen, STR_INI_EXTENSION, wcslen(STR_INI_EXTENSION)+1);
948cdf0e10cSrcweir 		nFileLen += wcslen(STR_INI_EXTENSION);
949cdf0e10cSrcweir 
950cdf0e10cSrcweir 		rtl_uString_release( strProgName );
951cdf0e10cSrcweir 	}
952cdf0e10cSrcweir 
953cdf0e10cSrcweir 	if (aFile[0] == 0)
954cdf0e10cSrcweir 		return sal_False;
955cdf0e10cSrcweir 
956cdf0e10cSrcweir 	/* build directory path */
957cdf0e10cSrcweir 	if (strPath && strPath->length)
958cdf0e10cSrcweir 	{
959cdf0e10cSrcweir 		sal_Unicode *pPath = rtl_uString_getStr(strPath);
960cdf0e10cSrcweir 		sal_Int32 nLen = rtl_uString_getLength(strPath);
961cdf0e10cSrcweir 
962cdf0e10cSrcweir 		if ((rtl_ustr_ascii_compare_WithLength(pPath, RTL_CONSTASCII_LENGTH(STR_INI_METAHOME) , STR_INI_METAHOME) == 0) &&
963cdf0e10cSrcweir             ((nLen == RTL_CONSTASCII_LENGTH(STR_INI_METAHOME)) || (pPath[RTL_CONSTASCII_LENGTH(STR_INI_METAHOME)] == '/')))
964cdf0e10cSrcweir 		{
965cdf0e10cSrcweir 			rtl_uString * strHome = NULL;
966cdf0e10cSrcweir 			oslSecurity security = osl_getCurrentSecurity();
967cdf0e10cSrcweir 
968cdf0e10cSrcweir 			bFailed = ! osl_getHomeDir(security, &strHome);
969cdf0e10cSrcweir 			osl_freeSecurityHandle(security);
970cdf0e10cSrcweir 
971cdf0e10cSrcweir 			if (bFailed) return (sal_False);
972cdf0e10cSrcweir 
973cdf0e10cSrcweir 			if ( ::sal::static_int_cast< sal_uInt32 >( strHome->length ) >= aPath.getBufSizeInSymbols())
974cdf0e10cSrcweir 				return sal_False;
975cdf0e10cSrcweir 
976cdf0e10cSrcweir 			copy_ustr_n( aPath, strHome->buffer, strHome->length+1);
977cdf0e10cSrcweir 			nPathLen = strHome->length;
978cdf0e10cSrcweir 
979cdf0e10cSrcweir 			if (nLen > RTL_CONSTASCII_LENGTH(STR_INI_METAHOME))
980cdf0e10cSrcweir 			{
981cdf0e10cSrcweir 				pPath += RTL_CONSTASCII_LENGTH(STR_INI_METAHOME);
982cdf0e10cSrcweir 				nLen -= RTL_CONSTASCII_LENGTH(STR_INI_METAHOME);
983cdf0e10cSrcweir 
984cdf0e10cSrcweir 				if (nLen + nPathLen >= aPath.getBufSizeInSymbols())
985cdf0e10cSrcweir 					return sal_False;
986cdf0e10cSrcweir 
987cdf0e10cSrcweir 				copy_ustr_n(aPath + nPathLen, pPath, nLen+1);
988cdf0e10cSrcweir 				nPathLen += nLen;
989cdf0e10cSrcweir 			}
990cdf0e10cSrcweir 
991cdf0e10cSrcweir 			rtl_uString_release(strHome);
992cdf0e10cSrcweir 		}
993cdf0e10cSrcweir 
994cdf0e10cSrcweir 		else if ((rtl_ustr_ascii_compare_WithLength(pPath, RTL_CONSTASCII_LENGTH(STR_INI_METACFG), STR_INI_METACFG) == 0) &&
995cdf0e10cSrcweir 			((nLen == RTL_CONSTASCII_LENGTH(STR_INI_METACFG)) || (pPath[RTL_CONSTASCII_LENGTH(STR_INI_METACFG)] == '/')))
996cdf0e10cSrcweir 		{
997cdf0e10cSrcweir 			rtl_uString * strConfig = NULL;
998cdf0e10cSrcweir 			oslSecurity security = osl_getCurrentSecurity();
999cdf0e10cSrcweir 
1000cdf0e10cSrcweir 			bFailed = ! osl_getConfigDir(security, &strConfig);
1001cdf0e10cSrcweir 			osl_freeSecurityHandle(security);
1002cdf0e10cSrcweir 
1003cdf0e10cSrcweir 			if (bFailed) return (sal_False);
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir 			if ( ::sal::static_int_cast< sal_uInt32 >( strConfig->length ) >= aPath.getBufSizeInSymbols())
1006cdf0e10cSrcweir 				return sal_False;
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir 			copy_ustr_n( aPath, strConfig->buffer, strConfig->length+1 );
1009cdf0e10cSrcweir 			nPathLen = strConfig->length;
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir 			if (nLen > RTL_CONSTASCII_LENGTH(STR_INI_METACFG))
1012cdf0e10cSrcweir 			{
1013cdf0e10cSrcweir 				pPath += RTL_CONSTASCII_LENGTH(STR_INI_METACFG);
1014cdf0e10cSrcweir 				nLen -= RTL_CONSTASCII_LENGTH(STR_INI_METACFG);
1015cdf0e10cSrcweir 
1016cdf0e10cSrcweir 				if (nLen + nPathLen >= aPath.getBufSizeInSymbols())
1017cdf0e10cSrcweir 					return sal_False;
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir 				copy_ustr_n(aPath + nPathLen, pPath, nLen+1);
1020cdf0e10cSrcweir 				nPathLen += nLen;
1021cdf0e10cSrcweir 			}
1022cdf0e10cSrcweir 
1023cdf0e10cSrcweir 			rtl_uString_release(strConfig);
1024cdf0e10cSrcweir 		}
1025cdf0e10cSrcweir 
1026cdf0e10cSrcweir 		else if ((rtl_ustr_ascii_compare_WithLength(pPath, RTL_CONSTASCII_LENGTH(STR_INI_METASYS), STR_INI_METASYS) == 0) &&
1027cdf0e10cSrcweir 			((nLen == RTL_CONSTASCII_LENGTH(STR_INI_METASYS)) || (pPath[RTL_CONSTASCII_LENGTH(STR_INI_METASYS)] == '/')))
1028cdf0e10cSrcweir 		{
1029cdf0e10cSrcweir 			if (((nPathLen = GetWindowsDirectoryW(::osl::mingw_reinterpret_cast<LPWSTR>(aPath), aPath.getBufSizeInSymbols())) == 0) || (nPathLen >= aPath.getBufSizeInSymbols()))
1030cdf0e10cSrcweir 				return (sal_False);
1031cdf0e10cSrcweir 
1032cdf0e10cSrcweir 			if (nLen > RTL_CONSTASCII_LENGTH(STR_INI_METASYS))
1033cdf0e10cSrcweir 			{
1034cdf0e10cSrcweir 				pPath += RTL_CONSTASCII_LENGTH(STR_INI_METASYS);
1035cdf0e10cSrcweir 				nLen -= RTL_CONSTASCII_LENGTH(STR_INI_METASYS);
1036cdf0e10cSrcweir 
1037cdf0e10cSrcweir 				if (nLen + nPathLen >= aPath.getBufSizeInSymbols())
1038cdf0e10cSrcweir 					return sal_False;
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir 				copy_ustr_n(aPath + nPathLen, pPath, nLen+1);
1041cdf0e10cSrcweir 				nPathLen += nLen;
1042cdf0e10cSrcweir 			}
1043cdf0e10cSrcweir 		}
1044cdf0e10cSrcweir 
1045cdf0e10cSrcweir 		else if ((rtl_ustr_ascii_compare_WithLength(pPath, RTL_CONSTASCII_LENGTH(STR_INI_METAINS), STR_INI_METAINS) == 0) &&
1046cdf0e10cSrcweir             ((nLen == RTL_CONSTASCII_LENGTH(STR_INI_METAINS)) || (pPath[RTL_CONSTASCII_LENGTH(STR_INI_METAINS)] == '/') ||
1047cdf0e10cSrcweir                 (pPath[RTL_CONSTASCII_LENGTH(STR_INI_METAINS)] == '"') ) )
1048cdf0e10cSrcweir 		{
1049cdf0e10cSrcweir 			if (! lookupProfile(pPath + RTL_CONSTASCII_LENGTH(STR_INI_METAINS), aFile, aPath))
1050cdf0e10cSrcweir 				return (sal_False);
1051cdf0e10cSrcweir 
1052cdf0e10cSrcweir 			nPathLen = rtl_ustr_getLength(aPath);
1053cdf0e10cSrcweir 		}
1054cdf0e10cSrcweir 
1055cdf0e10cSrcweir 		else if( ::sal::static_int_cast< sal_uInt32 >( nLen ) < aPath.getBufSizeInSymbols())
1056cdf0e10cSrcweir 		{
1057cdf0e10cSrcweir 			copy_ustr_n(aPath, pPath, nLen+1);
1058cdf0e10cSrcweir 			nPathLen = rtl_ustr_getLength(aPath);
1059cdf0e10cSrcweir 		}
1060cdf0e10cSrcweir 		else
1061cdf0e10cSrcweir 			return sal_False;
1062cdf0e10cSrcweir 	}
1063cdf0e10cSrcweir 	else
1064cdf0e10cSrcweir 	{
1065cdf0e10cSrcweir 		rtl_uString * strConfigDir = NULL;
1066cdf0e10cSrcweir 		oslSecurity security = osl_getCurrentSecurity();
1067cdf0e10cSrcweir 
1068cdf0e10cSrcweir 		bFailed = ! osl_getConfigDir(security, &strConfigDir);
1069cdf0e10cSrcweir 		osl_freeSecurityHandle(security);
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir 		if (bFailed) return (sal_False);
1072cdf0e10cSrcweir 		if ( ::sal::static_int_cast< sal_uInt32 >( strConfigDir->length ) >= aPath.getBufSizeInSymbols() )
1073cdf0e10cSrcweir 			return sal_False;
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir 		copy_ustr_n(aPath, strConfigDir->buffer, strConfigDir->length+1);
1076cdf0e10cSrcweir 		nPathLen = strConfigDir->length;
1077cdf0e10cSrcweir 	}
1078cdf0e10cSrcweir 
1079cdf0e10cSrcweir 	if (nPathLen && (aPath[nPathLen - 1] != L'/') && (aPath[nPathLen - 1] != L'\\'))
1080cdf0e10cSrcweir 	{
1081cdf0e10cSrcweir 		aPath[nPathLen++] = L'\\';
1082cdf0e10cSrcweir 		aPath[nPathLen] = 0;
1083cdf0e10cSrcweir 	}
1084cdf0e10cSrcweir 
1085cdf0e10cSrcweir 	if (nPathLen + nFileLen >= aPath.getBufSizeInSymbols())
1086cdf0e10cSrcweir 		return sal_False;
1087cdf0e10cSrcweir 
1088cdf0e10cSrcweir 	/* append file name */
1089cdf0e10cSrcweir 	copy_ustr_n(aPath + nPathLen, aFile, nFileLen+1);
1090cdf0e10cSrcweir 	nPathLen += nFileLen;
1091cdf0e10cSrcweir 
1092cdf0e10cSrcweir 	/* copy filename */
1093cdf0e10cSrcweir 	rtl_uString_newFromStr_WithLength(&strTmp, aPath, nPathLen);
1094cdf0e10cSrcweir 	nError = osl_getFileURLFromSystemPath(strTmp, strProfileName);
1095cdf0e10cSrcweir 	rtl_uString_release(strTmp);
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir 	return (sal_Bool) (nError == osl_File_E_None);
1098cdf0e10cSrcweir }
1099cdf0e10cSrcweir 
1100cdf0e10cSrcweir 
osl_getProfileSections(oslProfile Profile,sal_Char * pszBuffer,sal_uInt32 MaxLen)1101cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_getProfileSections(oslProfile Profile, sal_Char* pszBuffer, sal_uInt32 MaxLen)
1102cdf0e10cSrcweir {
1103cdf0e10cSrcweir 	sal_uInt32    i, n = 0;
1104cdf0e10cSrcweir 	osl_TProfileSection* pSec;
1105cdf0e10cSrcweir 	osl_TProfileImpl*    pProfile = acquireProfile(Profile, sal_False);
1106cdf0e10cSrcweir 
1107cdf0e10cSrcweir 	if (pProfile == NULL)
1108cdf0e10cSrcweir 		return (0);
1109cdf0e10cSrcweir 
1110cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
1111cdf0e10cSrcweir 	{
1112cdf0e10cSrcweir 		if (MaxLen != 0)
1113cdf0e10cSrcweir 		{
1114cdf0e10cSrcweir  			for (i = 0; i < pProfile->m_NoSections; i++)
1115cdf0e10cSrcweir 			{
1116cdf0e10cSrcweir 				pSec = &pProfile->m_Sections[i];
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir 				if ((n + pSec->m_Len + 1) < MaxLen)
1119cdf0e10cSrcweir 				{
1120cdf0e10cSrcweir 					strncpy(&pszBuffer[n], &pProfile->m_Lines[pSec->m_Line][pSec->m_Offset],
1121cdf0e10cSrcweir 					        pSec->m_Len);
1122cdf0e10cSrcweir 					n += pSec->m_Len;
1123cdf0e10cSrcweir 					pszBuffer[n++] = '\0';
1124cdf0e10cSrcweir 				}
1125cdf0e10cSrcweir 				else
1126cdf0e10cSrcweir 					break;
1127cdf0e10cSrcweir 			}
1128cdf0e10cSrcweir 
1129cdf0e10cSrcweir 			pszBuffer[n++] = '\0';
1130cdf0e10cSrcweir 		}
1131cdf0e10cSrcweir 		else
1132cdf0e10cSrcweir 		{
1133cdf0e10cSrcweir  			for (i = 0; i < pProfile->m_NoSections; i++)
1134cdf0e10cSrcweir 				n += pProfile->m_Sections[i].m_Len + 1;
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir 			n += 1;
1137cdf0e10cSrcweir 		}
1138cdf0e10cSrcweir 	}
1139cdf0e10cSrcweir 	else
1140cdf0e10cSrcweir 	{
1141cdf0e10cSrcweir         ::osl::LongPathBuffer< sal_Char > aFileName( MAX_LONG_PATH );
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir 		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(pProfile->m_strFileName->buffer), -1, aFileName, aFileName.getBufSizeInSymbols(), NULL, NULL);
1144cdf0e10cSrcweir 		n = GetPrivateProfileSectionNames(pszBuffer, MaxLen, aFileName);
1145cdf0e10cSrcweir 	}
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir 	releaseProfile(pProfile);
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir 	return (n);
1150cdf0e10cSrcweir }
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir 
1153cdf0e10cSrcweir 
1154cdf0e10cSrcweir 
1155cdf0e10cSrcweir /*****************************************************************************/
1156cdf0e10cSrcweir /* Static Module Functions */
1157cdf0e10cSrcweir /*****************************************************************************/
1158cdf0e10cSrcweir 
getFileStamp(osl_TFile * pFile)1159cdf0e10cSrcweir static osl_TStamp getFileStamp(osl_TFile* pFile)
1160cdf0e10cSrcweir {
1161cdf0e10cSrcweir 	FILETIME FileTime;
1162cdf0e10cSrcweir 
1163cdf0e10cSrcweir 	if ((pFile->m_Handle == INVALID_HANDLE_VALUE) ||
1164cdf0e10cSrcweir 		(! GetFileTime(pFile->m_Handle, NULL, NULL, &FileTime)))
1165cdf0e10cSrcweir 		memset(&FileTime, 0, sizeof(FileTime));
1166cdf0e10cSrcweir 
1167cdf0e10cSrcweir 	return (FileTime);
1168cdf0e10cSrcweir }
1169cdf0e10cSrcweir 
1170cdf0e10cSrcweir 
1171cdf0e10cSrcweir 
lockFile(const osl_TFile * pFile,osl_TLockMode eMode)1172cdf0e10cSrcweir static sal_Bool lockFile(const osl_TFile* pFile, osl_TLockMode eMode)
1173cdf0e10cSrcweir {
1174cdf0e10cSrcweir 	sal_Bool     status = sal_False;
1175cdf0e10cSrcweir 	OVERLAPPED  Overlapped;
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir 	if (pFile->m_Handle == INVALID_HANDLE_VALUE)
1178cdf0e10cSrcweir 		return (sal_False);
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir 	memset(&Overlapped, 0, sizeof(Overlapped));
1181cdf0e10cSrcweir 
1182cdf0e10cSrcweir 	switch (eMode)
1183cdf0e10cSrcweir 	{
1184cdf0e10cSrcweir 		case un_lock:
1185cdf0e10cSrcweir 			status = (sal_Bool) UnlockFileEx(
1186cdf0e10cSrcweir                 pFile->m_Handle, 0, 0xFFFFFFFF, 0, &Overlapped);
1187cdf0e10cSrcweir 			break;
1188cdf0e10cSrcweir 
1189cdf0e10cSrcweir 		case read_lock:
1190cdf0e10cSrcweir 			status = (sal_Bool) LockFileEx(
1191cdf0e10cSrcweir                 pFile->m_Handle, 0, 0, 0xFFFFFFFF, 0, &Overlapped);
1192cdf0e10cSrcweir 			break;
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir 		case write_lock:
1195cdf0e10cSrcweir 			status = (sal_Bool) LockFileEx(
1196cdf0e10cSrcweir                 pFile->m_Handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 0xFFFFFFFF, 0,
1197cdf0e10cSrcweir                 &Overlapped);
1198cdf0e10cSrcweir 			break;
1199cdf0e10cSrcweir 	}
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir 	return (status);
1202cdf0e10cSrcweir }
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir 
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir 
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir 
1210cdf0e10cSrcweir 
1211cdf0e10cSrcweir 
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir 
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir 
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir 
1218cdf0e10cSrcweir 
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir 
1222cdf0e10cSrcweir 
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir 
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir 
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir 
1233cdf0e10cSrcweir 
1234cdf0e10cSrcweir 
1235cdf0e10cSrcweir 
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir 
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir 
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir 
1243cdf0e10cSrcweir 
1244cdf0e10cSrcweir 
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir 
1247cdf0e10cSrcweir 
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir 
1250cdf0e10cSrcweir 
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir 
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir 
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir 
openFileImpl(rtl_uString * strFileName,oslProfileOption ProfileFlags)1257cdf0e10cSrcweir static osl_TFile* openFileImpl(rtl_uString * strFileName, oslProfileOption ProfileFlags )
1258cdf0e10cSrcweir {
1259cdf0e10cSrcweir 	osl_TFile* pFile = reinterpret_cast< osl_TFile*>( calloc( 1, sizeof(osl_TFile) ) );
1260cdf0e10cSrcweir 	sal_Bool bWriteable = sal_False;
1261cdf0e10cSrcweir 
1262cdf0e10cSrcweir /*    if ( ProfileFlags & ( osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE | osl_Profile_READWRITE ) )*/
1263cdf0e10cSrcweir     if ( ProfileFlags & ( osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE ) )
1264cdf0e10cSrcweir     {
1265cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1266cdf0e10cSrcweir         OSL_TRACE("setting bWriteable to TRUE\n");
1267cdf0e10cSrcweir #endif
1268cdf0e10cSrcweir         bWriteable=sal_True;
1269cdf0e10cSrcweir     }
1270cdf0e10cSrcweir 
1271cdf0e10cSrcweir 	if (! bWriteable)
1272cdf0e10cSrcweir 	{
1273cdf0e10cSrcweir #if 0
1274cdf0e10cSrcweir //#ifdef DEBUG_OSL_PROFILE
1275cdf0e10cSrcweir         OSL_TRACE("opening '%s' read only\n",pszFilename);
1276cdf0e10cSrcweir #endif
1277cdf0e10cSrcweir 
1278cdf0e10cSrcweir 		pFile->m_Handle = CreateFileW( reinterpret_cast<LPCWSTR>(rtl_uString_getStr( strFileName )), GENERIC_READ,
1279cdf0e10cSrcweir 										  FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1280cdf0e10cSrcweir 										  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1281cdf0e10cSrcweir 
1282cdf0e10cSrcweir         /* mfe: argghh!!! do not check if the file could be openend */
1283cdf0e10cSrcweir         /*      default mode expects it that way!!!                 */
1284cdf0e10cSrcweir 	}
1285cdf0e10cSrcweir 	else
1286cdf0e10cSrcweir 	{
1287cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1288cdf0e10cSrcweir         OSL_TRACE("opening '%s' read/write\n",pszFilename);
1289cdf0e10cSrcweir #endif
1290cdf0e10cSrcweir 
1291cdf0e10cSrcweir 		if ((pFile->m_Handle = CreateFileW( reinterpret_cast<LPCWSTR>(rtl_uString_getStr( strFileName )), GENERIC_READ | GENERIC_WRITE,
1292cdf0e10cSrcweir 											   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1293cdf0e10cSrcweir 											   OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL))
1294cdf0e10cSrcweir 			== INVALID_HANDLE_VALUE)
1295cdf0e10cSrcweir 		{
1296cdf0e10cSrcweir 			free(pFile);
1297cdf0e10cSrcweir 			return (NULL);
1298cdf0e10cSrcweir 		}
1299cdf0e10cSrcweir 	}
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir 	pFile->m_pWriteBuf=0;
1302cdf0e10cSrcweir 	pFile->m_nWriteBufFree=0;
1303cdf0e10cSrcweir 	pFile->m_nWriteBufLen=0;
1304cdf0e10cSrcweir 
1305cdf0e10cSrcweir     if ( ProfileFlags & (osl_Profile_WRITELOCK | osl_Profile_READLOCK ) )
1306cdf0e10cSrcweir     {
1307cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1308cdf0e10cSrcweir         OSL_TRACE("locking '%s' file\n",pszFilename);
1309cdf0e10cSrcweir #endif
1310cdf0e10cSrcweir 
1311cdf0e10cSrcweir 		lockFile(pFile, bWriteable ? write_lock : read_lock);
1312cdf0e10cSrcweir 	}
1313cdf0e10cSrcweir 
1314cdf0e10cSrcweir     /* mfe: new WriteBuf obsolete */
1315cdf0e10cSrcweir /*	pFile->m_pWritePtr = pFile->m_Buf;*/
1316cdf0e10cSrcweir /*	pFile->m_pReadPtr  = pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf);*/
1317cdf0e10cSrcweir 
1318cdf0e10cSrcweir 	return (pFile);
1319cdf0e10cSrcweir }
1320cdf0e10cSrcweir 
1321cdf0e10cSrcweir 
1322cdf0e10cSrcweir 
1323cdf0e10cSrcweir 
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir 
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir 
1328cdf0e10cSrcweir 
closeFileImpl(osl_TFile * pFile)1329cdf0e10cSrcweir static osl_TStamp closeFileImpl(osl_TFile* pFile)
1330cdf0e10cSrcweir {
1331cdf0e10cSrcweir 	osl_TStamp stamp = {0, 0};
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir     if ( pFile == 0 )
1334cdf0e10cSrcweir     {
1335cdf0e10cSrcweir         return stamp;
1336cdf0e10cSrcweir     }
1337cdf0e10cSrcweir 
1338cdf0e10cSrcweir 	if (pFile->m_Handle != INVALID_HANDLE_VALUE)
1339cdf0e10cSrcweir 	{
1340cdf0e10cSrcweir        /* mfe: new WriteBuf obsolete */
1341cdf0e10cSrcweir         /* we just closing the file here, DO NOT write, it has to be handled in higher levels */
1342cdf0e10cSrcweir /*		if (pFile->m_pWritePtr > pFile->m_Buf)*/
1343cdf0e10cSrcweir /*		{*/
1344cdf0e10cSrcweir /*			DWORD Bytes;*/
1345cdf0e10cSrcweir 
1346cdf0e10cSrcweir /*			WriteFile(pFile->m_Handle, pFile->m_WriteBuf,*/
1347cdf0e10cSrcweir /*					  pFile->m_pWritePtr - pFile->m_WriteBuf,*/
1348cdf0e10cSrcweir /*					  &Bytes, NULL);*/
1349cdf0e10cSrcweir /*		}*/
1350cdf0e10cSrcweir 
1351cdf0e10cSrcweir 		stamp = getFileStamp(pFile);
1352cdf0e10cSrcweir 
1353cdf0e10cSrcweir 		lockFile(pFile, un_lock);
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir 		CloseHandle(pFile->m_Handle);
1356cdf0e10cSrcweir 		pFile->m_Handle = INVALID_HANDLE_VALUE;
1357cdf0e10cSrcweir 	}
1358cdf0e10cSrcweir 
1359cdf0e10cSrcweir 	if ( pFile->m_pWriteBuf != 0 )
1360cdf0e10cSrcweir 	{
1361cdf0e10cSrcweir 		free(pFile->m_pWriteBuf);
1362cdf0e10cSrcweir 	}
1363cdf0e10cSrcweir 
1364cdf0e10cSrcweir 	free(pFile);
1365cdf0e10cSrcweir 
1366cdf0e10cSrcweir 	return(stamp);
1367cdf0e10cSrcweir }
1368cdf0e10cSrcweir 
1369cdf0e10cSrcweir 
1370cdf0e10cSrcweir 
1371cdf0e10cSrcweir 
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir 
1374cdf0e10cSrcweir 
1375cdf0e10cSrcweir 
rewindFile(osl_TFile * pFile,sal_Bool bTruncate)1376cdf0e10cSrcweir static sal_Bool rewindFile(osl_TFile* pFile, sal_Bool bTruncate)
1377cdf0e10cSrcweir {
1378cdf0e10cSrcweir 	if (pFile->m_Handle != INVALID_HANDLE_VALUE)
1379cdf0e10cSrcweir 	{
1380cdf0e10cSrcweir         /* mfe: new WriteBuf obsolete */
1381cdf0e10cSrcweir         /* we just closing the file here, DO NOT write, it has to be handled in higher levels */
1382cdf0e10cSrcweir /*		if (pFile->m_pWritePtr > pFile->m_WriteBuf)*/
1383cdf0e10cSrcweir /*		{*/
1384cdf0e10cSrcweir /*			DWORD Bytes;*/
1385cdf0e10cSrcweir 
1386cdf0e10cSrcweir /*			WriteFile(pFile->m_Handle, pFile->m_WriteBuf,*/
1387cdf0e10cSrcweir /*					  pFile->m_pWritePtr - pFile->m_WriteBuf,*/
1388cdf0e10cSrcweir /*					  &Bytes, NULL);*/
1389cdf0e10cSrcweir 
1390cdf0e10cSrcweir /*			pFile->m_pWritePtr = pFile->m_WriteBuf;*/
1391cdf0e10cSrcweir /*		}*/
1392cdf0e10cSrcweir 
1393cdf0e10cSrcweir 		pFile->m_pReadPtr = pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf);
1394cdf0e10cSrcweir 
1395cdf0e10cSrcweir 		SetFilePointer(pFile->m_Handle, 0, NULL, FILE_BEGIN);
1396cdf0e10cSrcweir 
1397cdf0e10cSrcweir 		if (bTruncate)
1398cdf0e10cSrcweir 			SetEndOfFile(pFile->m_Handle);
1399cdf0e10cSrcweir 	}
1400cdf0e10cSrcweir 
1401cdf0e10cSrcweir 	return (sal_True);
1402cdf0e10cSrcweir }
1403cdf0e10cSrcweir 
1404cdf0e10cSrcweir 
1405cdf0e10cSrcweir 
1406cdf0e10cSrcweir 
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir 
1409cdf0e10cSrcweir 
1410cdf0e10cSrcweir 
1411cdf0e10cSrcweir 
1412cdf0e10cSrcweir 
getLine(osl_TFile * pFile,const sal_Char * pszLine,int MaxLen)1413cdf0e10cSrcweir static sal_Bool getLine(osl_TFile* pFile, const sal_Char *pszLine, int MaxLen)
1414cdf0e10cSrcweir {
1415cdf0e10cSrcweir     DWORD Max;
1416cdf0e10cSrcweir 	size_t Free, Bytes;
1417cdf0e10cSrcweir 	sal_Char* pChr;
1418cdf0e10cSrcweir 	sal_Char* pLine = (sal_Char *)pszLine;
1419cdf0e10cSrcweir 
1420cdf0e10cSrcweir 	if (pFile->m_Handle == INVALID_HANDLE_VALUE)
1421cdf0e10cSrcweir 		return (sal_False);
1422cdf0e10cSrcweir 
1423cdf0e10cSrcweir 	MaxLen -= 1;
1424cdf0e10cSrcweir 
1425cdf0e10cSrcweir 	do
1426cdf0e10cSrcweir 	{
1427cdf0e10cSrcweir 		Bytes = sizeof(pFile->m_ReadBuf) - (pFile->m_pReadPtr - pFile->m_ReadBuf);
1428cdf0e10cSrcweir 
1429cdf0e10cSrcweir 		if (Bytes <= 1)
1430cdf0e10cSrcweir 		{
1431cdf0e10cSrcweir 			/* refill buffer */
1432cdf0e10cSrcweir 			memcpy(pFile->m_ReadBuf, pFile->m_pReadPtr, Bytes);
1433cdf0e10cSrcweir 			pFile->m_pReadPtr = pFile->m_ReadBuf;
1434cdf0e10cSrcweir 
1435cdf0e10cSrcweir 			Free = sizeof(pFile->m_ReadBuf) - Bytes;
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir 			if (! ReadFile(pFile->m_Handle, &pFile->m_ReadBuf[Bytes], Free, &Max, NULL))
1438cdf0e10cSrcweir 			{
1439cdf0e10cSrcweir 				*pLine = '\0';
1440cdf0e10cSrcweir 				return (sal_False);
1441cdf0e10cSrcweir 			}
1442cdf0e10cSrcweir 
1443cdf0e10cSrcweir 			if (Max < Free)
1444cdf0e10cSrcweir 			{
1445cdf0e10cSrcweir 				if ((Max == 0) && (pLine == pszLine))
1446cdf0e10cSrcweir 				{
1447cdf0e10cSrcweir 					*pLine = '\0';
1448cdf0e10cSrcweir 					return (sal_False);
1449cdf0e10cSrcweir 				}
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir 				pFile->m_ReadBuf[Bytes + Max] = '\0';
1452cdf0e10cSrcweir 			}
1453cdf0e10cSrcweir 		}
1454cdf0e10cSrcweir 
1455cdf0e10cSrcweir 		for (pChr = pFile->m_pReadPtr;
1456cdf0e10cSrcweir 			 (*pChr != '\n') && (*pChr != '\r') && (*pChr != '\0') &&
1457cdf0e10cSrcweir 			 (pChr < (pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf) - 1));
1458cdf0e10cSrcweir 			 pChr++);
1459cdf0e10cSrcweir 
1460cdf0e10cSrcweir 		Max = min(pChr - pFile->m_pReadPtr, MaxLen);
1461cdf0e10cSrcweir 		memcpy(pLine, pFile->m_pReadPtr, Max);
1462cdf0e10cSrcweir 		MaxLen -= Max;
1463cdf0e10cSrcweir 		pLine  += Max;
1464cdf0e10cSrcweir 
1465cdf0e10cSrcweir 		if (pChr < (pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf) - 1))
1466cdf0e10cSrcweir 		{
1467cdf0e10cSrcweir 			if (*pChr != '\0')
1468cdf0e10cSrcweir 			{
1469cdf0e10cSrcweir 				if ((pChr[0] == '\r') && (pChr[1] == '\n'))
1470cdf0e10cSrcweir 					pChr += 2;
1471cdf0e10cSrcweir 				else
1472cdf0e10cSrcweir 					pChr += 1;
1473cdf0e10cSrcweir 			}
1474cdf0e10cSrcweir 
1475cdf0e10cSrcweir 			if ((pChr < (pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf))) &&
1476cdf0e10cSrcweir 				(*pChr == '\0'))
1477cdf0e10cSrcweir 				pChr = pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf);
1478cdf0e10cSrcweir 
1479cdf0e10cSrcweir 			*pLine = '\0';
1480cdf0e10cSrcweir 
1481cdf0e10cSrcweir 			/* setting MaxLen to -1 indicates terminating read loop */
1482cdf0e10cSrcweir 			MaxLen = -1;
1483cdf0e10cSrcweir 		}
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir 		pFile->m_pReadPtr = pChr;
1486cdf0e10cSrcweir 	}
1487cdf0e10cSrcweir 	while (MaxLen > 0);
1488cdf0e10cSrcweir 
1489cdf0e10cSrcweir 	return (sal_True);
1490cdf0e10cSrcweir }
1491cdf0e10cSrcweir 
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir 
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir 
1496cdf0e10cSrcweir 
1497cdf0e10cSrcweir 
1498cdf0e10cSrcweir 
1499cdf0e10cSrcweir 
1500cdf0e10cSrcweir 
putLine(osl_TFile * pFile,const sal_Char * pszLine)1501cdf0e10cSrcweir static sal_Bool putLine(osl_TFile* pFile, const sal_Char *pszLine)
1502cdf0e10cSrcweir {
1503cdf0e10cSrcweir 	unsigned int Len = strlen(pszLine);
1504cdf0e10cSrcweir 
1505cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1506cdf0e10cSrcweir 	int strLen=0;
1507cdf0e10cSrcweir #endif
1508cdf0e10cSrcweir 
1509cdf0e10cSrcweir 	if ( pFile == 0 || pFile->m_Handle < 0 )
1510cdf0e10cSrcweir     {
1511cdf0e10cSrcweir 		return (sal_False);
1512cdf0e10cSrcweir     }
1513cdf0e10cSrcweir 
1514cdf0e10cSrcweir     if ( pFile->m_pWriteBuf == 0 )
1515cdf0e10cSrcweir     {
1516cdf0e10cSrcweir         pFile->m_pWriteBuf = (sal_Char*) malloc(Len+3);
1517cdf0e10cSrcweir         pFile->m_nWriteBufLen = Len+3;
1518cdf0e10cSrcweir 		pFile->m_nWriteBufFree = Len+3;
1519cdf0e10cSrcweir     }
1520cdf0e10cSrcweir     else
1521cdf0e10cSrcweir     {
1522cdf0e10cSrcweir         if ( pFile->m_nWriteBufFree <= Len + 3 )
1523cdf0e10cSrcweir         {
1524cdf0e10cSrcweir             sal_Char* pTmp;
1525cdf0e10cSrcweir 
1526cdf0e10cSrcweir             pTmp=(sal_Char*) realloc(pFile->m_pWriteBuf,( ( pFile->m_nWriteBufLen + Len ) * 2) );
1527cdf0e10cSrcweir             if ( pTmp == 0 )
1528cdf0e10cSrcweir             {
1529cdf0e10cSrcweir                 return sal_False;
1530cdf0e10cSrcweir             }
1531cdf0e10cSrcweir             pFile->m_pWriteBuf = pTmp;
1532cdf0e10cSrcweir             pFile->m_nWriteBufFree = pFile->m_nWriteBufFree + pFile->m_nWriteBufLen + ( 2 * Len );
1533cdf0e10cSrcweir             pFile->m_nWriteBufLen = ( pFile->m_nWriteBufLen + Len ) * 2;
1534cdf0e10cSrcweir             memset( (pFile->m_pWriteBuf) + ( pFile->m_nWriteBufLen - pFile->m_nWriteBufFree ), 0, pFile->m_nWriteBufFree);
1535cdf0e10cSrcweir         }
1536cdf0e10cSrcweir     }
1537cdf0e10cSrcweir 
1538cdf0e10cSrcweir 
1539cdf0e10cSrcweir 
1540cdf0e10cSrcweir     memcpy(pFile->m_pWriteBuf + ( pFile->m_nWriteBufLen - pFile->m_nWriteBufFree ),pszLine,Len+1);
1541cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1542cdf0e10cSrcweir 	strLen = strlen(pFile->m_pWriteBuf);
1543cdf0e10cSrcweir #endif
1544cdf0e10cSrcweir     pFile->m_pWriteBuf[pFile->m_nWriteBufLen - pFile->m_nWriteBufFree + Len]='\r';
1545cdf0e10cSrcweir     pFile->m_pWriteBuf[pFile->m_nWriteBufLen - pFile->m_nWriteBufFree + Len + 1]='\n';
1546cdf0e10cSrcweir     pFile->m_pWriteBuf[pFile->m_nWriteBufLen - pFile->m_nWriteBufFree + Len + 2]='\0';
1547cdf0e10cSrcweir 
1548cdf0e10cSrcweir     pFile->m_nWriteBufFree-=Len+2;
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1551cdf0e10cSrcweir /*    OSL_TRACE("File Buffer in _putLine '%s' '%i'(%i)\n",pFile->m_pWriteBuf,strlen(pFile->m_pWriteBuf),pFile->m_nWriteBufLen - pFile->m_nWriteBufFree);*/
1552cdf0e10cSrcweir #endif
1553cdf0e10cSrcweir 
1554cdf0e10cSrcweir 	return (sal_True);
1555cdf0e10cSrcweir }
1556cdf0e10cSrcweir 
1557cdf0e10cSrcweir /* platform specific end */
1558cdf0e10cSrcweir 
1559cdf0e10cSrcweir 
stripBlanks(const sal_Char * String,sal_uInt32 * pLen)1560cdf0e10cSrcweir static const sal_Char* stripBlanks(const sal_Char* String, sal_uInt32* pLen)
1561cdf0e10cSrcweir {
1562cdf0e10cSrcweir 	if ( (pLen != NULL) && ( *pLen != 0 ) )
1563cdf0e10cSrcweir 	{
1564cdf0e10cSrcweir 		while ((String[*pLen - 1] == ' ') || (String[*pLen - 1] == '\t'))
1565cdf0e10cSrcweir 			(*pLen)--;
1566cdf0e10cSrcweir 
1567cdf0e10cSrcweir 		while ((*String == ' ') || (*String == '\t'))
1568cdf0e10cSrcweir 		{
1569cdf0e10cSrcweir 			String++;
1570cdf0e10cSrcweir 			(*pLen)--;
1571cdf0e10cSrcweir 		}
1572cdf0e10cSrcweir 	}
1573cdf0e10cSrcweir 	else
1574cdf0e10cSrcweir 		while ((*String == ' ') || (*String == '\t'))
1575cdf0e10cSrcweir 			String++;
1576cdf0e10cSrcweir 
1577cdf0e10cSrcweir 	return (String);
1578cdf0e10cSrcweir }
1579cdf0e10cSrcweir 
addLine(osl_TProfileImpl * pProfile,const sal_Char * Line)1580cdf0e10cSrcweir static const sal_Char* addLine(osl_TProfileImpl* pProfile, const sal_Char* Line)
1581cdf0e10cSrcweir {
1582cdf0e10cSrcweir 	if (pProfile->m_NoLines >= pProfile->m_MaxLines)
1583cdf0e10cSrcweir 	{
1584cdf0e10cSrcweir 		if (pProfile->m_Lines == NULL)
1585cdf0e10cSrcweir 		{
1586cdf0e10cSrcweir 			pProfile->m_MaxLines = LINES_INI;
1587*ba392698SPedro Giffuni 			pProfile->m_Lines = (sal_Char **)calloc(pProfile->m_MaxLines, sizeof(sal_Char *));
1588cdf0e10cSrcweir 		}
1589cdf0e10cSrcweir 		else
1590cdf0e10cSrcweir 		{
1591cdf0e10cSrcweir 			unsigned int index=0;
1592cdf0e10cSrcweir 			unsigned int oldmax=pProfile->m_MaxLines;
1593cdf0e10cSrcweir 
1594cdf0e10cSrcweir 			pProfile->m_MaxLines += LINES_ADD;
1595cdf0e10cSrcweir 			pProfile->m_Lines = (sal_Char **)realloc(pProfile->m_Lines, pProfile->m_MaxLines * sizeof(sal_Char *));
1596cdf0e10cSrcweir 
1597cdf0e10cSrcweir 			for ( index = oldmax ; index < pProfile->m_MaxLines ; ++index )
1598cdf0e10cSrcweir 			{
1599cdf0e10cSrcweir 				pProfile->m_Lines[index]=0;
1600cdf0e10cSrcweir 			}
1601cdf0e10cSrcweir 		}
1602cdf0e10cSrcweir 
1603cdf0e10cSrcweir 		if (pProfile->m_Lines == NULL)
1604cdf0e10cSrcweir 		{
1605cdf0e10cSrcweir 			pProfile->m_NoLines  = 0;
1606cdf0e10cSrcweir 			pProfile->m_MaxLines = 0;
1607cdf0e10cSrcweir 			return (NULL);
1608cdf0e10cSrcweir 		}
1609cdf0e10cSrcweir 
1610cdf0e10cSrcweir 	}
1611cdf0e10cSrcweir 
1612cdf0e10cSrcweir 	if ( pProfile->m_Lines != 0 && pProfile->m_Lines[pProfile->m_NoLines] != 0 )
1613cdf0e10cSrcweir 	{
1614cdf0e10cSrcweir 			free(pProfile->m_Lines[pProfile->m_NoLines]);
1615cdf0e10cSrcweir 	}
1616cdf0e10cSrcweir 	pProfile->m_Lines[pProfile->m_NoLines++] = strdup(Line);
1617cdf0e10cSrcweir 
1618cdf0e10cSrcweir 	return (pProfile->m_Lines[pProfile->m_NoLines - 1]);
1619cdf0e10cSrcweir }
1620cdf0e10cSrcweir 
insertLine(osl_TProfileImpl * pProfile,const sal_Char * Line,sal_uInt32 LineNo)1621cdf0e10cSrcweir static const sal_Char* insertLine(osl_TProfileImpl* pProfile, const sal_Char* Line, sal_uInt32 LineNo)
1622cdf0e10cSrcweir {
1623cdf0e10cSrcweir 	if (pProfile->m_NoLines >= pProfile->m_MaxLines)
1624cdf0e10cSrcweir 	{
1625cdf0e10cSrcweir 		if (pProfile->m_Lines == NULL)
1626cdf0e10cSrcweir 		{
1627cdf0e10cSrcweir 			pProfile->m_MaxLines = LINES_INI;
1628*ba392698SPedro Giffuni 			pProfile->m_Lines = (sal_Char **)calloc(pProfile->m_MaxLines, sizeof(sal_Char *));
1629cdf0e10cSrcweir 		}
1630cdf0e10cSrcweir 		else
1631cdf0e10cSrcweir 		{
1632cdf0e10cSrcweir 			pProfile->m_MaxLines += LINES_ADD;
1633cdf0e10cSrcweir 			pProfile->m_Lines = (sal_Char **)realloc(pProfile->m_Lines,
1634cdf0e10cSrcweir 												 pProfile->m_MaxLines * sizeof(sal_Char *));
1635cdf0e10cSrcweir 
1636cdf0e10cSrcweir 			memset(&pProfile->m_Lines[pProfile->m_NoLines],
1637cdf0e10cSrcweir 				0,
1638cdf0e10cSrcweir 				(pProfile->m_MaxLines - pProfile->m_NoLines - 1) * sizeof(sal_Char*));
1639cdf0e10cSrcweir 		}
1640cdf0e10cSrcweir 
1641cdf0e10cSrcweir 		if (pProfile->m_Lines == NULL)
1642cdf0e10cSrcweir 		{
1643cdf0e10cSrcweir 			pProfile->m_NoLines  = 0;
1644cdf0e10cSrcweir 			pProfile->m_MaxLines = 0;
1645cdf0e10cSrcweir 			return (NULL);
1646cdf0e10cSrcweir 		}
1647cdf0e10cSrcweir 	}
1648cdf0e10cSrcweir 
1649cdf0e10cSrcweir 	LineNo = LineNo > pProfile->m_NoLines ? pProfile->m_NoLines : LineNo;
1650cdf0e10cSrcweir 
1651cdf0e10cSrcweir 	if (LineNo < pProfile->m_NoLines)
1652cdf0e10cSrcweir 	{
1653cdf0e10cSrcweir 		sal_uInt32 i, n;
1654cdf0e10cSrcweir 		osl_TProfileSection* pSec;
1655cdf0e10cSrcweir 
1656cdf0e10cSrcweir 		memmove(&pProfile->m_Lines[LineNo + 1], &pProfile->m_Lines[LineNo],
1657cdf0e10cSrcweir 				(pProfile->m_NoLines - LineNo) * sizeof(sal_Char *));
1658cdf0e10cSrcweir 
1659cdf0e10cSrcweir 
1660cdf0e10cSrcweir 		/* adjust line references */
1661cdf0e10cSrcweir 		for (i = 0; i < pProfile->m_NoSections; i++)
1662cdf0e10cSrcweir 		{
1663cdf0e10cSrcweir 			pSec = &pProfile->m_Sections[i];
1664cdf0e10cSrcweir 
1665cdf0e10cSrcweir 			if (pSec->m_Line >= LineNo)
1666cdf0e10cSrcweir 				pSec->m_Line++;
1667cdf0e10cSrcweir 
1668cdf0e10cSrcweir 			for (n = 0; n < pSec->m_NoEntries; n++)
1669cdf0e10cSrcweir 				if (pSec->m_Entries[n].m_Line >= LineNo)
1670cdf0e10cSrcweir 					pSec->m_Entries[n].m_Line++;
1671cdf0e10cSrcweir 		}
1672cdf0e10cSrcweir 	}
1673cdf0e10cSrcweir 
1674cdf0e10cSrcweir 	pProfile->m_NoLines++;
1675cdf0e10cSrcweir 
1676cdf0e10cSrcweir 	pProfile->m_Lines[LineNo] = strdup(Line);
1677cdf0e10cSrcweir 
1678cdf0e10cSrcweir 	return (pProfile->m_Lines[LineNo]);
1679cdf0e10cSrcweir }
1680cdf0e10cSrcweir 
removeLine(osl_TProfileImpl * pProfile,sal_uInt32 LineNo)1681cdf0e10cSrcweir static void removeLine(osl_TProfileImpl* pProfile, sal_uInt32 LineNo)
1682cdf0e10cSrcweir {
1683cdf0e10cSrcweir 	if (LineNo < pProfile->m_NoLines)
1684cdf0e10cSrcweir 	{
1685cdf0e10cSrcweir 		free(pProfile->m_Lines[LineNo]);
1686cdf0e10cSrcweir 		pProfile->m_Lines[LineNo]=0;
1687cdf0e10cSrcweir 		if (pProfile->m_NoLines - LineNo > 1)
1688cdf0e10cSrcweir 		{
1689cdf0e10cSrcweir 			sal_uInt32 i, n;
1690cdf0e10cSrcweir 			osl_TProfileSection* pSec;
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir 			memmove(&pProfile->m_Lines[LineNo], &pProfile->m_Lines[LineNo + 1],
1693cdf0e10cSrcweir 					(pProfile->m_NoLines - LineNo - 1) * sizeof(sal_Char *));
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir 			memset(&pProfile->m_Lines[pProfile->m_NoLines - 1],
1696cdf0e10cSrcweir 				0,
1697cdf0e10cSrcweir 				(pProfile->m_MaxLines - pProfile->m_NoLines) * sizeof(sal_Char*));
1698cdf0e10cSrcweir 
1699cdf0e10cSrcweir 			/* adjust line references */
1700cdf0e10cSrcweir 			for (i = 0; i < pProfile->m_NoSections; i++)
1701cdf0e10cSrcweir 			{
1702cdf0e10cSrcweir 				pSec = &pProfile->m_Sections[i];
1703cdf0e10cSrcweir 
1704cdf0e10cSrcweir 				if (pSec->m_Line > LineNo)
1705cdf0e10cSrcweir 					pSec->m_Line--;
1706cdf0e10cSrcweir 
1707cdf0e10cSrcweir 				for (n = 0; n < pSec->m_NoEntries; n++)
1708cdf0e10cSrcweir 					if (pSec->m_Entries[n].m_Line > LineNo)
1709cdf0e10cSrcweir 						pSec->m_Entries[n].m_Line--;
1710cdf0e10cSrcweir 			}
1711cdf0e10cSrcweir 		}
1712cdf0e10cSrcweir 		else
1713cdf0e10cSrcweir 		{
1714cdf0e10cSrcweir 			pProfile->m_Lines[LineNo] = 0;
1715cdf0e10cSrcweir 		}
1716cdf0e10cSrcweir 
1717cdf0e10cSrcweir 		pProfile->m_NoLines--;
1718cdf0e10cSrcweir 	}
1719cdf0e10cSrcweir 
1720cdf0e10cSrcweir 	return;
1721cdf0e10cSrcweir }
1722cdf0e10cSrcweir 
setEntry(osl_TProfileImpl * pProfile,osl_TProfileSection * pSection,sal_uInt32 NoEntry,sal_uInt32 Line,const sal_Char * Entry,sal_uInt32 Len)1723cdf0e10cSrcweir static void setEntry(osl_TProfileImpl* pProfile, osl_TProfileSection* pSection,
1724cdf0e10cSrcweir 					 sal_uInt32 NoEntry, sal_uInt32 Line,
1725cdf0e10cSrcweir 					 const sal_Char* Entry, sal_uInt32 Len)
1726cdf0e10cSrcweir {
1727cdf0e10cSrcweir 	Entry = stripBlanks(Entry, &Len);
1728cdf0e10cSrcweir 	pSection->m_Entries[NoEntry].m_Line   = Line;
1729cdf0e10cSrcweir 	pSection->m_Entries[NoEntry].m_Offset = Entry - pProfile->m_Lines[Line];
1730cdf0e10cSrcweir 	pSection->m_Entries[NoEntry].m_Len    = Len;
1731cdf0e10cSrcweir 
1732cdf0e10cSrcweir 	return;
1733cdf0e10cSrcweir }
1734cdf0e10cSrcweir 
addEntry(osl_TProfileImpl * pProfile,osl_TProfileSection * pSection,int Line,const sal_Char * Entry,sal_uInt32 Len)1735cdf0e10cSrcweir static sal_Bool addEntry(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection,
1736cdf0e10cSrcweir 						int Line, const sal_Char* Entry, sal_uInt32 Len)
1737cdf0e10cSrcweir {
1738cdf0e10cSrcweir 	if (pSection != NULL)
1739cdf0e10cSrcweir 	{
1740cdf0e10cSrcweir 		if (pSection->m_NoEntries >= pSection->m_MaxEntries)
1741cdf0e10cSrcweir 		{
1742cdf0e10cSrcweir 			if (pSection->m_Entries == NULL)
1743cdf0e10cSrcweir 			{
1744cdf0e10cSrcweir 				pSection->m_MaxEntries = ENTRIES_INI;
1745cdf0e10cSrcweir 				pSection->m_Entries = (osl_TProfileEntry *)malloc(
1746cdf0e10cSrcweir 								pSection->m_MaxEntries * sizeof(osl_TProfileEntry));
1747cdf0e10cSrcweir 			}
1748cdf0e10cSrcweir 			else
1749cdf0e10cSrcweir 			{
1750cdf0e10cSrcweir 				pSection->m_MaxEntries += ENTRIES_ADD;
1751cdf0e10cSrcweir 				pSection->m_Entries = (osl_TProfileEntry *)realloc(pSection->m_Entries,
1752cdf0e10cSrcweir 								pSection->m_MaxEntries * sizeof(osl_TProfileEntry));
1753cdf0e10cSrcweir 			}
1754cdf0e10cSrcweir 
1755cdf0e10cSrcweir 			if (pSection->m_Entries == NULL)
1756cdf0e10cSrcweir 			{
1757cdf0e10cSrcweir 				pSection->m_NoEntries  = 0;
1758cdf0e10cSrcweir 				pSection->m_MaxEntries = 0;
1759cdf0e10cSrcweir 				return (sal_False);
1760cdf0e10cSrcweir 			}
1761cdf0e10cSrcweir 		}
1762cdf0e10cSrcweir 
1763cdf0e10cSrcweir 		pSection->m_NoEntries++;
1764cdf0e10cSrcweir 
1765cdf0e10cSrcweir 		Entry = stripBlanks(Entry, &Len);
1766cdf0e10cSrcweir 		setEntry(pProfile, pSection, pSection->m_NoEntries - 1, Line,
1767cdf0e10cSrcweir 				 Entry, Len);
1768cdf0e10cSrcweir 
1769cdf0e10cSrcweir 		return (sal_True);
1770cdf0e10cSrcweir 	}
1771cdf0e10cSrcweir 
1772cdf0e10cSrcweir 	return (sal_False);
1773cdf0e10cSrcweir }
1774cdf0e10cSrcweir 
removeEntry(osl_TProfileSection * pSection,sal_uInt32 NoEntry)1775cdf0e10cSrcweir static void removeEntry(osl_TProfileSection *pSection, sal_uInt32 NoEntry)
1776cdf0e10cSrcweir {
1777cdf0e10cSrcweir 	if (NoEntry < pSection->m_NoEntries)
1778cdf0e10cSrcweir 	{
1779cdf0e10cSrcweir 		if (pSection->m_NoEntries - NoEntry > 1)
1780cdf0e10cSrcweir 		{
1781cdf0e10cSrcweir 			memmove(&pSection->m_Entries[NoEntry],
1782cdf0e10cSrcweir 					&pSection->m_Entries[NoEntry + 1],
1783cdf0e10cSrcweir 					(pSection->m_NoEntries - NoEntry - 1) * sizeof(osl_TProfileEntry));
1784cdf0e10cSrcweir 			pSection->m_Entries[pSection->m_NoEntries - 1].m_Line=0;
1785cdf0e10cSrcweir 			pSection->m_Entries[pSection->m_NoEntries - 1].m_Offset=0;
1786cdf0e10cSrcweir 			pSection->m_Entries[pSection->m_NoEntries - 1].m_Len=0;
1787cdf0e10cSrcweir 		}
1788cdf0e10cSrcweir 
1789cdf0e10cSrcweir 		pSection->m_NoEntries--;
1790cdf0e10cSrcweir 	}
1791cdf0e10cSrcweir 
1792cdf0e10cSrcweir 	return;
1793cdf0e10cSrcweir }
1794cdf0e10cSrcweir 
addSection(osl_TProfileImpl * pProfile,int Line,const sal_Char * Section,sal_uInt32 Len)1795cdf0e10cSrcweir static sal_Bool addSection(osl_TProfileImpl* pProfile, int Line, const sal_Char* Section, sal_uInt32 Len)
1796cdf0e10cSrcweir {
1797cdf0e10cSrcweir 	if (pProfile->m_NoSections >= pProfile->m_MaxSections)
1798cdf0e10cSrcweir 	{
1799cdf0e10cSrcweir 		if (pProfile->m_Sections == NULL)
1800cdf0e10cSrcweir 		{
1801cdf0e10cSrcweir 			pProfile->m_MaxSections = SECTIONS_INI;
1802*ba392698SPedro Giffuni 			pProfile->m_Sections = (osl_TProfileSection *)calloc(pProfile->m_MaxSections, sizeof(osl_TProfileSection));
1803cdf0e10cSrcweir 		}
1804cdf0e10cSrcweir 		else
1805cdf0e10cSrcweir 		{
1806cdf0e10cSrcweir 			unsigned int index=0;
1807cdf0e10cSrcweir 			unsigned int oldmax=pProfile->m_MaxSections;
1808cdf0e10cSrcweir 
1809cdf0e10cSrcweir 			pProfile->m_MaxSections += SECTIONS_ADD;
1810cdf0e10cSrcweir 			pProfile->m_Sections = (osl_TProfileSection *)realloc(pProfile->m_Sections,
1811cdf0e10cSrcweir 										  pProfile->m_MaxSections * sizeof(osl_TProfileSection));
1812cdf0e10cSrcweir 			for ( index = oldmax ; index < pProfile->m_MaxSections ; ++index )
1813cdf0e10cSrcweir 			{
1814cdf0e10cSrcweir 				pProfile->m_Sections[index].m_Entries=0;
1815cdf0e10cSrcweir 			}
1816cdf0e10cSrcweir 		}
1817cdf0e10cSrcweir 
1818cdf0e10cSrcweir 		if (pProfile->m_Sections == NULL)
1819cdf0e10cSrcweir 		{
1820cdf0e10cSrcweir 			pProfile->m_NoSections = 0;
1821cdf0e10cSrcweir 			pProfile->m_MaxSections = 0;
1822cdf0e10cSrcweir 			return (sal_False);
1823cdf0e10cSrcweir 		}
1824cdf0e10cSrcweir 	}
1825cdf0e10cSrcweir 
1826cdf0e10cSrcweir 	pProfile->m_NoSections++;
1827cdf0e10cSrcweir 
1828cdf0e10cSrcweir 	if ( pProfile->m_Sections[(pProfile->m_NoSections) - 1].m_Entries != 0 )
1829cdf0e10cSrcweir 	{
1830cdf0e10cSrcweir 		free(pProfile->m_Sections[(pProfile->m_NoSections) - 1].m_Entries);
1831cdf0e10cSrcweir 	}
1832cdf0e10cSrcweir 	pProfile->m_Sections[pProfile->m_NoSections - 1].m_Entries    = NULL;
1833cdf0e10cSrcweir 	pProfile->m_Sections[pProfile->m_NoSections - 1].m_NoEntries  = 0;
1834cdf0e10cSrcweir 	pProfile->m_Sections[pProfile->m_NoSections - 1].m_MaxEntries = 0;
1835cdf0e10cSrcweir 
1836cdf0e10cSrcweir 	Section = (sal_Char *)stripBlanks(Section, &Len);
1837cdf0e10cSrcweir 	pProfile->m_Sections[pProfile->m_NoSections - 1].m_Line = Line;
1838cdf0e10cSrcweir 	pProfile->m_Sections[pProfile->m_NoSections - 1].m_Offset = Section - pProfile->m_Lines[Line];
1839cdf0e10cSrcweir 	pProfile->m_Sections[pProfile->m_NoSections - 1].m_Len = Len;
1840cdf0e10cSrcweir 
1841cdf0e10cSrcweir 	return (sal_True);
1842cdf0e10cSrcweir }
1843cdf0e10cSrcweir 
removeSection(osl_TProfileImpl * pProfile,osl_TProfileSection * pSection)1844cdf0e10cSrcweir static void removeSection(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection)
1845cdf0e10cSrcweir {
1846cdf0e10cSrcweir 	sal_uInt32 Section;
1847cdf0e10cSrcweir 
1848cdf0e10cSrcweir 	if ((Section = pSection - pProfile->m_Sections) < pProfile->m_NoSections)
1849cdf0e10cSrcweir 	{
1850cdf0e10cSrcweir 		free (pSection->m_Entries);
1851cdf0e10cSrcweir 		pSection->m_Entries=0;
1852cdf0e10cSrcweir 		if (pProfile->m_NoSections - Section > 1)
1853cdf0e10cSrcweir 		{
1854cdf0e10cSrcweir 			memmove(&pProfile->m_Sections[Section], &pProfile->m_Sections[Section + 1],
1855cdf0e10cSrcweir 					(pProfile->m_NoSections - Section - 1) * sizeof(osl_TProfileSection));
1856cdf0e10cSrcweir 
1857cdf0e10cSrcweir 			memset(&pProfile->m_Sections[pProfile->m_NoSections - 1],
1858cdf0e10cSrcweir 				0,
1859cdf0e10cSrcweir 				(pProfile->m_MaxSections - pProfile->m_NoSections) * sizeof(osl_TProfileSection));
1860cdf0e10cSrcweir 			pProfile->m_Sections[pProfile->m_NoSections - 1].m_Entries = 0;
1861cdf0e10cSrcweir 		}
1862cdf0e10cSrcweir 		else
1863cdf0e10cSrcweir 		{
1864cdf0e10cSrcweir 			pSection->m_Entries = 0;
1865cdf0e10cSrcweir 		}
1866cdf0e10cSrcweir 
1867cdf0e10cSrcweir 		pProfile->m_NoSections--;
1868cdf0e10cSrcweir 	}
1869cdf0e10cSrcweir 
1870cdf0e10cSrcweir 	return;
1871cdf0e10cSrcweir }
1872cdf0e10cSrcweir 
findEntry(osl_TProfileImpl * pProfile,const sal_Char * Section,const sal_Char * Entry,sal_uInt32 * pNoEntry)1873cdf0e10cSrcweir static osl_TProfileSection* findEntry(osl_TProfileImpl* pProfile, const sal_Char* Section,
1874cdf0e10cSrcweir                                       const sal_Char* Entry, sal_uInt32 *pNoEntry)
1875cdf0e10cSrcweir {
1876cdf0e10cSrcweir static  sal_uInt32    Sect = 0;
1877cdf0e10cSrcweir 		sal_uInt32    i, n;
1878cdf0e10cSrcweir 		sal_uInt32    Len;
1879cdf0e10cSrcweir 		const sal_Char* pStr;
1880cdf0e10cSrcweir 		osl_TProfileSection* pSec = NULL;
1881cdf0e10cSrcweir 
1882cdf0e10cSrcweir 	Len = strlen(Section);
1883cdf0e10cSrcweir 	Section = (sal_Char *)stripBlanks(Section, &Len);
1884cdf0e10cSrcweir 
1885cdf0e10cSrcweir 	n = Sect;
1886cdf0e10cSrcweir 
1887cdf0e10cSrcweir 	for (i = 0; i < pProfile->m_NoSections; i++)
1888cdf0e10cSrcweir 	{
1889cdf0e10cSrcweir 		n %= pProfile->m_NoSections;
1890cdf0e10cSrcweir 		pSec = &pProfile->m_Sections[n];
1891cdf0e10cSrcweir 		if ((Len == pSec->m_Len) &&
1892cdf0e10cSrcweir 			(strnicmp(Section, &pProfile->m_Lines[pSec->m_Line][pSec->m_Offset], pSec->m_Len)
1893cdf0e10cSrcweir 			 == 0))
1894cdf0e10cSrcweir 			break;
1895cdf0e10cSrcweir 		n++;
1896cdf0e10cSrcweir 	}
1897cdf0e10cSrcweir 
1898cdf0e10cSrcweir 	Sect = n;
1899cdf0e10cSrcweir 
1900cdf0e10cSrcweir 	if (i < pProfile->m_NoSections)
1901cdf0e10cSrcweir 	{
1902cdf0e10cSrcweir 		Len = strlen(Entry);
1903cdf0e10cSrcweir 		Entry = stripBlanks(Entry, &Len);
1904cdf0e10cSrcweir 
1905cdf0e10cSrcweir 		*pNoEntry = pSec->m_NoEntries;
1906cdf0e10cSrcweir 
1907cdf0e10cSrcweir 		for (i = 0; i < pSec->m_NoEntries; i++)
1908cdf0e10cSrcweir 		{
1909cdf0e10cSrcweir 			pStr = &pProfile->m_Lines[pSec->m_Entries[i].m_Line]
1910cdf0e10cSrcweir 									 [pSec->m_Entries[i].m_Offset];
1911cdf0e10cSrcweir 			if ((Len == pSec->m_Entries[i].m_Len) &&
1912cdf0e10cSrcweir 				(strnicmp(Entry, pStr, pSec->m_Entries[i].m_Len)
1913cdf0e10cSrcweir 				 == 0))
1914cdf0e10cSrcweir 			{
1915cdf0e10cSrcweir 				*pNoEntry = i;
1916cdf0e10cSrcweir 				break;
1917cdf0e10cSrcweir 			}
1918cdf0e10cSrcweir 		}
1919cdf0e10cSrcweir 	}
1920cdf0e10cSrcweir 	else
1921cdf0e10cSrcweir 		pSec = NULL;
1922cdf0e10cSrcweir 
1923cdf0e10cSrcweir 	return (pSec);
1924cdf0e10cSrcweir }
1925cdf0e10cSrcweir 
loadProfile(osl_TFile * pFile,osl_TProfileImpl * pProfile)1926cdf0e10cSrcweir static sal_Bool loadProfile(osl_TFile* pFile, osl_TProfileImpl* pProfile)
1927cdf0e10cSrcweir {
1928cdf0e10cSrcweir 	sal_uInt32    i;
1929cdf0e10cSrcweir 	sal_Char*       pStr;
1930cdf0e10cSrcweir 	sal_Char*       pChar;
1931cdf0e10cSrcweir 	sal_Char        Line[4096];
1932cdf0e10cSrcweir 
1933cdf0e10cSrcweir 	pProfile->m_NoLines    = 0;
1934cdf0e10cSrcweir 	pProfile->m_NoSections = 0;
1935cdf0e10cSrcweir 
1936cdf0e10cSrcweir 	OSL_VERIFY(rewindFile(pFile, sal_False));
1937cdf0e10cSrcweir 
1938cdf0e10cSrcweir 	while (getLine(pFile, Line, sizeof(Line)))
1939cdf0e10cSrcweir 	{
1940cdf0e10cSrcweir 		if (! addLine(pProfile, Line))
1941cdf0e10cSrcweir 			return (sal_False);
1942cdf0e10cSrcweir 	}
1943cdf0e10cSrcweir 
1944cdf0e10cSrcweir 	for (i = 0; i < pProfile->m_NoLines; i++)
1945cdf0e10cSrcweir 	{
1946cdf0e10cSrcweir 		pStr = (sal_Char *)stripBlanks(pProfile->m_Lines[i], NULL);
1947cdf0e10cSrcweir 
1948cdf0e10cSrcweir 		if ((*pStr == '\0') || (*pStr == ';'))
1949cdf0e10cSrcweir 			continue;
1950cdf0e10cSrcweir 
1951cdf0e10cSrcweir 		if ((*pStr != '[') || ((pChar = strrchr(pStr, ']')) == NULL) ||
1952cdf0e10cSrcweir 			((pChar - pStr) <= 2))
1953cdf0e10cSrcweir 		{
1954cdf0e10cSrcweir 			/* insert entry */
1955cdf0e10cSrcweir 
1956cdf0e10cSrcweir 			if (pProfile->m_NoSections < 1)
1957cdf0e10cSrcweir 				continue;
1958cdf0e10cSrcweir 
1959cdf0e10cSrcweir 			if ((pChar = strchr(pStr, '=')) == NULL)
1960cdf0e10cSrcweir 				pChar = pStr + strlen(pStr);
1961cdf0e10cSrcweir 
1962cdf0e10cSrcweir 			if (! addEntry(pProfile, &pProfile->m_Sections[pProfile->m_NoSections - 1],
1963cdf0e10cSrcweir 						   i, pStr, pChar - pStr))
1964cdf0e10cSrcweir 				return (sal_False);
1965cdf0e10cSrcweir 		}
1966cdf0e10cSrcweir 		else
1967cdf0e10cSrcweir 		{
1968cdf0e10cSrcweir 			/* new section */
1969cdf0e10cSrcweir 
1970cdf0e10cSrcweir 			if (! addSection(pProfile, i, pStr + 1, pChar - pStr - 1))
1971cdf0e10cSrcweir 				return (sal_False);
1972cdf0e10cSrcweir 		}
1973cdf0e10cSrcweir 	}
1974cdf0e10cSrcweir 
1975cdf0e10cSrcweir 	return (sal_True);
1976cdf0e10cSrcweir }
1977cdf0e10cSrcweir 
1978cdf0e10cSrcweir 
1979cdf0e10cSrcweir 
1980cdf0e10cSrcweir 
1981cdf0e10cSrcweir 
storeProfile(osl_TProfileImpl * pProfile,sal_Bool bCleanup)1982cdf0e10cSrcweir static sal_Bool storeProfile(osl_TProfileImpl* pProfile, sal_Bool bCleanup)
1983cdf0e10cSrcweir {
1984cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1985cdf0e10cSrcweir     OSL_TRACE("In  storeProfile\n");
1986cdf0e10cSrcweir #endif
1987cdf0e10cSrcweir 
1988cdf0e10cSrcweir 	if (pProfile->m_Lines != NULL)
1989cdf0e10cSrcweir 	{
1990cdf0e10cSrcweir 		if (pProfile->m_Flags & FLG_MODIFIED)
1991cdf0e10cSrcweir 		{
1992cdf0e10cSrcweir 			sal_uInt32 i;
1993cdf0e10cSrcweir 
1994cdf0e10cSrcweir 			osl_TFile* pTmpFile = osl_openTmpProfileImpl(pProfile);
1995cdf0e10cSrcweir 
1996cdf0e10cSrcweir 			if ( pTmpFile == 0 )
1997cdf0e10cSrcweir 			{
1998cdf0e10cSrcweir 				return sal_False;
1999cdf0e10cSrcweir 			}
2000cdf0e10cSrcweir 
2001cdf0e10cSrcweir 			OSL_VERIFY(rewindFile(pTmpFile, sal_True));
2002cdf0e10cSrcweir 
2003cdf0e10cSrcweir 			for (i = 0; i < pProfile->m_NoLines; i++)
2004cdf0e10cSrcweir 			{
2005cdf0e10cSrcweir 				OSL_VERIFY(putLine(pTmpFile, pProfile->m_Lines[i]));
2006cdf0e10cSrcweir 			}
2007cdf0e10cSrcweir 
2008cdf0e10cSrcweir 			if ( ! writeProfileImpl(pTmpFile) )
2009cdf0e10cSrcweir             {
2010cdf0e10cSrcweir 				if ( pTmpFile->m_pWriteBuf != 0 )
2011cdf0e10cSrcweir 				{
2012cdf0e10cSrcweir 					free(pTmpFile->m_pWriteBuf);
2013cdf0e10cSrcweir 				}
2014cdf0e10cSrcweir 
2015cdf0e10cSrcweir 				pTmpFile->m_pWriteBuf=0;
2016cdf0e10cSrcweir 				pTmpFile->m_nWriteBufLen=0;
2017cdf0e10cSrcweir 				pTmpFile->m_nWriteBufFree=0;
2018cdf0e10cSrcweir 
2019cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2020cdf0e10cSrcweir                 OSL_TRACE("Out storeProfile [not flushed]\n");
2021cdf0e10cSrcweir #endif
2022cdf0e10cSrcweir 				closeFileImpl(pTmpFile);
2023cdf0e10cSrcweir 
2024cdf0e10cSrcweir                 return sal_False;
2025cdf0e10cSrcweir             }
2026cdf0e10cSrcweir 
2027cdf0e10cSrcweir 			pProfile->m_Flags &= ~FLG_MODIFIED;
2028cdf0e10cSrcweir 
2029cdf0e10cSrcweir 			closeFileImpl(pProfile->m_pFile);
2030cdf0e10cSrcweir 			closeFileImpl(pTmpFile);
2031cdf0e10cSrcweir 
2032cdf0e10cSrcweir 			osl_ProfileSwapProfileNames(pProfile);
2033cdf0e10cSrcweir 
2034cdf0e10cSrcweir /*			free(pProfile->m_pFile);*/
2035cdf0e10cSrcweir /*			free(pTmpFile);*/
2036cdf0e10cSrcweir 
2037cdf0e10cSrcweir 			pProfile->m_pFile = openFileImpl(pProfile->m_strFileName,pProfile->m_Flags);
2038cdf0e10cSrcweir 
2039cdf0e10cSrcweir 		}
2040cdf0e10cSrcweir 
2041cdf0e10cSrcweir 		if (bCleanup)
2042cdf0e10cSrcweir 		{
2043cdf0e10cSrcweir 			while (pProfile->m_NoLines > 0)
2044cdf0e10cSrcweir 				removeLine(pProfile, pProfile->m_NoLines - 1);
2045cdf0e10cSrcweir 
2046cdf0e10cSrcweir 			free(pProfile->m_Lines);
2047cdf0e10cSrcweir 			pProfile->m_Lines = NULL;
2048cdf0e10cSrcweir 			pProfile->m_MaxLines = 0;
2049cdf0e10cSrcweir 
2050cdf0e10cSrcweir 			while (pProfile->m_NoSections > 0)
2051cdf0e10cSrcweir 				removeSection(pProfile, &pProfile->m_Sections[pProfile->m_NoSections - 1]);
2052cdf0e10cSrcweir 
2053cdf0e10cSrcweir 			free(pProfile->m_Sections);
2054cdf0e10cSrcweir 			pProfile->m_Sections = NULL;
2055cdf0e10cSrcweir 			pProfile->m_MaxSections = 0;
2056cdf0e10cSrcweir 		}
2057cdf0e10cSrcweir 	}
2058cdf0e10cSrcweir 
2059cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2060cdf0e10cSrcweir     OSL_TRACE("Out storeProfile [ok]\n");
2061cdf0e10cSrcweir #endif
2062cdf0e10cSrcweir 	return (sal_True);
2063cdf0e10cSrcweir }
2064cdf0e10cSrcweir 
2065cdf0e10cSrcweir 
osl_openTmpProfileImpl(osl_TProfileImpl * pProfile)2066cdf0e10cSrcweir static osl_TFile* osl_openTmpProfileImpl(osl_TProfileImpl* pProfile)
2067cdf0e10cSrcweir {
2068cdf0e10cSrcweir 	osl_TFile* pFile=0;
2069cdf0e10cSrcweir 	rtl_uString* ustrExtension=0;
2070cdf0e10cSrcweir 	rtl_uString* ustrTmpName=0;
2071cdf0e10cSrcweir 	oslProfileOption PFlags=0;
2072cdf0e10cSrcweir 
2073cdf0e10cSrcweir 	rtl_uString_newFromAscii(&ustrExtension,"tmp");
2074cdf0e10cSrcweir 
2075cdf0e10cSrcweir 
2076cdf0e10cSrcweir 	/* generate tmp profilename */
2077cdf0e10cSrcweir 	ustrTmpName=osl_ProfileGenerateExtension(pProfile->m_strFileName,ustrExtension);
2078cdf0e10cSrcweir 	rtl_uString_release(ustrExtension);
2079cdf0e10cSrcweir 
2080cdf0e10cSrcweir 	if ( ustrTmpName == 0 )
2081cdf0e10cSrcweir 	{
2082cdf0e10cSrcweir 		return 0;
2083cdf0e10cSrcweir 	}
2084cdf0e10cSrcweir 
2085cdf0e10cSrcweir 
2086cdf0e10cSrcweir 	if ( ! ( pProfile->m_Flags & osl_Profile_READLOCK ) )
2087cdf0e10cSrcweir 	{
2088cdf0e10cSrcweir 		PFlags |= osl_Profile_WRITELOCK;
2089cdf0e10cSrcweir 	}
2090cdf0e10cSrcweir 
2091cdf0e10cSrcweir 	/* open this file */
2092cdf0e10cSrcweir 	pFile = openFileImpl(ustrTmpName,pProfile->m_Flags | PFlags);
2093cdf0e10cSrcweir 
2094cdf0e10cSrcweir 
2095cdf0e10cSrcweir 	/* return new pFile */
2096cdf0e10cSrcweir 	return pFile;
2097cdf0e10cSrcweir }
2098cdf0e10cSrcweir 
2099cdf0e10cSrcweir 
osl_ProfileSwapProfileNames(osl_TProfileImpl * pProfile)2100cdf0e10cSrcweir static sal_Bool osl_ProfileSwapProfileNames(osl_TProfileImpl* pProfile)
2101cdf0e10cSrcweir {
2102cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
2103cdf0e10cSrcweir 
2104cdf0e10cSrcweir 	rtl_uString* ustrBakFile=0;
2105cdf0e10cSrcweir 	rtl_uString* ustrTmpFile=0;
2106cdf0e10cSrcweir 	rtl_uString* ustrIniFile=0;
2107cdf0e10cSrcweir 	rtl_uString* ustrExtension=0;
2108cdf0e10cSrcweir 
2109cdf0e10cSrcweir 
2110cdf0e10cSrcweir 	rtl_uString_newFromAscii(&ustrExtension,"bak");
2111cdf0e10cSrcweir 
2112cdf0e10cSrcweir 	ustrBakFile=osl_ProfileGenerateExtension(pProfile->m_strFileName,ustrExtension);
2113cdf0e10cSrcweir 	rtl_uString_release(ustrExtension);
2114cdf0e10cSrcweir 	ustrExtension=0;
2115cdf0e10cSrcweir 
2116cdf0e10cSrcweir 
2117cdf0e10cSrcweir 	rtl_uString_newFromAscii(&ustrExtension,"ini");
2118cdf0e10cSrcweir 
2119cdf0e10cSrcweir 	ustrIniFile=osl_ProfileGenerateExtension(pProfile->m_strFileName,ustrExtension);
2120cdf0e10cSrcweir 	rtl_uString_release(ustrExtension);
2121cdf0e10cSrcweir 	ustrExtension=0;
2122cdf0e10cSrcweir 
2123cdf0e10cSrcweir 
2124cdf0e10cSrcweir 	rtl_uString_newFromAscii(&ustrExtension,"tmp");
2125cdf0e10cSrcweir 
2126cdf0e10cSrcweir 	ustrTmpFile=osl_ProfileGenerateExtension(pProfile->m_strFileName,ustrExtension);
2127cdf0e10cSrcweir 	rtl_uString_release(ustrExtension);
2128cdf0e10cSrcweir 	ustrExtension=0;
2129cdf0e10cSrcweir 
2130cdf0e10cSrcweir 
2131cdf0e10cSrcweir 	/* unlink bak */
2132cdf0e10cSrcweir 	DeleteFileW( reinterpret_cast<LPCWSTR>(rtl_uString_getStr( ustrBakFile )) );
2133cdf0e10cSrcweir 
2134cdf0e10cSrcweir 	/* rename ini bak */
2135cdf0e10cSrcweir 	MoveFileExW( reinterpret_cast<LPCWSTR>(rtl_uString_getStr( ustrIniFile )), reinterpret_cast<LPCWSTR>(rtl_uString_getStr( ustrBakFile )), MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH );
2136cdf0e10cSrcweir 
2137cdf0e10cSrcweir 	/* rename tmp ini */
2138cdf0e10cSrcweir 	MoveFileExW( reinterpret_cast<LPCWSTR>(rtl_uString_getStr( ustrTmpFile )), reinterpret_cast<LPCWSTR>(rtl_uString_getStr( ustrIniFile )), MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH );
2139cdf0e10cSrcweir 
2140cdf0e10cSrcweir 	return bRet;
2141cdf0e10cSrcweir }
2142cdf0e10cSrcweir 
2143cdf0e10cSrcweir 
osl_ProfileGenerateExtension(rtl_uString * ustrFileName,rtl_uString * ustrExtension)2144cdf0e10cSrcweir static rtl_uString* osl_ProfileGenerateExtension(rtl_uString* ustrFileName, rtl_uString* ustrExtension)
2145cdf0e10cSrcweir {
2146cdf0e10cSrcweir 	rtl_uString* ustrNewFileName=0;
2147cdf0e10cSrcweir 	rtl_uString* ustrOldExtension = 0;
2148cdf0e10cSrcweir 	sal_Unicode* pExtensionBuf = 0;
2149cdf0e10cSrcweir 	sal_Unicode* pFileNameBuf  = 0;
2150cdf0e10cSrcweir 	sal_Int32 nIndex = -1;
2151cdf0e10cSrcweir 
2152cdf0e10cSrcweir 	pFileNameBuf = rtl_uString_getStr(ustrFileName);
2153cdf0e10cSrcweir 
2154cdf0e10cSrcweir 	rtl_uString_newFromAscii(&ustrOldExtension,".");
2155cdf0e10cSrcweir 
2156cdf0e10cSrcweir 	pExtensionBuf = rtl_uString_getStr(ustrOldExtension);
2157cdf0e10cSrcweir 
2158cdf0e10cSrcweir 	nIndex = rtl_ustr_lastIndexOfChar(pFileNameBuf,*pExtensionBuf);
2159cdf0e10cSrcweir 
2160cdf0e10cSrcweir 	rtl_uString_newReplaceStrAt(&ustrNewFileName,
2161cdf0e10cSrcweir 								ustrFileName,
2162cdf0e10cSrcweir 								nIndex+1,
2163cdf0e10cSrcweir 								3,
2164cdf0e10cSrcweir 								ustrExtension);
2165cdf0e10cSrcweir 
2166cdf0e10cSrcweir 	return ustrNewFileName;
2167cdf0e10cSrcweir }
2168cdf0e10cSrcweir 
2169cdf0e10cSrcweir 
acquireProfile(oslProfile Profile,sal_Bool bWriteable)2170cdf0e10cSrcweir static osl_TProfileImpl* acquireProfile(oslProfile Profile, sal_Bool bWriteable)
2171cdf0e10cSrcweir {
2172cdf0e10cSrcweir 	osl_TProfileImpl* pProfile = (osl_TProfileImpl*)Profile;
2173cdf0e10cSrcweir     oslProfileOption PFlags=0;
2174cdf0e10cSrcweir 
2175cdf0e10cSrcweir 
2176cdf0e10cSrcweir     if ( bWriteable )
2177cdf0e10cSrcweir     {
2178cdf0e10cSrcweir /*          PFlags = osl_Profile_DEFAULT | osl_Profile_READWRITE; */
2179cdf0e10cSrcweir         PFlags = osl_Profile_DEFAULT | osl_Profile_WRITELOCK;
2180cdf0e10cSrcweir     }
2181cdf0e10cSrcweir     else
2182cdf0e10cSrcweir     {
2183cdf0e10cSrcweir         PFlags = osl_Profile_DEFAULT;
2184cdf0e10cSrcweir     }
2185cdf0e10cSrcweir 
2186cdf0e10cSrcweir 
2187cdf0e10cSrcweir 	if (pProfile == NULL)
2188cdf0e10cSrcweir 	{
2189cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2190cdf0e10cSrcweir         OSL_TRACE("AUTOOPEN MODE\n");
2191cdf0e10cSrcweir #endif
2192cdf0e10cSrcweir 
2193cdf0e10cSrcweir 
2194cdf0e10cSrcweir 
2195cdf0e10cSrcweir 		if ( ( pProfile = (osl_TProfileImpl*)osl_openProfile( NULL, PFlags ) ) != NULL )
2196cdf0e10cSrcweir         {
2197cdf0e10cSrcweir 			pProfile->m_Flags |= FLG_AUTOOPEN;
2198cdf0e10cSrcweir         }
2199cdf0e10cSrcweir 	}
2200cdf0e10cSrcweir 	else
2201cdf0e10cSrcweir 	{
2202cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2203cdf0e10cSrcweir         OSL_TRACE("try to acquire\n");
2204cdf0e10cSrcweir #endif
2205cdf0e10cSrcweir 
2206cdf0e10cSrcweir 
2207cdf0e10cSrcweir 
2208cdf0e10cSrcweir 		if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
2209cdf0e10cSrcweir 		{
2210cdf0e10cSrcweir 			if (! (pProfile->m_Flags & (osl_Profile_READLOCK |
2211cdf0e10cSrcweir                                         osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE)))
2212cdf0e10cSrcweir 			{
2213cdf0e10cSrcweir 				osl_TStamp Stamp;
2214cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2215cdf0e10cSrcweir                 OSL_TRACE("DEFAULT MODE\n");
2216cdf0e10cSrcweir #endif
2217cdf0e10cSrcweir                 pProfile->m_pFile = openFileImpl(
2218cdf0e10cSrcweir                     pProfile->m_strFileName, pProfile->m_Flags | PFlags);
2219cdf0e10cSrcweir 				if (!pProfile->m_pFile)
2220cdf0e10cSrcweir 					return NULL;
2221cdf0e10cSrcweir 
2222cdf0e10cSrcweir 				Stamp = getFileStamp(pProfile->m_pFile);
2223cdf0e10cSrcweir 
2224cdf0e10cSrcweir 				if (memcmp(&Stamp, &(pProfile->m_Stamp), sizeof(osl_TStamp)))
2225cdf0e10cSrcweir 				{
2226cdf0e10cSrcweir 					pProfile->m_Stamp = Stamp;
2227cdf0e10cSrcweir 
2228cdf0e10cSrcweir 					loadProfile(pProfile->m_pFile, pProfile);
2229cdf0e10cSrcweir 				}
2230cdf0e10cSrcweir 			}
2231cdf0e10cSrcweir 			else
2232cdf0e10cSrcweir             {
2233cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2234cdf0e10cSrcweir                 OSL_TRACE("READ/WRITELOCK MODE\n");
2235cdf0e10cSrcweir #endif
2236cdf0e10cSrcweir 
2237cdf0e10cSrcweir 
2238cdf0e10cSrcweir 				/* A readlock file could not be written */
2239cdf0e10cSrcweir                 if ((pProfile->m_Flags & osl_Profile_READLOCK) && bWriteable)
2240cdf0e10cSrcweir                 {
2241cdf0e10cSrcweir                     return (NULL);
2242cdf0e10cSrcweir                 }
2243cdf0e10cSrcweir             }
2244cdf0e10cSrcweir 		}
2245cdf0e10cSrcweir 	}
2246cdf0e10cSrcweir 
2247cdf0e10cSrcweir 	return (pProfile);
2248cdf0e10cSrcweir }
2249cdf0e10cSrcweir 
releaseProfile(osl_TProfileImpl * pProfile)2250cdf0e10cSrcweir static sal_Bool releaseProfile(osl_TProfileImpl* pProfile)
2251cdf0e10cSrcweir {
2252cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2253cdf0e10cSrcweir     OSL_TRACE("In  releaseProfile\n");
2254cdf0e10cSrcweir #endif
2255cdf0e10cSrcweir 
2256cdf0e10cSrcweir     if ( pProfile == 0 )
2257cdf0e10cSrcweir     {
2258cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2259cdf0e10cSrcweir         OSL_TRACE("Out releaseProfile [profile==0]\n");
2260cdf0e10cSrcweir #endif
2261cdf0e10cSrcweir         return sal_False;
2262cdf0e10cSrcweir     }
2263cdf0e10cSrcweir 
2264cdf0e10cSrcweir 	if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
2265cdf0e10cSrcweir 	{
2266cdf0e10cSrcweir 		if (pProfile->m_Flags & FLG_AUTOOPEN)
2267cdf0e10cSrcweir         {
2268cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2269cdf0e10cSrcweir         OSL_TRACE("Out releaseProfile [AUTOOPEN]\n");
2270cdf0e10cSrcweir #endif
2271cdf0e10cSrcweir 			return (osl_closeProfile((oslProfile)pProfile));
2272cdf0e10cSrcweir         }
2273cdf0e10cSrcweir 		else
2274cdf0e10cSrcweir 		{
2275cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2276cdf0e10cSrcweir         OSL_TRACE("DEFAULT MODE\n");
2277cdf0e10cSrcweir #endif
2278cdf0e10cSrcweir         if (! (pProfile->m_Flags & (osl_Profile_READLOCK |
2279cdf0e10cSrcweir                                     osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE)))
2280cdf0e10cSrcweir 			{
2281cdf0e10cSrcweir 				if (pProfile->m_Flags & FLG_MODIFIED)
2282cdf0e10cSrcweir 					storeProfile(pProfile, sal_False);
2283cdf0e10cSrcweir 
2284cdf0e10cSrcweir 				closeFileImpl(pProfile->m_pFile);
2285cdf0e10cSrcweir 				pProfile->m_pFile = NULL;
2286cdf0e10cSrcweir 			}
2287cdf0e10cSrcweir 		}
2288cdf0e10cSrcweir 	}
2289cdf0e10cSrcweir 
2290cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2291cdf0e10cSrcweir     OSL_TRACE("Out releaseProfile [ok]\n");
2292cdf0e10cSrcweir #endif
2293cdf0e10cSrcweir 	return (sal_True);
2294cdf0e10cSrcweir }
2295cdf0e10cSrcweir 
lookupProfile(const sal_Unicode * strPath,const sal_Unicode * strFile,sal_Unicode * strProfile)2296cdf0e10cSrcweir static sal_Bool lookupProfile(const sal_Unicode *strPath, const sal_Unicode *strFile, sal_Unicode *strProfile)
2297cdf0e10cSrcweir {
2298cdf0e10cSrcweir 	sal_Char *pChr, *pStr;
2299cdf0e10cSrcweir 	sal_Char Buffer[4096] = "";
2300cdf0e10cSrcweir 	sal_Char Product[132] = "";
2301cdf0e10cSrcweir 
2302cdf0e10cSrcweir     ::osl::LongPathBuffer< sal_Unicode > aPath( MAX_LONG_PATH );
2303cdf0e10cSrcweir     aPath[0] = 0;
2304cdf0e10cSrcweir 	DWORD dwPathLen = 0;
2305cdf0e10cSrcweir 
2306cdf0e10cSrcweir 	if (*strPath == L'"')
2307cdf0e10cSrcweir 	{
2308cdf0e10cSrcweir 		int i = 0;
2309cdf0e10cSrcweir 
2310cdf0e10cSrcweir 		strPath++;
2311cdf0e10cSrcweir 
2312cdf0e10cSrcweir 		while ((strPath[i] != L'"') && (strPath[i] != L'\0'))
2313cdf0e10cSrcweir 			i++;
2314cdf0e10cSrcweir 
2315cdf0e10cSrcweir 		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(strPath), i, Product, sizeof(Product), NULL, NULL);
2316cdf0e10cSrcweir 		Product[i] = '\0';
2317cdf0e10cSrcweir 		strPath += i;
2318cdf0e10cSrcweir 
2319cdf0e10cSrcweir 		if (*strPath == L'"')
2320cdf0e10cSrcweir 			strPath++;
2321cdf0e10cSrcweir 
2322cdf0e10cSrcweir 		if ( (*strPath == L'/') || (*strPath == L'\\') )
2323cdf0e10cSrcweir 		{
2324cdf0e10cSrcweir 			strPath++;
2325cdf0e10cSrcweir 		}
2326cdf0e10cSrcweir 	}
2327cdf0e10cSrcweir 
2328cdf0e10cSrcweir 	else
2329cdf0e10cSrcweir 	{
2330cdf0e10cSrcweir 		/* if we have not product identfication, do a special handling for soffice.ini */
2331cdf0e10cSrcweir 		if (rtl_ustr_ascii_compare(strFile, SVERSION_PROFILE) == 0)
2332cdf0e10cSrcweir 		{
2333cdf0e10cSrcweir 			rtl_uString * strSVProfile  = NULL;
2334cdf0e10cSrcweir 			rtl_uString * strSVFallback = NULL;
2335cdf0e10cSrcweir 			rtl_uString * strSVLocation = NULL;
2336cdf0e10cSrcweir 			rtl_uString * strSVName     = NULL;
2337cdf0e10cSrcweir             ::osl::LongPathBuffer< sal_Char > aDir( MAX_LONG_PATH );
2338cdf0e10cSrcweir 			oslProfile hProfile;
2339cdf0e10cSrcweir 
2340cdf0e10cSrcweir 			rtl_uString_newFromAscii(&strSVFallback, SVERSION_FALLBACK);
2341cdf0e10cSrcweir 			rtl_uString_newFromAscii(&strSVLocation, SVERSION_LOCATION);
2342cdf0e10cSrcweir 			rtl_uString_newFromAscii(&strSVName, SVERSION_NAME);
2343cdf0e10cSrcweir 
2344cdf0e10cSrcweir 			/* open sversion.ini in the system directory, and try to locate the entry
2345cdf0e10cSrcweir 			   with the highest version for StarOffice */
2346cdf0e10cSrcweir 			if (osl_getProfileName( strSVFallback, strSVName, &strSVProfile))
2347cdf0e10cSrcweir             {
2348cdf0e10cSrcweir 				hProfile = osl_openProfile(strSVProfile, osl_Profile_READLOCK);
2349cdf0e10cSrcweir                 if (hProfile)
2350cdf0e10cSrcweir                 {
2351cdf0e10cSrcweir                     osl_getProfileSectionEntries(
2352cdf0e10cSrcweir                         hProfile, SVERSION_SECTION, Buffer, sizeof(Buffer));
2353cdf0e10cSrcweir 
2354cdf0e10cSrcweir                     for (pChr = Buffer; *pChr != '\0'; pChr += strlen(pChr) + 1)
2355cdf0e10cSrcweir                     {
2356cdf0e10cSrcweir                         if ((strnicmp(
2357cdf0e10cSrcweir                                  pChr, SVERSION_SOFFICE,
2358cdf0e10cSrcweir                                  sizeof(SVERSION_SOFFICE) - 1)
2359cdf0e10cSrcweir                              == 0)
2360cdf0e10cSrcweir                             && (stricmp(Product, pChr) < 0))
2361cdf0e10cSrcweir                         {
2362cdf0e10cSrcweir                             osl_readProfileString(
2363cdf0e10cSrcweir                                 hProfile, SVERSION_SECTION, pChr, aDir,
2364cdf0e10cSrcweir                                 aDir.getBufSizeInSymbols(), "");
2365cdf0e10cSrcweir 
2366cdf0e10cSrcweir                             /* check for existence of path */
2367cdf0e10cSrcweir                             if (access(aDir, 0) >= 0)
2368cdf0e10cSrcweir                                 strcpy(Product, pChr);
2369cdf0e10cSrcweir                         }
2370cdf0e10cSrcweir                     }
2371cdf0e10cSrcweir 
2372cdf0e10cSrcweir                     osl_closeProfile(hProfile);
2373cdf0e10cSrcweir                 }
2374cdf0e10cSrcweir 				rtl_uString_release(strSVProfile);
2375cdf0e10cSrcweir 				strSVProfile = NULL;
2376cdf0e10cSrcweir 			}
2377cdf0e10cSrcweir 
2378cdf0e10cSrcweir 			/* open sversion.ini in the users directory, and try to locate the entry
2379cdf0e10cSrcweir 			   with the highest version for StarOffice */
2380cdf0e10cSrcweir 			if ((strcmp(SVERSION_LOCATION, SVERSION_FALLBACK) != 0) &&
2381cdf0e10cSrcweir 			    (osl_getProfileName(strSVLocation, strSVName, &strSVProfile)))
2382cdf0e10cSrcweir             {
2383cdf0e10cSrcweir 				hProfile = osl_openProfile(strSVProfile, osl_Profile_READLOCK);
2384cdf0e10cSrcweir                 if (hProfile)
2385cdf0e10cSrcweir                 {
2386cdf0e10cSrcweir                     osl_getProfileSectionEntries(
2387cdf0e10cSrcweir                         hProfile, SVERSION_SECTION, Buffer, sizeof(Buffer));
2388cdf0e10cSrcweir 
2389cdf0e10cSrcweir                     for (pChr = Buffer; *pChr != '\0'; pChr += strlen(pChr) + 1)
2390cdf0e10cSrcweir                     {
2391cdf0e10cSrcweir                         if ((strnicmp(
2392cdf0e10cSrcweir                                  pChr, SVERSION_SOFFICE,
2393cdf0e10cSrcweir                                  sizeof(SVERSION_SOFFICE) - 1)
2394cdf0e10cSrcweir                              == 0)
2395cdf0e10cSrcweir                             && (stricmp(Product, pChr) < 0))
2396cdf0e10cSrcweir                         {
2397cdf0e10cSrcweir                             osl_readProfileString(
2398cdf0e10cSrcweir                                 hProfile, SVERSION_SECTION, pChr, aDir,
2399cdf0e10cSrcweir                                 aDir.getBufSizeInSymbols(), "");
2400cdf0e10cSrcweir 
2401cdf0e10cSrcweir                             /* check for existence of path */
2402cdf0e10cSrcweir                             if (access(aDir, 0) >= 0)
2403cdf0e10cSrcweir                                 strcpy(Product, pChr);
2404cdf0e10cSrcweir                         }
2405cdf0e10cSrcweir                     }
2406cdf0e10cSrcweir 
2407cdf0e10cSrcweir                     osl_closeProfile(hProfile);
2408cdf0e10cSrcweir                 }
2409cdf0e10cSrcweir 				rtl_uString_release(strSVProfile);
2410cdf0e10cSrcweir 			}
2411cdf0e10cSrcweir 
2412cdf0e10cSrcweir 			rtl_uString_release(strSVFallback);
2413cdf0e10cSrcweir 			rtl_uString_release(strSVLocation);
2414cdf0e10cSrcweir 			rtl_uString_release(strSVName);
2415cdf0e10cSrcweir 
2416cdf0e10cSrcweir 			/* remove any trailing build number */
2417cdf0e10cSrcweir 			if ((pChr = strrchr(Product, '/')) != NULL)
2418cdf0e10cSrcweir 				*pChr = '\0';
2419cdf0e10cSrcweir 		}
2420cdf0e10cSrcweir 	}
2421cdf0e10cSrcweir 
2422cdf0e10cSrcweir 	/* if we have an userid option eg. "-userid:rh[/usr/home/rh/staroffice]",
2423cdf0e10cSrcweir 	   this will supercede all other locations */
2424cdf0e10cSrcweir 	{
2425cdf0e10cSrcweir 		sal_uInt32 n, nArgs = osl_getCommandArgCount();
2426cdf0e10cSrcweir 
2427cdf0e10cSrcweir 		for (n = 0; n < nArgs; n++)
2428cdf0e10cSrcweir 		{
2429cdf0e10cSrcweir 			rtl_uString * strCommandArg = NULL;
2430cdf0e10cSrcweir 
2431cdf0e10cSrcweir 			if ((osl_getCommandArg( n, &strCommandArg ) == osl_Process_E_None) &&
2432cdf0e10cSrcweir 				((strCommandArg->buffer[0] == L'-') || (strCommandArg->buffer[0] == L'+')) &&
2433cdf0e10cSrcweir 				(rtl_ustr_ascii_compare_WithLength(strCommandArg->buffer, RTL_CONSTASCII_LENGTH(SVERSION_OPTION), SVERSION_OPTION)))
2434cdf0e10cSrcweir 			{
2435cdf0e10cSrcweir 				sal_Unicode *pCommandArg = strCommandArg->buffer + RTL_CONSTASCII_LENGTH(SVERSION_OPTION);
2436cdf0e10cSrcweir 				sal_Int32 nStart, nEnd;
2437cdf0e10cSrcweir 
2438cdf0e10cSrcweir 				if (((nStart = rtl_ustr_indexOfChar(pCommandArg, L'[')) != -1) &&
2439cdf0e10cSrcweir 					((nEnd = rtl_ustr_indexOfChar(pCommandArg + nStart + 1, L']')) != -1))
2440cdf0e10cSrcweir 				{
2441cdf0e10cSrcweir 					dwPathLen = nEnd;
2442cdf0e10cSrcweir 				    copy_ustr_n(aPath, pCommandArg + nStart + 1, dwPathLen);
2443cdf0e10cSrcweir 					aPath[dwPathLen] = 0;
2444cdf0e10cSrcweir 
2445cdf0e10cSrcweir 					/* build full path */
2446cdf0e10cSrcweir 					if ((aPath[dwPathLen - 1] != L'/') && (aPath[dwPathLen - 1] != L'\\'))
2447cdf0e10cSrcweir 					{
2448cdf0e10cSrcweir 						copy_ustr_n(aPath + dwPathLen++, L"/", 2);
2449cdf0e10cSrcweir 					}
2450cdf0e10cSrcweir 
2451cdf0e10cSrcweir 					if (*strPath)
2452cdf0e10cSrcweir 					{
2453cdf0e10cSrcweir 						copy_ustr_n(aPath + dwPathLen, strPath, rtl_ustr_getLength(strPath)+1);
2454cdf0e10cSrcweir 						dwPathLen += rtl_ustr_getLength(strPath);
2455cdf0e10cSrcweir 					}
2456cdf0e10cSrcweir 					else
2457cdf0e10cSrcweir 					{
2458cdf0e10cSrcweir                         ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );
2459cdf0e10cSrcweir 						int n;
2460cdf0e10cSrcweir 
2461cdf0e10cSrcweir 						if ((n = WideCharToMultiByte(CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath, aTmpPath.getBufSizeInSymbols(), NULL, NULL)) > 0)
2462cdf0e10cSrcweir 						{
2463cdf0e10cSrcweir 							strcpy(aTmpPath + n, SVERSION_USER);
2464cdf0e10cSrcweir 							if (access(aTmpPath, 0) >= 0)
2465cdf0e10cSrcweir 							{
2466cdf0e10cSrcweir 								dwPathLen += MultiByteToWideChar( CP_ACP, 0, SVERSION_USER, -1, reinterpret_cast<LPWSTR>(aPath + dwPathLen), aPath.getBufSizeInSymbols() - dwPathLen );
2467cdf0e10cSrcweir 							}
2468cdf0e10cSrcweir 						}
2469cdf0e10cSrcweir 					}
2470cdf0e10cSrcweir 
2471cdf0e10cSrcweir 					break;
2472cdf0e10cSrcweir 				}
2473cdf0e10cSrcweir 			}
2474cdf0e10cSrcweir 		}
2475cdf0e10cSrcweir 	}
2476cdf0e10cSrcweir 
2477cdf0e10cSrcweir 
2478cdf0e10cSrcweir 	if (dwPathLen == 0)
2479cdf0e10cSrcweir 	{
2480cdf0e10cSrcweir 		rtl_uString * strExecutable = NULL;
2481cdf0e10cSrcweir 		rtl_uString * strTmp = NULL;
2482cdf0e10cSrcweir 		sal_Int32 nPos;
2483cdf0e10cSrcweir 
2484cdf0e10cSrcweir 		/* try to find the file in the directory of the executbale */
2485cdf0e10cSrcweir 		if (osl_getExecutableFile(&strTmp) != osl_Process_E_None)
2486cdf0e10cSrcweir 			return (sal_False);
2487cdf0e10cSrcweir 
2488cdf0e10cSrcweir 		/* convert to native path */
2489cdf0e10cSrcweir 		if (osl_getSystemPathFromFileURL(strTmp, &strExecutable) != osl_File_E_None)
2490cdf0e10cSrcweir 		{
2491cdf0e10cSrcweir 			rtl_uString_release(strTmp);
2492cdf0e10cSrcweir 			return sal_False;
2493cdf0e10cSrcweir 		}
2494cdf0e10cSrcweir 
2495cdf0e10cSrcweir 		rtl_uString_release(strTmp);
2496cdf0e10cSrcweir 
249786e1cf34SPedro Giffuni 		/* separate path from filename */
2498cdf0e10cSrcweir 		if ((nPos = rtl_ustr_lastIndexOfChar(strExecutable->buffer, L'\\')) == -1)
2499cdf0e10cSrcweir 		{
2500cdf0e10cSrcweir 			if ((nPos = rtl_ustr_lastIndexOfChar(strExecutable->buffer, L':')) == -1)
2501cdf0e10cSrcweir 			{
2502cdf0e10cSrcweir 				return sal_False;
2503cdf0e10cSrcweir 			}
2504cdf0e10cSrcweir 			else
2505cdf0e10cSrcweir 			{
2506cdf0e10cSrcweir                 copy_ustr_n(aPath, strExecutable->buffer, nPos);
2507cdf0e10cSrcweir                 aPath[nPos] = 0;
2508cdf0e10cSrcweir                 dwPathLen = nPos;
2509cdf0e10cSrcweir 			}
2510cdf0e10cSrcweir 		}
2511cdf0e10cSrcweir 		else
2512cdf0e10cSrcweir 		{
2513cdf0e10cSrcweir             copy_ustr_n(aPath, strExecutable->buffer, nPos);
2514cdf0e10cSrcweir             dwPathLen = nPos;
2515cdf0e10cSrcweir 			aPath[dwPathLen] = 0;
2516cdf0e10cSrcweir 		}
2517cdf0e10cSrcweir 
2518cdf0e10cSrcweir 		/* if we have no product identification use the executable file name */
2519cdf0e10cSrcweir 		if (*Product == 0)
2520cdf0e10cSrcweir 		{
2521cdf0e10cSrcweir 			WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(strExecutable->buffer + nPos + 1), -1, Product, sizeof(Product), NULL, NULL);
2522cdf0e10cSrcweir 
2523cdf0e10cSrcweir 			/* remove extension */
2524cdf0e10cSrcweir 			if ((pChr = strrchr(Product, '.')) != NULL)
2525cdf0e10cSrcweir 				*pChr = '\0';
2526cdf0e10cSrcweir 		}
2527cdf0e10cSrcweir 
2528cdf0e10cSrcweir 		rtl_uString_release(strExecutable);
2529cdf0e10cSrcweir 
2530cdf0e10cSrcweir 		/* remember last subdir */
2531cdf0e10cSrcweir 		nPos = rtl_ustr_lastIndexOfChar(aPath, L'\\');
2532cdf0e10cSrcweir 
2533cdf0e10cSrcweir 		copy_ustr_n(aPath + dwPathLen++, L"\\", 2);
2534cdf0e10cSrcweir 
2535cdf0e10cSrcweir 		if (*strPath)
2536cdf0e10cSrcweir 		{
2537cdf0e10cSrcweir 			copy_ustr_n(aPath + dwPathLen, strPath, rtl_ustr_getLength(strPath)+1);
2538cdf0e10cSrcweir 			dwPathLen += rtl_ustr_getLength(strPath);
2539cdf0e10cSrcweir 		}
2540cdf0e10cSrcweir 
2541cdf0e10cSrcweir 		{
2542cdf0e10cSrcweir             ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );
2543cdf0e10cSrcweir 
2544cdf0e10cSrcweir 			WideCharToMultiByte(CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath, aTmpPath.getBufSizeInSymbols(), NULL, NULL);
2545cdf0e10cSrcweir 
2546cdf0e10cSrcweir 			/* if file not exists, remove any specified subdirectories
2547cdf0e10cSrcweir 			   like "bin" or "program" */
2548cdf0e10cSrcweir 
2549cdf0e10cSrcweir 			if (((access(aTmpPath, 0) < 0) && (nPos != -1)) || (*strPath == 0))
2550cdf0e10cSrcweir 			{
2551cdf0e10cSrcweir 				static sal_Char *SubDirs[] = SVERSION_DIRS;
2552cdf0e10cSrcweir 
2553cdf0e10cSrcweir 				int i = 0;
2554cdf0e10cSrcweir 				pStr = aTmpPath + nPos;
2555cdf0e10cSrcweir 
2556cdf0e10cSrcweir 				for (i = 0; i < (sizeof(SubDirs) / sizeof(SubDirs[0])); i++)
2557cdf0e10cSrcweir 					if (strnicmp(pStr + 1, SubDirs[i], strlen(SubDirs[i])) == 0)
2558cdf0e10cSrcweir 					{
2559cdf0e10cSrcweir 						if ( *strPath == 0)
2560cdf0e10cSrcweir 						{
2561cdf0e10cSrcweir 							strcpy(pStr + 1,SVERSION_USER);
2562cdf0e10cSrcweir 							if ( access(aTmpPath, 0) < 0 )
2563cdf0e10cSrcweir 							{
2564cdf0e10cSrcweir 								*(pStr+1)='\0';
2565cdf0e10cSrcweir 							}
2566cdf0e10cSrcweir 							else
2567cdf0e10cSrcweir 							{
2568cdf0e10cSrcweir                                 dwPathLen = nPos + MultiByteToWideChar( CP_ACP, 0, SVERSION_USER, -1, reinterpret_cast<LPWSTR>(aPath + nPos + 1), aPath.getBufSizeInSymbols() - (nPos + 1) );
2569cdf0e10cSrcweir 							}
2570cdf0e10cSrcweir 						}
2571cdf0e10cSrcweir 						else
2572cdf0e10cSrcweir 						{
2573cdf0e10cSrcweir 							copy_ustr_n(aPath + nPos + 1, strPath, rtl_ustr_getLength(strPath)+1);
2574cdf0e10cSrcweir 							dwPathLen = nPos + 1 + rtl_ustr_getLength(strPath);
2575cdf0e10cSrcweir 						}
2576cdf0e10cSrcweir 
2577cdf0e10cSrcweir 						break;
2578cdf0e10cSrcweir 					}
2579cdf0e10cSrcweir 			}
2580cdf0e10cSrcweir 		}
2581cdf0e10cSrcweir 
2582cdf0e10cSrcweir 		if ((aPath[dwPathLen - 1] != L'/') && (aPath[dwPathLen - 1] != L'\\'))
2583cdf0e10cSrcweir 		{
2584cdf0e10cSrcweir             aPath[dwPathLen++] = L'\\';
2585cdf0e10cSrcweir 			aPath[dwPathLen] = 0;
2586cdf0e10cSrcweir 		}
2587cdf0e10cSrcweir 
2588cdf0e10cSrcweir 		copy_ustr_n(aPath + dwPathLen, strFile, rtl_ustr_getLength(strFile)+1);
2589cdf0e10cSrcweir 
2590cdf0e10cSrcweir 		{
2591cdf0e10cSrcweir             ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );
2592cdf0e10cSrcweir 
2593cdf0e10cSrcweir 			WideCharToMultiByte(CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath, aTmpPath.getBufSizeInSymbols(), NULL, NULL);
2594cdf0e10cSrcweir 
2595cdf0e10cSrcweir 			if ((access(aTmpPath, 0) < 0) && (strlen(Product) > 0))
2596cdf0e10cSrcweir 			{
2597cdf0e10cSrcweir 				rtl_uString * strSVFallback = NULL;
2598cdf0e10cSrcweir 				rtl_uString * strSVProfile  = NULL;
2599cdf0e10cSrcweir 				rtl_uString * strSVLocation = NULL;
2600cdf0e10cSrcweir 				rtl_uString * strSVName     = NULL;
2601cdf0e10cSrcweir 				oslProfile hProfile;
2602cdf0e10cSrcweir 
2603cdf0e10cSrcweir 				rtl_uString_newFromAscii(&strSVFallback, SVERSION_FALLBACK);
2604cdf0e10cSrcweir 				rtl_uString_newFromAscii(&strSVLocation, SVERSION_LOCATION);
2605cdf0e10cSrcweir 				rtl_uString_newFromAscii(&strSVName, SVERSION_NAME);
2606cdf0e10cSrcweir 
2607cdf0e10cSrcweir 				/* open sversion.ini in the system directory, and try to locate the entry
2608cdf0e10cSrcweir 				   with the highest version for StarOffice */
2609cdf0e10cSrcweir 				if (osl_getProfileName(strSVLocation, strSVName, &strSVProfile))
2610cdf0e10cSrcweir                 {
2611cdf0e10cSrcweir 					hProfile = osl_openProfile(
2612cdf0e10cSrcweir                         strSVProfile, osl_Profile_READLOCK);
2613cdf0e10cSrcweir                     if (hProfile)
2614cdf0e10cSrcweir                     {
2615cdf0e10cSrcweir                         osl_readProfileString(
2616cdf0e10cSrcweir                             hProfile, SVERSION_SECTION, Product, Buffer,
2617cdf0e10cSrcweir                             sizeof(Buffer), "");
2618cdf0e10cSrcweir                         osl_closeProfile(hProfile);
2619cdf0e10cSrcweir 
2620cdf0e10cSrcweir                         /* if not found, try the fallback */
2621cdf0e10cSrcweir                         if ((strlen(Buffer) <= 0)
2622cdf0e10cSrcweir                             && (strcmp(SVERSION_LOCATION, SVERSION_FALLBACK)
2623cdf0e10cSrcweir                                 != 0))
2624cdf0e10cSrcweir                         {
2625cdf0e10cSrcweir                             if (osl_getProfileName(
2626cdf0e10cSrcweir                                     strSVFallback, strSVName, &strSVProfile))
2627cdf0e10cSrcweir                             {
2628cdf0e10cSrcweir                                 hProfile = osl_openProfile(
2629cdf0e10cSrcweir                                     strSVProfile, osl_Profile_READLOCK);
2630cdf0e10cSrcweir                                 if (hProfile)
2631cdf0e10cSrcweir                                 {
2632cdf0e10cSrcweir                                     osl_readProfileString(
2633cdf0e10cSrcweir                                         hProfile, SVERSION_SECTION, Product,
2634cdf0e10cSrcweir                                         Buffer, sizeof(Buffer), "");
2635cdf0e10cSrcweir                                 }
2636cdf0e10cSrcweir                             }
2637cdf0e10cSrcweir 
2638cdf0e10cSrcweir                             osl_closeProfile(hProfile);
2639cdf0e10cSrcweir                         }
2640cdf0e10cSrcweir 
2641cdf0e10cSrcweir                         if (strlen(Buffer) > 0)
2642cdf0e10cSrcweir                         {
2643cdf0e10cSrcweir                             dwPathLen = MultiByteToWideChar(
2644cdf0e10cSrcweir                                 CP_ACP, 0, Buffer, -1, ::osl::mingw_reinterpret_cast<LPWSTR>(aPath), aPath.getBufSizeInSymbols() );
2645cdf0e10cSrcweir                             dwPathLen -=1;
2646cdf0e10cSrcweir 
2647cdf0e10cSrcweir                             /* build full path */
2648cdf0e10cSrcweir                             if ((aPath[dwPathLen - 1] != L'/')
2649cdf0e10cSrcweir                                 && (aPath[dwPathLen - 1] != L'\\'))
2650cdf0e10cSrcweir                             {
2651cdf0e10cSrcweir                                 copy_ustr_n(aPath + dwPathLen++, L"\\", 2);
2652cdf0e10cSrcweir                             }
2653cdf0e10cSrcweir 
2654cdf0e10cSrcweir                             if (*strPath)
2655cdf0e10cSrcweir                             {
2656cdf0e10cSrcweir                                 copy_ustr_n(aPath + dwPathLen, strPath, rtl_ustr_getLength(strPath)+1);
2657cdf0e10cSrcweir                                 dwPathLen += rtl_ustr_getLength(strPath);
2658cdf0e10cSrcweir                             }
2659cdf0e10cSrcweir                             else
2660cdf0e10cSrcweir                             {
2661cdf0e10cSrcweir                                 ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );
2662cdf0e10cSrcweir                                 int n;
2663cdf0e10cSrcweir 
2664cdf0e10cSrcweir                                 if ((n = WideCharToMultiByte(
2665cdf0e10cSrcweir                                          CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath,
2666cdf0e10cSrcweir                                          aTmpPath.getBufSizeInSymbols(), NULL, NULL))
2667cdf0e10cSrcweir                                     > 0)
2668cdf0e10cSrcweir                                 {
2669cdf0e10cSrcweir                                     strcpy(aTmpPath + n, SVERSION_USER);
2670cdf0e10cSrcweir                                     if (access(aTmpPath, 0) >= 0)
2671cdf0e10cSrcweir                                     {
2672cdf0e10cSrcweir                                         dwPathLen += MultiByteToWideChar(
2673cdf0e10cSrcweir                                             CP_ACP, 0, SVERSION_USER, -1,
2674cdf0e10cSrcweir                                             reinterpret_cast<LPWSTR>(aPath + dwPathLen),
2675cdf0e10cSrcweir                                             aPath.getBufSizeInSymbols() - dwPathLen );
2676cdf0e10cSrcweir                                     }
2677cdf0e10cSrcweir                                 }
2678cdf0e10cSrcweir                             }
2679cdf0e10cSrcweir                         }
2680cdf0e10cSrcweir                     }
2681cdf0e10cSrcweir 
2682cdf0e10cSrcweir 					rtl_uString_release(strSVProfile);
2683cdf0e10cSrcweir 				}
2684cdf0e10cSrcweir 
2685cdf0e10cSrcweir 				rtl_uString_release(strSVFallback);
2686cdf0e10cSrcweir 				rtl_uString_release(strSVLocation);
2687cdf0e10cSrcweir 				rtl_uString_release(strSVName);
2688cdf0e10cSrcweir 			}
2689cdf0e10cSrcweir 		}
2690cdf0e10cSrcweir 
2691cdf0e10cSrcweir 		aPath[dwPathLen] = 0;
2692cdf0e10cSrcweir 	}
2693cdf0e10cSrcweir 
2694cdf0e10cSrcweir 	/* copy filename */
2695cdf0e10cSrcweir 	copy_ustr_n(strProfile, aPath, dwPathLen+1);
2696cdf0e10cSrcweir 
2697cdf0e10cSrcweir 	return sal_True;
2698cdf0e10cSrcweir }
2699cdf0e10cSrcweir 
2700cdf0e10cSrcweir 
2701