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