1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file
5647f063dSAndrew Rist * distributed with this work for additional information
6647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist * with the License. You may obtain a copy of the License at
10647f063dSAndrew Rist *
11647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist *
13647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist * software distributed under the License is distributed on an
15647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist * KIND, either express or implied. See the License for the
17647f063dSAndrew Rist * specific language governing permissions and limitations
18647f063dSAndrew Rist * under the License.
19647f063dSAndrew Rist *
20647f063dSAndrew Rist *************************************************************/
21647f063dSAndrew Rist
22647f063dSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir #include "system.h"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <osl/profile.h>
29cdf0e10cSrcweir #include <osl/process.h>
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir #include <rtl/alloc.h>
32cdf0e10cSrcweir #include <osl/util.h>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #define LINES_INI 32
35cdf0e10cSrcweir #define LINES_ADD 10
36cdf0e10cSrcweir #define SECTIONS_INI 5
37cdf0e10cSrcweir #define SECTIONS_ADD 3
38cdf0e10cSrcweir #define ENTRIES_INI 5
39cdf0e10cSrcweir #define ENTRIES_ADD 3
40cdf0e10cSrcweir
41cdf0e10cSrcweir
42cdf0e10cSrcweir #define STR_INI_EXTENSION "rc"
43cdf0e10cSrcweir #define STR_INI_METAHOME "?~"
44cdf0e10cSrcweir #define STR_INI_METASYS "?$"
45cdf0e10cSrcweir #define STR_INI_METACFG "?^"
46cdf0e10cSrcweir #define STR_INI_METAINS "?#"
47cdf0e10cSrcweir
48cdf0e10cSrcweir #define STR_INI_BOOLYES "yes"
49cdf0e10cSrcweir #define STR_INI_BOOLON "on"
50cdf0e10cSrcweir #define STR_INI_BOOLONE "1"
51cdf0e10cSrcweir #define STR_INI_BOOLNO "no"
52cdf0e10cSrcweir #define STR_INI_BOOLOFF "off"
53cdf0e10cSrcweir #define STR_INI_BOOLZERO "0"
54cdf0e10cSrcweir
55cdf0e10cSrcweir #define FLG_USER 0x00FF
56cdf0e10cSrcweir #define FLG_AUTOOPEN 0x0100
57cdf0e10cSrcweir #define FLG_MODIFIED 0x0200
58cdf0e10cSrcweir
59cdf0e10cSrcweir #define SVERSION_LOCATION STR_INI_METACFG
60cdf0e10cSrcweir #define SVERSION_FALLBACK STR_INI_METASYS
61cdf0e10cSrcweir #define SVERSION_NAME "sversion"
62cdf0e10cSrcweir #define SVERSION_SECTION "Versions"
63cdf0e10cSrcweir #define SVERSION_SOFFICE "StarOffice"
64cdf0e10cSrcweir #define SVERSION_PROFILE "sofficerc"
65cdf0e10cSrcweir #define SVERSION_OPTION "userid:"
66cdf0e10cSrcweir #define SVERSION_DIRS { "bin", "program" }
67cdf0e10cSrcweir #define SVERSION_USER "user"
68cdf0e10cSrcweir
69cdf0e10cSrcweir #define DEFAULT_PMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
70cdf0e10cSrcweir
71cdf0e10cSrcweir #define _BUILD_STR_(n) # n
72cdf0e10cSrcweir #define BUILD_STR(n) _BUILD_STR_(n)
73cdf0e10cSrcweir
74cdf0e10cSrcweir
75cdf0e10cSrcweir /*#define DEBUG_OSL_PROFILE*/
76cdf0e10cSrcweir /*#define TRACE_OSL_PROFILE*/
77cdf0e10cSrcweir
78cdf0e10cSrcweir /*****************************************************************************/
79cdf0e10cSrcweir /* Data Type Definition */
80cdf0e10cSrcweir /*****************************************************************************/
81cdf0e10cSrcweir
82cdf0e10cSrcweir typedef time_t osl_TStamp;
83cdf0e10cSrcweir
84cdf0e10cSrcweir typedef enum _osl_TLockMode
85cdf0e10cSrcweir {
86cdf0e10cSrcweir un_lock, read_lock, write_lock
87cdf0e10cSrcweir } osl_TLockMode;
88cdf0e10cSrcweir
89cdf0e10cSrcweir typedef struct _osl_TFile
90cdf0e10cSrcweir {
91cdf0e10cSrcweir int m_Handle;
92cdf0e10cSrcweir sal_Char* m_pReadPtr;
93cdf0e10cSrcweir sal_Char m_ReadBuf[512];
94cdf0e10cSrcweir sal_Char* m_pWriteBuf;
95cdf0e10cSrcweir sal_uInt32 m_nWriteBufLen;
96cdf0e10cSrcweir sal_uInt32 m_nWriteBufFree;
97cdf0e10cSrcweir } osl_TFile;
98cdf0e10cSrcweir
99cdf0e10cSrcweir typedef struct _osl_TProfileEntry
100cdf0e10cSrcweir {
101cdf0e10cSrcweir sal_uInt32 m_Line;
102cdf0e10cSrcweir sal_uInt32 m_Offset;
103cdf0e10cSrcweir sal_uInt32 m_Len;
104cdf0e10cSrcweir } osl_TProfileEntry;
105cdf0e10cSrcweir
106cdf0e10cSrcweir typedef struct _osl_TProfileSection
107cdf0e10cSrcweir {
108cdf0e10cSrcweir sal_uInt32 m_Line;
109cdf0e10cSrcweir sal_uInt32 m_Offset;
110cdf0e10cSrcweir sal_uInt32 m_Len;
111cdf0e10cSrcweir sal_uInt32 m_NoEntries;
112cdf0e10cSrcweir sal_uInt32 m_MaxEntries;
113cdf0e10cSrcweir osl_TProfileEntry* m_Entries;
114cdf0e10cSrcweir } osl_TProfileSection;
115cdf0e10cSrcweir
116cdf0e10cSrcweir
117cdf0e10cSrcweir /*
118cdf0e10cSrcweir Profile-data structure hidden behind oslProfile:
119cdf0e10cSrcweir */
120cdf0e10cSrcweir typedef struct _osl_TProfileImpl
121cdf0e10cSrcweir {
122cdf0e10cSrcweir sal_uInt32 m_Flags;
123cdf0e10cSrcweir osl_TFile* m_pFile;
124cdf0e10cSrcweir osl_TStamp m_Stamp;
125cdf0e10cSrcweir sal_Char m_FileName[PATH_MAX + 1];
126cdf0e10cSrcweir sal_uInt32 m_NoLines;
127cdf0e10cSrcweir sal_uInt32 m_MaxLines;
128cdf0e10cSrcweir sal_uInt32 m_NoSections;
129cdf0e10cSrcweir sal_uInt32 m_MaxSections;
130cdf0e10cSrcweir sal_Char** m_Lines;
131cdf0e10cSrcweir osl_TProfileSection* m_Sections;
132cdf0e10cSrcweir pthread_mutex_t m_AccessLock;
133cdf0e10cSrcweir sal_Bool m_bIsValid;
134cdf0e10cSrcweir } osl_TProfileImpl;
135cdf0e10cSrcweir
136cdf0e10cSrcweir
137cdf0e10cSrcweir /*****************************************************************************/
138cdf0e10cSrcweir /* Static Module Function Declarations */
139cdf0e10cSrcweir /*****************************************************************************/
140cdf0e10cSrcweir
141cdf0e10cSrcweir static osl_TFile* openFileImpl(const sal_Char* pszFilename, oslProfileOption ProfileFlags);
142cdf0e10cSrcweir static osl_TStamp closeFileImpl(osl_TFile* pFile, oslProfileOption Flags);
143cdf0e10cSrcweir static sal_Bool OslProfile_lockFile(const osl_TFile* pFile, osl_TLockMode eMode);
144cdf0e10cSrcweir static sal_Bool OslProfile_rewindFile(osl_TFile* pFile, sal_Bool bTruncate);
145cdf0e10cSrcweir static osl_TStamp OslProfile_getFileStamp(osl_TFile* pFile);
146cdf0e10cSrcweir
147cdf0e10cSrcweir static sal_Char* OslProfile_getLine(osl_TFile* pFile);
148cdf0e10cSrcweir static sal_Bool OslProfile_putLine(osl_TFile* pFile, const sal_Char *pszLine);
149cdf0e10cSrcweir static sal_Char* stripBlanks(sal_Char* String, sal_uInt32* pLen);
150cdf0e10cSrcweir static sal_Char* addLine(osl_TProfileImpl* pProfile, const sal_Char* Line);
151cdf0e10cSrcweir static sal_Char* insertLine(osl_TProfileImpl* pProfile, const sal_Char* Line, sal_uInt32 LineNo);
152cdf0e10cSrcweir static void removeLine(osl_TProfileImpl* pProfile, sal_uInt32 LineNo);
153cdf0e10cSrcweir static void setEntry(osl_TProfileImpl* pProfile, osl_TProfileSection* pSection,
154cdf0e10cSrcweir sal_uInt32 NoEntry, sal_uInt32 Line,
155cdf0e10cSrcweir sal_Char* Entry, sal_uInt32 Len);
156cdf0e10cSrcweir static sal_Bool addEntry(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection,
157cdf0e10cSrcweir int Line, sal_Char* Entry, sal_uInt32 Len);
158cdf0e10cSrcweir static void removeEntry(osl_TProfileSection *pSection, sal_uInt32 NoEntry);
159cdf0e10cSrcweir static sal_Bool addSection(osl_TProfileImpl* pProfile, int Line, const sal_Char* Section, sal_uInt32 Len);
160cdf0e10cSrcweir static void removeSection(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection);
161cdf0e10cSrcweir static osl_TProfileSection* findEntry(osl_TProfileImpl* pProfile, const sal_Char* Section,
162cdf0e10cSrcweir const sal_Char* Entry, sal_uInt32 *pNoEntry);
163cdf0e10cSrcweir static sal_Bool loadProfile(osl_TFile* pFile, osl_TProfileImpl* pProfile);
164cdf0e10cSrcweir static sal_Bool storeProfile(osl_TProfileImpl* pProfile, sal_Bool bCleanup);
165cdf0e10cSrcweir static osl_TProfileImpl* acquireProfile(oslProfile Profile, sal_Bool bWriteable);
166cdf0e10cSrcweir static sal_Bool releaseProfile(osl_TProfileImpl* pProfile);
167cdf0e10cSrcweir
168cdf0e10cSrcweir static sal_Bool writeProfileImpl (osl_TFile* pFile);
169cdf0e10cSrcweir static osl_TFile* osl_openTmpProfileImpl(osl_TProfileImpl*);
170cdf0e10cSrcweir static sal_Bool osl_ProfileSwapProfileNames(osl_TProfileImpl*);
171cdf0e10cSrcweir static void osl_ProfileGenerateExtension(sal_Char* pszFileName, sal_Char* pszExtension, sal_Char* pszTmpName);
172cdf0e10cSrcweir static oslProfile SAL_CALL osl_psz_openProfile(const sal_Char *pszProfileName, oslProfileOption Flags);
173cdf0e10cSrcweir
174cdf0e10cSrcweir /* implemented in file.c */
175cdf0e10cSrcweir extern oslFileError FileURLToPath( char *, size_t, rtl_uString* );
176cdf0e10cSrcweir
177cdf0e10cSrcweir /*****************************************************************************/
178cdf0e10cSrcweir /* Exported Module Functions */
179cdf0e10cSrcweir /*****************************************************************************/
osl_openProfile(rtl_uString * ustrProfileName,oslProfileOption Options)180cdf0e10cSrcweir oslProfile SAL_CALL osl_openProfile(rtl_uString *ustrProfileName, oslProfileOption Options)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir char profilePath[PATH_MAX] = "";
183cdf0e10cSrcweir
184cdf0e10cSrcweir if ( ustrProfileName != 0 && ustrProfileName->buffer[0] != 0 )
185cdf0e10cSrcweir FileURLToPath( profilePath, PATH_MAX, ustrProfileName );
186cdf0e10cSrcweir
187cdf0e10cSrcweir return osl_psz_openProfile( profilePath,Options );
188cdf0e10cSrcweir }
189cdf0e10cSrcweir
190cdf0e10cSrcweir
osl_psz_openProfile(const sal_Char * pszProfileName,oslProfileOption Flags)191cdf0e10cSrcweir static oslProfile SAL_CALL osl_psz_openProfile(const sal_Char *pszProfileName, oslProfileOption Flags)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir osl_TFile* pFile;
194cdf0e10cSrcweir osl_TProfileImpl* pProfile;
195cdf0e10cSrcweir sal_Char Filename[PATH_MAX];
196cdf0e10cSrcweir sal_Bool bRet = sal_False;
197cdf0e10cSrcweir
198cdf0e10cSrcweir Filename[0] = '\0';
199cdf0e10cSrcweir
200cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
201cdf0e10cSrcweir OSL_TRACE("In osl_openProfile\n");
202cdf0e10cSrcweir #endif
203cdf0e10cSrcweir
204cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
205cdf0e10cSrcweir Flags=osl_Profile_FLUSHWRITE;
206cdf0e10cSrcweir
207cdf0e10cSrcweir OSL_TRACE("opening '%s'\n",pszProfileName);
208cdf0e10cSrcweir if ( Flags == osl_Profile_DEFAULT )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir OSL_TRACE("with osl_Profile_DEFAULT\n");
211cdf0e10cSrcweir }
212cdf0e10cSrcweir if ( Flags & osl_Profile_SYSTEM )
213cdf0e10cSrcweir {
214cdf0e10cSrcweir OSL_TRACE("with osl_Profile_SYSTEM\n");
215cdf0e10cSrcweir }
216cdf0e10cSrcweir if ( Flags & osl_Profile_READLOCK )
217cdf0e10cSrcweir {
218cdf0e10cSrcweir OSL_TRACE("with osl_Profile_READLOCK\n");
219cdf0e10cSrcweir }
220cdf0e10cSrcweir if ( Flags & osl_Profile_WRITELOCK )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir OSL_TRACE("with osl_Profile_WRITELOCK\n");
223cdf0e10cSrcweir }
224cdf0e10cSrcweir if ( Flags & osl_Profile_FLUSHWRITE )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir OSL_TRACE("with osl_Profile_FLUSHWRITE\n");
227cdf0e10cSrcweir }
228cdf0e10cSrcweir #endif
229cdf0e10cSrcweir
230cdf0e10cSrcweir
231cdf0e10cSrcweir if ( ( pFile = openFileImpl(pszProfileName, Flags ) ) == NULL )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
234cdf0e10cSrcweir OSL_TRACE("Out osl_openProfile [not opened]\n");
235cdf0e10cSrcweir #endif
236cdf0e10cSrcweir return (NULL);
237cdf0e10cSrcweir }
238cdf0e10cSrcweir
239cdf0e10cSrcweir
240cdf0e10cSrcweir pProfile = (osl_TProfileImpl*)calloc(1, sizeof(osl_TProfileImpl));
241cdf0e10cSrcweir
242cdf0e10cSrcweir if ( pProfile == 0 )
243cdf0e10cSrcweir {
244cdf0e10cSrcweir return 0;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir
247cdf0e10cSrcweir pProfile->m_Flags = Flags & FLG_USER;
248cdf0e10cSrcweir
249cdf0e10cSrcweir if ( Flags & ( osl_Profile_READLOCK | osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE ) )
250cdf0e10cSrcweir {
251cdf0e10cSrcweir pProfile->m_pFile = pFile;
252cdf0e10cSrcweir }
253cdf0e10cSrcweir
254cdf0e10cSrcweir pthread_mutex_init(&(pProfile->m_AccessLock),PTHREAD_MUTEXATTR_DEFAULT);
255cdf0e10cSrcweir pProfile->m_bIsValid=sal_True;
256cdf0e10cSrcweir
257cdf0e10cSrcweir pProfile->m_Stamp = OslProfile_getFileStamp(pFile);
258cdf0e10cSrcweir bRet=loadProfile(pFile, pProfile);
259cdf0e10cSrcweir bRet &= realpath(pszProfileName, pProfile->m_FileName) != NULL;
260cdf0e10cSrcweir OSL_ASSERT(bRet);
261cdf0e10cSrcweir
262cdf0e10cSrcweir if (pProfile->m_pFile == NULL)
263cdf0e10cSrcweir closeFileImpl(pFile,pProfile->m_Flags);
264cdf0e10cSrcweir
265cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
266cdf0e10cSrcweir OSL_TRACE("Out osl_openProfile [ok]\n");
267cdf0e10cSrcweir #endif
268cdf0e10cSrcweir return (pProfile);
269cdf0e10cSrcweir }
270cdf0e10cSrcweir
osl_closeProfile(oslProfile Profile)271cdf0e10cSrcweir sal_Bool SAL_CALL osl_closeProfile(oslProfile Profile)
272cdf0e10cSrcweir {
273cdf0e10cSrcweir osl_TProfileImpl* pProfile = (osl_TProfileImpl*)Profile;
274cdf0e10cSrcweir sal_Bool bRet = sal_False;
275cdf0e10cSrcweir
276cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
277cdf0e10cSrcweir OSL_TRACE("In osl_closeProfile\n");
278cdf0e10cSrcweir #endif
279cdf0e10cSrcweir
280cdf0e10cSrcweir if ( Profile == 0 )
281cdf0e10cSrcweir {
282cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
283cdf0e10cSrcweir OSL_TRACE("Out osl_closeProfile [profile==0]\n");
284cdf0e10cSrcweir #endif
285cdf0e10cSrcweir return sal_False;
286cdf0e10cSrcweir }
287cdf0e10cSrcweir
288cdf0e10cSrcweir pthread_mutex_lock(&(pProfile->m_AccessLock));
289cdf0e10cSrcweir
290cdf0e10cSrcweir if ( pProfile->m_bIsValid == sal_False )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir OSL_ASSERT(pProfile->m_bIsValid);
293cdf0e10cSrcweir pthread_mutex_unlock(&(pProfile->m_AccessLock));
294cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
295cdf0e10cSrcweir OSL_TRACE("Out osl_closeProfile [not valid]\n");
296cdf0e10cSrcweir #endif
297cdf0e10cSrcweir return sal_False;
298cdf0e10cSrcweir }
299cdf0e10cSrcweir
300cdf0e10cSrcweir pProfile->m_bIsValid=sal_False;
301cdf0e10cSrcweir
302cdf0e10cSrcweir if ( ! ( pProfile->m_Flags & osl_Profile_READLOCK ) && ( pProfile->m_Flags & FLG_MODIFIED ) )
303cdf0e10cSrcweir {
304cdf0e10cSrcweir pProfile = acquireProfile(Profile,sal_True);
305cdf0e10cSrcweir
306cdf0e10cSrcweir if ( pProfile != 0 )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir bRet=storeProfile(pProfile, sal_True);
309cdf0e10cSrcweir OSL_ASSERT(bRet);
310cdf0e10cSrcweir }
311cdf0e10cSrcweir }
312cdf0e10cSrcweir else
313cdf0e10cSrcweir {
314cdf0e10cSrcweir pProfile = acquireProfile(Profile,sal_False);
315cdf0e10cSrcweir }
316cdf0e10cSrcweir
317cdf0e10cSrcweir
318cdf0e10cSrcweir if ( pProfile == 0 )
319cdf0e10cSrcweir {
320cdf0e10cSrcweir pthread_mutex_unlock(&(pProfile->m_AccessLock));
321cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
322cdf0e10cSrcweir OSL_TRACE("Out osl_closeProfile [pProfile==0]\n");
323cdf0e10cSrcweir #endif
324cdf0e10cSrcweir return sal_False;
325cdf0e10cSrcweir }
326cdf0e10cSrcweir
327cdf0e10cSrcweir if (pProfile->m_pFile != NULL)
328cdf0e10cSrcweir closeFileImpl(pProfile->m_pFile,pProfile->m_Flags);
329cdf0e10cSrcweir
330cdf0e10cSrcweir pProfile->m_pFile = NULL;
331cdf0e10cSrcweir pProfile->m_FileName[0] = '\0';
332cdf0e10cSrcweir
333cdf0e10cSrcweir /* release whole profile data types memory */
334cdf0e10cSrcweir if ( pProfile->m_NoLines > 0)
335cdf0e10cSrcweir {
336cdf0e10cSrcweir unsigned int idx=0;
337cdf0e10cSrcweir if ( pProfile->m_Lines != 0 )
338cdf0e10cSrcweir {
339cdf0e10cSrcweir for ( idx = 0 ; idx < pProfile->m_NoLines ; ++idx)
340cdf0e10cSrcweir {
341cdf0e10cSrcweir if ( pProfile->m_Lines[idx] != 0 )
342cdf0e10cSrcweir {
343cdf0e10cSrcweir free(pProfile->m_Lines[idx]);
344cdf0e10cSrcweir pProfile->m_Lines[idx]=0;
345cdf0e10cSrcweir }
346cdf0e10cSrcweir }
347cdf0e10cSrcweir free(pProfile->m_Lines);
348cdf0e10cSrcweir pProfile->m_Lines=0;
349cdf0e10cSrcweir }
350cdf0e10cSrcweir if ( pProfile->m_Sections != 0 )
351cdf0e10cSrcweir {
352cdf0e10cSrcweir /*osl_TProfileSection* pSections=pProfile->m_Sections;*/
353cdf0e10cSrcweir for ( idx = 0 ; idx < pProfile->m_NoSections ; ++idx )
354cdf0e10cSrcweir {
355cdf0e10cSrcweir if ( pProfile->m_Sections[idx].m_Entries != 0 )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir free(pProfile->m_Sections[idx].m_Entries);
358cdf0e10cSrcweir pProfile->m_Sections[idx].m_Entries=0;
359cdf0e10cSrcweir }
360cdf0e10cSrcweir }
361cdf0e10cSrcweir free(pProfile->m_Sections);
362cdf0e10cSrcweir pProfile->m_Sections=0;
363cdf0e10cSrcweir }
364cdf0e10cSrcweir }
365cdf0e10cSrcweir
366cdf0e10cSrcweir pthread_mutex_unlock(&(pProfile->m_AccessLock));
367cdf0e10cSrcweir
368cdf0e10cSrcweir pthread_mutex_destroy(&(pProfile->m_AccessLock));
369cdf0e10cSrcweir
370cdf0e10cSrcweir free(pProfile);
371cdf0e10cSrcweir
372cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
373cdf0e10cSrcweir OSL_TRACE("Out osl_closeProfile [ok]\n");
374cdf0e10cSrcweir #endif
375cdf0e10cSrcweir return (sal_True);
376cdf0e10cSrcweir }
377cdf0e10cSrcweir
378cdf0e10cSrcweir
osl_flushProfile(oslProfile Profile)379cdf0e10cSrcweir sal_Bool SAL_CALL osl_flushProfile(oslProfile Profile)
380cdf0e10cSrcweir {
381cdf0e10cSrcweir osl_TProfileImpl* pProfile = (osl_TProfileImpl*) Profile;
382cdf0e10cSrcweir osl_TFile* pFile;
383cdf0e10cSrcweir sal_Bool bRet = sal_False;
384cdf0e10cSrcweir
385cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
386cdf0e10cSrcweir OSL_TRACE("In osl_flushProfile()\n");
387cdf0e10cSrcweir #endif
388cdf0e10cSrcweir
389cdf0e10cSrcweir if ( pProfile == 0 )
390cdf0e10cSrcweir {
391cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
392cdf0e10cSrcweir OSL_TRACE("Out osl_flushProfile() [pProfile == 0]\n");
393cdf0e10cSrcweir #endif
394cdf0e10cSrcweir return sal_False;
395cdf0e10cSrcweir }
396cdf0e10cSrcweir
397cdf0e10cSrcweir pthread_mutex_lock(&(pProfile->m_AccessLock));
398cdf0e10cSrcweir
399cdf0e10cSrcweir if ( pProfile->m_bIsValid == sal_False )
400cdf0e10cSrcweir {
401cdf0e10cSrcweir OSL_ASSERT(pProfile->m_bIsValid);
402cdf0e10cSrcweir pthread_mutex_unlock(&(pProfile->m_AccessLock));
403cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
404cdf0e10cSrcweir OSL_TRACE("Out osl_flushProfile [not valid]\n");
405cdf0e10cSrcweir #endif
406cdf0e10cSrcweir return sal_False;
407cdf0e10cSrcweir }
408cdf0e10cSrcweir
409cdf0e10cSrcweir pFile = pProfile->m_pFile;
410cdf0e10cSrcweir if ( !( pFile != 0 && pFile->m_Handle >= 0 ) )
411cdf0e10cSrcweir {
412cdf0e10cSrcweir pthread_mutex_unlock(&(pProfile->m_AccessLock));
413cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
414cdf0e10cSrcweir OSL_TRACE("Out osl_flushProfile() [invalid file]\n");
415cdf0e10cSrcweir #endif
416cdf0e10cSrcweir return sal_False;
417cdf0e10cSrcweir }
418cdf0e10cSrcweir
419cdf0e10cSrcweir if ( pProfile->m_Flags & FLG_MODIFIED )
420cdf0e10cSrcweir {
421cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
422cdf0e10cSrcweir OSL_TRACE("swapping to storeprofile\n");
423cdf0e10cSrcweir #endif
424cdf0e10cSrcweir bRet = storeProfile(pProfile,sal_False);
425cdf0e10cSrcweir OSL_ASSERT(bRet);
426cdf0e10cSrcweir }
427cdf0e10cSrcweir
428cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
429cdf0e10cSrcweir OSL_TRACE("Out osl_flushProfile() [ok]\n");
430cdf0e10cSrcweir #endif
431cdf0e10cSrcweir pthread_mutex_unlock(&(pProfile->m_AccessLock));
432cdf0e10cSrcweir return bRet;
433cdf0e10cSrcweir }
434cdf0e10cSrcweir
writeProfileImpl(osl_TFile * pFile)435cdf0e10cSrcweir static sal_Bool writeProfileImpl(osl_TFile* pFile)
436cdf0e10cSrcweir {
437cdf0e10cSrcweir int BytesWritten=0;
438cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
439cdf0e10cSrcweir unsigned int nLen=0;
440cdf0e10cSrcweir #endif
441cdf0e10cSrcweir
442cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
443cdf0e10cSrcweir OSL_TRACE("In osl_writeProfileImpl()\n");
444cdf0e10cSrcweir #endif
445cdf0e10cSrcweir
446cdf0e10cSrcweir if ( !( pFile != 0 && pFile->m_Handle >= 0 ) || ( pFile->m_pWriteBuf == 0 ) )
447cdf0e10cSrcweir {
448cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
449cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileImpl() [invalid args]\n");
450cdf0e10cSrcweir #endif
451cdf0e10cSrcweir return sal_False;
452cdf0e10cSrcweir }
453cdf0e10cSrcweir
454cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
455cdf0e10cSrcweir nLen=strlen(pFile->m_pWriteBuf);
456cdf0e10cSrcweir OSL_ASSERT(nLen == (pFile->m_nWriteBufLen - pFile->m_nWriteBufFree));
457cdf0e10cSrcweir #endif
458cdf0e10cSrcweir
459cdf0e10cSrcweir BytesWritten = write(pFile->m_Handle, pFile->m_pWriteBuf, pFile->m_nWriteBufLen - pFile->m_nWriteBufFree);
460cdf0e10cSrcweir
461cdf0e10cSrcweir if ( BytesWritten <= 0 )
462cdf0e10cSrcweir {
463cdf0e10cSrcweir OSL_TRACE("write failed '%s'\n",strerror(errno));
464cdf0e10cSrcweir return (sal_False);
465cdf0e10cSrcweir }
466cdf0e10cSrcweir
467cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
468cdf0e10cSrcweir OSL_ASSERT(
469cdf0e10cSrcweir BytesWritten >= 0 && SAL_INT_CAST(unsigned int, BytesWritten) == nLen);
470cdf0e10cSrcweir #endif
471cdf0e10cSrcweir
472cdf0e10cSrcweir free(pFile->m_pWriteBuf);
473cdf0e10cSrcweir pFile->m_pWriteBuf=0;
474cdf0e10cSrcweir pFile->m_nWriteBufLen=0;
475cdf0e10cSrcweir pFile->m_nWriteBufFree=0;
476cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
477cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileImpl() [ok]\n");
478cdf0e10cSrcweir #endif
479cdf0e10cSrcweir return sal_True;
480cdf0e10cSrcweir }
481cdf0e10cSrcweir
482cdf0e10cSrcweir
osl_readProfileString(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_Char * pszString,sal_uInt32 MaxLen,const sal_Char * pszDefault)483cdf0e10cSrcweir sal_Bool SAL_CALL osl_readProfileString(oslProfile Profile,
484cdf0e10cSrcweir const sal_Char* pszSection, const sal_Char* pszEntry,
485cdf0e10cSrcweir sal_Char* pszString, sal_uInt32 MaxLen,
486cdf0e10cSrcweir const sal_Char* pszDefault)
487cdf0e10cSrcweir {
488cdf0e10cSrcweir sal_uInt32 NoEntry;
489cdf0e10cSrcweir sal_Char* pStr=0;
490cdf0e10cSrcweir osl_TProfileSection* pSec;
491cdf0e10cSrcweir osl_TProfileImpl* pProfile=0;
492cdf0e10cSrcweir osl_TProfileImpl* pTmpProfile=0;
493cdf0e10cSrcweir sal_Bool bRet = sal_False;
494cdf0e10cSrcweir
495cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
496cdf0e10cSrcweir OSL_TRACE("In osl_readProfileString\n");
497cdf0e10cSrcweir #endif
498cdf0e10cSrcweir
499cdf0e10cSrcweir pTmpProfile = (osl_TProfileImpl*) Profile;
500cdf0e10cSrcweir
501cdf0e10cSrcweir if ( pTmpProfile == 0 )
502cdf0e10cSrcweir {
503cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
504cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileString [pTmpProfile==0]\n");
505cdf0e10cSrcweir #endif
506cdf0e10cSrcweir return sal_False;
507cdf0e10cSrcweir }
508cdf0e10cSrcweir
509cdf0e10cSrcweir pthread_mutex_lock(&(pTmpProfile->m_AccessLock));
510cdf0e10cSrcweir
511cdf0e10cSrcweir if ( pTmpProfile->m_bIsValid == sal_False )
512cdf0e10cSrcweir {
513cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
514cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
515cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileString [not valid]\n");
516cdf0e10cSrcweir #endif
517cdf0e10cSrcweir return sal_False;
518cdf0e10cSrcweir }
519cdf0e10cSrcweir
520cdf0e10cSrcweir pProfile = acquireProfile(Profile, sal_False);
521cdf0e10cSrcweir
522cdf0e10cSrcweir if ( pProfile == NULL )
523cdf0e10cSrcweir {
524cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
525cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileString [pProfile==0]\n");
526cdf0e10cSrcweir #endif
527cdf0e10cSrcweir return (sal_False);
528cdf0e10cSrcweir }
529cdf0e10cSrcweir
530cdf0e10cSrcweir if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
531cdf0e10cSrcweir {
532cdf0e10cSrcweir if (((pSec = findEntry(pProfile, pszSection, pszEntry, &NoEntry)) != NULL) &&
533cdf0e10cSrcweir (NoEntry < pSec->m_NoEntries) &&
534cdf0e10cSrcweir ((pStr = strchr(pProfile->m_Lines[pSec->m_Entries[NoEntry].m_Line],
535cdf0e10cSrcweir '=')) != NULL))
536cdf0e10cSrcweir {
537cdf0e10cSrcweir pStr++;
538cdf0e10cSrcweir }
539cdf0e10cSrcweir else
540cdf0e10cSrcweir {
541cdf0e10cSrcweir pStr=(sal_Char*)pszDefault;
542cdf0e10cSrcweir }
543cdf0e10cSrcweir
544cdf0e10cSrcweir if ( pStr != 0 )
545cdf0e10cSrcweir {
546cdf0e10cSrcweir pStr = stripBlanks(pStr, NULL);
547cdf0e10cSrcweir MaxLen = (MaxLen - 1 < strlen(pStr)) ? (MaxLen - 1) : strlen(pStr);
548cdf0e10cSrcweir pStr = stripBlanks(pStr, &MaxLen);
549cdf0e10cSrcweir strncpy(pszString, pStr, MaxLen);
550cdf0e10cSrcweir pszString[MaxLen] = '\0';
551cdf0e10cSrcweir }
552cdf0e10cSrcweir }
553cdf0e10cSrcweir else
554cdf0e10cSrcweir { /* not implemented */ }
555cdf0e10cSrcweir
556cdf0e10cSrcweir
557cdf0e10cSrcweir bRet=releaseProfile(pProfile);
558cdf0e10cSrcweir OSL_ASSERT(bRet);
559cdf0e10cSrcweir
560cdf0e10cSrcweir if ( pStr == 0 )
561cdf0e10cSrcweir {
562cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
563cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
564cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileString [pStr==0]\n");
565cdf0e10cSrcweir #endif
566cdf0e10cSrcweir return sal_False;
567cdf0e10cSrcweir }
568cdf0e10cSrcweir
569cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
570cdf0e10cSrcweir
571cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
572cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileString [ok]\n");
573cdf0e10cSrcweir #endif
574cdf0e10cSrcweir
575cdf0e10cSrcweir return (sal_True);
576cdf0e10cSrcweir }
577cdf0e10cSrcweir
578cdf0e10cSrcweir
osl_readProfileBool(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_Bool Default)579cdf0e10cSrcweir sal_Bool SAL_CALL osl_readProfileBool(oslProfile Profile,
580cdf0e10cSrcweir const sal_Char* pszSection, const sal_Char* pszEntry,
581cdf0e10cSrcweir sal_Bool Default)
582cdf0e10cSrcweir {
583cdf0e10cSrcweir sal_Char Line[32];
584cdf0e10cSrcweir Line[0] = '\0';
585cdf0e10cSrcweir
586cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
587cdf0e10cSrcweir OSL_TRACE("In osl_readProfileBool\n");
588cdf0e10cSrcweir #endif
589cdf0e10cSrcweir
590cdf0e10cSrcweir if (osl_readProfileString(Profile, pszSection, pszEntry, Line, sizeof(Line), ""))
591cdf0e10cSrcweir {
592cdf0e10cSrcweir if ((strcasecmp(Line, STR_INI_BOOLYES) == 0) ||
593cdf0e10cSrcweir (strcasecmp(Line, STR_INI_BOOLON) == 0) ||
594cdf0e10cSrcweir (strcasecmp(Line, STR_INI_BOOLONE) == 0))
595cdf0e10cSrcweir Default = sal_True;
596cdf0e10cSrcweir else
597cdf0e10cSrcweir if ((strcasecmp(Line, STR_INI_BOOLNO) == 0) ||
598cdf0e10cSrcweir (strcasecmp(Line, STR_INI_BOOLOFF) == 0) ||
599cdf0e10cSrcweir (strcasecmp(Line, STR_INI_BOOLZERO) == 0))
600cdf0e10cSrcweir Default = sal_False;
601cdf0e10cSrcweir }
602cdf0e10cSrcweir
603cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
604cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileBool [ok]\n");
605cdf0e10cSrcweir #endif
606cdf0e10cSrcweir
607cdf0e10cSrcweir return (Default);
608cdf0e10cSrcweir }
609cdf0e10cSrcweir
610cdf0e10cSrcweir
osl_readProfileIdent(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_uInt32 FirstId,const sal_Char * Strings[],sal_uInt32 Default)611cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_readProfileIdent(oslProfile Profile,
612cdf0e10cSrcweir const sal_Char* pszSection, const sal_Char* pszEntry,
613cdf0e10cSrcweir sal_uInt32 FirstId, const sal_Char* Strings[],
614cdf0e10cSrcweir sal_uInt32 Default)
615cdf0e10cSrcweir {
616cdf0e10cSrcweir sal_uInt32 i;
617cdf0e10cSrcweir sal_Char Line[256];
618cdf0e10cSrcweir Line[0] = '\0';
619cdf0e10cSrcweir
620cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
621cdf0e10cSrcweir OSL_TRACE("In osl_readProfileIdent\n");
622cdf0e10cSrcweir #endif
623cdf0e10cSrcweir
624cdf0e10cSrcweir if (osl_readProfileString(Profile, pszSection, pszEntry, Line, sizeof(Line), ""))
625cdf0e10cSrcweir {
626cdf0e10cSrcweir i = 0;
627cdf0e10cSrcweir while (Strings[i] != NULL)
628cdf0e10cSrcweir {
629cdf0e10cSrcweir if (strcasecmp(Line, Strings[i]) == 0)
630cdf0e10cSrcweir {
631cdf0e10cSrcweir Default = i + FirstId;
632cdf0e10cSrcweir break;
633cdf0e10cSrcweir }
634cdf0e10cSrcweir i++;
635cdf0e10cSrcweir }
636cdf0e10cSrcweir }
637cdf0e10cSrcweir
638cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
639cdf0e10cSrcweir OSL_TRACE("Out osl_readProfileIdent [ok]\n");
640cdf0e10cSrcweir #endif
641cdf0e10cSrcweir return (Default);
642cdf0e10cSrcweir }
643cdf0e10cSrcweir
osl_writeProfileString(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,const sal_Char * pszString)644cdf0e10cSrcweir sal_Bool SAL_CALL osl_writeProfileString(oslProfile Profile,
645cdf0e10cSrcweir const sal_Char* pszSection, const sal_Char* pszEntry,
646cdf0e10cSrcweir const sal_Char* pszString)
647cdf0e10cSrcweir {
648cdf0e10cSrcweir sal_uInt32 i;
649cdf0e10cSrcweir sal_Bool bRet = sal_False;
650cdf0e10cSrcweir sal_uInt32 NoEntry;
651cdf0e10cSrcweir sal_Char* pStr;
652cdf0e10cSrcweir sal_Char* Line = 0;
653cdf0e10cSrcweir osl_TProfileSection* pSec;
654cdf0e10cSrcweir osl_TProfileImpl* pProfile = 0;
655cdf0e10cSrcweir osl_TProfileImpl* pTmpProfile = 0;
656cdf0e10cSrcweir
657cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
658cdf0e10cSrcweir OSL_TRACE("In osl_writeProfileString\n");
659cdf0e10cSrcweir #endif
660cdf0e10cSrcweir
661cdf0e10cSrcweir pTmpProfile = (osl_TProfileImpl*) Profile;
662cdf0e10cSrcweir
663cdf0e10cSrcweir if ( pTmpProfile == 0 )
664cdf0e10cSrcweir {
665cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
666cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileString [pTmpProfile==0]\n");
667cdf0e10cSrcweir #endif
668cdf0e10cSrcweir return sal_False;
669cdf0e10cSrcweir }
670cdf0e10cSrcweir
671cdf0e10cSrcweir pthread_mutex_lock(&(pTmpProfile->m_AccessLock));
672cdf0e10cSrcweir
673cdf0e10cSrcweir if ( pTmpProfile->m_bIsValid == sal_False )
674cdf0e10cSrcweir {
675cdf0e10cSrcweir OSL_ASSERT(pTmpProfile->m_bIsValid);
676cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
677cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
678cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileString [not valid]\n");
679cdf0e10cSrcweir #endif
680cdf0e10cSrcweir return sal_False;
681cdf0e10cSrcweir }
682cdf0e10cSrcweir
683cdf0e10cSrcweir pProfile=acquireProfile(Profile, sal_True);
684cdf0e10cSrcweir
685cdf0e10cSrcweir if (pProfile == NULL)
686cdf0e10cSrcweir {
687cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
688cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
689cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileString [pProfile==0]\n");
690cdf0e10cSrcweir #endif
691cdf0e10cSrcweir return (sal_False);
692cdf0e10cSrcweir }
693cdf0e10cSrcweir
694cdf0e10cSrcweir Line = (sal_Char*) malloc(strlen(pszEntry)+strlen(pszString)+48);
695cdf0e10cSrcweir
696cdf0e10cSrcweir if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
697cdf0e10cSrcweir {
698cdf0e10cSrcweir if ((pSec = findEntry(pProfile, pszSection, pszEntry, &NoEntry)) == NULL)
699cdf0e10cSrcweir {
700cdf0e10cSrcweir Line[0] = '\0';
701cdf0e10cSrcweir addLine(pProfile, Line);
702cdf0e10cSrcweir
703cdf0e10cSrcweir Line[0] = '[';
704cdf0e10cSrcweir strcpy(&Line[1], pszSection);
705cdf0e10cSrcweir Line[1 + strlen(pszSection)] = ']';
706cdf0e10cSrcweir Line[2 + strlen(pszSection)] = '\0';
707cdf0e10cSrcweir
708cdf0e10cSrcweir if (((pStr = addLine(pProfile, Line)) == NULL) ||
709cdf0e10cSrcweir (! addSection(pProfile, pProfile->m_NoLines - 1, &pStr[1], strlen(pszSection))))
710cdf0e10cSrcweir {
711cdf0e10cSrcweir bRet=releaseProfile(pProfile);
712cdf0e10cSrcweir OSL_ASSERT(bRet);
713cdf0e10cSrcweir
714cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
715cdf0e10cSrcweir
716cdf0e10cSrcweir free(Line);
717cdf0e10cSrcweir
718cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
719cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileString [not added]\n");
720cdf0e10cSrcweir #endif
721cdf0e10cSrcweir return (sal_False);
722cdf0e10cSrcweir }
723cdf0e10cSrcweir
724cdf0e10cSrcweir pSec = &pProfile->m_Sections[pProfile->m_NoSections - 1];
725cdf0e10cSrcweir NoEntry = pSec->m_NoEntries;
726cdf0e10cSrcweir }
727cdf0e10cSrcweir
728cdf0e10cSrcweir Line[0] = '\0';
729cdf0e10cSrcweir strcpy(&Line[0], pszEntry);
730cdf0e10cSrcweir Line[0 + strlen(pszEntry)] = '=';
731cdf0e10cSrcweir strcpy(&Line[1 + strlen(pszEntry)], pszString);
732cdf0e10cSrcweir
733cdf0e10cSrcweir if (NoEntry >= pSec->m_NoEntries)
734cdf0e10cSrcweir {
735cdf0e10cSrcweir if (pSec->m_NoEntries > 0)
736cdf0e10cSrcweir i = pSec->m_Entries[pSec->m_NoEntries - 1].m_Line + 1;
737cdf0e10cSrcweir else
738cdf0e10cSrcweir i = pSec->m_Line + 1;
739cdf0e10cSrcweir
740cdf0e10cSrcweir if (((pStr = insertLine(pProfile, Line, i)) == NULL) ||
741cdf0e10cSrcweir (! addEntry(pProfile, pSec, i, pStr, strlen(pszEntry))))
742cdf0e10cSrcweir {
743cdf0e10cSrcweir bRet=releaseProfile(pProfile);
744cdf0e10cSrcweir OSL_ASSERT(bRet);
745cdf0e10cSrcweir
746cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
747cdf0e10cSrcweir free(Line);
748cdf0e10cSrcweir
749cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
750cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileString [not inserted]\n");
751cdf0e10cSrcweir #endif
752cdf0e10cSrcweir return (sal_False);
753cdf0e10cSrcweir }
754cdf0e10cSrcweir
755cdf0e10cSrcweir pProfile->m_Flags |= FLG_MODIFIED;
756cdf0e10cSrcweir }
757cdf0e10cSrcweir else
758cdf0e10cSrcweir {
759cdf0e10cSrcweir i = pSec->m_Entries[NoEntry].m_Line;
760cdf0e10cSrcweir free(pProfile->m_Lines[i]);
761cdf0e10cSrcweir pProfile->m_Lines[i] = strdup(Line);
762cdf0e10cSrcweir setEntry(pProfile, pSec, NoEntry, i, pProfile->m_Lines[i], strlen(pszEntry));
763cdf0e10cSrcweir
764cdf0e10cSrcweir pProfile->m_Flags |= FLG_MODIFIED;
765cdf0e10cSrcweir }
766cdf0e10cSrcweir }
767cdf0e10cSrcweir else {
768cdf0e10cSrcweir /* not implemented */
769cdf0e10cSrcweir }
770cdf0e10cSrcweir
771cdf0e10cSrcweir bRet = releaseProfile(pProfile);
772cdf0e10cSrcweir OSL_ASSERT(bRet);
773cdf0e10cSrcweir
774cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
775cdf0e10cSrcweir if ( Line!= 0 )
776cdf0e10cSrcweir {
777cdf0e10cSrcweir free(Line);
778cdf0e10cSrcweir }
779cdf0e10cSrcweir
780cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
781cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileString [ok]\n");
782cdf0e10cSrcweir #endif
783cdf0e10cSrcweir
784cdf0e10cSrcweir return bRet;
785cdf0e10cSrcweir }
786cdf0e10cSrcweir
787cdf0e10cSrcweir
osl_writeProfileBool(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_Bool Value)788cdf0e10cSrcweir sal_Bool SAL_CALL osl_writeProfileBool(oslProfile Profile,
789cdf0e10cSrcweir const sal_Char* pszSection, const sal_Char* pszEntry,
790cdf0e10cSrcweir sal_Bool Value)
791cdf0e10cSrcweir {
792cdf0e10cSrcweir sal_Bool bRet=sal_False;
793cdf0e10cSrcweir
794cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
795cdf0e10cSrcweir OSL_TRACE("In osl_writeProfileBool\n");
796cdf0e10cSrcweir #endif
797cdf0e10cSrcweir
798cdf0e10cSrcweir if (Value)
799cdf0e10cSrcweir bRet=osl_writeProfileString(Profile, pszSection, pszEntry, STR_INI_BOOLONE);
800cdf0e10cSrcweir else
801cdf0e10cSrcweir bRet=osl_writeProfileString(Profile, pszSection, pszEntry, STR_INI_BOOLZERO);
802cdf0e10cSrcweir
803cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
804cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileBool [ok]\n");
805cdf0e10cSrcweir #endif
806cdf0e10cSrcweir
807cdf0e10cSrcweir return bRet;
808cdf0e10cSrcweir }
809cdf0e10cSrcweir
810cdf0e10cSrcweir
osl_writeProfileIdent(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry,sal_uInt32 FirstId,const sal_Char * Strings[],sal_uInt32 Value)811cdf0e10cSrcweir sal_Bool SAL_CALL osl_writeProfileIdent(oslProfile Profile,
812cdf0e10cSrcweir const sal_Char* pszSection, const sal_Char* pszEntry,
813cdf0e10cSrcweir sal_uInt32 FirstId, const sal_Char* Strings[],
814cdf0e10cSrcweir sal_uInt32 Value)
815cdf0e10cSrcweir {
816cdf0e10cSrcweir int i, n;
817cdf0e10cSrcweir sal_Bool bRet=sal_False;
818cdf0e10cSrcweir
819cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
820cdf0e10cSrcweir OSL_TRACE("In osl_writeProfileIdent\n");
821cdf0e10cSrcweir #endif
822cdf0e10cSrcweir
823cdf0e10cSrcweir for (n = 0; Strings[n] != NULL; n++);
824cdf0e10cSrcweir
825cdf0e10cSrcweir if ((i = Value - FirstId) >= n)
826cdf0e10cSrcweir bRet=sal_False;
827cdf0e10cSrcweir else
828cdf0e10cSrcweir bRet = osl_writeProfileString(Profile, pszSection, pszEntry, Strings[i]);
829cdf0e10cSrcweir
830cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
831cdf0e10cSrcweir OSL_TRACE("Out osl_writeProfileIdent\n");
832cdf0e10cSrcweir #endif
833cdf0e10cSrcweir return bRet;
834cdf0e10cSrcweir }
835cdf0e10cSrcweir
836cdf0e10cSrcweir
osl_removeProfileEntry(oslProfile Profile,const sal_Char * pszSection,const sal_Char * pszEntry)837cdf0e10cSrcweir sal_Bool SAL_CALL osl_removeProfileEntry(oslProfile Profile,
838cdf0e10cSrcweir const sal_Char *pszSection, const sal_Char *pszEntry)
839cdf0e10cSrcweir {
840cdf0e10cSrcweir sal_uInt32 NoEntry;
841cdf0e10cSrcweir osl_TProfileSection* pSec;
842cdf0e10cSrcweir osl_TProfileImpl* pProfile = 0;
843cdf0e10cSrcweir osl_TProfileImpl* pTmpProfile = 0;
844cdf0e10cSrcweir sal_Bool bRet = sal_False;
845cdf0e10cSrcweir
846cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
847cdf0e10cSrcweir OSL_TRACE("In osl_removeProfileEntry\n");
848cdf0e10cSrcweir #endif
849cdf0e10cSrcweir
850cdf0e10cSrcweir pTmpProfile = (osl_TProfileImpl*) Profile;
851cdf0e10cSrcweir
852cdf0e10cSrcweir if ( pTmpProfile == 0 )
853cdf0e10cSrcweir {
854cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
855cdf0e10cSrcweir OSL_TRACE("Out osl_removeProfileEntry [pProfile==0]\n");
856cdf0e10cSrcweir #endif
857cdf0e10cSrcweir return sal_False;
858cdf0e10cSrcweir }
859cdf0e10cSrcweir
860cdf0e10cSrcweir pthread_mutex_lock(&(pTmpProfile->m_AccessLock));
861cdf0e10cSrcweir
862cdf0e10cSrcweir if ( pTmpProfile->m_bIsValid == sal_False )
863cdf0e10cSrcweir {
864cdf0e10cSrcweir OSL_ASSERT(pTmpProfile->m_bIsValid);
865cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
866cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
867cdf0e10cSrcweir OSL_TRACE("Out osl_removeProfileEntry [not valid]\n");
868cdf0e10cSrcweir #endif
869cdf0e10cSrcweir return sal_False;
870cdf0e10cSrcweir }
871cdf0e10cSrcweir
872cdf0e10cSrcweir
873cdf0e10cSrcweir pProfile = acquireProfile(Profile, sal_True);
874cdf0e10cSrcweir
875cdf0e10cSrcweir if (pProfile == NULL)
876cdf0e10cSrcweir {
877cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
878cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
879cdf0e10cSrcweir OSL_TRACE("Out osl_removeProfileEntry [pProfile==0]\n");
880cdf0e10cSrcweir #endif
881cdf0e10cSrcweir return (sal_False);
882cdf0e10cSrcweir }
883cdf0e10cSrcweir
884cdf0e10cSrcweir
885cdf0e10cSrcweir if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
886cdf0e10cSrcweir {
887cdf0e10cSrcweir if (((pSec = findEntry(pProfile, pszSection, pszEntry, &NoEntry)) != NULL) &&
888cdf0e10cSrcweir (NoEntry < pSec->m_NoEntries))
889cdf0e10cSrcweir {
890cdf0e10cSrcweir removeLine(pProfile, pSec->m_Entries[NoEntry].m_Line);
891cdf0e10cSrcweir removeEntry(pSec, NoEntry);
892cdf0e10cSrcweir if (pSec->m_NoEntries == 0)
893cdf0e10cSrcweir {
894cdf0e10cSrcweir removeLine(pProfile, pSec->m_Line);
895cdf0e10cSrcweir
896cdf0e10cSrcweir /* remove any empty separation line */
897cdf0e10cSrcweir if ((pSec->m_Line > 0) && (pProfile->m_Lines[pSec->m_Line - 1][0] == '\0'))
898cdf0e10cSrcweir removeLine(pProfile, pSec->m_Line - 1);
899cdf0e10cSrcweir
900cdf0e10cSrcweir removeSection(pProfile, pSec);
901cdf0e10cSrcweir }
902cdf0e10cSrcweir
903cdf0e10cSrcweir pProfile->m_Flags |= FLG_MODIFIED;
904cdf0e10cSrcweir }
905cdf0e10cSrcweir }
906cdf0e10cSrcweir else
907cdf0e10cSrcweir { /* not implemented */ }
908cdf0e10cSrcweir
909cdf0e10cSrcweir
910cdf0e10cSrcweir bRet = releaseProfile(pProfile);
911cdf0e10cSrcweir OSL_ASSERT(bRet);
912cdf0e10cSrcweir
913cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
914cdf0e10cSrcweir
915cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
916cdf0e10cSrcweir OSL_TRACE("Out osl_removeProfileEntry [ok]\n");
917cdf0e10cSrcweir #endif
918cdf0e10cSrcweir return bRet;
919cdf0e10cSrcweir }
920cdf0e10cSrcweir
921cdf0e10cSrcweir
osl_getProfileSectionEntries(oslProfile Profile,const sal_Char * pszSection,sal_Char * pszBuffer,sal_uInt32 MaxLen)922cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_getProfileSectionEntries(oslProfile Profile, const sal_Char *pszSection,
923cdf0e10cSrcweir sal_Char* pszBuffer, sal_uInt32 MaxLen)
924cdf0e10cSrcweir {
925cdf0e10cSrcweir sal_uInt32 i, n = 0;
926cdf0e10cSrcweir sal_uInt32 NoEntry;
927cdf0e10cSrcweir osl_TProfileSection* pSec;
928cdf0e10cSrcweir osl_TProfileImpl* pProfile = 0;
929cdf0e10cSrcweir osl_TProfileImpl* pTmpProfile = 0;
930cdf0e10cSrcweir sal_Bool bRet = sal_False;
931cdf0e10cSrcweir
932cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
933cdf0e10cSrcweir OSL_TRACE("In osl_getProfileSectionEntries\n");
934cdf0e10cSrcweir #endif
935cdf0e10cSrcweir
936cdf0e10cSrcweir pTmpProfile = (osl_TProfileImpl*) Profile;
937cdf0e10cSrcweir
938cdf0e10cSrcweir if ( pTmpProfile == 0 )
939cdf0e10cSrcweir {
940cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
941cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSectionEntries [pTmpProfile==0]\n");
942cdf0e10cSrcweir #endif
943cdf0e10cSrcweir return sal_False;
944cdf0e10cSrcweir
945cdf0e10cSrcweir }
946cdf0e10cSrcweir
947cdf0e10cSrcweir pthread_mutex_lock(&(pTmpProfile->m_AccessLock));
948cdf0e10cSrcweir
949cdf0e10cSrcweir if ( pTmpProfile->m_bIsValid == sal_False )
950cdf0e10cSrcweir {
951cdf0e10cSrcweir OSL_ASSERT(pTmpProfile->m_bIsValid);
952cdf0e10cSrcweir
953cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
954cdf0e10cSrcweir
955cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
956cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSectionEntries [not valid]\n");
957cdf0e10cSrcweir #endif
958cdf0e10cSrcweir
959cdf0e10cSrcweir return sal_False;
960cdf0e10cSrcweir }
961cdf0e10cSrcweir
962cdf0e10cSrcweir pProfile = acquireProfile(Profile, sal_False);
963cdf0e10cSrcweir
964cdf0e10cSrcweir if (pProfile == NULL)
965cdf0e10cSrcweir {
966cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
967cdf0e10cSrcweir
968cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
969cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSectionEntries [pProfile=0]\n");
970cdf0e10cSrcweir #endif
971cdf0e10cSrcweir
972cdf0e10cSrcweir return (0);
973cdf0e10cSrcweir }
974cdf0e10cSrcweir
975cdf0e10cSrcweir
976cdf0e10cSrcweir if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
977cdf0e10cSrcweir {
978cdf0e10cSrcweir if ((pSec = findEntry(pProfile, pszSection, "", &NoEntry)) != NULL)
979cdf0e10cSrcweir {
980cdf0e10cSrcweir if (MaxLen != 0)
981cdf0e10cSrcweir {
982cdf0e10cSrcweir for (i = 0; i < pSec->m_NoEntries; i++)
983cdf0e10cSrcweir {
984cdf0e10cSrcweir if ((n + pSec->m_Entries[i].m_Len + 1) < MaxLen)
985cdf0e10cSrcweir {
986cdf0e10cSrcweir strncpy(&pszBuffer[n], &pProfile->m_Lines[pSec->m_Entries[i].m_Line]
987cdf0e10cSrcweir [pSec->m_Entries[i].m_Offset], pSec->m_Entries[i].m_Len);
988cdf0e10cSrcweir n += pSec->m_Entries[i].m_Len;
989cdf0e10cSrcweir pszBuffer[n++] = '\0';
990cdf0e10cSrcweir }
991cdf0e10cSrcweir else
992cdf0e10cSrcweir break;
993cdf0e10cSrcweir
994cdf0e10cSrcweir }
995cdf0e10cSrcweir
996cdf0e10cSrcweir pszBuffer[n++] = '\0';
997cdf0e10cSrcweir }
998cdf0e10cSrcweir else
999cdf0e10cSrcweir {
1000cdf0e10cSrcweir for (i = 0; i < pSec->m_NoEntries; i++)
1001cdf0e10cSrcweir n += pSec->m_Entries[i].m_Len + 1;
1002cdf0e10cSrcweir
1003cdf0e10cSrcweir n += 1;
1004cdf0e10cSrcweir }
1005cdf0e10cSrcweir }
1006cdf0e10cSrcweir else
1007cdf0e10cSrcweir n = 0;
1008cdf0e10cSrcweir }
1009cdf0e10cSrcweir else {
1010cdf0e10cSrcweir /* not implemented */
1011cdf0e10cSrcweir }
1012cdf0e10cSrcweir
1013cdf0e10cSrcweir bRet=releaseProfile(pProfile);
1014cdf0e10cSrcweir OSL_ASSERT(bRet);
1015cdf0e10cSrcweir
1016cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
1017cdf0e10cSrcweir
1018cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1019cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSectionEntries [ok]\n");
1020cdf0e10cSrcweir #endif
1021cdf0e10cSrcweir
1022cdf0e10cSrcweir return (n);
1023cdf0e10cSrcweir }
1024cdf0e10cSrcweir
osl_getProfileSections(oslProfile Profile,sal_Char * pszBuffer,sal_uInt32 MaxLen)1025cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_getProfileSections(oslProfile Profile, sal_Char* pszBuffer, sal_uInt32 MaxLen)
1026cdf0e10cSrcweir {
1027cdf0e10cSrcweir sal_uInt32 i, n = 0;
1028cdf0e10cSrcweir osl_TProfileSection* pSec;
1029cdf0e10cSrcweir osl_TProfileImpl* pProfile = 0;
1030cdf0e10cSrcweir osl_TProfileImpl* pTmpProfile = 0;
1031cdf0e10cSrcweir sal_Bool bRet = sal_False;
1032cdf0e10cSrcweir
1033cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1034cdf0e10cSrcweir OSL_TRACE("In osl_getProfileSections\n");
1035cdf0e10cSrcweir #endif
1036cdf0e10cSrcweir
1037cdf0e10cSrcweir pTmpProfile = (osl_TProfileImpl*) Profile;
1038cdf0e10cSrcweir
1039cdf0e10cSrcweir if ( pTmpProfile == 0 )
1040cdf0e10cSrcweir {
1041cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1042cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSections [pTmpProfile==0]\n");
1043cdf0e10cSrcweir #endif
1044cdf0e10cSrcweir return sal_False;
1045cdf0e10cSrcweir }
1046cdf0e10cSrcweir
1047cdf0e10cSrcweir pthread_mutex_lock(&(pTmpProfile->m_AccessLock));
1048cdf0e10cSrcweir
1049cdf0e10cSrcweir if ( pTmpProfile->m_bIsValid == sal_False )
1050cdf0e10cSrcweir {
1051cdf0e10cSrcweir OSL_ASSERT(pTmpProfile->m_bIsValid);
1052cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
1053cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1054cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSections [not valid]\n");
1055cdf0e10cSrcweir #endif
1056cdf0e10cSrcweir return sal_False;
1057cdf0e10cSrcweir }
1058cdf0e10cSrcweir
1059cdf0e10cSrcweir pProfile = acquireProfile(Profile, sal_False);
1060cdf0e10cSrcweir
1061cdf0e10cSrcweir if (pProfile == NULL)
1062cdf0e10cSrcweir {
1063cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
1064cdf0e10cSrcweir
1065cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1066cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSections [pProfile==0]\n");
1067cdf0e10cSrcweir #endif
1068cdf0e10cSrcweir return (0);
1069cdf0e10cSrcweir }
1070cdf0e10cSrcweir
1071cdf0e10cSrcweir if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
1072cdf0e10cSrcweir {
1073cdf0e10cSrcweir if (MaxLen != 0)
1074cdf0e10cSrcweir {
1075cdf0e10cSrcweir for (i = 0; i < pProfile->m_NoSections; i++)
1076cdf0e10cSrcweir {
1077cdf0e10cSrcweir pSec = &pProfile->m_Sections[i];
1078cdf0e10cSrcweir
1079cdf0e10cSrcweir if ((n + pSec->m_Len + 1) < MaxLen)
1080cdf0e10cSrcweir {
1081cdf0e10cSrcweir strncpy(&pszBuffer[n], &pProfile->m_Lines[pSec->m_Line][pSec->m_Offset],
1082cdf0e10cSrcweir pSec->m_Len);
1083cdf0e10cSrcweir n += pSec->m_Len;
1084cdf0e10cSrcweir pszBuffer[n++] = '\0';
1085cdf0e10cSrcweir }
1086cdf0e10cSrcweir else
1087cdf0e10cSrcweir break;
1088cdf0e10cSrcweir }
1089cdf0e10cSrcweir
1090cdf0e10cSrcweir pszBuffer[n++] = '\0';
1091cdf0e10cSrcweir }
1092cdf0e10cSrcweir else
1093cdf0e10cSrcweir {
1094cdf0e10cSrcweir for (i = 0; i < pProfile->m_NoSections; i++)
1095cdf0e10cSrcweir n += pProfile->m_Sections[i].m_Len + 1;
1096cdf0e10cSrcweir
1097cdf0e10cSrcweir n += 1;
1098cdf0e10cSrcweir }
1099cdf0e10cSrcweir }
1100cdf0e10cSrcweir else
1101cdf0e10cSrcweir { /* not implemented */ }
1102cdf0e10cSrcweir
1103cdf0e10cSrcweir
1104cdf0e10cSrcweir bRet=releaseProfile(pProfile);
1105cdf0e10cSrcweir OSL_ASSERT(bRet);
1106cdf0e10cSrcweir
1107cdf0e10cSrcweir pthread_mutex_unlock(&(pTmpProfile->m_AccessLock));
1108cdf0e10cSrcweir
1109cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1110cdf0e10cSrcweir OSL_TRACE("Out osl_getProfileSections [ok]\n");
1111cdf0e10cSrcweir #endif
1112cdf0e10cSrcweir
1113cdf0e10cSrcweir return (n);
1114cdf0e10cSrcweir }
1115cdf0e10cSrcweir
1116cdf0e10cSrcweir /*****************************************************************************/
1117cdf0e10cSrcweir /* Static Module Functions */
1118cdf0e10cSrcweir /*****************************************************************************/
1119cdf0e10cSrcweir
OslProfile_getFileStamp(osl_TFile * pFile)1120cdf0e10cSrcweir static osl_TStamp OslProfile_getFileStamp(osl_TFile* pFile)
1121cdf0e10cSrcweir {
1122cdf0e10cSrcweir struct stat status;
1123cdf0e10cSrcweir
1124cdf0e10cSrcweir if ( (pFile->m_Handle < 0) || (fstat(pFile->m_Handle, &status) < 0) )
1125cdf0e10cSrcweir {
1126cdf0e10cSrcweir return (0);
1127cdf0e10cSrcweir }
1128cdf0e10cSrcweir
1129cdf0e10cSrcweir
1130cdf0e10cSrcweir return (status.st_mtime);
1131cdf0e10cSrcweir }
1132cdf0e10cSrcweir
OslProfile_lockFile(const osl_TFile * pFile,osl_TLockMode eMode)1133cdf0e10cSrcweir static sal_Bool OslProfile_lockFile(const osl_TFile* pFile, osl_TLockMode eMode)
1134cdf0e10cSrcweir {
1135cdf0e10cSrcweir struct flock lock;
1136cdf0e10cSrcweir /* boring hack, but initializers for static vars must be constant */
1137cdf0e10cSrcweir static sal_Bool bIsInitialized = sal_False;
1138cdf0e10cSrcweir static sal_Bool bLockingDisabled;
1139cdf0e10cSrcweir
1140cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1141cdf0e10cSrcweir OSL_TRACE("In OslProfile_lockFile\n");
1142cdf0e10cSrcweir #endif
1143cdf0e10cSrcweir
1144cdf0e10cSrcweir if ( !bIsInitialized )
1145cdf0e10cSrcweir {
1146cdf0e10cSrcweir sal_Char* pEnvValue;
1147cdf0e10cSrcweir pEnvValue = getenv( "STAR_PROFILE_LOCKING_DISABLED" );
1148cdf0e10cSrcweir
1149cdf0e10cSrcweir if ( pEnvValue == 0 )
1150cdf0e10cSrcweir {
1151cdf0e10cSrcweir bLockingDisabled = sal_False;
1152cdf0e10cSrcweir
1153cdf0e10cSrcweir }
1154cdf0e10cSrcweir else
1155cdf0e10cSrcweir {
1156cdf0e10cSrcweir bLockingDisabled = sal_True;
1157cdf0e10cSrcweir }
1158cdf0e10cSrcweir
1159cdf0e10cSrcweir bIsInitialized = sal_True;
1160cdf0e10cSrcweir }
1161cdf0e10cSrcweir
1162cdf0e10cSrcweir if (pFile->m_Handle < 0)
1163cdf0e10cSrcweir {
1164cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1165cdf0e10cSrcweir OSL_TRACE("Out OslProfile_lockFile [invalid file handle]\n");
1166cdf0e10cSrcweir #endif
1167cdf0e10cSrcweir return (sal_False);
1168cdf0e10cSrcweir }
1169cdf0e10cSrcweir
1170cdf0e10cSrcweir
1171cdf0e10cSrcweir if ( bLockingDisabled )
1172cdf0e10cSrcweir {
1173cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1174cdf0e10cSrcweir OSL_TRACE("Out OslProfile_lockFile [locking disabled]\n");
1175cdf0e10cSrcweir #endif
1176cdf0e10cSrcweir return (sal_True);
1177cdf0e10cSrcweir }
1178cdf0e10cSrcweir
1179cdf0e10cSrcweir
1180cdf0e10cSrcweir lock.l_start = 0;
1181cdf0e10cSrcweir lock.l_whence = SEEK_SET;
1182cdf0e10cSrcweir lock.l_len = 0;
1183cdf0e10cSrcweir
1184cdf0e10cSrcweir switch (eMode)
1185cdf0e10cSrcweir {
1186cdf0e10cSrcweir case un_lock:
1187cdf0e10cSrcweir lock.l_type = F_UNLCK;
1188cdf0e10cSrcweir break;
1189cdf0e10cSrcweir
1190cdf0e10cSrcweir case read_lock:
1191cdf0e10cSrcweir lock.l_type = F_RDLCK;
1192cdf0e10cSrcweir break;
1193cdf0e10cSrcweir
1194cdf0e10cSrcweir case write_lock:
1195cdf0e10cSrcweir lock.l_type = F_WRLCK;
1196cdf0e10cSrcweir break;
1197cdf0e10cSrcweir }
1198cdf0e10cSrcweir
1199cdf0e10cSrcweir #ifndef MACOSX // not MAC OSX
1200cdf0e10cSrcweir if ( fcntl(pFile->m_Handle, F_SETLKW, &lock) == -1 )
1201cdf0e10cSrcweir #else
1202cdf0e10cSrcweir /* Mac OSX will return ENOTSUP for webdav drives so we should ignore it */
1203cdf0e10cSrcweir if ( fcntl(pFile->m_Handle, F_SETLKW, &lock) == -1 && errno != ENOTSUP )
1204cdf0e10cSrcweir #endif /* MACOSX */
1205cdf0e10cSrcweir {
1206cdf0e10cSrcweir OSL_TRACE("fcntl returned -1 (%s)\n",strerror(errno));
1207cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1208cdf0e10cSrcweir OSL_TRACE("Out OslProfile_lockFile [fcntl F_SETLKW]\n");
1209cdf0e10cSrcweir #endif
1210cdf0e10cSrcweir return sal_False;
1211cdf0e10cSrcweir }
1212cdf0e10cSrcweir
1213cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1214cdf0e10cSrcweir OSL_TRACE("Out OslProfile_lockFile [ok]\n");
1215cdf0e10cSrcweir #endif
1216cdf0e10cSrcweir return sal_True;
1217cdf0e10cSrcweir }
1218cdf0e10cSrcweir
openFileImpl(const sal_Char * pszFilename,oslProfileOption ProfileFlags)1219cdf0e10cSrcweir static osl_TFile* openFileImpl(const sal_Char* pszFilename, oslProfileOption ProfileFlags )
1220cdf0e10cSrcweir {
1221cdf0e10cSrcweir int Flags;
1222cdf0e10cSrcweir osl_TFile* pFile = (osl_TFile*) calloc(1, sizeof(osl_TFile));
1223cdf0e10cSrcweir sal_Bool bWriteable = sal_False;
1224cdf0e10cSrcweir
1225cdf0e10cSrcweir if ( ProfileFlags & ( osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE ) )
1226cdf0e10cSrcweir {
1227cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1228cdf0e10cSrcweir OSL_TRACE("setting bWriteable to TRUE\n");
1229cdf0e10cSrcweir #endif
1230cdf0e10cSrcweir bWriteable=sal_True;
1231cdf0e10cSrcweir }
1232cdf0e10cSrcweir
1233cdf0e10cSrcweir if (! bWriteable)
1234cdf0e10cSrcweir {
1235cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1236cdf0e10cSrcweir OSL_TRACE("opening '%s' read only\n",pszFilename);
1237cdf0e10cSrcweir #endif
1238cdf0e10cSrcweir
1239cdf0e10cSrcweir pFile->m_Handle = open(pszFilename, O_RDONLY);
1240cdf0e10cSrcweir /* mfe: argghh!!! do not check if the file could be openend */
1241cdf0e10cSrcweir /* default mode expects it that way!!! */
1242cdf0e10cSrcweir }
1243cdf0e10cSrcweir else
1244cdf0e10cSrcweir {
1245cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1246cdf0e10cSrcweir OSL_TRACE("opening '%s' read/write\n",pszFilename);
1247cdf0e10cSrcweir #endif
1248cdf0e10cSrcweir if (((pFile->m_Handle = open(pszFilename, O_RDWR | O_CREAT | O_EXCL, DEFAULT_PMODE)) < 0) &&
1249cdf0e10cSrcweir ((pFile->m_Handle = open(pszFilename, O_RDWR)) < 0))
1250cdf0e10cSrcweir {
1251cdf0e10cSrcweir free(pFile);
1252cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1253cdf0e10cSrcweir OSL_TRACE("Out openFileImpl [open read/write]\n");
1254cdf0e10cSrcweir #endif
1255cdf0e10cSrcweir return (NULL);
1256cdf0e10cSrcweir }
1257cdf0e10cSrcweir }
1258cdf0e10cSrcweir
1259cdf0e10cSrcweir /* set close-on-exec flag */
1260cdf0e10cSrcweir if ((Flags = fcntl(pFile->m_Handle, F_GETFD, 0)) != -1)
1261cdf0e10cSrcweir {
1262cdf0e10cSrcweir Flags |= FD_CLOEXEC;
1263cdf0e10cSrcweir fcntl(pFile->m_Handle, F_SETFD, Flags);
1264cdf0e10cSrcweir }
1265cdf0e10cSrcweir
1266cdf0e10cSrcweir pFile->m_pWriteBuf=0;
1267cdf0e10cSrcweir pFile->m_nWriteBufFree=0;
1268cdf0e10cSrcweir pFile->m_nWriteBufLen=0;
1269cdf0e10cSrcweir
1270cdf0e10cSrcweir if ( ProfileFlags & (osl_Profile_WRITELOCK | osl_Profile_READLOCK ) )
1271cdf0e10cSrcweir {
1272cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1273cdf0e10cSrcweir OSL_TRACE("locking '%s' file\n",pszFilename);
1274cdf0e10cSrcweir #endif
1275cdf0e10cSrcweir OslProfile_lockFile(pFile, bWriteable ? write_lock : read_lock);
1276cdf0e10cSrcweir }
1277cdf0e10cSrcweir
1278cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1279cdf0e10cSrcweir OSL_TRACE("Out openFileImpl [ok]\n");
1280cdf0e10cSrcweir #endif
1281cdf0e10cSrcweir return (pFile);
1282cdf0e10cSrcweir }
1283cdf0e10cSrcweir
closeFileImpl(osl_TFile * pFile,oslProfileOption Flags)1284cdf0e10cSrcweir static osl_TStamp closeFileImpl(osl_TFile* pFile, oslProfileOption Flags)
1285cdf0e10cSrcweir {
1286cdf0e10cSrcweir osl_TStamp stamp = 0;
1287cdf0e10cSrcweir
1288cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1289cdf0e10cSrcweir OSL_TRACE("In closeFileImpl\n");
1290cdf0e10cSrcweir #endif
1291cdf0e10cSrcweir
1292cdf0e10cSrcweir if ( pFile == 0 )
1293cdf0e10cSrcweir {
1294cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1295cdf0e10cSrcweir OSL_TRACE("Out closeFileImpl [pFile == 0]\n");
1296cdf0e10cSrcweir #endif
1297cdf0e10cSrcweir return stamp;
1298cdf0e10cSrcweir }
1299cdf0e10cSrcweir
1300cdf0e10cSrcweir if ( pFile->m_Handle >= 0 )
1301cdf0e10cSrcweir {
1302cdf0e10cSrcweir stamp = OslProfile_getFileStamp(pFile);
1303cdf0e10cSrcweir
1304cdf0e10cSrcweir if ( Flags & (osl_Profile_WRITELOCK | osl_Profile_WRITELOCK ) )
1305cdf0e10cSrcweir {
1306cdf0e10cSrcweir OslProfile_lockFile(pFile, un_lock);
1307cdf0e10cSrcweir }
1308cdf0e10cSrcweir
1309cdf0e10cSrcweir close(pFile->m_Handle);
1310cdf0e10cSrcweir pFile->m_Handle = -1;
1311cdf0e10cSrcweir }
1312cdf0e10cSrcweir
1313cdf0e10cSrcweir
1314cdf0e10cSrcweir if ( pFile->m_pWriteBuf )
1315cdf0e10cSrcweir {
1316cdf0e10cSrcweir free(pFile->m_pWriteBuf);
1317cdf0e10cSrcweir }
1318cdf0e10cSrcweir
1319cdf0e10cSrcweir free(pFile);
1320cdf0e10cSrcweir
1321cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1322cdf0e10cSrcweir OSL_TRACE("Out closeFileImpl [ok]\n");
1323cdf0e10cSrcweir #endif
1324cdf0e10cSrcweir
1325cdf0e10cSrcweir return(stamp);
1326cdf0e10cSrcweir }
1327cdf0e10cSrcweir
OslProfile_rewindFile(osl_TFile * pFile,sal_Bool bTruncate)1328cdf0e10cSrcweir static sal_Bool OslProfile_rewindFile(osl_TFile* pFile, sal_Bool bTruncate)
1329cdf0e10cSrcweir {
1330cdf0e10cSrcweir sal_Bool bRet = sal_True;
1331cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1332cdf0e10cSrcweir OSL_TRACE("In osl_OslProfile_rewindFile\n");
1333cdf0e10cSrcweir #endif
1334cdf0e10cSrcweir
1335cdf0e10cSrcweir if (pFile->m_Handle >= 0)
1336cdf0e10cSrcweir {
1337cdf0e10cSrcweir pFile->m_pReadPtr = pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf);
1338cdf0e10cSrcweir
1339cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1340cdf0e10cSrcweir OSL_TRACE("rewinding\n");
1341cdf0e10cSrcweir #endif
1342cdf0e10cSrcweir bRet = (lseek(pFile->m_Handle, SEEK_SET, 0L) == 0L);
1343cdf0e10cSrcweir
1344cdf0e10cSrcweir if (bTruncate)
1345cdf0e10cSrcweir {
1346cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1347cdf0e10cSrcweir OSL_TRACE("truncating\n");
1348cdf0e10cSrcweir #endif
1349cdf0e10cSrcweir bRet &= (ftruncate(pFile->m_Handle, 0L) == 0);
1350cdf0e10cSrcweir }
1351cdf0e10cSrcweir
1352cdf0e10cSrcweir }
1353cdf0e10cSrcweir
1354cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1355cdf0e10cSrcweir OSL_TRACE("Out osl_OslProfile_rewindFile [ok]\n");
1356cdf0e10cSrcweir #endif
1357cdf0e10cSrcweir return bRet;
1358cdf0e10cSrcweir }
1359cdf0e10cSrcweir
1360cdf0e10cSrcweir
OslProfile_getLine(osl_TFile * pFile)1361cdf0e10cSrcweir static sal_Char* OslProfile_getLine(osl_TFile* pFile)
1362cdf0e10cSrcweir {
1363cdf0e10cSrcweir int Max, Free, Bytes, nLineBytes = 0;
1364cdf0e10cSrcweir sal_Char* pChr;
1365cdf0e10cSrcweir sal_Char* pLine = NULL;
1366cdf0e10cSrcweir sal_Char* pNewLine;
1367cdf0e10cSrcweir
1368cdf0e10cSrcweir if ( pFile == 0 )
1369cdf0e10cSrcweir {
1370cdf0e10cSrcweir return 0;
1371cdf0e10cSrcweir }
1372cdf0e10cSrcweir
1373cdf0e10cSrcweir if (pFile->m_Handle < 0)
1374cdf0e10cSrcweir return NULL;
1375cdf0e10cSrcweir
1376cdf0e10cSrcweir do
1377cdf0e10cSrcweir {
1378cdf0e10cSrcweir Bytes = sizeof(pFile->m_ReadBuf) - (pFile->m_pReadPtr - pFile->m_ReadBuf);
1379cdf0e10cSrcweir
1380cdf0e10cSrcweir if (Bytes <= 1)
1381cdf0e10cSrcweir {
1382cdf0e10cSrcweir /* refill buffer */
1383cdf0e10cSrcweir memcpy(pFile->m_ReadBuf, pFile->m_pReadPtr, Bytes);
1384cdf0e10cSrcweir pFile->m_pReadPtr = pFile->m_ReadBuf;
1385cdf0e10cSrcweir
1386cdf0e10cSrcweir Free = sizeof(pFile->m_ReadBuf) - Bytes;
1387cdf0e10cSrcweir
1388cdf0e10cSrcweir if ((Max = read(pFile->m_Handle, &pFile->m_ReadBuf[Bytes], Free)) < 0)
1389cdf0e10cSrcweir {
1390cdf0e10cSrcweir OSL_TRACE("read failed '%s'\n",strerror(errno));
1391cdf0e10cSrcweir
1392cdf0e10cSrcweir if( pLine )
1393cdf0e10cSrcweir rtl_freeMemory( pLine );
1394cdf0e10cSrcweir pLine = NULL;
1395cdf0e10cSrcweir break;
1396cdf0e10cSrcweir }
1397cdf0e10cSrcweir
1398cdf0e10cSrcweir if (Max < Free)
1399cdf0e10cSrcweir {
1400cdf0e10cSrcweir if ((Max == 0) && ! pLine)
1401cdf0e10cSrcweir break;
1402cdf0e10cSrcweir
1403cdf0e10cSrcweir pFile->m_ReadBuf[Bytes + Max] = '\0';
1404cdf0e10cSrcweir }
1405cdf0e10cSrcweir }
1406cdf0e10cSrcweir
1407cdf0e10cSrcweir for (pChr = pFile->m_pReadPtr;
1408cdf0e10cSrcweir (*pChr != '\n') && (*pChr != '\r') && (*pChr != '\0') &&
1409cdf0e10cSrcweir (pChr < (pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf) - 1));
1410cdf0e10cSrcweir pChr++);
1411cdf0e10cSrcweir
1412cdf0e10cSrcweir Max = pChr - pFile->m_pReadPtr;
1413cdf0e10cSrcweir pNewLine = (sal_Char*) rtl_allocateMemory( nLineBytes + Max + 1 );
1414cdf0e10cSrcweir if( pLine )
1415cdf0e10cSrcweir {
1416cdf0e10cSrcweir memcpy( pNewLine, pLine, nLineBytes );
1417cdf0e10cSrcweir rtl_freeMemory( pLine );
1418cdf0e10cSrcweir }
1419cdf0e10cSrcweir memcpy(pNewLine+nLineBytes, pFile->m_pReadPtr, Max);
1420cdf0e10cSrcweir nLineBytes += Max;
1421cdf0e10cSrcweir pNewLine[ nLineBytes ] = 0;
1422cdf0e10cSrcweir pLine = pNewLine;
1423cdf0e10cSrcweir
1424cdf0e10cSrcweir if (pChr < (pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf) - 1))
1425cdf0e10cSrcweir {
1426cdf0e10cSrcweir if (*pChr != '\0')
1427cdf0e10cSrcweir {
1428cdf0e10cSrcweir if ((pChr[0] == '\r') && (pChr[1] == '\n'))
1429cdf0e10cSrcweir pChr += 2;
1430cdf0e10cSrcweir else
1431cdf0e10cSrcweir pChr += 1;
1432cdf0e10cSrcweir }
1433cdf0e10cSrcweir
1434cdf0e10cSrcweir if ((pChr < (pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf))) &&
1435cdf0e10cSrcweir (*pChr == '\0'))
1436cdf0e10cSrcweir pChr = pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf);
1437cdf0e10cSrcweir
1438cdf0e10cSrcweir /* setting Max to -1 indicates terminating read loop */
1439cdf0e10cSrcweir Max = -1;
1440cdf0e10cSrcweir }
1441cdf0e10cSrcweir
1442cdf0e10cSrcweir pFile->m_pReadPtr = pChr;
1443cdf0e10cSrcweir }
1444cdf0e10cSrcweir while (Max > 0);
1445cdf0e10cSrcweir
1446cdf0e10cSrcweir return pLine;
1447cdf0e10cSrcweir }
1448cdf0e10cSrcweir
OslProfile_putLine(osl_TFile * pFile,const sal_Char * pszLine)1449cdf0e10cSrcweir static sal_Bool OslProfile_putLine(osl_TFile* pFile, const sal_Char *pszLine)
1450cdf0e10cSrcweir {
1451cdf0e10cSrcweir unsigned int Len = strlen(pszLine);
1452cdf0e10cSrcweir
1453cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1454cdf0e10cSrcweir int strLen=0;
1455cdf0e10cSrcweir #endif
1456cdf0e10cSrcweir
1457cdf0e10cSrcweir if ( pFile == 0 || pFile->m_Handle < 0 )
1458cdf0e10cSrcweir {
1459cdf0e10cSrcweir return (sal_False);
1460cdf0e10cSrcweir }
1461cdf0e10cSrcweir
1462cdf0e10cSrcweir if ( pFile->m_pWriteBuf == 0 )
1463cdf0e10cSrcweir {
1464cdf0e10cSrcweir pFile->m_pWriteBuf = (sal_Char*) malloc(Len+3);
1465cdf0e10cSrcweir pFile->m_nWriteBufLen = Len+3;
1466cdf0e10cSrcweir pFile->m_nWriteBufFree = Len+3;
1467cdf0e10cSrcweir }
1468cdf0e10cSrcweir else
1469cdf0e10cSrcweir {
1470cdf0e10cSrcweir if ( pFile->m_nWriteBufFree <= Len + 3 )
1471cdf0e10cSrcweir {
1472cdf0e10cSrcweir sal_Char* pTmp;
1473cdf0e10cSrcweir
1474cdf0e10cSrcweir pTmp=(sal_Char*) realloc(pFile->m_pWriteBuf,( ( pFile->m_nWriteBufLen + Len ) * 2) );
1475cdf0e10cSrcweir if ( pTmp == 0 )
1476cdf0e10cSrcweir {
1477cdf0e10cSrcweir return sal_False;
1478cdf0e10cSrcweir }
1479cdf0e10cSrcweir pFile->m_pWriteBuf = pTmp;
1480cdf0e10cSrcweir pFile->m_nWriteBufFree = pFile->m_nWriteBufFree + pFile->m_nWriteBufLen + ( 2 * Len );
1481cdf0e10cSrcweir pFile->m_nWriteBufLen = ( pFile->m_nWriteBufLen + Len ) * 2;
1482cdf0e10cSrcweir memset( (pFile->m_pWriteBuf) + ( pFile->m_nWriteBufLen - pFile->m_nWriteBufFree ), 0, pFile->m_nWriteBufFree);
1483cdf0e10cSrcweir }
1484cdf0e10cSrcweir }
1485cdf0e10cSrcweir
1486cdf0e10cSrcweir
1487cdf0e10cSrcweir
1488cdf0e10cSrcweir memcpy(pFile->m_pWriteBuf + ( pFile->m_nWriteBufLen - pFile->m_nWriteBufFree ),pszLine,Len+1);
1489cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
1490cdf0e10cSrcweir strLen = strlen(pFile->m_pWriteBuf);
1491cdf0e10cSrcweir #endif
1492cdf0e10cSrcweir pFile->m_pWriteBuf[pFile->m_nWriteBufLen - pFile->m_nWriteBufFree + Len]='\n';
1493cdf0e10cSrcweir pFile->m_pWriteBuf[pFile->m_nWriteBufLen - pFile->m_nWriteBufFree + Len + 1]='\0';
1494cdf0e10cSrcweir
1495cdf0e10cSrcweir pFile->m_nWriteBufFree-=Len+1;
1496cdf0e10cSrcweir
1497cdf0e10cSrcweir return sal_True;
1498cdf0e10cSrcweir }
1499cdf0e10cSrcweir
1500cdf0e10cSrcweir /* platform specific end */
1501cdf0e10cSrcweir
stripBlanks(sal_Char * String,sal_uInt32 * pLen)1502cdf0e10cSrcweir static sal_Char* stripBlanks(sal_Char* String, sal_uInt32* pLen)
1503cdf0e10cSrcweir {
1504cdf0e10cSrcweir if ( ( pLen != NULL ) && ( *pLen != 0 ) )
1505cdf0e10cSrcweir {
1506cdf0e10cSrcweir while ((String[*pLen - 1] == ' ') || (String[*pLen - 1] == '\t'))
1507cdf0e10cSrcweir (*pLen)--;
1508cdf0e10cSrcweir
1509cdf0e10cSrcweir while ( (*String == ' ') || (*String == '\t') )
1510cdf0e10cSrcweir {
1511cdf0e10cSrcweir String++;
1512cdf0e10cSrcweir (*pLen)--;
1513cdf0e10cSrcweir }
1514cdf0e10cSrcweir }
1515cdf0e10cSrcweir else
1516cdf0e10cSrcweir while ( (*String == ' ') || (*String == '\t') )
1517cdf0e10cSrcweir String++;
1518cdf0e10cSrcweir
1519cdf0e10cSrcweir return (String);
1520cdf0e10cSrcweir }
1521cdf0e10cSrcweir
addLine(osl_TProfileImpl * pProfile,const sal_Char * Line)1522cdf0e10cSrcweir static sal_Char* addLine(osl_TProfileImpl* pProfile, const sal_Char* Line)
1523cdf0e10cSrcweir {
1524cdf0e10cSrcweir if (pProfile->m_NoLines >= pProfile->m_MaxLines)
1525cdf0e10cSrcweir {
1526cdf0e10cSrcweir if (pProfile->m_Lines == NULL)
1527cdf0e10cSrcweir {
1528cdf0e10cSrcweir pProfile->m_MaxLines = LINES_INI;
1529*e5f048b3SPedro Giffuni pProfile->m_Lines = calloc(pProfile->m_MaxLines, sizeof(sal_Char *));
1530cdf0e10cSrcweir }
1531cdf0e10cSrcweir else
1532cdf0e10cSrcweir {
1533cdf0e10cSrcweir unsigned int idx=0;
1534cdf0e10cSrcweir unsigned int oldmax=pProfile->m_MaxLines;
1535cdf0e10cSrcweir
1536cdf0e10cSrcweir pProfile->m_MaxLines += LINES_ADD;
1537cdf0e10cSrcweir pProfile->m_Lines = (sal_Char **)realloc(pProfile->m_Lines,
1538cdf0e10cSrcweir pProfile->m_MaxLines * sizeof(sal_Char *));
1539cdf0e10cSrcweir for ( idx = oldmax ; idx < pProfile->m_MaxLines ; ++idx )
1540cdf0e10cSrcweir {
1541cdf0e10cSrcweir pProfile->m_Lines[idx]=0;
1542cdf0e10cSrcweir }
1543cdf0e10cSrcweir }
1544cdf0e10cSrcweir
1545cdf0e10cSrcweir if (pProfile->m_Lines == NULL)
1546cdf0e10cSrcweir {
1547cdf0e10cSrcweir pProfile->m_NoLines = 0;
1548cdf0e10cSrcweir pProfile->m_MaxLines = 0;
1549cdf0e10cSrcweir return (NULL);
1550cdf0e10cSrcweir }
1551cdf0e10cSrcweir
1552cdf0e10cSrcweir }
1553cdf0e10cSrcweir
1554cdf0e10cSrcweir if ( pProfile->m_Lines != 0 && pProfile->m_Lines[pProfile->m_NoLines] != 0 )
1555cdf0e10cSrcweir {
1556cdf0e10cSrcweir free(pProfile->m_Lines[pProfile->m_NoLines]);
1557cdf0e10cSrcweir }
1558cdf0e10cSrcweir pProfile->m_Lines[pProfile->m_NoLines++] = strdup(Line);
1559cdf0e10cSrcweir
1560cdf0e10cSrcweir return (pProfile->m_Lines[pProfile->m_NoLines - 1]);
1561cdf0e10cSrcweir }
1562cdf0e10cSrcweir
insertLine(osl_TProfileImpl * pProfile,const sal_Char * Line,sal_uInt32 LineNo)1563cdf0e10cSrcweir static sal_Char* insertLine(osl_TProfileImpl* pProfile, const sal_Char* Line, sal_uInt32 LineNo)
1564cdf0e10cSrcweir {
1565cdf0e10cSrcweir if (pProfile->m_NoLines >= pProfile->m_MaxLines)
1566cdf0e10cSrcweir {
1567cdf0e10cSrcweir if (pProfile->m_Lines == NULL)
1568cdf0e10cSrcweir {
1569cdf0e10cSrcweir pProfile->m_MaxLines = LINES_INI;
1570*e5f048b3SPedro Giffuni pProfile->m_Lines = calloc(pProfile->m_MaxLines, sizeof(sal_Char *));
1571cdf0e10cSrcweir }
1572cdf0e10cSrcweir else
1573cdf0e10cSrcweir {
1574cdf0e10cSrcweir pProfile->m_MaxLines += LINES_ADD;
1575cdf0e10cSrcweir pProfile->m_Lines = (sal_Char **)realloc(pProfile->m_Lines,
1576cdf0e10cSrcweir pProfile->m_MaxLines * sizeof(sal_Char *));
1577cdf0e10cSrcweir
1578cdf0e10cSrcweir memset(&pProfile->m_Lines[pProfile->m_NoLines],
1579cdf0e10cSrcweir 0,
1580cdf0e10cSrcweir (pProfile->m_MaxLines - pProfile->m_NoLines - 1) * sizeof(sal_Char*));
1581cdf0e10cSrcweir }
1582cdf0e10cSrcweir
1583cdf0e10cSrcweir if (pProfile->m_Lines == NULL)
1584cdf0e10cSrcweir {
1585cdf0e10cSrcweir pProfile->m_NoLines = 0;
1586cdf0e10cSrcweir pProfile->m_MaxLines = 0;
1587cdf0e10cSrcweir return (NULL);
1588cdf0e10cSrcweir }
1589cdf0e10cSrcweir }
1590cdf0e10cSrcweir
1591cdf0e10cSrcweir LineNo = LineNo > pProfile->m_NoLines ? pProfile->m_NoLines : LineNo;
1592cdf0e10cSrcweir
1593cdf0e10cSrcweir if (LineNo < pProfile->m_NoLines)
1594cdf0e10cSrcweir {
1595cdf0e10cSrcweir sal_uInt32 i, n;
1596cdf0e10cSrcweir osl_TProfileSection* pSec;
1597cdf0e10cSrcweir
1598cdf0e10cSrcweir memmove(&pProfile->m_Lines[LineNo + 1], &pProfile->m_Lines[LineNo],
1599cdf0e10cSrcweir (pProfile->m_NoLines - LineNo) * sizeof(sal_Char *));
1600cdf0e10cSrcweir
1601cdf0e10cSrcweir
1602cdf0e10cSrcweir /* adjust line references */
1603cdf0e10cSrcweir for (i = 0; i < pProfile->m_NoSections; i++)
1604cdf0e10cSrcweir {
1605cdf0e10cSrcweir pSec = &pProfile->m_Sections[i];
1606cdf0e10cSrcweir
1607cdf0e10cSrcweir if (pSec->m_Line >= LineNo)
1608cdf0e10cSrcweir pSec->m_Line++;
1609cdf0e10cSrcweir
1610cdf0e10cSrcweir for (n = 0; n < pSec->m_NoEntries; n++)
1611cdf0e10cSrcweir if (pSec->m_Entries[n].m_Line >= LineNo)
1612cdf0e10cSrcweir pSec->m_Entries[n].m_Line++;
1613cdf0e10cSrcweir }
1614cdf0e10cSrcweir }
1615cdf0e10cSrcweir
1616cdf0e10cSrcweir pProfile->m_NoLines++;
1617cdf0e10cSrcweir
1618cdf0e10cSrcweir pProfile->m_Lines[LineNo] = strdup(Line);
1619cdf0e10cSrcweir
1620cdf0e10cSrcweir return (pProfile->m_Lines[LineNo]);
1621cdf0e10cSrcweir }
1622cdf0e10cSrcweir
removeLine(osl_TProfileImpl * pProfile,sal_uInt32 LineNo)1623cdf0e10cSrcweir static void removeLine(osl_TProfileImpl* pProfile, sal_uInt32 LineNo)
1624cdf0e10cSrcweir {
1625cdf0e10cSrcweir if (LineNo < pProfile->m_NoLines)
1626cdf0e10cSrcweir {
1627cdf0e10cSrcweir free(pProfile->m_Lines[LineNo]);
1628cdf0e10cSrcweir pProfile->m_Lines[LineNo]=0;
1629cdf0e10cSrcweir if (pProfile->m_NoLines - LineNo > 1)
1630cdf0e10cSrcweir {
1631cdf0e10cSrcweir sal_uInt32 i, n;
1632cdf0e10cSrcweir osl_TProfileSection* pSec;
1633cdf0e10cSrcweir
1634cdf0e10cSrcweir memmove(&pProfile->m_Lines[LineNo], &pProfile->m_Lines[LineNo + 1],
1635cdf0e10cSrcweir (pProfile->m_NoLines - LineNo - 1) * sizeof(sal_Char *));
1636cdf0e10cSrcweir
1637cdf0e10cSrcweir memset(&pProfile->m_Lines[pProfile->m_NoLines - 1],
1638cdf0e10cSrcweir 0,
1639cdf0e10cSrcweir (pProfile->m_MaxLines - pProfile->m_NoLines) * sizeof(sal_Char*));
1640cdf0e10cSrcweir
1641cdf0e10cSrcweir /* adjust line references */
1642cdf0e10cSrcweir for (i = 0; i < pProfile->m_NoSections; i++)
1643cdf0e10cSrcweir {
1644cdf0e10cSrcweir pSec = &pProfile->m_Sections[i];
1645cdf0e10cSrcweir
1646cdf0e10cSrcweir if (pSec->m_Line > LineNo)
1647cdf0e10cSrcweir pSec->m_Line--;
1648cdf0e10cSrcweir
1649cdf0e10cSrcweir for (n = 0; n < pSec->m_NoEntries; n++)
1650cdf0e10cSrcweir if (pSec->m_Entries[n].m_Line > LineNo)
1651cdf0e10cSrcweir pSec->m_Entries[n].m_Line--;
1652cdf0e10cSrcweir }
1653cdf0e10cSrcweir }
1654cdf0e10cSrcweir else
1655cdf0e10cSrcweir {
1656cdf0e10cSrcweir pProfile->m_Lines[LineNo] = 0;
1657cdf0e10cSrcweir }
1658cdf0e10cSrcweir
1659cdf0e10cSrcweir pProfile->m_NoLines--;
1660cdf0e10cSrcweir }
1661cdf0e10cSrcweir
1662cdf0e10cSrcweir return;
1663cdf0e10cSrcweir }
1664cdf0e10cSrcweir
setEntry(osl_TProfileImpl * pProfile,osl_TProfileSection * pSection,sal_uInt32 NoEntry,sal_uInt32 Line,sal_Char * Entry,sal_uInt32 Len)1665cdf0e10cSrcweir static void setEntry(osl_TProfileImpl* pProfile, osl_TProfileSection* pSection,
1666cdf0e10cSrcweir sal_uInt32 NoEntry, sal_uInt32 Line,
1667cdf0e10cSrcweir sal_Char* Entry, sal_uInt32 Len)
1668cdf0e10cSrcweir {
1669cdf0e10cSrcweir Entry = stripBlanks(Entry, &Len);
1670cdf0e10cSrcweir pSection->m_Entries[NoEntry].m_Line = Line;
1671cdf0e10cSrcweir pSection->m_Entries[NoEntry].m_Offset = Entry - pProfile->m_Lines[Line];
1672cdf0e10cSrcweir pSection->m_Entries[NoEntry].m_Len = Len;
1673cdf0e10cSrcweir
1674cdf0e10cSrcweir return;
1675cdf0e10cSrcweir }
1676cdf0e10cSrcweir
addEntry(osl_TProfileImpl * pProfile,osl_TProfileSection * pSection,int Line,sal_Char * Entry,sal_uInt32 Len)1677cdf0e10cSrcweir static sal_Bool addEntry(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection,
1678cdf0e10cSrcweir int Line, sal_Char* Entry, sal_uInt32 Len)
1679cdf0e10cSrcweir {
1680cdf0e10cSrcweir if (pSection != NULL)
1681cdf0e10cSrcweir {
1682cdf0e10cSrcweir if (pSection->m_NoEntries >= pSection->m_MaxEntries)
1683cdf0e10cSrcweir {
1684cdf0e10cSrcweir if (pSection->m_Entries == NULL)
1685cdf0e10cSrcweir {
1686cdf0e10cSrcweir pSection->m_MaxEntries = ENTRIES_INI;
1687cdf0e10cSrcweir pSection->m_Entries = (osl_TProfileEntry *)malloc(
1688cdf0e10cSrcweir pSection->m_MaxEntries * sizeof(osl_TProfileEntry));
1689cdf0e10cSrcweir }
1690cdf0e10cSrcweir else
1691cdf0e10cSrcweir {
1692cdf0e10cSrcweir pSection->m_MaxEntries += ENTRIES_ADD;
1693cdf0e10cSrcweir pSection->m_Entries = (osl_TProfileEntry *)realloc(pSection->m_Entries,
1694cdf0e10cSrcweir pSection->m_MaxEntries * sizeof(osl_TProfileEntry));
1695cdf0e10cSrcweir }
1696cdf0e10cSrcweir
1697cdf0e10cSrcweir if (pSection->m_Entries == NULL)
1698cdf0e10cSrcweir {
1699cdf0e10cSrcweir pSection->m_NoEntries = 0;
1700cdf0e10cSrcweir pSection->m_MaxEntries = 0;
1701cdf0e10cSrcweir return (sal_False);
1702cdf0e10cSrcweir }
1703cdf0e10cSrcweir }
1704cdf0e10cSrcweir
1705cdf0e10cSrcweir pSection->m_NoEntries++;
1706cdf0e10cSrcweir
1707cdf0e10cSrcweir Entry = stripBlanks(Entry, &Len);
1708cdf0e10cSrcweir setEntry(pProfile, pSection, pSection->m_NoEntries - 1, Line,
1709cdf0e10cSrcweir Entry, Len);
1710cdf0e10cSrcweir
1711cdf0e10cSrcweir return (sal_True);
1712cdf0e10cSrcweir }
1713cdf0e10cSrcweir
1714cdf0e10cSrcweir return (sal_False);
1715cdf0e10cSrcweir }
1716cdf0e10cSrcweir
removeEntry(osl_TProfileSection * pSection,sal_uInt32 NoEntry)1717cdf0e10cSrcweir static void removeEntry(osl_TProfileSection *pSection, sal_uInt32 NoEntry)
1718cdf0e10cSrcweir {
1719cdf0e10cSrcweir if (NoEntry < pSection->m_NoEntries)
1720cdf0e10cSrcweir {
1721cdf0e10cSrcweir if (pSection->m_NoEntries - NoEntry > 1)
1722cdf0e10cSrcweir {
1723cdf0e10cSrcweir memmove(&pSection->m_Entries[NoEntry],
1724cdf0e10cSrcweir &pSection->m_Entries[NoEntry + 1],
1725cdf0e10cSrcweir (pSection->m_NoEntries - NoEntry - 1) * sizeof(osl_TProfileEntry));
1726cdf0e10cSrcweir pSection->m_Entries[pSection->m_NoEntries - 1].m_Line=0;
1727cdf0e10cSrcweir pSection->m_Entries[pSection->m_NoEntries - 1].m_Offset=0;
1728cdf0e10cSrcweir pSection->m_Entries[pSection->m_NoEntries - 1].m_Len=0;
1729cdf0e10cSrcweir }
1730cdf0e10cSrcweir
1731cdf0e10cSrcweir pSection->m_NoEntries--;
1732cdf0e10cSrcweir }
1733cdf0e10cSrcweir
1734cdf0e10cSrcweir return;
1735cdf0e10cSrcweir }
1736cdf0e10cSrcweir
addSection(osl_TProfileImpl * pProfile,int Line,const sal_Char * Section,sal_uInt32 Len)1737cdf0e10cSrcweir static sal_Bool addSection(osl_TProfileImpl* pProfile, int Line, const sal_Char* Section, sal_uInt32 Len)
1738cdf0e10cSrcweir {
1739cdf0e10cSrcweir if (pProfile->m_NoSections >= pProfile->m_MaxSections)
1740cdf0e10cSrcweir {
1741cdf0e10cSrcweir if (pProfile->m_Sections == NULL)
1742cdf0e10cSrcweir {
1743cdf0e10cSrcweir pProfile->m_MaxSections = SECTIONS_INI;
1744*e5f048b3SPedro Giffuni pProfile->m_Sections = calloc(pProfile->m_MaxSections, sizeof(osl_TProfileSection));
1745cdf0e10cSrcweir }
1746cdf0e10cSrcweir else
1747cdf0e10cSrcweir {
1748cdf0e10cSrcweir unsigned int idx=0;
1749cdf0e10cSrcweir unsigned int oldmax=pProfile->m_MaxSections;
1750cdf0e10cSrcweir
1751cdf0e10cSrcweir pProfile->m_MaxSections += SECTIONS_ADD;
1752cdf0e10cSrcweir pProfile->m_Sections = (osl_TProfileSection *)realloc(pProfile->m_Sections,
1753cdf0e10cSrcweir pProfile->m_MaxSections * sizeof(osl_TProfileSection));
1754cdf0e10cSrcweir for ( idx = oldmax ; idx < pProfile->m_MaxSections ; ++idx )
1755cdf0e10cSrcweir {
1756cdf0e10cSrcweir pProfile->m_Sections[idx].m_Entries=0;
1757cdf0e10cSrcweir }
1758cdf0e10cSrcweir }
1759cdf0e10cSrcweir
1760cdf0e10cSrcweir if (pProfile->m_Sections == NULL)
1761cdf0e10cSrcweir {
1762cdf0e10cSrcweir pProfile->m_NoSections = 0;
1763cdf0e10cSrcweir pProfile->m_MaxSections = 0;
1764cdf0e10cSrcweir return (sal_False);
1765cdf0e10cSrcweir }
1766cdf0e10cSrcweir }
1767cdf0e10cSrcweir
1768cdf0e10cSrcweir pProfile->m_NoSections++;
1769cdf0e10cSrcweir
1770cdf0e10cSrcweir if ( pProfile->m_Sections[(pProfile->m_NoSections) - 1].m_Entries != 0 )
1771cdf0e10cSrcweir {
1772cdf0e10cSrcweir free(pProfile->m_Sections[(pProfile->m_NoSections) - 1].m_Entries);
1773cdf0e10cSrcweir }
1774cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_Entries = NULL;
1775cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_NoEntries = 0;
1776cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_MaxEntries = 0;
1777cdf0e10cSrcweir
1778cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_Line = Line;
1779cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_Offset = Section - pProfile->m_Lines[Line];
1780cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_Len = Len;
1781cdf0e10cSrcweir
1782cdf0e10cSrcweir return (sal_True);
1783cdf0e10cSrcweir }
1784cdf0e10cSrcweir
removeSection(osl_TProfileImpl * pProfile,osl_TProfileSection * pSection)1785cdf0e10cSrcweir static void removeSection(osl_TProfileImpl* pProfile, osl_TProfileSection *pSection)
1786cdf0e10cSrcweir {
1787cdf0e10cSrcweir sal_uInt32 Section;
1788cdf0e10cSrcweir
1789cdf0e10cSrcweir if ((Section = pSection - pProfile->m_Sections) < pProfile->m_NoSections)
1790cdf0e10cSrcweir {
1791cdf0e10cSrcweir free (pSection->m_Entries);
1792cdf0e10cSrcweir pSection->m_Entries=0;
1793cdf0e10cSrcweir if (pProfile->m_NoSections - Section > 1)
1794cdf0e10cSrcweir {
1795cdf0e10cSrcweir memmove(&pProfile->m_Sections[Section], &pProfile->m_Sections[Section + 1],
1796cdf0e10cSrcweir (pProfile->m_NoSections - Section - 1) * sizeof(osl_TProfileSection));
1797cdf0e10cSrcweir
1798cdf0e10cSrcweir memset(&pProfile->m_Sections[pProfile->m_NoSections - 1],
1799cdf0e10cSrcweir 0,
1800cdf0e10cSrcweir (pProfile->m_MaxSections - pProfile->m_NoSections) * sizeof(osl_TProfileSection));
1801cdf0e10cSrcweir pProfile->m_Sections[pProfile->m_NoSections - 1].m_Entries = 0;
1802cdf0e10cSrcweir }
1803cdf0e10cSrcweir else
1804cdf0e10cSrcweir {
1805cdf0e10cSrcweir pSection->m_Entries = 0;
1806cdf0e10cSrcweir }
1807cdf0e10cSrcweir
1808cdf0e10cSrcweir pProfile->m_NoSections--;
1809cdf0e10cSrcweir }
1810cdf0e10cSrcweir
1811cdf0e10cSrcweir return;
1812cdf0e10cSrcweir }
1813cdf0e10cSrcweir
findEntry(osl_TProfileImpl * pProfile,const sal_Char * Section,const sal_Char * Entry,sal_uInt32 * pNoEntry)1814cdf0e10cSrcweir static osl_TProfileSection* findEntry(osl_TProfileImpl* pProfile, const sal_Char* Section,
1815cdf0e10cSrcweir const sal_Char* Entry, sal_uInt32 *pNoEntry)
1816cdf0e10cSrcweir {
1817cdf0e10cSrcweir static sal_uInt32 Sect = 0;
1818cdf0e10cSrcweir sal_uInt32 i, n;
1819cdf0e10cSrcweir sal_uInt32 Len;
1820cdf0e10cSrcweir const sal_Char* pStr;
1821cdf0e10cSrcweir osl_TProfileSection* pSec=0;
1822cdf0e10cSrcweir
1823cdf0e10cSrcweir Len = strlen(Section);
1824cdf0e10cSrcweir
1825cdf0e10cSrcweir n = Sect;
1826cdf0e10cSrcweir
1827cdf0e10cSrcweir for (i = 0; i < pProfile->m_NoSections; i++)
1828cdf0e10cSrcweir {
1829cdf0e10cSrcweir n %= pProfile->m_NoSections;
1830cdf0e10cSrcweir pSec = &pProfile->m_Sections[n];
1831cdf0e10cSrcweir if ((Len == pSec->m_Len) &&
1832cdf0e10cSrcweir (strncasecmp(Section, &pProfile->m_Lines[pSec->m_Line][pSec->m_Offset], pSec->m_Len)
1833cdf0e10cSrcweir == 0))
1834cdf0e10cSrcweir break;
1835cdf0e10cSrcweir n++;
1836cdf0e10cSrcweir }
1837cdf0e10cSrcweir
1838cdf0e10cSrcweir Sect = n;
1839cdf0e10cSrcweir
1840cdf0e10cSrcweir if (i < pProfile->m_NoSections)
1841cdf0e10cSrcweir {
1842cdf0e10cSrcweir Len = strlen(Entry);
1843cdf0e10cSrcweir
1844cdf0e10cSrcweir *pNoEntry = pSec->m_NoEntries;
1845cdf0e10cSrcweir
1846cdf0e10cSrcweir for (i = 0; i < pSec->m_NoEntries; i++)
1847cdf0e10cSrcweir {
1848cdf0e10cSrcweir pStr = &pProfile->m_Lines[pSec->m_Entries[i].m_Line]
1849cdf0e10cSrcweir [pSec->m_Entries[i].m_Offset];
1850cdf0e10cSrcweir if ((Len == pSec->m_Entries[i].m_Len) &&
1851cdf0e10cSrcweir (strncasecmp(Entry, pStr, pSec->m_Entries[i].m_Len)
1852cdf0e10cSrcweir == 0))
1853cdf0e10cSrcweir {
1854cdf0e10cSrcweir *pNoEntry = i;
1855cdf0e10cSrcweir break;
1856cdf0e10cSrcweir }
1857cdf0e10cSrcweir }
1858cdf0e10cSrcweir }
1859cdf0e10cSrcweir else
1860cdf0e10cSrcweir pSec = NULL;
1861cdf0e10cSrcweir
1862cdf0e10cSrcweir return (pSec);
1863cdf0e10cSrcweir }
1864cdf0e10cSrcweir
loadProfile(osl_TFile * pFile,osl_TProfileImpl * pProfile)1865cdf0e10cSrcweir static sal_Bool loadProfile(osl_TFile* pFile, osl_TProfileImpl* pProfile)
1866cdf0e10cSrcweir {
1867cdf0e10cSrcweir sal_uInt32 i;
1868cdf0e10cSrcweir sal_Char* pStr;
1869cdf0e10cSrcweir sal_Char* pChar;
1870cdf0e10cSrcweir
1871cdf0e10cSrcweir sal_Char* pLine;
1872cdf0e10cSrcweir sal_Char* bWasAdded = NULL;
1873cdf0e10cSrcweir
1874cdf0e10cSrcweir pProfile->m_NoLines = 0;
1875cdf0e10cSrcweir pProfile->m_NoSections = 0;
1876cdf0e10cSrcweir
1877cdf0e10cSrcweir if ( pFile == 0 )
1878cdf0e10cSrcweir {
1879cdf0e10cSrcweir return sal_False;
1880cdf0e10cSrcweir }
1881cdf0e10cSrcweir
1882cdf0e10cSrcweir if ( pProfile == 0 )
1883cdf0e10cSrcweir {
1884cdf0e10cSrcweir return sal_False;
1885cdf0e10cSrcweir }
1886cdf0e10cSrcweir
1887cdf0e10cSrcweir OSL_VERIFY(OslProfile_rewindFile(pFile, sal_False));
1888cdf0e10cSrcweir
1889cdf0e10cSrcweir while ( ( pLine=OslProfile_getLine(pFile) ) != 0 )
1890cdf0e10cSrcweir {
1891cdf0e10cSrcweir bWasAdded = addLine( pProfile, pLine );
1892cdf0e10cSrcweir rtl_freeMemory( pLine );
1893cdf0e10cSrcweir OSL_ASSERT(bWasAdded);
1894cdf0e10cSrcweir if ( ! bWasAdded )
1895cdf0e10cSrcweir return (sal_False);
1896cdf0e10cSrcweir }
1897cdf0e10cSrcweir
1898cdf0e10cSrcweir for (i = 0; i < pProfile->m_NoLines; i++)
1899cdf0e10cSrcweir {
1900cdf0e10cSrcweir pStr = (sal_Char *)stripBlanks(pProfile->m_Lines[i], NULL);
1901cdf0e10cSrcweir
1902cdf0e10cSrcweir if ((*pStr == '\0') || (*pStr == ';'))
1903cdf0e10cSrcweir continue;
1904cdf0e10cSrcweir
1905cdf0e10cSrcweir if ((*pStr != '[') || ((pChar = strrchr(pStr, ']')) == NULL) ||
1906cdf0e10cSrcweir ((pChar - pStr) <= 2))
1907cdf0e10cSrcweir {
1908cdf0e10cSrcweir /* insert entry */
1909cdf0e10cSrcweir
1910cdf0e10cSrcweir if (pProfile->m_NoSections < 1)
1911cdf0e10cSrcweir continue;
1912cdf0e10cSrcweir
1913cdf0e10cSrcweir if ((pChar = strchr(pStr, '=')) == NULL)
1914cdf0e10cSrcweir pChar = pStr + strlen(pStr);
1915cdf0e10cSrcweir
1916cdf0e10cSrcweir if (! addEntry(pProfile, &pProfile->m_Sections[pProfile->m_NoSections - 1],
1917cdf0e10cSrcweir i, pStr, pChar - pStr))
1918cdf0e10cSrcweir {
1919cdf0e10cSrcweir OSL_ASSERT(0);
1920cdf0e10cSrcweir continue;
1921cdf0e10cSrcweir }
1922cdf0e10cSrcweir
1923cdf0e10cSrcweir }
1924cdf0e10cSrcweir else
1925cdf0e10cSrcweir {
1926cdf0e10cSrcweir /* new section */
1927cdf0e10cSrcweir
1928cdf0e10cSrcweir if (! addSection(pProfile, i, pStr + 1, pChar - pStr - 1))
1929cdf0e10cSrcweir {
1930cdf0e10cSrcweir OSL_ASSERT(0);
1931cdf0e10cSrcweir continue;
1932cdf0e10cSrcweir }
1933cdf0e10cSrcweir
1934cdf0e10cSrcweir }
1935cdf0e10cSrcweir }
1936cdf0e10cSrcweir
1937cdf0e10cSrcweir return (sal_True);
1938cdf0e10cSrcweir }
1939cdf0e10cSrcweir
storeProfile(osl_TProfileImpl * pProfile,sal_Bool bCleanup)1940cdf0e10cSrcweir static sal_Bool storeProfile(osl_TProfileImpl* pProfile, sal_Bool bCleanup)
1941cdf0e10cSrcweir {
1942cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1943cdf0e10cSrcweir OSL_TRACE("In storeProfile\n");
1944cdf0e10cSrcweir #endif
1945cdf0e10cSrcweir
1946cdf0e10cSrcweir if (pProfile->m_Lines != NULL)
1947cdf0e10cSrcweir {
1948cdf0e10cSrcweir if (pProfile->m_Flags & FLG_MODIFIED)
1949cdf0e10cSrcweir {
1950cdf0e10cSrcweir sal_uInt32 i;
1951cdf0e10cSrcweir
1952cdf0e10cSrcweir osl_TFile* pTmpFile = osl_openTmpProfileImpl(pProfile);
1953cdf0e10cSrcweir
1954cdf0e10cSrcweir if ( pTmpFile == 0 )
1955cdf0e10cSrcweir {
1956cdf0e10cSrcweir return sal_False;
1957cdf0e10cSrcweir }
1958cdf0e10cSrcweir
1959cdf0e10cSrcweir OSL_VERIFY(OslProfile_rewindFile(pTmpFile, sal_True));
1960cdf0e10cSrcweir
1961cdf0e10cSrcweir for ( i = 0 ; i < pProfile->m_NoLines ; i++ )
1962cdf0e10cSrcweir {
1963cdf0e10cSrcweir OSL_VERIFY(OslProfile_putLine(pTmpFile, pProfile->m_Lines[i]));
1964cdf0e10cSrcweir }
1965cdf0e10cSrcweir
1966cdf0e10cSrcweir if ( ! writeProfileImpl(pTmpFile) )
1967cdf0e10cSrcweir {
1968cdf0e10cSrcweir if ( pTmpFile->m_pWriteBuf != 0 )
1969cdf0e10cSrcweir {
1970cdf0e10cSrcweir free(pTmpFile->m_pWriteBuf);
1971cdf0e10cSrcweir }
1972cdf0e10cSrcweir
1973cdf0e10cSrcweir pTmpFile->m_pWriteBuf=0;
1974cdf0e10cSrcweir pTmpFile->m_nWriteBufLen=0;
1975cdf0e10cSrcweir pTmpFile->m_nWriteBufFree=0;
1976cdf0e10cSrcweir
1977cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
1978cdf0e10cSrcweir OSL_TRACE("Out storeProfile [not flushed]\n");
1979cdf0e10cSrcweir #endif
1980cdf0e10cSrcweir closeFileImpl(pTmpFile,pProfile->m_Flags);
1981cdf0e10cSrcweir
1982cdf0e10cSrcweir return sal_False;
1983cdf0e10cSrcweir }
1984cdf0e10cSrcweir
1985cdf0e10cSrcweir pProfile->m_Flags &= ~FLG_MODIFIED;
1986cdf0e10cSrcweir
1987cdf0e10cSrcweir closeFileImpl(pProfile->m_pFile,pProfile->m_Flags);
1988cdf0e10cSrcweir closeFileImpl(pTmpFile,pProfile->m_Flags);
1989cdf0e10cSrcweir
1990cdf0e10cSrcweir osl_ProfileSwapProfileNames(pProfile);
1991cdf0e10cSrcweir
1992cdf0e10cSrcweir pProfile->m_pFile = openFileImpl(pProfile->m_FileName,pProfile->m_Flags);
1993cdf0e10cSrcweir
1994cdf0e10cSrcweir }
1995cdf0e10cSrcweir
1996cdf0e10cSrcweir if (bCleanup)
1997cdf0e10cSrcweir {
1998cdf0e10cSrcweir while (pProfile->m_NoLines > 0)
1999cdf0e10cSrcweir removeLine(pProfile, pProfile->m_NoLines - 1);
2000cdf0e10cSrcweir
2001cdf0e10cSrcweir free(pProfile->m_Lines);
2002cdf0e10cSrcweir pProfile->m_Lines = NULL;
2003cdf0e10cSrcweir pProfile->m_NoLines = 0;
2004cdf0e10cSrcweir pProfile->m_MaxLines = 0;
2005cdf0e10cSrcweir
2006cdf0e10cSrcweir while (pProfile->m_NoSections > 0)
2007cdf0e10cSrcweir removeSection(pProfile, &pProfile->m_Sections[pProfile->m_NoSections - 1]);
2008cdf0e10cSrcweir
2009cdf0e10cSrcweir free(pProfile->m_Sections);
2010cdf0e10cSrcweir pProfile->m_Sections = NULL;
2011cdf0e10cSrcweir pProfile->m_NoSections = 0;
2012cdf0e10cSrcweir pProfile->m_MaxSections = 0;
2013cdf0e10cSrcweir }
2014cdf0e10cSrcweir }
2015cdf0e10cSrcweir
2016cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2017cdf0e10cSrcweir OSL_TRACE("Out storeProfile [ok]\n");
2018cdf0e10cSrcweir #endif
2019cdf0e10cSrcweir return (sal_True);
2020cdf0e10cSrcweir }
2021cdf0e10cSrcweir
2022cdf0e10cSrcweir
osl_openTmpProfileImpl(osl_TProfileImpl * pProfile)2023cdf0e10cSrcweir static osl_TFile* osl_openTmpProfileImpl(osl_TProfileImpl* pProfile)
2024cdf0e10cSrcweir {
2025cdf0e10cSrcweir osl_TFile* pFile=0;
2026cdf0e10cSrcweir sal_Char* pszExtension = "tmp";
2027cdf0e10cSrcweir sal_Char pszTmpName[PATH_MAX];
2028cdf0e10cSrcweir oslProfileOption PFlags=0;
2029cdf0e10cSrcweir
2030cdf0e10cSrcweir pszTmpName[0] = '\0';
2031cdf0e10cSrcweir
2032cdf0e10cSrcweir /* generate tmp profilename */
2033cdf0e10cSrcweir osl_ProfileGenerateExtension(pProfile->m_FileName,pszExtension,pszTmpName);
2034cdf0e10cSrcweir
2035cdf0e10cSrcweir if ( pszTmpName[0] == 0 )
2036cdf0e10cSrcweir {
2037cdf0e10cSrcweir return 0;
2038cdf0e10cSrcweir }
2039cdf0e10cSrcweir
2040cdf0e10cSrcweir if ( ! ( pProfile->m_Flags & osl_Profile_READLOCK ) )
2041cdf0e10cSrcweir {
2042cdf0e10cSrcweir PFlags |= osl_Profile_WRITELOCK;
2043cdf0e10cSrcweir }
2044cdf0e10cSrcweir
2045cdf0e10cSrcweir /* open this file */
2046cdf0e10cSrcweir pFile = openFileImpl(pszTmpName,pProfile->m_Flags | PFlags);
2047cdf0e10cSrcweir
2048cdf0e10cSrcweir
2049cdf0e10cSrcweir /* return new pFile */
2050cdf0e10cSrcweir return pFile;
2051cdf0e10cSrcweir }
2052cdf0e10cSrcweir
osl_ProfileSwapProfileNames(osl_TProfileImpl * pProfile)2053cdf0e10cSrcweir static sal_Bool osl_ProfileSwapProfileNames(osl_TProfileImpl* pProfile)
2054cdf0e10cSrcweir {
2055cdf0e10cSrcweir sal_Bool bRet = sal_False;
2056cdf0e10cSrcweir
2057cdf0e10cSrcweir sal_Char pszBakFile[PATH_MAX];
2058cdf0e10cSrcweir sal_Char pszTmpFile[PATH_MAX];
2059cdf0e10cSrcweir sal_Char pszIniFile[PATH_MAX];
2060cdf0e10cSrcweir
2061cdf0e10cSrcweir pszBakFile[0] = '\0';
2062cdf0e10cSrcweir pszTmpFile[0] = '\0';
2063cdf0e10cSrcweir pszIniFile[0] = '\0';
2064cdf0e10cSrcweir
2065cdf0e10cSrcweir osl_ProfileGenerateExtension(pProfile->m_FileName,"bak",pszBakFile);
2066cdf0e10cSrcweir
2067cdf0e10cSrcweir strcpy(pszIniFile,pProfile->m_FileName);
2068cdf0e10cSrcweir
2069cdf0e10cSrcweir osl_ProfileGenerateExtension(pProfile->m_FileName,"tmp",pszTmpFile);
2070cdf0e10cSrcweir
2071cdf0e10cSrcweir /* unlink bak */
2072cdf0e10cSrcweir unlink( pszBakFile );
2073cdf0e10cSrcweir
2074cdf0e10cSrcweir /* rename ini bak */
2075cdf0e10cSrcweir rename( pszIniFile, pszBakFile );
2076cdf0e10cSrcweir
2077cdf0e10cSrcweir /* rename tmp ini */
2078cdf0e10cSrcweir rename( pszTmpFile, pszIniFile );
2079cdf0e10cSrcweir
2080cdf0e10cSrcweir return bRet;
2081cdf0e10cSrcweir }
2082cdf0e10cSrcweir
2083cdf0e10cSrcweir
osl_ProfileGenerateExtension(sal_Char * pszFileName,sal_Char * pszExtension,sal_Char * pszTmpName)2084cdf0e10cSrcweir static void osl_ProfileGenerateExtension(sal_Char* pszFileName, sal_Char* pszExtension, sal_Char* pszTmpName)
2085cdf0e10cSrcweir {
2086cdf0e10cSrcweir
2087cdf0e10cSrcweir strcpy(pszTmpName,pszFileName);
2088cdf0e10cSrcweir strcat(pszTmpName,".");
2089cdf0e10cSrcweir strcat(pszTmpName,pszExtension);
2090cdf0e10cSrcweir
2091cdf0e10cSrcweir return;
2092cdf0e10cSrcweir }
2093cdf0e10cSrcweir
2094cdf0e10cSrcweir
acquireProfile(oslProfile Profile,sal_Bool bWriteable)2095cdf0e10cSrcweir static osl_TProfileImpl* acquireProfile(oslProfile Profile, sal_Bool bWriteable)
2096cdf0e10cSrcweir {
2097cdf0e10cSrcweir osl_TProfileImpl* pProfile = (osl_TProfileImpl*)Profile;
2098cdf0e10cSrcweir oslProfileOption PFlags=0;
2099cdf0e10cSrcweir sal_Bool bRet=sal_False;
2100cdf0e10cSrcweir
2101cdf0e10cSrcweir if ( bWriteable )
2102cdf0e10cSrcweir {
2103cdf0e10cSrcweir PFlags = osl_Profile_DEFAULT | osl_Profile_WRITELOCK;
2104cdf0e10cSrcweir }
2105cdf0e10cSrcweir else
2106cdf0e10cSrcweir {
2107cdf0e10cSrcweir PFlags = osl_Profile_DEFAULT;
2108cdf0e10cSrcweir }
2109cdf0e10cSrcweir
2110cdf0e10cSrcweir
2111cdf0e10cSrcweir if (pProfile == NULL)
2112cdf0e10cSrcweir {
2113cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2114cdf0e10cSrcweir OSL_TRACE("AUTOOPEN MODE\n");
2115cdf0e10cSrcweir #endif
2116cdf0e10cSrcweir
2117cdf0e10cSrcweir if ( ( pProfile = (osl_TProfileImpl*) osl_openProfile(0, PFlags ) ) != NULL )
2118cdf0e10cSrcweir {
2119cdf0e10cSrcweir pProfile->m_Flags |= FLG_AUTOOPEN;
2120cdf0e10cSrcweir }
2121cdf0e10cSrcweir }
2122cdf0e10cSrcweir else
2123cdf0e10cSrcweir {
2124cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2125cdf0e10cSrcweir OSL_TRACE("try to acquire\n");
2126cdf0e10cSrcweir #endif
2127cdf0e10cSrcweir
2128cdf0e10cSrcweir if (! (pProfile->m_Flags & osl_Profile_SYSTEM))
2129cdf0e10cSrcweir {
2130cdf0e10cSrcweir if (! (pProfile->m_Flags & (osl_Profile_READLOCK | osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE )))
2131cdf0e10cSrcweir {
2132cdf0e10cSrcweir osl_TStamp Stamp;
2133cdf0e10cSrcweir
2134cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2135cdf0e10cSrcweir OSL_TRACE("Profile acquire DEFAULT MODE\n");
2136cdf0e10cSrcweir #endif
2137cdf0e10cSrcweir if (! (pProfile->m_pFile = openFileImpl(pProfile->m_FileName, pProfile->m_Flags | PFlags )))
2138cdf0e10cSrcweir return NULL;
2139cdf0e10cSrcweir
2140cdf0e10cSrcweir Stamp = OslProfile_getFileStamp(pProfile->m_pFile);
2141cdf0e10cSrcweir
2142cdf0e10cSrcweir if (memcmp(&Stamp, &(pProfile->m_Stamp), sizeof(osl_TStamp)))
2143cdf0e10cSrcweir {
2144cdf0e10cSrcweir pProfile->m_Stamp = Stamp;
2145cdf0e10cSrcweir
2146cdf0e10cSrcweir bRet=loadProfile(pProfile->m_pFile, pProfile);
2147cdf0e10cSrcweir OSL_ASSERT(bRet);
2148cdf0e10cSrcweir }
2149cdf0e10cSrcweir }
2150cdf0e10cSrcweir else
2151cdf0e10cSrcweir {
2152cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2153cdf0e10cSrcweir OSL_TRACE("Profile acquire READ/WRITELOCK MODE\n");
2154cdf0e10cSrcweir #endif
2155cdf0e10cSrcweir /* A readlock file could not be written */
2156cdf0e10cSrcweir if ((pProfile->m_Flags & osl_Profile_READLOCK) && bWriteable)
2157cdf0e10cSrcweir {
2158cdf0e10cSrcweir return (NULL);
2159cdf0e10cSrcweir }
2160cdf0e10cSrcweir }
2161cdf0e10cSrcweir }
2162cdf0e10cSrcweir }
2163cdf0e10cSrcweir
2164cdf0e10cSrcweir return (pProfile);
2165cdf0e10cSrcweir }
2166cdf0e10cSrcweir
releaseProfile(osl_TProfileImpl * pProfile)2167cdf0e10cSrcweir static sal_Bool releaseProfile(osl_TProfileImpl* pProfile)
2168cdf0e10cSrcweir {
2169cdf0e10cSrcweir sal_Bool bRet=sal_False;
2170cdf0e10cSrcweir
2171cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2172cdf0e10cSrcweir OSL_TRACE("In releaseProfile\n");
2173cdf0e10cSrcweir #endif
2174cdf0e10cSrcweir
2175cdf0e10cSrcweir if ( pProfile == 0 )
2176cdf0e10cSrcweir {
2177cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2178cdf0e10cSrcweir OSL_TRACE("Out releaseProfile [profile==0]\n");
2179cdf0e10cSrcweir #endif
2180cdf0e10cSrcweir return sal_False;
2181cdf0e10cSrcweir }
2182cdf0e10cSrcweir
2183cdf0e10cSrcweir if (pProfile->m_Flags & FLG_AUTOOPEN)
2184cdf0e10cSrcweir {
2185cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2186cdf0e10cSrcweir OSL_TRACE("Out releaseProfile [AUTOOPEN]\n");
2187cdf0e10cSrcweir #endif
2188cdf0e10cSrcweir return (osl_closeProfile((oslProfile)pProfile));
2189cdf0e10cSrcweir }
2190cdf0e10cSrcweir else
2191cdf0e10cSrcweir {
2192cdf0e10cSrcweir #ifdef DEBUG_OSL_PROFILE
2193cdf0e10cSrcweir OSL_TRACE("DEFAULT MODE\n");
2194cdf0e10cSrcweir #endif
2195cdf0e10cSrcweir if (! (pProfile->m_Flags & (osl_Profile_READLOCK | osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE )))
2196cdf0e10cSrcweir {
2197cdf0e10cSrcweir if (pProfile->m_Flags & FLG_MODIFIED)
2198cdf0e10cSrcweir {
2199cdf0e10cSrcweir bRet=storeProfile(pProfile, sal_False);
2200cdf0e10cSrcweir OSL_ASSERT(bRet);
2201cdf0e10cSrcweir }
2202cdf0e10cSrcweir
2203cdf0e10cSrcweir
2204cdf0e10cSrcweir closeFileImpl(pProfile->m_pFile,pProfile->m_Flags);
2205cdf0e10cSrcweir pProfile->m_pFile = NULL;
2206cdf0e10cSrcweir }
2207cdf0e10cSrcweir }
2208cdf0e10cSrcweir
2209cdf0e10cSrcweir #ifdef TRACE_OSL_PROFILE
2210cdf0e10cSrcweir OSL_TRACE("Out releaseProfile [ok]\n");
2211cdf0e10cSrcweir #endif
2212cdf0e10cSrcweir return (sal_True);
2213cdf0e10cSrcweir }
2214