1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 // YD #define min(a,b) (((a) < (b)) ? (a) : (b))
32 #include <osl/security.h>
33 #include <osl/diagnose.h>
34 #include <osl/profile.h>
35 #include <osl/process.h>
36 #include <osl/thread.h>
41 #define SECTIONS_INI 5
42 #define SECTIONS_ADD 3
47 #define STR_INI_EXTENSION ".ini"
48 #define STR_INI_METAHOME "?~"
49 #define STR_INI_METASYS "?$"
50 #define STR_INI_METACFG "?^"
51 #define STR_INI_METAINS "?#"
53 #define STR_INI_BOOLYES "yes"
54 #define STR_INI_BOOLON "on"
55 #define STR_INI_BOOLONE "1"
56 #define STR_INI_BOOLNO "no"
57 #define STR_INI_BOOLOFF "off"
58 #define STR_INI_BOOLZERO "0"
60 #define FLG_USER 0x00FF
61 #define FLG_AUTOOPEN 0x0100
62 #define FLG_MODIFIED 0x0200
64 #define SVERSION_LOCATION STR_INI_METACFG
65 #define SVERSION_FALLBACK STR_INI_METASYS
66 #define SVERSION_NAME "sversion"
67 #define SVERSION_SECTION "Versions"
68 #define SVERSION_SOFFICE "StarOffice"
69 #define SVERSION_PROFILE "soffice.ini"
70 #define SVERSION_OPTION "userid:"
71 #define SVERSION_DIRS { "bin", "program" }
72 #define SVERSION_USER "user"
74 #define _BUILD_STR_(n) # n
75 #define BUILD_STR(n) _BUILD_STR_(n)
77 /* implemented in file.c */
78 extern oslFileError
FileURLToPath( char *, size_t, rtl_uString
* );
80 /*****************************************************************************/
81 /* Data Type Definition */
82 /*****************************************************************************/
84 typedef struct _osl_TStamp
90 typedef enum _osl_TLockMode
92 un_lock
, read_lock
, write_lock
95 typedef struct _osl_TFile
100 sal_Char m_ReadBuf[512];
101 sal_Char* m_pWritePtr;
102 sal_Char m_WriteBuf[512];
104 sal_Char
* m_pReadPtr
;
105 sal_Char m_ReadBuf
[512];
106 /* sal_Char* m_pWritePtr; */
107 /* sal_Char m_WriteBuf[512]; */
108 sal_Char
* m_pWriteBuf
;
109 sal_uInt32 m_nWriteBufLen
;
110 sal_uInt32 m_nWriteBufFree
;
113 typedef struct _osl_TProfileEntry
120 typedef struct _osl_TProfileSection
125 sal_uInt32 m_NoEntries
;
126 sal_uInt32 m_MaxEntries
;
127 osl_TProfileEntry
* m_Entries
;
128 } osl_TProfileSection
;
132 Profile-data structure hidden behind oslProfile:
134 typedef struct _osl_TProfileImpl
139 //sal_Char m_Filename[_MAX_PATH + 1];
140 sal_uInt32 m_NoLines
;
141 sal_uInt32 m_MaxLines
;
142 sal_uInt32 m_NoSections
;
143 sal_uInt32 m_MaxSections
;
145 rtl_uString
*m_strFileName
;
146 osl_TProfileSection
* m_Sections
;
151 /*****************************************************************************/
152 /* Static Module Function Declarations */
153 /*****************************************************************************/
155 //static osl_TFile* openFile(rtl_uString* pszFilename, sal_Bool bWriteable);
156 //static osl_TStamp closeFile(osl_TFile* pFile);
157 static osl_TFile
* openFileImpl(rtl_uString
* strFileName
, oslProfileOption ProfileFlags
);
158 static osl_TStamp
closeFileImpl(osl_TFile
* pFile
);
159 static sal_Bool
lockFile(const osl_TFile
* pFile
, osl_TLockMode eMode
);
160 static sal_Bool
rewindFile(osl_TFile
* pFile
, sal_Bool bTruncate
);
161 static osl_TStamp
getFileStamp(osl_TFile
* pFile
);
163 static sal_Bool
getLine(osl_TFile
* pFile
, const sal_Char
*pszLine
, int MaxLen
);
164 static sal_Bool
putLine(osl_TFile
* pFile
, const sal_Char
*pszLine
);
165 static const sal_Char
* stripBlanks(const sal_Char
* String
, sal_uInt32
* pLen
);
166 static const sal_Char
* addLine(osl_TProfileImpl
* pProfile
, const sal_Char
* Line
);
167 static const sal_Char
* insertLine(osl_TProfileImpl
* pProfile
, const sal_Char
* Line
, sal_uInt32 LineNo
);
168 static void removeLine(osl_TProfileImpl
* pProfile
, sal_uInt32 LineNo
);
169 static void setEntry(osl_TProfileImpl
* pProfile
, osl_TProfileSection
* pSection
,
170 sal_uInt32 NoEntry
, sal_uInt32 Line
,
171 const sal_Char
* Entry
, sal_uInt32 Len
);
172 static sal_Bool
addEntry(osl_TProfileImpl
* pProfile
, osl_TProfileSection
*pSection
,
173 int Line
, const sal_Char
* Entry
, sal_uInt32 Len
);
174 static void removeEntry(osl_TProfileSection
*pSection
, sal_uInt32 NoEntry
);
175 static sal_Bool
addSection(osl_TProfileImpl
* pProfile
, int Line
, const sal_Char
* Section
, sal_uInt32 Len
);
176 static void removeSection(osl_TProfileImpl
* pProfile
, osl_TProfileSection
*pSection
);
177 static osl_TProfileSection
* findEntry(osl_TProfileImpl
* pProfile
, const sal_Char
* Section
,
178 const sal_Char
* Entry
, sal_uInt32
*pNoEntry
);
179 static sal_Bool
loadProfile(osl_TFile
* pFile
, osl_TProfileImpl
* pProfile
);
180 static sal_Bool
storeProfile(osl_TFile
* pFile
, osl_TProfileImpl
* pProfile
, sal_Bool bCleanup
);
181 static osl_TProfileImpl
* acquireProfile(oslProfile Profile
, sal_Bool bWriteable
);
182 static sal_Bool
releaseProfile(osl_TProfileImpl
* pProfile
);
183 static sal_Bool
lookupProfile(const sal_Char
*pszPath
, const sal_Char
*pszFile
, sal_Char
*pPath
);
186 static sal_Bool SAL_CALL
osl_getProfileName(rtl_uString
* strPath
, rtl_uString
* strName
, rtl_uString
** strProfileName
);
188 sal_Bool SAL_CALL
osl_getFullPath(rtl_uString
* pszFilename
, sal_Char
* pszPath
, sal_uInt32 MaxLen
)
190 return NO_ERROR
== DosQueryPathInfo( (PCSZ
)pszFilename
, FIL_QUERYFULLNAME
, pszPath
, MaxLen
);
195 /*****************************************************************************/
196 /* Exported Module Functions */
197 /*****************************************************************************/
199 oslProfile SAL_CALL
osl_openProfile(rtl_uString
*strProfileName
, sal_uInt32 Flags
)
202 osl_TProfileImpl
* pProfile
;
203 rtl_uString
*FileName
=NULL
;
205 #ifdef TRACE_OSL_PROFILE
206 OSL_TRACE("In osl_openProfile\n");
208 OSL_VERIFY(strProfileName
);
210 /* if (rtl_uString_getLength(strProfileName) == 0 )
212 OSL_VERIFY(osl_getProfileName(NULL, NULL, &FileName));
217 rtl_uString_assign(&FileName
, strProfileName
);
220 osl_getSystemPathFromFileURL(FileName
, &FileName
);
222 #ifdef DEBUG_OSL_PROFILE
223 Flags
=osl_Profile_FLUSHWRITE
;
225 // OSL_TRACE("opening '%s'\n",FileName);
226 if ( Flags
== osl_Profile_DEFAULT
)
228 OSL_TRACE("with osl_Profile_DEFAULT \n");
230 if ( Flags
& osl_Profile_SYSTEM
)
232 OSL_TRACE("with osl_Profile_SYSTEM \n");
234 if ( Flags
& osl_Profile_READLOCK
)
236 OSL_TRACE("with osl_Profile_READLOCK \n");
238 if ( Flags
& osl_Profile_WRITELOCK
)
240 OSL_TRACE("with osl_Profile_WRITELOCK \n");
242 /* if ( Flags & osl_Profile_READWRITE ) */
244 /* OSL_TRACE("with osl_Profile_READWRITE \n"); */
246 if ( Flags
& osl_Profile_FLUSHWRITE
)
248 OSL_TRACE("with osl_Profile_FLUSHWRITE \n");
252 if ((! (Flags
& osl_Profile_SYSTEM
)) &&
253 ((pFile
= openFileImpl(FileName
, (Flags
& osl_Profile_WRITELOCK
) ? sal_True
: sal_False
)) == NULL
))
255 #ifdef TRACE_OSL_PROFILE
256 OSL_TRACE("Out osl_openProfile [not opened]\n");
259 rtl_uString_release( FileName
);
264 pProfile
= (osl_TProfileImpl
*)calloc(1, sizeof(osl_TProfileImpl
));
266 pProfile
->m_Flags
= Flags
& FLG_USER
;
267 osl_getSystemPathFromFileURL(strProfileName
, &pProfile
->m_strFileName
);
268 // rtl_uString_assign(&pProfile->m_strFileName, strProfileName);
270 if (Flags
& (osl_Profile_READLOCK
| osl_Profile_WRITELOCK
))
271 pProfile
->m_pFile
= pFile
;
273 pProfile
->m_Stamp
= getFileStamp(pFile
);
275 loadProfile(pFile
, pProfile
);
277 if (pProfile
->m_pFile
== NULL
)
278 closeFileImpl(pFile
);
280 #ifdef TRACE_OSL_PROFILE
281 OSL_TRACE("Out osl_openProfile [ok]\n");
284 rtl_uString_release( FileName
);
289 sal_Bool SAL_CALL
osl_closeProfile(oslProfile Profile
)
291 osl_TProfileImpl
* pProfile
= (osl_TProfileImpl
*)Profile
;
293 #ifdef TRACE_OSL_PROFILE
294 OSL_TRACE("In osl_closeProfile\n");
299 #ifdef TRACE_OSL_PROFILE
300 OSL_TRACE("Out osl_closeProfile [profile==0]\n");
305 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
307 pProfile
= acquireProfile(Profile
,sal_True
);
311 if ( !( pProfile
->m_Flags
& osl_Profile_READLOCK
) && ( pProfile
->m_Flags
& FLG_MODIFIED
) )
313 /* if (pProfile->m_pFile == NULL) */
314 /* pProfile->m_pFile = openFileImpl(pProfile->m_Filename, sal_True); */
316 storeProfile(pProfile
->m_pFile
, pProfile
, sal_False
);
321 pProfile
= acquireProfile(Profile
,sal_False
);
326 #ifdef TRACE_OSL_PROFILE
327 OSL_TRACE("Out osl_closeProfile [pProfile==0]\n");
332 if (pProfile
->m_pFile
!= NULL
)
333 closeFileImpl(pProfile
->m_pFile
);
336 pProfile
->m_pFile
= NULL
;
337 rtl_uString_release(pProfile
->m_strFileName
);
338 pProfile
->m_strFileName
= NULL
;
340 /* release whole profile data types memory */
341 if ( pProfile
->m_NoLines
> 0)
343 unsigned int index
=0;
344 if ( pProfile
->m_Lines
!= 0 )
346 for ( index
= 0 ; index
< pProfile
->m_NoLines
; ++index
)
348 if ( pProfile
->m_Lines
[index
] != 0 )
350 free(pProfile
->m_Lines
[index
]);
353 free(pProfile
->m_Lines
);
355 if ( pProfile
->m_Sections
!= 0 )
357 /*osl_TProfileSection* pSections=pProfile->m_Sections;*/
358 for ( index
= 0 ; index
< pProfile
->m_NoSections
; ++index
)
360 if ( pProfile
->m_Sections
[index
].m_Entries
!= 0 )
362 free(pProfile
->m_Sections
[index
].m_Entries
);
365 free(pProfile
->m_Sections
);
371 #ifdef TRACE_OSL_PROFILE
372 OSL_TRACE("Out osl_closeProfile [ok]\n");
377 sal_Bool SAL_CALL
osl_flushProfile(oslProfile Profile
)
379 osl_TProfileImpl
* pProfile
= (osl_TProfileImpl
*) Profile
;
381 sal_Bool bRet
= sal_False
;
383 #ifdef TRACE_OSL_PROFILE
384 OSL_TRACE("In osl_flushProfile()\n");
389 #ifdef TRACE_OSL_PROFILE
390 OSL_TRACE("Out osl_flushProfile() [pProfile == 0]\n");
395 pFile
= pProfile
->m_pFile
;
396 if ( !( pFile
!= 0 && pFile
->m_Handle
>= 0 ) )
398 #ifdef TRACE_OSL_PROFILE
399 OSL_TRACE("Out osl_flushProfile() [invalid file]\n");
404 if ( pProfile
->m_Flags
& FLG_MODIFIED
)
406 #ifdef DEBUG_OSL_PROFILE
407 OSL_TRACE("swapping to storeprofile\n");
409 bRet
= storeProfile(pFile
,pProfile
,sal_False
);
412 #ifdef TRACE_OSL_PROFILE
413 OSL_TRACE("Out osl_flushProfile() [ok]\n");
418 sal_Bool SAL_CALL
osl_readProfileString(oslProfile Profile
,
419 const sal_Char
* pszSection
, const sal_Char
* pszEntry
,
420 sal_Char
* pszString
, sal_uInt32 MaxLen
,
421 const sal_Char
* pszDefault
)
424 const sal_Char
* pStr
= 0;
425 osl_TProfileSection
* pSec
;
426 osl_TProfileImpl
* pProfile
= 0;
429 #ifdef TRACE_OSL_PROFILE
430 OSL_TRACE("In osl_readProfileString\n");
433 pProfile
= acquireProfile(Profile
, sal_False
);
435 if (pProfile
== NULL
)
437 #ifdef TRACE_OSL_PROFILE
438 OSL_TRACE("Out osl_readProfileString [pProfile==0]\n");
446 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
448 if (((pSec
= findEntry(pProfile
, pszSection
, pszEntry
, &NoEntry
)) != NULL
) &&
449 (NoEntry
< pSec
->m_NoEntries
) &&
450 ((pStr
= strchr(pProfile
->m_Lines
[pSec
->m_Entries
[NoEntry
].m_Line
],
458 pStr
= stripBlanks(pStr
, NULL
);
459 MaxLen
= (MaxLen
- 1 < strlen(pStr
)) ? (MaxLen
- 1) : strlen(pStr
);
460 pStr
= stripBlanks(pStr
, &MaxLen
);
461 strncpy(pszString
, pStr
, MaxLen
);
462 pszString
[MaxLen
] = '\0';
466 PrfQueryProfileString(pProfile
->m_hIni
, (PCSZ
)pszSection
,
467 (PCSZ
)pszEntry
, (PCSZ
)pszDefault
,
470 releaseProfile(pProfile
);
474 #ifdef TRACE_OSL_PROFILE
475 OSL_TRACE("Out osl_readProfileString [pStr==0]\n");
482 #ifdef TRACE_OSL_PROFILE
483 OSL_TRACE("Out osl_readProfileString [ok]\n");
490 sal_Bool SAL_CALL
osl_readProfileBool(oslProfile Profile
,
491 const sal_Char
* pszSection
, const sal_Char
* pszEntry
,
496 #ifdef TRACE_OSL_PROFILE
497 OSL_TRACE("In osl_readProfileBool\n");
500 if (osl_readProfileString(Profile
, pszSection
, pszEntry
, Line
, sizeof(Line
), ""))
502 if ((stricmp(Line
, STR_INI_BOOLYES
) == 0) ||
503 (stricmp(Line
, STR_INI_BOOLON
) == 0) ||
504 (stricmp(Line
, STR_INI_BOOLONE
) == 0))
507 if ((stricmp(Line
, STR_INI_BOOLNO
) == 0) ||
508 (stricmp(Line
, STR_INI_BOOLOFF
) == 0) ||
509 (stricmp(Line
, STR_INI_BOOLZERO
) == 0))
513 #ifdef TRACE_OSL_PROFILE
514 OSL_TRACE("Out osl_readProfileBool [ok]\n");
521 sal_uInt32 SAL_CALL
osl_readProfileIdent(oslProfile Profile
,
522 const sal_Char
* pszSection
, const sal_Char
* pszEntry
,
523 sal_uInt32 FirstId
, const sal_Char
* Strings
[],
529 #ifdef TRACE_OSL_PROFILE
530 OSL_TRACE("In osl_readProfileIdent\n");
533 if (osl_readProfileString(Profile
, pszSection
, pszEntry
, Line
, sizeof(Line
), ""))
536 while (Strings
[i
] != NULL
)
538 if (stricmp(Line
, Strings
[i
]) == 0)
540 Default
= i
+ FirstId
;
547 #ifdef TRACE_OSL_PROFILE
548 OSL_TRACE("Out osl_readProfileIdent [ok]\n");
554 sal_Bool SAL_CALL
osl_writeProfileString(oslProfile Profile
,
555 const sal_Char
* pszSection
, const sal_Char
* pszEntry
,
556 const sal_Char
* pszString
)
559 sal_Bool bRet
= sal_False
;
561 const sal_Char
* pStr
;
563 osl_TProfileSection
* pSec
;
564 osl_TProfileImpl
* pProfile
= 0;
566 #ifdef TRACE_OSL_PROFILE
567 OSL_TRACE("In osl_writeProfileString\n");
570 pProfile
= acquireProfile(Profile
, sal_True
);
572 if (pProfile
== NULL
)
574 #ifdef TRACE_OSL_PROFILE
575 OSL_TRACE("Out osl_writeProfileString [pProfile==0]\n");
581 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
583 if ((pSec
= findEntry(pProfile
, pszSection
, pszEntry
, &NoEntry
)) == NULL
)
586 addLine(pProfile
, Line
);
589 strcpy(&Line
[1], pszSection
);
590 Line
[1 + strlen(pszSection
)] = ']';
591 Line
[2 + strlen(pszSection
)] = '\0';
593 if (((pStr
= addLine(pProfile
, Line
)) == NULL
) ||
594 (! addSection(pProfile
, pProfile
->m_NoLines
- 1, &pStr
[1], strlen(pszSection
))))
596 releaseProfile(pProfile
);
597 #ifdef TRACE_OSL_PROFILE
598 OSL_TRACE("Out osl_writeProfileString [not added]\n");
603 pSec
= &pProfile
->m_Sections
[pProfile
->m_NoSections
- 1];
604 NoEntry
= pSec
->m_NoEntries
;
608 strcpy(&Line
[0], pszEntry
);
609 Line
[0 + strlen(pszEntry
)] = '=';
610 strcpy(&Line
[1 + strlen(pszEntry
)], pszString
);
612 if (NoEntry
>= pSec
->m_NoEntries
)
614 if (pSec
->m_NoEntries
> 0)
615 i
= pSec
->m_Entries
[pSec
->m_NoEntries
- 1].m_Line
+ 1;
617 i
= pSec
->m_Line
+ 1;
619 if (((pStr
= insertLine(pProfile
, Line
, i
)) == NULL
) ||
620 (! addEntry(pProfile
, pSec
, i
, pStr
, strlen(pszEntry
))))
622 releaseProfile(pProfile
);
623 #ifdef TRACE_OSL_PROFILE
624 OSL_TRACE("Out osl_writeProfileString [not inserted]\n");
629 pProfile
->m_Flags
|= FLG_MODIFIED
;
633 i
= pSec
->m_Entries
[NoEntry
].m_Line
;
634 free(pProfile
->m_Lines
[i
]);
635 pProfile
->m_Lines
[i
] = strdup(Line
);
636 setEntry(pProfile
, pSec
, NoEntry
, i
, pProfile
->m_Lines
[i
], strlen(pszEntry
));
638 pProfile
->m_Flags
|= FLG_MODIFIED
;
642 PrfWriteProfileString(pProfile
->m_hIni
, (PCSZ
)pszSection
,
643 (PCSZ
)pszEntry
, (PCSZ
)pszString
);
645 bRet
= releaseProfile(pProfile
);
646 #ifdef TRACE_OSL_PROFILE
647 OSL_TRACE("Out osl_writeProfileString [ok]\n");
653 sal_Bool SAL_CALL
osl_writeProfileBool(oslProfile Profile
,
654 const sal_Char
* pszSection
, const sal_Char
* pszEntry
,
657 sal_Bool bRet
= sal_False
;
659 #ifdef TRACE_OSL_PROFILE
660 OSL_TRACE("In osl_writeProfileBool\n");
664 bRet
=osl_writeProfileString(Profile
, pszSection
, pszEntry
, STR_INI_BOOLONE
);
666 bRet
=osl_writeProfileString(Profile
, pszSection
, pszEntry
, STR_INI_BOOLZERO
);
668 #ifdef TRACE_OSL_PROFILE
669 OSL_TRACE("Out osl_writeProfileBool [ok]\n");
676 sal_Bool SAL_CALL
osl_writeProfileIdent(oslProfile Profile
,
677 const sal_Char
* pszSection
, const sal_Char
* pszEntry
,
678 sal_uInt32 FirstId
, const sal_Char
* Strings
[],
682 sal_Bool bRet
= sal_False
;
684 #ifdef TRACE_OSL_PROFILE
685 OSL_TRACE("In osl_writeProfileIdent\n");
688 for (n
= 0; Strings
[n
] != NULL
; n
++);
690 if ((i
= Value
- FirstId
) >= n
)
693 bRet
=osl_writeProfileString(Profile
, pszSection
, pszEntry
, Strings
[i
]);
695 #ifdef TRACE_OSL_PROFILE
696 OSL_TRACE("Out osl_writeProfileIdent\n");
702 sal_Bool SAL_CALL
osl_removeProfileEntry(oslProfile Profile
,
703 const sal_Char
*pszSection
, const sal_Char
*pszEntry
)
706 osl_TProfileSection
* pSec
;
707 osl_TProfileImpl
* pProfile
= 0;
708 sal_Bool bRet
= sal_False
;
710 #ifdef TRACE_OSL_PROFILE
711 OSL_TRACE("In osl_removeProfileEntry\n");
714 pProfile
= acquireProfile(Profile
, sal_True
);
716 if (pProfile
== NULL
)
718 #ifdef TRACE_OSL_PROFILE
719 OSL_TRACE("Out osl_removeProfileEntry [pProfile==0]\n");
727 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
729 if (((pSec
= findEntry(pProfile
, pszSection
, pszEntry
, &NoEntry
)) != NULL
) &&
730 (NoEntry
< pSec
->m_NoEntries
))
732 removeLine(pProfile
, pSec
->m_Entries
[NoEntry
].m_Line
);
733 removeEntry(pSec
, NoEntry
);
734 if (pSec
->m_NoEntries
== 0)
736 removeLine(pProfile
, pSec
->m_Line
);
738 /* remove any empty separation line */
739 if ((pSec
->m_Line
> 0) && (pProfile
->m_Lines
[pSec
->m_Line
- 1][0] == '\0'))
740 removeLine(pProfile
, pSec
->m_Line
- 1);
742 removeSection(pProfile
, pSec
);
745 pProfile
->m_Flags
|= FLG_MODIFIED
;
749 PrfWriteProfileString(pProfile
->m_hIni
, (PCSZ
)pszSection
, (PCSZ
)pszEntry
, NULL
);
751 bRet
= releaseProfile(pProfile
);
752 #ifdef TRACE_OSL_PROFILE
753 OSL_TRACE("Out osl_removeProfileEntry [ok]\n");
759 sal_uInt32 SAL_CALL
osl_getProfileSectionEntries(oslProfile Profile
, const sal_Char
*pszSection
,
760 sal_Char
* pszBuffer
, sal_uInt32 MaxLen
)
764 osl_TProfileSection
* pSec
;
765 osl_TProfileImpl
* pProfile
= 0;
767 #ifdef TRACE_OSL_PROFILE
768 OSL_TRACE("In osl_getProfileSectionEntries\n");
771 pProfile
= acquireProfile(Profile
, sal_False
);
773 if (pProfile
== NULL
)
775 #ifdef TRACE_OSL_PROFILE
776 OSL_TRACE("Out osl_getProfileSectionEntries [pProfile=0]\n");
784 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
786 if ((pSec
= findEntry(pProfile
, pszSection
, "", &NoEntry
)) != NULL
)
790 for (i
= 0; i
< pSec
->m_NoEntries
; i
++)
792 if ((n
+ pSec
->m_Entries
[i
].m_Len
+ 1) < MaxLen
)
794 strncpy(&pszBuffer
[n
], &pProfile
->m_Lines
[pSec
->m_Entries
[i
].m_Line
]
795 [pSec
->m_Entries
[i
].m_Offset
], pSec
->m_Entries
[i
].m_Len
);
796 n
+= pSec
->m_Entries
[i
].m_Len
;
797 pszBuffer
[n
++] = '\0';
804 pszBuffer
[n
++] = '\0';
808 for (i
= 0; i
< pSec
->m_NoEntries
; i
++)
809 n
+= pSec
->m_Entries
[i
].m_Len
+ 1;
818 n
= PrfQueryProfileString(pProfile
->m_hIni
, (PCSZ
)pszSection
, NULL
, NULL
,
821 releaseProfile(pProfile
);
823 #ifdef TRACE_OSL_PROFILE
824 OSL_TRACE("Out osl_getProfileSectionEntries [ok]\n");
830 sal_uInt32 SAL_CALL
osl_getProfileSections(oslProfile Profile
, sal_Char
* pszBuffer
, sal_uInt32 MaxLen
)
833 osl_TProfileSection
* pSec
;
834 osl_TProfileImpl
* pProfile
= acquireProfile(Profile
, sal_False
);
836 if (pProfile
== NULL
)
839 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
843 for (i
= 0; i
< pProfile
->m_NoSections
; i
++)
845 pSec
= &pProfile
->m_Sections
[i
];
847 if ((n
+ pSec
->m_Len
+ 1) < MaxLen
)
849 strncpy(&pszBuffer
[n
], &pProfile
->m_Lines
[pSec
->m_Line
][pSec
->m_Offset
],
852 pszBuffer
[n
++] = '\0';
858 pszBuffer
[n
++] = '\0';
862 for (i
= 0; i
< pProfile
->m_NoSections
; i
++)
863 n
+= pProfile
->m_Sections
[i
].m_Len
+ 1;
869 n
= PrfQueryProfileString(pProfile
->m_hIni
, NULL
, NULL
, NULL
,
872 releaseProfile(pProfile
);
878 sal_Bool SAL_CALL
osl_getProfileName(rtl_uString
* strPath
, rtl_uString
* strName
, rtl_uString
** strProfileName
)
881 sal_Char File
[_MAX_PATH
];
882 sal_Char Path
[_MAX_PATH
];
884 sal_uInt32 nPathLen
= 0;
886 rtl_uString
* strTmp
= NULL
;
889 /* build file name */
890 if (strName
&& strName
->length
)
892 if(strName
->length
>= _MAX_PATH
)
895 strcpy(File
, (char*)strName
->buffer
);
896 nFileLen
= strName
->length
;
898 if (rtl_ustr_indexOfChar( File
, L
'.' ) == -1)
900 if (nFileLen
+ strlen(STR_INI_EXTENSION
) >= _MAX_PATH
)
903 /* add default extension */
904 strcpy(File
+ nFileLen
, STR_INI_EXTENSION
);
905 nFileLen
+= strlen(STR_INI_EXTENSION
);
910 rtl_uString
*strProgName
= NULL
;
911 sal_Unicode
*pProgName
;
912 sal_Int32 nOffset
= 0;
916 if (osl_getExecutableFile(&strProgName
) != osl_Process_E_None
)
919 /* remove path and extension from filename */
920 pProgName
= strProgName
->buffer
;
921 nLen
= strProgName
->length
;
923 if ((nPos
= rtl_ustr_lastIndexOfChar( pProgName
, L
'/' )) != -1)
925 else if ((nPos
= rtl_ustr_lastIndexOfChar( pProgName
, L
':' )) != -1)
928 if ((nPos
= rtl_ustr_lastIndexOfChar( pProgName
, L
'.' )) != -1 )
931 if ((nFileLen
= nLen
- nOffset
) >= _MAX_PATH
)
934 strncpy(File
, pProgName
+ nOffset
, nFileLen
);
936 if (nFileLen
+ strlen(STR_INI_EXTENSION
) >= _MAX_PATH
)
939 /* add default extension */
940 strcpy(File
+ nFileLen
, STR_INI_EXTENSION
);
941 nFileLen
+= strlen(STR_INI_EXTENSION
);
943 rtl_uString_release( strProgName
);
949 /* build directory path */
950 if (strPath
&& strPath
->length
)
952 sal_Unicode
*pPath
= rtl_uString_getStr(strPath
);
953 sal_Int32 nLen
= rtl_uString_getLength(strPath
);
955 if ((rtl_ustr_ascii_compare_WithLength(pPath
, RTL_CONSTASCII_LENGTH(STR_INI_METAHOME
) , STR_INI_METAHOME
) == 0) &&
956 ((nLen
== RTL_CONSTASCII_LENGTH(STR_INI_METAHOME
)) || (pPath
[RTL_CONSTASCII_LENGTH(STR_INI_METAHOME
)] == '/')))
958 rtl_uString
* strHome
= NULL
;
959 oslSecurity security
= osl_getCurrentSecurity();
961 bFailed
= ! osl_getHomeDir(security
, &strHome
);
962 osl_freeSecurityHandle(security
);
964 if (bFailed
) return (sal_False
);
966 if (strHome
->length
>= _MAX_PATH
)
969 strcpy( Path
, strHome
->buffer
);
970 nPathLen
= strHome
->length
;
972 if (nLen
> RTL_CONSTASCII_LENGTH(STR_INI_METAHOME
))
974 pPath
+= RTL_CONSTASCII_LENGTH(STR_INI_METAHOME
);
975 nLen
-= RTL_CONSTASCII_LENGTH(STR_INI_METAHOME
);
977 if (nLen
+ nPathLen
>= _MAX_PATH
)
980 strcpy(Path
+ nPathLen
, pPath
);
984 rtl_uString_release(strHome
);
987 else if ((rtl_ustr_ascii_compare_WithLength(pPath
, RTL_CONSTASCII_LENGTH(STR_INI_METACFG
), STR_INI_METACFG
) == 0) &&
988 ((nLen
== RTL_CONSTASCII_LENGTH(STR_INI_METACFG
)) || (pPath
[RTL_CONSTASCII_LENGTH(STR_INI_METACFG
)] == '/')))
990 rtl_uString
* strConfig
= NULL
;
991 oslSecurity security
= osl_getCurrentSecurity();
993 bFailed
= ! osl_getConfigDir(security
, &strConfig
);
994 osl_freeSecurityHandle(security
);
996 if (bFailed
) return (sal_False
);
998 if (strConfig
->length
>= _MAX_PATH
)
1001 strcpy( Path
, strConfig
->buffer
);
1002 nPathLen
= strConfig
->length
;
1004 if (nLen
> RTL_CONSTASCII_LENGTH(STR_INI_METACFG
))
1006 pPath
+= RTL_CONSTASCII_LENGTH(STR_INI_METACFG
);
1007 nLen
-= RTL_CONSTASCII_LENGTH(STR_INI_METACFG
);
1009 if (nLen
+ nPathLen
>= _MAX_PATH
)
1012 strcpy(Path
+ nPathLen
, pPath
);
1016 rtl_uString_release(strConfig
);
1019 else if ((rtl_ustr_ascii_compare_WithLength(pPath
, RTL_CONSTASCII_LENGTH(STR_INI_METASYS
), STR_INI_METASYS
) == 0) &&
1020 ((nLen
== RTL_CONSTASCII_LENGTH(STR_INI_METASYS
)) || (pPath
[RTL_CONSTASCII_LENGTH(STR_INI_METASYS
)] == '/')))
1022 if (((nPathLen
= GetWindowsDirectoryW(Path
, _MAX_PATH
)) == 0) || (nPathLen
>= _MAX_PATH
))
1025 if (nLen
> RTL_CONSTASCII_LENGTH(STR_INI_METASYS
))
1027 pPath
+= RTL_CONSTASCII_LENGTH(STR_INI_METASYS
);
1028 nLen
-= RTL_CONSTASCII_LENGTH(STR_INI_METASYS
);
1030 if (nLen
+ nPathLen
>= MAX_PATH
)
1033 strcpy(Path
+ nPathLen
, pPath
);
1038 else if ((rtl_ustr_ascii_compare_WithLength(pPath
, RTL_CONSTASCII_LENGTH(STR_INI_METAINS
), STR_INI_METAINS
) == 0) &&
1039 ((nLen
== RTL_CONSTASCII_LENGTH(STR_INI_METAINS
)) || (pPath
[RTL_CONSTASCII_LENGTH(STR_INI_METAINS
)] == '/') ||
1040 (pPath
[RTL_CONSTASCII_LENGTH(STR_INI_METAINS
)] == '"') ) )
1042 if (! lookupProfile(pPath
+ RTL_CONSTASCII_LENGTH(STR_INI_METAINS
), File
, Path
))
1045 nPathLen
= strlen(Path
);
1048 else if(nLen
< MAX_PATH
)
1050 strcpy(Path
, pPath
);
1051 nPathLen
= strlen(Path
);
1058 rtl_uString
* strConfigDir
= NULL
;
1059 oslSecurity security
= osl_getCurrentSecurity();
1061 bFailed
= ! osl_getConfigDir(security
, &strConfigDir
);
1062 osl_freeSecurityHandle(security
);
1064 if (bFailed
) return (sal_False
);
1065 if (strConfigDir
->length
>= MAX_PATH
)
1068 strcpy(Path
, strConfigDir
->buffer
);
1069 nPathLen
= strConfigDir
->length
;
1072 if (nPathLen
&& (Path
[nPathLen
- 1] != L
'/') && (Path
[nPathLen
- 1] != L
'\\'))
1074 Path
[nPathLen
++] = L
'\\';
1078 if (nPathLen
+ nFileLen
>= MAX_PATH
)
1081 /* append file name */
1082 strcpy(Path
+ nPathLen
, File
);
1083 nPathLen
+= nFileLen
;
1086 rtl_uString_newFromStr_WithLength(&strTmp
, Path
, nPathLen
);
1087 nError
= osl_getFileURLFromSystemPath(strTmp
, strProfileName
);
1088 rtl_uString_release(strTmp
);
1090 return nError
== osl_File_E_None
;
1095 /*****************************************************************************/
1096 /* Static Module Functions */
1097 /*****************************************************************************/
1099 static osl_TStamp
getFileStamp(osl_TFile
* pFile
)
1101 osl_TStamp FileTime
;
1102 FILESTATUS3 FileStatus
;
1105 Bytes
= sizeof( FILESTATUS3
);
1106 if ( (!pFile
->m_Handle
) ||
1107 DosQueryFileInfo(pFile
->m_Handle
, FIL_STANDARD
, &FileStatus
, Bytes
))
1108 memset(&FileTime
, 0, sizeof(FileTime
));
1111 FileTime
.m_Date
= FileStatus
.fdateLastWrite
;
1112 FileTime
.m_Time
= FileStatus
.ftimeLastWrite
;
1118 static sal_Bool
lockFile(const osl_TFile
* pFile
, osl_TLockMode eMode
)
1120 sal_uInt32 status
= 1;
1123 if (!pFile
->m_Handle
)
1127 Lock
.lRange
= 0xFFFFFFFF;
1132 status
= DosSetFileLocks(pFile
->m_Handle
, &Lock
, NULL
, 1000, 0);
1136 status
= DosSetFileLocks(pFile
->m_Handle
, NULL
, &Lock
, 1000, 1);
1140 status
= DosSetFileLocks(pFile
->m_Handle
, NULL
, &Lock
, 1000, 0);
1144 return (status
== 0);
1147 //static osl_TFile* openFile(rtl_uString* pszFilename, sal_Bool bWriteable)
1148 static osl_TFile
* openFileImpl(rtl_uString
*ustrFileName
, oslProfileOption ProfileFlags
)
1152 osl_TFile
* pFile
= (osl_TFile
*)calloc(1, sizeof(osl_TFile
));
1157 sal_Bool bWriteable
= sal_False
;
1158 rtl_String
* strFileName
=0;
1159 sal_Char
* pszFileName
=0;
1161 /* check parameters */
1162 OSL_ASSERT( ustrFileName
);
1164 rtl_uString2String( &strFileName
,
1165 rtl_uString_getStr(ustrFileName
),
1166 rtl_uString_getLength(ustrFileName
),
1167 osl_getThreadTextEncoding(),
1168 OUSTRING_TO_OSTRING_CVTFLAGS
);
1169 pszFileName
= rtl_string_getStr(strFileName
);
1171 /* if ( ProfileFlags & ( osl_Profile_WRITELOCK | osl_Profile_FLUSHWRITE | osl_Profile_READWRITE ) )*/
1172 if ( ProfileFlags
& ( osl_Profile_WRITELOCK
| osl_Profile_FLUSHWRITE
) )
1174 #ifdef DEBUG_OSL_PROFILE
1175 OSL_TRACE("setting bWriteable to TRUE\n");
1177 bWriteable
=sal_True
;
1182 flags
= FILE_NORMAL
| FILE_ARCHIVED
;
1183 attributes
= OPEN_ACTION_CREATE_IF_NEW
| OPEN_ACTION_OPEN_IF_EXISTS
;
1184 mode
= OPEN_SHARE_DENYNONE
| OPEN_ACCESS_READWRITE
;
1188 flags
= FILE_NORMAL
;
1189 attributes
= OPEN_ACTION_FAIL_IF_NEW
| OPEN_ACTION_OPEN_IF_EXISTS
;
1190 mode
= OPEN_SHARE_DENYNONE
| OPEN_ACCESS_READONLY
;
1193 if (rc
= DosOpen((PCSZ
)pszFileName
, &pFile
->m_Handle
, &action
, 0, flags
, attributes
, mode
, NULL
))
1195 if (rc
== ERROR_TOO_MANY_OPEN_FILES
)
1199 rc
= DosSetRelMaxFH(&fhToAdd
, &fhOld
);
1200 rc
= DosOpen((PCSZ
)pszFileName
, &pFile
->m_Handle
, &action
, 0, flags
, attributes
, mode
, NULL
);
1204 if ( (rc
!= NO_ERROR
) && bWriteable
)
1207 rtl_string_release(strFileName
);
1211 rtl_string_release(strFileName
);
1213 pFile
->m_pWriteBuf
=0;
1214 pFile
->m_nWriteBufFree
=0;
1215 pFile
->m_nWriteBufLen
=0;
1217 if ( ProfileFlags
& (osl_Profile_WRITELOCK
| osl_Profile_READLOCK
) )
1219 #ifdef DEBUG_OSL_PROFILE
1220 OSL_TRACE("locking '%s' file\n",pszFilename
);
1223 lockFile(pFile
, bWriteable
? write_lock
: read_lock
);
1226 /* mfe: new WriteBuf obsolete */
1227 /* pFile->m_pWritePtr = pFile->m_Buf;*/
1228 /* pFile->m_pReadPtr = pFile->m_ReadBuf + sizeof(pFile->m_ReadBuf);*/
1233 //static osl_TStamp closeFile(osl_TFile* pFile)
1234 static osl_TStamp
closeFileImpl(osl_TFile
* pFile
)
1236 osl_TStamp stamp
= {0, 0};
1243 if (pFile
->m_Handle
)
1245 /* mfe: new WriteBuf obsolete */
1246 /* we just closing the file here, DO NOT write, it has to be handled in higher levels */
1247 //if (pFile->m_pWritePtr > pFile->m_WriteBuf)
1249 // sal_uInt32 Bytes;
1251 // DosWrite(pFile->m_Handle, pFile->m_WriteBuf,
1252 // pFile->m_pWritePtr - pFile->m_WriteBuf,
1256 stamp
= getFileStamp(pFile
);
1258 lockFile(pFile
, un_lock
);
1260 DosClose(pFile
->m_Handle
);
1263 if ( pFile
->m_pWriteBuf
!= 0 )
1265 free(pFile
->m_pWriteBuf
);
1273 static sal_Bool
rewindFile(osl_TFile
* pFile
, sal_Bool bTruncate
)
1275 if (pFile
->m_Handle
)
1277 sal_uInt32 Position
;
1279 /* mfe: new WriteBuf obsolete */
1280 /* we just closing the file here, DO NOT write, it has to be handled in higher levels */
1281 /* if (pFile->m_pWritePtr > pFile->m_WriteBuf)
1285 DosWrite(pFile->m_Handle, pFile->m_WriteBuf,
1286 pFile->m_pWritePtr - pFile->m_WriteBuf,
1289 pFile->m_pWritePtr = pFile->m_WriteBuf;
1292 pFile
->m_pReadPtr
= pFile
->m_ReadBuf
+ sizeof(pFile
->m_ReadBuf
);
1294 DosSetFilePtr(pFile
->m_Handle
, 0, FILE_BEGIN
, &Position
);
1297 DosSetFileSize(pFile
->m_Handle
, 0);
1303 static sal_Bool
getLine(osl_TFile
* pFile
, const sal_Char
*pszLine
, int MaxLen
)
1307 sal_Char
* pLine
= (sal_Char
*)pszLine
;
1310 if (pFile
->m_Handle
== 0)
1317 Bytes
= sizeof(pFile
->m_ReadBuf
) - (pFile
->m_pReadPtr
- pFile
->m_ReadBuf
);
1322 memcpy(pFile
->m_ReadBuf
, pFile
->m_pReadPtr
, Bytes
);
1323 pFile
->m_pReadPtr
= pFile
->m_ReadBuf
;
1325 Free
= sizeof(pFile
->m_ReadBuf
) - Bytes
;
1327 if (DosRead(pFile
->m_Handle
, &pFile
->m_ReadBuf
[Bytes
], Free
, &Max
))
1335 if ((Max
== 0) && (pLine
== pszLine
))
1341 pFile
->m_ReadBuf
[Bytes
+ Max
] = '\0';
1345 for (pChr
= pFile
->m_pReadPtr
;
1346 (*pChr
!= '\n') && (*pChr
!= '\r') && (*pChr
!= '\0') &&
1347 (pChr
< (pFile
->m_ReadBuf
+ sizeof(pFile
->m_ReadBuf
) - 1));
1350 Max
= min(pChr
- pFile
->m_pReadPtr
, MaxLen
);
1351 memcpy(pLine
, pFile
->m_pReadPtr
, Max
);
1355 if (pChr
< (pFile
->m_ReadBuf
+ sizeof(pFile
->m_ReadBuf
) - 1))
1359 if ((pChr
[0] == '\r') && (pChr
[1] == '\n'))
1365 if ((pChr
< (pFile
->m_ReadBuf
+ sizeof(pFile
->m_ReadBuf
))) &&
1367 pChr
= pFile
->m_ReadBuf
+ sizeof(pFile
->m_ReadBuf
);
1371 /* setting MaxLen to -1 indicates terminating read loop */
1375 pFile
->m_pReadPtr
= pChr
;
1382 static sal_Bool
putLine(osl_TFile
* pFile
, const sal_Char
*pszLine
)
1384 unsigned int Len
= strlen(pszLine
);
1386 #ifdef DEBUG_OSL_PROFILE
1390 if ( pFile
== 0 || pFile
->m_Handle
< 0 )
1395 if ( pFile
->m_pWriteBuf
== 0 )
1397 pFile
->m_pWriteBuf
= (sal_Char
*) malloc(Len
+3);
1398 pFile
->m_nWriteBufLen
= Len
+3;
1399 pFile
->m_nWriteBufFree
= Len
+3;
1403 if ( pFile
->m_nWriteBufFree
<= Len
+ 3 )
1407 pTmp
=(sal_Char
*) realloc(pFile
->m_pWriteBuf
,( ( pFile
->m_nWriteBufLen
+ Len
) * 2) );
1412 pFile
->m_pWriteBuf
= pTmp
;
1413 pFile
->m_nWriteBufFree
= pFile
->m_nWriteBufFree
+ pFile
->m_nWriteBufLen
+ ( 2 * Len
);
1414 pFile
->m_nWriteBufLen
= ( pFile
->m_nWriteBufLen
+ Len
) * 2;
1415 memset( (pFile
->m_pWriteBuf
) + ( pFile
->m_nWriteBufLen
- pFile
->m_nWriteBufFree
), 0, pFile
->m_nWriteBufFree
);
1421 memcpy(pFile
->m_pWriteBuf
+ ( pFile
->m_nWriteBufLen
- pFile
->m_nWriteBufFree
),pszLine
,Len
+1);
1422 #ifdef DEBUG_OSL_PROFILE
1423 strLen
= strlen(pFile
->m_pWriteBuf
);
1425 pFile
->m_pWriteBuf
[pFile
->m_nWriteBufLen
- pFile
->m_nWriteBufFree
+ Len
]='\r';
1426 pFile
->m_pWriteBuf
[pFile
->m_nWriteBufLen
- pFile
->m_nWriteBufFree
+ Len
+ 1]='\n';
1427 pFile
->m_pWriteBuf
[pFile
->m_nWriteBufLen
- pFile
->m_nWriteBufFree
+ Len
+ 2]='\0';
1429 pFile
->m_nWriteBufFree
-=Len
+2;
1431 #ifdef DEBUG_OSL_PROFILE
1432 /* OSL_TRACE("File Buffer in _putLine '%s' '%i'(%i)\n",pFile->m_pWriteBuf,strlen(pFile->m_pWriteBuf),pFile->m_nWriteBufLen - pFile->m_nWriteBufFree);*/
1438 /* platform specific end */
1440 static const sal_Char
* stripBlanks(const sal_Char
* String
, sal_uInt32
* pLen
)
1442 if ( (pLen
!= NULL
) && ( *pLen
!= 0 ) )
1444 while ((String
[*pLen
- 1] == ' ') || (String
[*pLen
- 1] == '\t'))
1447 while ((*String
== ' ') || (*String
== '\t'))
1454 while ((*String
== ' ') || (*String
== '\t'))
1460 static const sal_Char
* addLine(osl_TProfileImpl
* pProfile
, const sal_Char
* Line
)
1462 if (pProfile
->m_NoLines
>= pProfile
->m_MaxLines
)
1464 if (pProfile
->m_Lines
== NULL
)
1466 pProfile
->m_MaxLines
= LINES_INI
;
1467 pProfile
->m_Lines
= (sal_Char
**)malloc(pProfile
->m_MaxLines
* sizeof(sal_Char
*));
1471 pProfile
->m_MaxLines
+= LINES_ADD
;
1472 pProfile
->m_Lines
= (sal_Char
**)realloc(pProfile
->m_Lines
,
1473 pProfile
->m_MaxLines
* sizeof(sal_Char
*));
1476 if (pProfile
->m_Lines
== NULL
)
1478 pProfile
->m_NoLines
= 0;
1479 pProfile
->m_MaxLines
= 0;
1485 pProfile
->m_Lines
[pProfile
->m_NoLines
++] = strdup(Line
);
1487 return (pProfile
->m_Lines
[pProfile
->m_NoLines
- 1]);
1490 static const sal_Char
* insertLine(osl_TProfileImpl
* pProfile
, const sal_Char
* Line
, sal_uInt32 LineNo
)
1492 if (pProfile
->m_NoLines
>= pProfile
->m_MaxLines
)
1494 if (pProfile
->m_Lines
== NULL
)
1496 pProfile
->m_MaxLines
= LINES_INI
;
1497 pProfile
->m_Lines
= (sal_Char
**)malloc(pProfile
->m_MaxLines
* sizeof(sal_Char
*));
1501 pProfile
->m_MaxLines
+= LINES_ADD
;
1502 pProfile
->m_Lines
= (sal_Char
**)realloc(pProfile
->m_Lines
,
1503 pProfile
->m_MaxLines
* sizeof(sal_Char
*));
1506 if (pProfile
->m_Lines
== NULL
)
1508 pProfile
->m_NoLines
= 0;
1509 pProfile
->m_MaxLines
= 0;
1515 LineNo
= LineNo
> pProfile
->m_NoLines
? pProfile
->m_NoLines
: LineNo
;
1517 if (LineNo
< pProfile
->m_NoLines
)
1520 osl_TProfileSection
* pSec
;
1522 memmove(&pProfile
->m_Lines
[LineNo
+ 1], &pProfile
->m_Lines
[LineNo
],
1523 (pProfile
->m_NoLines
- LineNo
) * sizeof(sal_Char
*));
1525 /* adjust line references */
1526 for (i
= 0; i
< pProfile
->m_NoSections
; i
++)
1528 pSec
= &pProfile
->m_Sections
[i
];
1530 if (pSec
->m_Line
>= LineNo
)
1533 for (n
= 0; n
< pSec
->m_NoEntries
; n
++)
1534 if (pSec
->m_Entries
[n
].m_Line
>= LineNo
)
1535 pSec
->m_Entries
[n
].m_Line
++;
1539 pProfile
->m_NoLines
++;
1541 pProfile
->m_Lines
[LineNo
] = strdup(Line
);
1543 return (pProfile
->m_Lines
[LineNo
]);
1546 static void removeLine(osl_TProfileImpl
* pProfile
, sal_uInt32 LineNo
)
1548 if (LineNo
< pProfile
->m_NoLines
)
1550 free(pProfile
->m_Lines
[LineNo
]);
1551 if (pProfile
->m_NoLines
- LineNo
> 1)
1554 osl_TProfileSection
* pSec
;
1556 memmove(&pProfile
->m_Lines
[LineNo
], &pProfile
->m_Lines
[LineNo
+ 1],
1557 (pProfile
->m_NoLines
- LineNo
- 1) * sizeof(sal_Char
*));
1559 /* adjust line references */
1560 for (i
= 0; i
< pProfile
->m_NoSections
; i
++)
1562 pSec
= &pProfile
->m_Sections
[i
];
1564 if (pSec
->m_Line
> LineNo
)
1567 for (n
= 0; n
< pSec
->m_NoEntries
; n
++)
1568 if (pSec
->m_Entries
[n
].m_Line
> LineNo
)
1569 pSec
->m_Entries
[n
].m_Line
--;
1574 pProfile
->m_Lines
[LineNo
] = 0;
1577 pProfile
->m_NoLines
--;
1583 static void setEntry(osl_TProfileImpl
* pProfile
, osl_TProfileSection
* pSection
,
1584 sal_uInt32 NoEntry
, sal_uInt32 Line
,
1585 const sal_Char
* Entry
, sal_uInt32 Len
)
1587 Entry
= stripBlanks(Entry
, &Len
);
1588 pSection
->m_Entries
[NoEntry
].m_Line
= Line
;
1589 pSection
->m_Entries
[NoEntry
].m_Offset
= Entry
- pProfile
->m_Lines
[Line
];
1590 pSection
->m_Entries
[NoEntry
].m_Len
= Len
;
1595 static sal_Bool
addEntry(osl_TProfileImpl
* pProfile
, osl_TProfileSection
*pSection
,
1596 int Line
, const sal_Char
* Entry
, sal_uInt32 Len
)
1598 if (pSection
!= NULL
)
1600 if (pSection
->m_NoEntries
>= pSection
->m_MaxEntries
)
1602 if (pSection
->m_Entries
== NULL
)
1604 pSection
->m_MaxEntries
= ENTRIES_INI
;
1605 pSection
->m_Entries
= (osl_TProfileEntry
*)malloc(
1606 pSection
->m_MaxEntries
* sizeof(osl_TProfileEntry
));
1610 pSection
->m_MaxEntries
+= ENTRIES_ADD
;
1611 pSection
->m_Entries
= (osl_TProfileEntry
*)realloc(pSection
->m_Entries
,
1612 pSection
->m_MaxEntries
* sizeof(osl_TProfileEntry
));
1615 if (pSection
->m_Entries
== NULL
)
1617 pSection
->m_NoEntries
= 0;
1618 pSection
->m_MaxEntries
= 0;
1623 pSection
->m_NoEntries
++;
1625 Entry
= stripBlanks(Entry
, &Len
);
1626 setEntry(pProfile
, pSection
, pSection
->m_NoEntries
- 1, Line
,
1635 static void removeEntry(osl_TProfileSection
*pSection
, sal_uInt32 NoEntry
)
1637 if (NoEntry
< pSection
->m_NoEntries
)
1639 if (pSection
->m_NoEntries
- NoEntry
> 1)
1640 memmove(&pSection
->m_Entries
[NoEntry
],
1641 &pSection
->m_Entries
[NoEntry
+ 1],
1642 (pSection
->m_NoEntries
- NoEntry
- 1) * sizeof(osl_TProfileEntry
));
1643 pSection
->m_NoEntries
--;
1649 static sal_Bool
addSection(osl_TProfileImpl
* pProfile
, int Line
, const sal_Char
* Section
, sal_uInt32 Len
)
1651 if (pProfile
->m_NoSections
>= pProfile
->m_MaxSections
)
1653 if (pProfile
->m_Sections
== NULL
)
1655 pProfile
->m_MaxSections
= SECTIONS_INI
;
1656 pProfile
->m_Sections
= (osl_TProfileSection
*)malloc(pProfile
->m_MaxSections
* sizeof(osl_TProfileSection
));
1660 pProfile
->m_MaxSections
+= SECTIONS_ADD
;
1661 pProfile
->m_Sections
= (osl_TProfileSection
*)realloc(pProfile
->m_Sections
,
1662 pProfile
->m_MaxSections
* sizeof(osl_TProfileSection
));
1665 if (pProfile
->m_Sections
== NULL
)
1667 pProfile
->m_NoSections
= 0;
1668 pProfile
->m_MaxSections
= 0;
1673 pProfile
->m_NoSections
++;
1675 pProfile
->m_Sections
[pProfile
->m_NoSections
- 1].m_Entries
= NULL
;
1676 pProfile
->m_Sections
[pProfile
->m_NoSections
- 1].m_NoEntries
= 0;
1677 pProfile
->m_Sections
[pProfile
->m_NoSections
- 1].m_MaxEntries
= 0;
1679 Section
= (sal_Char
*)stripBlanks(Section
, &Len
);
1680 pProfile
->m_Sections
[pProfile
->m_NoSections
- 1].m_Line
= Line
;
1681 pProfile
->m_Sections
[pProfile
->m_NoSections
- 1].m_Offset
= Section
- pProfile
->m_Lines
[Line
];
1682 pProfile
->m_Sections
[pProfile
->m_NoSections
- 1].m_Len
= Len
;
1687 static void removeSection(osl_TProfileImpl
* pProfile
, osl_TProfileSection
*pSection
)
1691 if ((Section
= pSection
- pProfile
->m_Sections
) < pProfile
->m_NoSections
)
1693 free (pSection
->m_Entries
);
1694 if (pProfile
->m_NoSections
- Section
> 1)
1696 memmove(&pProfile
->m_Sections
[Section
], &pProfile
->m_Sections
[Section
+ 1],
1697 (pProfile
->m_NoSections
- Section
- 1) * sizeof(osl_TProfileSection
));
1701 pSection
->m_Entries
= 0;
1704 pProfile
->m_NoSections
--;
1710 static osl_TProfileSection
* findEntry(osl_TProfileImpl
* pProfile
, const sal_Char
* Section
,
1711 const sal_Char
* Entry
, sal_uInt32
*pNoEntry
)
1713 static sal_uInt32 Sect
= 0;
1716 const sal_Char
* pStr
;
1717 osl_TProfileSection
* pSec
;
1719 Len
= strlen(Section
);
1720 Section
= (sal_Char
*)stripBlanks(Section
, &Len
);
1724 for (i
= 0; i
< pProfile
->m_NoSections
; i
++)
1726 n
%= pProfile
->m_NoSections
;
1727 pSec
= &pProfile
->m_Sections
[n
];
1728 if ((Len
== pSec
->m_Len
) &&
1729 (strnicmp(Section
, &pProfile
->m_Lines
[pSec
->m_Line
][pSec
->m_Offset
], pSec
->m_Len
)
1737 if (i
< pProfile
->m_NoSections
)
1739 Len
= strlen(Entry
);
1740 Entry
= stripBlanks(Entry
, &Len
);
1742 *pNoEntry
= pSec
->m_NoEntries
;
1744 for (i
= 0; i
< pSec
->m_NoEntries
; i
++)
1746 pStr
= &pProfile
->m_Lines
[pSec
->m_Entries
[i
].m_Line
]
1747 [pSec
->m_Entries
[i
].m_Offset
];
1748 if ((Len
== pSec
->m_Entries
[i
].m_Len
) &&
1749 (strnicmp(Entry
, pStr
, pSec
->m_Entries
[i
].m_Len
)
1763 static sal_Bool
loadProfile(osl_TFile
* pFile
, osl_TProfileImpl
* pProfile
)
1768 sal_Char Line
[1024];
1770 pProfile
->m_NoLines
= 0;
1771 pProfile
->m_NoSections
= 0;
1773 OSL_VERIFY(rewindFile(pFile
, sal_False
));
1775 while (getLine(pFile
, Line
, sizeof(Line
)))
1777 if (! addLine(pProfile
, Line
))
1781 for (i
= 0; i
< pProfile
->m_NoLines
; i
++)
1783 pStr
= (sal_Char
*)stripBlanks(pProfile
->m_Lines
[i
], NULL
);
1785 if ((*pStr
== '\0') || (*pStr
== ';'))
1788 if ((*pStr
!= '[') || ((pChar
= strrchr(pStr
, ']')) == NULL
) ||
1789 ((pChar
- pStr
) <= 2))
1793 if (pProfile
->m_NoSections
< 1)
1796 if ((pChar
= strchr(pStr
, '=')) == NULL
)
1797 pChar
= pStr
+ strlen(pStr
);
1799 if (! addEntry(pProfile
, &pProfile
->m_Sections
[pProfile
->m_NoSections
- 1],
1800 i
, pStr
, pChar
- pStr
))
1807 if (! addSection(pProfile
, i
, pStr
+ 1, pChar
- pStr
- 1))
1815 static sal_Bool
storeProfile(osl_TFile
* pFile
, osl_TProfileImpl
* pProfile
, sal_Bool bCleanup
)
1817 if (pProfile
->m_Lines
!= NULL
)
1819 if (pProfile
->m_Flags
& FLG_MODIFIED
)
1823 OSL_VERIFY(rewindFile(pFile
, sal_True
));
1825 for (i
= 0; i
< pProfile
->m_NoLines
; i
++)
1826 OSL_VERIFY(putLine(pFile
, pProfile
->m_Lines
[i
]));
1828 pProfile
->m_Flags
&= ~FLG_MODIFIED
;
1833 while (pProfile
->m_NoLines
> 0)
1834 removeLine(pProfile
, pProfile
->m_NoLines
- 1);
1836 free(pProfile
->m_Lines
);
1837 pProfile
->m_Lines
= NULL
;
1838 pProfile
->m_MaxLines
= 0;
1840 while (pProfile
->m_NoSections
> 0)
1841 removeSection(pProfile
, &pProfile
->m_Sections
[pProfile
->m_NoSections
- 1]);
1843 free(pProfile
->m_Sections
);
1844 pProfile
->m_Sections
= NULL
;
1845 pProfile
->m_MaxSections
= 0;
1852 static osl_TProfileImpl
* acquireProfile(oslProfile Profile
, sal_Bool bWriteable
)
1854 osl_TProfileImpl
* pProfile
= (osl_TProfileImpl
*)Profile
;
1855 oslProfileOption PFlags
=0;
1860 /* PFlags = osl_Profile_DEFAULT | osl_Profile_READWRITE; */
1861 PFlags
= osl_Profile_DEFAULT
| osl_Profile_WRITELOCK
;
1865 PFlags
= osl_Profile_DEFAULT
;
1869 if (pProfile
== NULL
)
1871 #ifdef DEBUG_OSL_PROFILE
1872 OSL_TRACE("AUTOOPEN MODE\n");
1875 if ((pProfile
= (osl_TProfileImpl
*)osl_openProfile(NULL
, PFlags
)) != NULL
)
1877 pProfile
->m_Flags
|= FLG_AUTOOPEN
;
1882 #ifdef DEBUG_OSL_PROFILE
1883 OSL_TRACE("try to acquire\n");
1888 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
1890 if (! (pProfile
->m_Flags
& (osl_Profile_READLOCK
|
1891 osl_Profile_WRITELOCK
| osl_Profile_FLUSHWRITE
)))
1894 #ifdef DEBUG_OSL_PROFILE
1895 OSL_TRACE("DEFAULT MODE\n");
1897 if (! (pProfile
->m_pFile
= openFileImpl(pProfile
->m_strFileName
, pProfile
->m_Flags
| PFlags
)))
1900 Stamp
= getFileStamp(pProfile
->m_pFile
);
1902 if (memcmp(&Stamp
, &(pProfile
->m_Stamp
), sizeof(osl_TStamp
)))
1904 pProfile
->m_Stamp
= Stamp
;
1906 loadProfile(pProfile
->m_pFile
, pProfile
);
1911 #ifdef DEBUG_OSL_PROFILE
1912 OSL_TRACE("READ/WRITELOCK MODE\n");
1916 /* A readlock file could not be written */
1917 if ((pProfile
->m_Flags
& osl_Profile_READLOCK
) && bWriteable
)
1925 sal_Bool bWriteable
= sal_False
;
1926 char pszFilename
[PATH_MAX
] = "";
1928 if ( pProfile
->m_strFileName
!= 0 && pProfile
->m_strFileName
->buffer
[0] != 0 )
1929 FileURLToPath( pszFilename
, PATH_MAX
, pProfile
->m_strFileName
);
1930 /* hack: usualy you have a specific HAB, but NULL works here... */
1931 pProfile
->m_hIni
= PrfOpenProfile(NULL
, (PCSZ
)pszFilename
);
1932 if (! pProfile
->m_hIni
)
1940 static sal_Bool
releaseProfile(osl_TProfileImpl
* pProfile
)
1942 #ifdef TRACE_OSL_PROFILE
1943 OSL_TRACE("In releaseProfile\n");
1946 if ( pProfile
== 0 )
1948 #ifdef TRACE_OSL_PROFILE
1949 OSL_TRACE("Out releaseProfile [profile==0]\n");
1954 if (! (pProfile
->m_Flags
& osl_Profile_SYSTEM
))
1956 if (pProfile
->m_Flags
& FLG_AUTOOPEN
)
1958 #ifdef TRACE_OSL_PROFILE
1959 OSL_TRACE("Out releaseProfile [AUTOOPEN]\n");
1961 return (osl_closeProfile((oslProfile
)pProfile
));
1965 #ifdef DEBUG_OSL_PROFILE
1966 OSL_TRACE("DEFAULT MODE\n");
1968 if (! (pProfile
->m_Flags
& (osl_Profile_READLOCK
|
1969 osl_Profile_WRITELOCK
| osl_Profile_FLUSHWRITE
)))
1971 if (pProfile
->m_Flags
& FLG_MODIFIED
)
1972 storeProfile(pProfile
->m_pFile
, pProfile
, sal_False
);
1974 closeFileImpl(pProfile
->m_pFile
);
1975 pProfile
->m_pFile
= NULL
;
1980 PrfCloseProfile(pProfile
->m_hIni
);
1982 #ifdef TRACE_OSL_PROFILE
1983 OSL_TRACE("Out releaseProfile [ok]\n");
1990 static sal_Bool
lookupProfile(const sal_Char
*pszPath
, const sal_Char
*pszFile
, sal_Char
*pPath
)
1992 sal_Char
*pChr
, *pStr
;
1993 sal_Char Path
[_MAX_PATH
] = "";
1994 sal_Char Product
[132] = "";
1995 sal_Char Buffer
[1024];
1997 if (*pszPath
== '"')
2003 while ((*pszPath
!= '"') && (*pszPath
!= '\0'))
2004 Product
[i
++] = *pszPath
++;
2008 if (*pszPath
== '"')
2011 if ( (*pszPath
== '/') || (*pszPath
== '\\') )
2018 /* if we have not product identfication, do a special handling for soffice.ini */
2019 if (stricmp(SVERSION_PROFILE
, pszFile
) == 0)
2021 sal_Char Profile
[_MAX_PATH
];
2022 sal_Char Dir
[_MAX_PATH
];
2023 oslProfile hProfile
;
2025 /* open sversion.ini in the system directory, and try to locate the entry
2026 with the highest version for StarOffice */
2027 if ((osl_getProfileName(SVERSION_FALLBACK
, SVERSION_NAME
, Profile
, sizeof(Profile
))) &&
2028 (hProfile
= osl_openProfile(Profile
, osl_Profile_READLOCK
)))
2030 osl_getProfileSectionEntries(hProfile
, SVERSION_SECTION
,
2031 Buffer
, sizeof(Buffer
));
2033 for (pChr
= Buffer
; *pChr
!= '\0'; pChr
+= strlen(pChr
) + 1)
2035 if ((strnicmp(pChr
, SVERSION_SOFFICE
, sizeof(SVERSION_SOFFICE
) - 1) == 0) &&
2036 (stricmp(Product
, pChr
) < 0))
2038 osl_readProfileString(hProfile
, SVERSION_SECTION
, pChr
,
2039 Dir
, sizeof(Dir
), "");
2041 /* check for existence of path */
2042 if (access(Dir
, 0) >= 0)
2043 strcpy(Product
, pChr
);
2047 osl_closeProfile(hProfile
);
2050 /* open sversion.ini in the users directory, and try to locate the entry
2051 with the highest version for StarOffice */
2052 if ((strcmp(SVERSION_LOCATION
, SVERSION_FALLBACK
) != 0) &&
2053 (osl_getProfileName(SVERSION_LOCATION
, SVERSION_NAME
, Profile
, sizeof(Profile
))) &&
2054 (hProfile
= osl_openProfile(Profile
, osl_Profile_READLOCK
)))
2056 osl_getProfileSectionEntries(hProfile
, SVERSION_SECTION
,
2057 Buffer
, sizeof(Buffer
));
2059 for (pChr
= Buffer
; *pChr
!= '\0'; pChr
+= strlen(pChr
) + 1)
2061 if ((strnicmp(pChr
, SVERSION_SOFFICE
, sizeof(SVERSION_SOFFICE
) - 1) == 0) &&
2062 (stricmp(Product
, pChr
) < 0))
2064 osl_readProfileString(hProfile
, SVERSION_SECTION
, pChr
,
2065 Dir
, sizeof(Dir
), "");
2067 /* check for existence of path */
2068 if (access(Dir
, 0) >= 0)
2069 strcpy(Product
, pChr
);
2073 osl_closeProfile(hProfile
);
2076 /* remove any trailing build number */
2077 if ((pChr
= strrchr(Product
, '/')) != NULL
)
2083 /* if we have an userid option eg. "-userid:rh[/usr/home/rh/staroffice]",
2084 this will supercede all other locations */
2085 if (osl_getCommandArgs(Buffer
, sizeof(Buffer
)) == osl_Process_E_None
)
2087 sal_Char
*pStart
, *pEnd
;
2089 for (pChr
= Buffer
; *pChr
!= '\0'; pChr
+= strlen(pChr
) + 1)
2090 if (((*pChr
== '-') || (*pChr
== '+')) &&
2091 (strnicmp(pChr
+ 1, SVERSION_OPTION
, sizeof(SVERSION_OPTION
) - 1) == 0))
2093 if (((pStart
= strchr(pChr
+ sizeof(SVERSION_OPTION
), '[')) != NULL
) &&
2094 ((pEnd
= strchr(pStart
+ 1, ']')) != NULL
))
2096 strncpy(Path
, pStart
+ 1, pEnd
- (pStart
+ 1));
2097 Path
[pEnd
- (pStart
+ 1)] = '\0';
2099 /* build full path */
2100 if ((Path
[strlen(Path
) - 1] != '/') && (Path
[strlen(Path
) - 1] != '\\'))
2105 pChr
=&Path
[strlen(Path
)];
2106 if ( strlen(pszPath
) <= 0 )
2108 strcat(Path
,SVERSION_USER
);
2110 if ( access(Path
, 0) < 0 )
2117 strcat(Path
, pszPath
);
2125 if (strlen(Path
) <= 0)
2127 /* try to find the file in the directory of the executbale */
2128 if (osl_getExecutableFile(Path
, sizeof(Path
)) != osl_Process_E_None
)
2131 /* seperate path from filename */
2132 if ((pChr
= strrchr(Path
, '\\')) == NULL
)
2133 if ((pChr
= strrchr(Path
, ':')) == NULL
)
2140 /* if we have no product identification use the executable file name */
2141 if (strlen(Product
) <= 0)
2143 strcpy(Product
, pChr
+ 1);
2145 /* remove extension */
2146 if ((pChr
= strrchr(Product
, '.')) != NULL
)
2150 /* remember last subdir */
2151 pStr
= strrchr(Path
, '\\');
2155 if ( strlen(pszPath
) <= 0 )
2157 strcat(Path
, pszPath
);
2161 strcat(Path
,pszPath
);
2164 /* if file not exists, remove any specified subdirectories
2165 like "bin" or "program" */
2166 if (((access(Path
, 0) < 0) && (pStr
!= NULL
)) || (strlen(pszPath
) <= 0))
2168 static sal_Char
*SubDirs
[] = SVERSION_DIRS
;
2172 for (i
= 0; i
< (sizeof(SubDirs
) / sizeof(SubDirs
[0])); i
++)
2173 if (strnicmp(pStr
+ 1, SubDirs
[i
], strlen(SubDirs
[i
])) == 0)
2175 if ( strlen(pszPath
) <= 0)
2177 strcpy(pStr
+ 1,SVERSION_USER
);
2178 if ( access(Path
, 0) < 0 )
2185 strcpy(pStr
+ 1, pszPath
);
2192 pChr
= &Path
[strlen(Path
)];
2193 if ((Path
[strlen(Path
) - 1] != '/') && (Path
[strlen(Path
) - 1] != '\\'))
2195 strcat(Path
, pszFile
);
2197 if ((access(Path
, 0) < 0) && (strlen(Product
) > 0))
2199 sal_Char Profile
[_MAX_PATH
];
2200 oslProfile hProfile
;
2202 /* remove appended filename */
2205 /* open sversion.ini in the system directory, and try to locate the entry
2206 with the highest version for StarOffice */
2207 if ((osl_getProfileName(SVERSION_LOCATION
, SVERSION_NAME
, Profile
, sizeof(Profile
))) &&
2208 (hProfile
= osl_openProfile(Profile
, osl_Profile_READLOCK
)))
2210 pChr
= &Product
[strlen(Product
)];
2212 /* append build number */
2213 strcat(Product
, "/");
2214 strcat(Product
, BUILD_STR(SUPD
));
2216 osl_readProfileString(hProfile
, SVERSION_SECTION
, Product
,
2217 Buffer
, sizeof(Buffer
), "");
2219 /* if not found, try it without build number */
2220 if (strlen(Buffer
) <= 0)
2224 osl_readProfileString(hProfile
, SVERSION_SECTION
, Product
,
2225 Buffer
, sizeof(Buffer
), "");
2227 osl_closeProfile(hProfile
);
2229 /* if not found, try the fallback */
2230 if ((strlen(Buffer
) <= 0) && (strcmp(SVERSION_LOCATION
, SVERSION_FALLBACK
) != 0))
2232 if ((osl_getProfileName(SVERSION_FALLBACK
, SVERSION_NAME
, Profile
, sizeof(Profile
))) &&
2233 (hProfile
= osl_openProfile(Profile
, osl_Profile_READLOCK
)))
2235 /* prepare build number */
2238 osl_readProfileString(hProfile
, SVERSION_SECTION
, Product
,
2239 Buffer
, sizeof(Buffer
), "");
2241 /* if not found, try it without build number */
2242 if (strlen(Buffer
) <= 0)
2246 osl_readProfileString(hProfile
, SVERSION_SECTION
, Product
,
2247 Buffer
, sizeof(Buffer
), "");
2250 osl_closeProfile(hProfile
);
2255 osl_closeProfile(hProfile
);
2257 if (strlen(Buffer
) > 0)
2259 strcpy(Path
, Buffer
);
2261 /* build full path */
2262 if ((Path
[strlen(Path
) - 1] != '/') && (Path
[strlen(Path
) - 1] != '\\'))
2264 if ((*pszPath
!= '/') && (*pszPath
!= '\\'))
2268 pChr
=&Path
[strlen(pszPath
)];
2269 if ( strlen(pszPath
) > 0 )
2271 strcat(Path
, pszPath
);
2275 strcat(Path
,SVERSION_USER
);
2276 if ( access(Path
, 0) < 0 )
2285 /* remove appended filename */
2289 strcpy(pPath
, Path
);