4 * Copyright 1993 Miguel de Icaza
5 * Copyright 1996 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
33 #include "wine/winbase16.h"
34 #include "wine/unicode.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(profile
);
40 static const char bom_utf8
[] = {0xEF,0xBB,0xBF};
50 typedef struct tagPROFILEKEY
53 struct tagPROFILEKEY
*next
;
57 typedef struct tagPROFILESECTION
59 struct tagPROFILEKEY
*key
;
60 struct tagPROFILESECTION
*next
;
68 PROFILESECTION
*section
;
70 FILETIME LastWriteTime
;
75 #define N_CACHED_PROFILES 10
77 /* Cached profile files */
78 static PROFILE
*MRUProfile
[N_CACHED_PROFILES
]={NULL
};
80 #define CurProfile (MRUProfile[0])
82 /* Check for comments in profile */
83 #define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
85 static const WCHAR emptystringW
[] = {0};
86 static const WCHAR wininiW
[] = { 'w','i','n','.','i','n','i',0 };
88 static CRITICAL_SECTION PROFILE_CritSect
;
89 static CRITICAL_SECTION_DEBUG critsect_debug
=
91 0, 0, &PROFILE_CritSect
,
92 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
93 0, 0, { (DWORD_PTR
)(__FILE__
": PROFILE_CritSect") }
95 static CRITICAL_SECTION PROFILE_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
97 static const char hex
[16] = "0123456789ABCDEF";
99 /***********************************************************************
102 * Copy the content of an entry into a buffer, removing quotes, and possibly
103 * translating environment variables.
105 static void PROFILE_CopyEntry( LPWSTR buffer
, LPCWSTR value
, int len
,
112 if (strip_quote
&& ((*value
== '\'') || (*value
== '\"')))
114 if (value
[1] && (value
[strlenW(value
)-1] == *value
)) quote
= *value
++;
117 lstrcpynW( buffer
, value
, len
);
118 if (quote
&& (len
>= strlenW(value
))) buffer
[strlenW(buffer
)-1] = '\0';
121 /* byte-swaps shorts in-place in a buffer. len is in WCHARs */
122 static inline void PROFILE_ByteSwapShortBuffer(WCHAR
* buffer
, int len
)
125 USHORT
* shortbuffer
= (USHORT
*)buffer
;
126 for (i
= 0; i
< len
; i
++)
127 shortbuffer
[i
] = RtlUshortByteSwap(shortbuffer
[i
]);
130 /* writes any necessary encoding marker to the file */
131 static inline void PROFILE_WriteMarker(HANDLE hFile
, ENCODING encoding
)
133 DWORD dwBytesWritten
;
140 WriteFile(hFile
, bom_utf8
, sizeof(bom_utf8
), &dwBytesWritten
, NULL
);
142 case ENCODING_UTF16LE
:
144 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
146 case ENCODING_UTF16BE
:
148 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
153 static void PROFILE_WriteLine( HANDLE hFile
, WCHAR
* szLine
, int len
, ENCODING encoding
)
156 int write_buffer_len
;
157 DWORD dwBytesWritten
;
159 TRACE("writing: %s\n", debugstr_wn(szLine
, len
));
164 write_buffer_len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
165 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
166 if (!write_buffer
) return;
167 len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
168 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
169 HeapFree(GetProcessHeap(), 0, write_buffer
);
172 write_buffer_len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
173 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
174 if (!write_buffer
) return;
175 len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
176 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
177 HeapFree(GetProcessHeap(), 0, write_buffer
);
179 case ENCODING_UTF16LE
:
180 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
182 case ENCODING_UTF16BE
:
183 PROFILE_ByteSwapShortBuffer(szLine
, len
);
184 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
187 FIXME("encoding type %d not implemented\n", encoding
);
191 /***********************************************************************
194 * Save a profile tree to a file.
196 static void PROFILE_Save( HANDLE hFile
, const PROFILESECTION
*section
, ENCODING encoding
)
201 PROFILE_WriteMarker(hFile
, encoding
);
203 for ( ; section
; section
= section
->next
)
207 if (section
->name
[0]) len
+= strlenW(section
->name
) + 6;
209 for (key
= section
->key
; key
; key
= key
->next
)
211 len
+= strlenW(key
->name
) + 2;
212 if (key
->value
) len
+= strlenW(key
->value
) + 1;
215 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
219 if (section
->name
[0])
224 strcpyW( p
, section
->name
);
230 for (key
= section
->key
; key
; key
= key
->next
)
232 strcpyW( p
, key
->name
);
237 strcpyW( p
, key
->value
);
243 PROFILE_WriteLine( hFile
, buffer
, len
, encoding
);
244 HeapFree(GetProcessHeap(), 0, buffer
);
249 /***********************************************************************
252 * Free a profile tree.
254 static void PROFILE_Free( PROFILESECTION
*section
)
256 PROFILESECTION
*next_section
;
257 PROFILEKEY
*key
, *next_key
;
259 for ( ; section
; section
= next_section
)
261 for (key
= section
->key
; key
; key
= next_key
)
263 next_key
= key
->next
;
264 HeapFree( GetProcessHeap(), 0, key
->value
);
265 HeapFree( GetProcessHeap(), 0, key
);
267 next_section
= section
->next
;
268 HeapFree( GetProcessHeap(), 0, section
);
272 /* returns 1 if a character white space else 0 */
273 static inline int PROFILE_isspaceW(WCHAR c
)
275 if (isspaceW(c
)) return 1;
276 if (c
=='\r' || c
==0x1a) return 1;
277 /* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */
281 static inline ENCODING
PROFILE_DetectTextEncoding(const void * buffer
, int * len
)
283 int flags
= IS_TEXT_UNICODE_SIGNATURE
|
284 IS_TEXT_UNICODE_REVERSE_SIGNATURE
|
285 IS_TEXT_UNICODE_ODD_LENGTH
;
286 if (*len
>= sizeof(bom_utf8
) && !memcmp(buffer
, bom_utf8
, sizeof(bom_utf8
)))
288 *len
= sizeof(bom_utf8
);
289 return ENCODING_UTF8
;
291 RtlIsTextUnicode(buffer
, *len
, &flags
);
292 if (flags
& IS_TEXT_UNICODE_SIGNATURE
)
294 *len
= sizeof(WCHAR
);
295 return ENCODING_UTF16LE
;
297 if (flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
299 *len
= sizeof(WCHAR
);
300 return ENCODING_UTF16BE
;
303 return ENCODING_ANSI
;
307 /***********************************************************************
310 * Load a profile tree from a file.
312 static PROFILESECTION
*PROFILE_Load(HANDLE hFile
, ENCODING
* pEncoding
)
314 void *buffer_base
, *pBuffer
;
316 const WCHAR
*szLineStart
, *szLineEnd
;
317 const WCHAR
*szValueStart
, *szEnd
, *next_line
;
319 PROFILESECTION
*section
, *first_section
;
320 PROFILESECTION
**next_section
;
321 PROFILEKEY
*key
, *prev_key
, **next_key
;
324 TRACE("%p\n", hFile
);
326 dwFileSize
= GetFileSize(hFile
, NULL
);
327 if (dwFileSize
== INVALID_FILE_SIZE
)
330 buffer_base
= HeapAlloc(GetProcessHeap(), 0 , dwFileSize
);
331 if (!buffer_base
) return NULL
;
333 if (!ReadFile(hFile
, buffer_base
, dwFileSize
, &dwFileSize
, NULL
))
335 HeapFree(GetProcessHeap(), 0, buffer_base
);
336 WARN("Error %d reading file\n", GetLastError());
340 *pEncoding
= PROFILE_DetectTextEncoding(buffer_base
, &len
);
341 /* len is set to the number of bytes in the character marker.
342 * we want to skip these bytes */
343 pBuffer
= (char *)buffer_base
+ len
;
348 TRACE("ANSI encoding\n");
350 len
= MultiByteToWideChar(CP_ACP
, 0, (char *)pBuffer
, dwFileSize
, NULL
, 0);
351 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
354 HeapFree(GetProcessHeap(), 0, buffer_base
);
357 MultiByteToWideChar(CP_ACP
, 0, (char *)pBuffer
, dwFileSize
, szFile
, len
);
358 szEnd
= szFile
+ len
;
361 TRACE("UTF8 encoding\n");
363 len
= MultiByteToWideChar(CP_UTF8
, 0, (char *)pBuffer
, dwFileSize
, NULL
, 0);
364 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
367 HeapFree(GetProcessHeap(), 0, buffer_base
);
370 MultiByteToWideChar(CP_UTF8
, 0, (char *)pBuffer
, dwFileSize
, szFile
, len
);
371 szEnd
= szFile
+ len
;
373 case ENCODING_UTF16LE
:
374 TRACE("UTF16 Little Endian encoding\n");
375 szFile
= (WCHAR
*)pBuffer
;
376 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
378 case ENCODING_UTF16BE
:
379 TRACE("UTF16 Big Endian encoding\n");
380 szFile
= (WCHAR
*)pBuffer
;
381 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
382 PROFILE_ByteSwapShortBuffer(szFile
, dwFileSize
/ sizeof(WCHAR
));
385 FIXME("encoding type %d not implemented\n", *pEncoding
);
386 HeapFree(GetProcessHeap(), 0, buffer_base
);
390 first_section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) );
391 if(first_section
== NULL
)
393 if (szFile
!= pBuffer
)
394 HeapFree(GetProcessHeap(), 0, szFile
);
395 HeapFree(GetProcessHeap(), 0, buffer_base
);
398 first_section
->name
[0] = 0;
399 first_section
->key
= NULL
;
400 first_section
->next
= NULL
;
401 next_section
= &first_section
->next
;
402 next_key
= &first_section
->key
;
406 while (next_line
< szEnd
)
408 szLineStart
= next_line
;
409 next_line
= memchrW(szLineStart
, '\n', szEnd
- szLineStart
);
410 if (!next_line
) next_line
= szEnd
;
412 szLineEnd
= next_line
;
416 /* get rid of white space */
417 while (szLineStart
< szLineEnd
&& PROFILE_isspaceW(*szLineStart
)) szLineStart
++;
418 while ((szLineEnd
> szLineStart
) && ((szLineEnd
[-1] == '\n') || PROFILE_isspaceW(szLineEnd
[-1]))) szLineEnd
--;
420 if (szLineStart
>= szLineEnd
) continue;
422 if (*szLineStart
== '[') /* section start */
424 const WCHAR
* szSectionEnd
;
425 if (!(szSectionEnd
= memrchrW( szLineStart
, ']', szLineEnd
- szLineStart
)))
427 WARN("Invalid section header at line %d: %s\n",
428 line
, debugstr_wn(szLineStart
, (int)(szLineEnd
- szLineStart
)) );
433 len
= (int)(szSectionEnd
- szLineStart
);
434 /* no need to allocate +1 for NULL terminating character as
435 * already included in structure */
436 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) + len
* sizeof(WCHAR
) )))
438 memcpy(section
->name
, szLineStart
, len
* sizeof(WCHAR
));
439 section
->name
[len
] = '\0';
441 section
->next
= NULL
;
442 *next_section
= section
;
443 next_section
= §ion
->next
;
444 next_key
= §ion
->key
;
447 TRACE("New section: %s\n", debugstr_w(section
->name
));
453 /* get rid of white space after the name and before the start
455 len
= szLineEnd
- szLineStart
;
456 if ((szValueStart
= memchrW( szLineStart
, '=', szLineEnd
- szLineStart
)) != NULL
)
458 const WCHAR
*szNameEnd
= szValueStart
;
459 while ((szNameEnd
> szLineStart
) && PROFILE_isspaceW(szNameEnd
[-1])) szNameEnd
--;
460 len
= szNameEnd
- szLineStart
;
462 while (szValueStart
< szLineEnd
&& PROFILE_isspaceW(*szValueStart
)) szValueStart
++;
465 if (len
|| !prev_key
|| *prev_key
->name
)
467 /* no need to allocate +1 for NULL terminating character as
468 * already included in structure */
469 if (!(key
= HeapAlloc( GetProcessHeap(), 0, sizeof(*key
) + len
* sizeof(WCHAR
) ))) break;
470 memcpy(key
->name
, szLineStart
, len
* sizeof(WCHAR
));
471 key
->name
[len
] = '\0';
474 len
= (int)(szLineEnd
- szValueStart
);
475 key
->value
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
476 memcpy(key
->value
, szValueStart
, len
* sizeof(WCHAR
));
477 key
->value
[len
] = '\0';
479 else key
->value
= NULL
;
483 next_key
= &key
->next
;
486 TRACE("New key: name=%s, value=%s\n",
487 debugstr_w(key
->name
), key
->value
? debugstr_w(key
->value
) : "(none)");
490 if (szFile
!= pBuffer
)
491 HeapFree(GetProcessHeap(), 0, szFile
);
492 HeapFree(GetProcessHeap(), 0, buffer_base
);
493 return first_section
;
497 /***********************************************************************
498 * PROFILE_DeleteSection
500 * Delete a section from a profile tree.
502 static BOOL
PROFILE_DeleteSection( PROFILESECTION
**section
, LPCWSTR name
)
506 if ((*section
)->name
[0] && !strcmpiW( (*section
)->name
, name
))
508 PROFILESECTION
*to_del
= *section
;
509 *section
= to_del
->next
;
511 PROFILE_Free( to_del
);
514 section
= &(*section
)->next
;
520 /***********************************************************************
523 * Delete a key from a profile tree.
525 static BOOL
PROFILE_DeleteKey( PROFILESECTION
**section
,
526 LPCWSTR section_name
, LPCWSTR key_name
)
530 if ((*section
)->name
[0] && !strcmpiW( (*section
)->name
, section_name
))
532 PROFILEKEY
**key
= &(*section
)->key
;
535 if (!strcmpiW( (*key
)->name
, key_name
))
537 PROFILEKEY
*to_del
= *key
;
539 HeapFree( GetProcessHeap(), 0, to_del
->value
);
540 HeapFree( GetProcessHeap(), 0, to_del
);
546 section
= &(*section
)->next
;
552 /***********************************************************************
553 * PROFILE_DeleteAllKeys
555 * Delete all keys from a profile tree.
557 static void PROFILE_DeleteAllKeys( LPCWSTR section_name
)
559 PROFILESECTION
**section
= &CurProfile
->section
;
562 if ((*section
)->name
[0] && !strcmpiW( (*section
)->name
, section_name
))
564 PROFILEKEY
**key
= &(*section
)->key
;
567 PROFILEKEY
*to_del
= *key
;
569 HeapFree( GetProcessHeap(), 0, to_del
->value
);
570 HeapFree( GetProcessHeap(), 0, to_del
);
571 CurProfile
->changed
=TRUE
;
574 section
= &(*section
)->next
;
579 /***********************************************************************
582 * Find a key in a profile tree, optionally creating it.
584 static PROFILEKEY
*PROFILE_Find( PROFILESECTION
**section
, LPCWSTR section_name
,
585 LPCWSTR key_name
, BOOL create
, BOOL create_always
)
590 while (PROFILE_isspaceW(*section_name
)) section_name
++;
591 p
= section_name
+ strlenW(section_name
) - 1;
592 while ((p
> section_name
) && PROFILE_isspaceW(*p
)) p
--;
593 seclen
= p
- section_name
+ 1;
595 while (PROFILE_isspaceW(*key_name
)) key_name
++;
596 p
= key_name
+ strlenW(key_name
) - 1;
597 while ((p
> key_name
) && PROFILE_isspaceW(*p
)) p
--;
598 keylen
= p
- key_name
+ 1;
602 if ( ((*section
)->name
[0])
603 && (!(strncmpiW( (*section
)->name
, section_name
, seclen
)))
604 && (((*section
)->name
)[seclen
] == '\0') )
606 PROFILEKEY
**key
= &(*section
)->key
;
610 /* If create_always is FALSE then we check if the keyname
611 * already exists. Otherwise we add it regardless of its
612 * existence, to allow keys to be added more than once in
617 if ( (!(strncmpiW( (*key
)->name
, key_name
, keylen
)))
618 && (((*key
)->name
)[keylen
] == '\0') )
623 if (!create
) return NULL
;
624 if (!(*key
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY
) + strlenW(key_name
) * sizeof(WCHAR
) )))
626 strcpyW( (*key
)->name
, key_name
);
627 (*key
)->value
= NULL
;
631 section
= &(*section
)->next
;
633 if (!create
) return NULL
;
634 *section
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION
) + strlenW(section_name
) * sizeof(WCHAR
) );
635 if(*section
== NULL
) return NULL
;
636 strcpyW( (*section
)->name
, section_name
);
637 (*section
)->next
= NULL
;
638 if (!((*section
)->key
= HeapAlloc( GetProcessHeap(), 0,
639 sizeof(PROFILEKEY
) + strlenW(key_name
) * sizeof(WCHAR
) )))
641 HeapFree(GetProcessHeap(), 0, *section
);
644 strcpyW( (*section
)->key
->name
, key_name
);
645 (*section
)->key
->value
= NULL
;
646 (*section
)->key
->next
= NULL
;
647 return (*section
)->key
;
651 /***********************************************************************
654 * Flush the current profile to disk if changed.
656 static BOOL
PROFILE_FlushFile(void)
659 FILETIME LastWriteTime
;
663 WARN("No current profile!\n");
667 if (!CurProfile
->changed
) return TRUE
;
669 hFile
= CreateFileW(CurProfile
->filename
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
670 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
672 if (hFile
== INVALID_HANDLE_VALUE
)
674 WARN("could not save profile file %s (error was %d)\n", debugstr_w(CurProfile
->filename
), GetLastError());
678 TRACE("Saving %s\n", debugstr_w(CurProfile
->filename
));
679 PROFILE_Save( hFile
, CurProfile
->section
, CurProfile
->encoding
);
680 if(GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
))
681 CurProfile
->LastWriteTime
=LastWriteTime
;
682 CloseHandle( hFile
);
683 CurProfile
->changed
= FALSE
;
688 /***********************************************************************
689 * PROFILE_ReleaseFile
691 * Flush the current profile to disk and remove it from the cache.
693 static void PROFILE_ReleaseFile(void)
696 PROFILE_Free( CurProfile
->section
);
697 HeapFree( GetProcessHeap(), 0, CurProfile
->filename
);
698 CurProfile
->changed
= FALSE
;
699 CurProfile
->section
= NULL
;
700 CurProfile
->filename
= NULL
;
701 CurProfile
->encoding
= ENCODING_ANSI
;
702 ZeroMemory(&CurProfile
->LastWriteTime
, sizeof(CurProfile
->LastWriteTime
));
706 /***********************************************************************
709 * Open a profile file, checking the cached file first.
711 static BOOL
PROFILE_Open( LPCWSTR filename
, BOOL write_access
)
713 WCHAR windirW
[MAX_PATH
];
714 WCHAR buffer
[MAX_PATH
];
715 HANDLE hFile
= INVALID_HANDLE_VALUE
;
716 FILETIME LastWriteTime
;
718 PROFILE
*tempProfile
;
720 ZeroMemory(&LastWriteTime
, sizeof(LastWriteTime
));
722 /* First time around */
725 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
727 MRUProfile
[i
]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE
) );
728 if(MRUProfile
[i
] == NULL
) break;
729 MRUProfile
[i
]->changed
=FALSE
;
730 MRUProfile
[i
]->section
=NULL
;
731 MRUProfile
[i
]->filename
=NULL
;
732 MRUProfile
[i
]->encoding
=ENCODING_ANSI
;
733 ZeroMemory(&MRUProfile
[i
]->LastWriteTime
, sizeof(FILETIME
));
736 GetWindowsDirectoryW( windirW
, MAX_PATH
);
741 if ((RtlDetermineDosPathNameType_U(filename
) == RELATIVE_PATH
) &&
742 !strchrW(filename
, '\\') && !strchrW(filename
, '/'))
744 static const WCHAR wszSeparator
[] = {'\\', 0};
745 strcpyW(buffer
, windirW
);
746 strcatW(buffer
, wszSeparator
);
747 strcatW(buffer
, filename
);
752 GetFullPathNameW(filename
, sizeof(buffer
)/sizeof(buffer
[0]), buffer
, &dummy
);
755 TRACE("path: %s\n", debugstr_w(buffer
));
757 hFile
= CreateFileW(buffer
, GENERIC_READ
| (write_access
? GENERIC_WRITE
: 0),
758 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
759 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
761 if ((hFile
== INVALID_HANDLE_VALUE
) && (GetLastError() != ERROR_FILE_NOT_FOUND
))
763 WARN("Error %d opening file %s\n", GetLastError(), debugstr_w(buffer
));
767 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
769 if ((MRUProfile
[i
]->filename
&& !strcmpiW( buffer
, MRUProfile
[i
]->filename
)))
771 TRACE("MRU Filename: %s, new filename: %s\n", debugstr_w(MRUProfile
[i
]->filename
), debugstr_w(buffer
));
775 tempProfile
=MRUProfile
[i
];
777 MRUProfile
[j
]=MRUProfile
[j
-1];
778 CurProfile
=tempProfile
;
781 if (hFile
!= INVALID_HANDLE_VALUE
)
783 if (TRACE_ON(profile
))
785 GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
);
786 if (memcmp(&CurProfile
->LastWriteTime
, &LastWriteTime
, sizeof(FILETIME
)))
787 TRACE("(%s): already opened (mru=%d)\n",
788 debugstr_w(buffer
), i
);
790 TRACE("(%s): already opened, needs refreshing (mru=%d)\n",
791 debugstr_w(buffer
), i
);
795 else TRACE("(%s): already opened, not yet created (mru=%d)\n",
796 debugstr_w(buffer
), i
);
801 /* Flush the old current profile */
804 /* Make the oldest profile the current one only in order to get rid of it */
805 if(i
==N_CACHED_PROFILES
)
807 tempProfile
=MRUProfile
[N_CACHED_PROFILES
-1];
808 for(i
=N_CACHED_PROFILES
-1;i
>0;i
--)
809 MRUProfile
[i
]=MRUProfile
[i
-1];
810 CurProfile
=tempProfile
;
812 if(CurProfile
->filename
) PROFILE_ReleaseFile();
814 /* OK, now that CurProfile is definitely free we assign it our new file */
815 CurProfile
->filename
= HeapAlloc( GetProcessHeap(), 0, (strlenW(buffer
)+1) * sizeof(WCHAR
) );
816 strcpyW( CurProfile
->filename
, buffer
);
818 if (hFile
!= INVALID_HANDLE_VALUE
)
820 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
821 GetFileTime(hFile
, NULL
, NULL
, &CurProfile
->LastWriteTime
);
826 /* Does not exist yet, we will create it in PROFILE_FlushFile */
827 WARN("profile file %s not found\n", debugstr_w(buffer
) );
833 /***********************************************************************
836 * Returns all keys of a section.
837 * If return_values is TRUE, also include the corresponding values.
839 static INT
PROFILE_GetSection( PROFILESECTION
*section
, LPCWSTR section_name
,
840 LPWSTR buffer
, UINT len
, BOOL return_values
, BOOL return_noequalkeys
)
844 if(!buffer
) return 0;
846 TRACE("%s,%p,%u\n", debugstr_w(section_name
), buffer
, len
);
850 if (section
->name
[0] && !strcmpiW( section
->name
, section_name
))
853 for (key
= section
->key
; key
; key
= key
->next
)
856 if (!*key
->name
) continue; /* Skip empty lines */
857 if (IS_ENTRY_COMMENT(key
->name
)) continue; /* Skip comments */
858 if (!return_noequalkeys
&& !return_values
&& !key
->value
) continue; /* Skip lines w.o. '=' */
859 PROFILE_CopyEntry( buffer
, key
->name
, len
- 1, 0 );
860 len
-= strlenW(buffer
) + 1;
861 buffer
+= strlenW(buffer
) + 1;
864 if (return_values
&& key
->value
) {
866 PROFILE_CopyEntry ( buffer
, key
->value
, len
- 1, 0 );
867 len
-= strlenW(buffer
) + 1;
868 buffer
+= strlenW(buffer
) + 1;
873 /*If either lpszSection or lpszKey is NULL and the supplied
874 destination buffer is too small to hold all the strings,
875 the last string is truncated and followed by two null characters.
876 In this case, the return value is equal to cchReturnBuffer
884 section
= section
->next
;
886 buffer
[0] = buffer
[1] = '\0';
890 /* See GetPrivateProfileSectionNamesA for documentation */
891 static INT
PROFILE_GetSectionNames( LPWSTR buffer
, UINT len
)
895 PROFILESECTION
*section
;
897 TRACE("(%p, %d)\n", buffer
, len
);
908 section
= CurProfile
->section
;
909 while ((section
!=NULL
)) {
910 if (section
->name
[0]) {
911 tmplen
= strlenW(section
->name
)+1;
912 if (tmplen
>= buflen
) {
914 memcpy(buf
, section
->name
, (buflen
-1) * sizeof(WCHAR
));
921 memcpy(buf
, section
->name
, tmplen
* sizeof(WCHAR
));
925 section
= section
->next
;
932 /***********************************************************************
935 * Get a profile string.
937 * Tests with GetPrivateProfileString16, W95a,
938 * with filled buffer ("****...") and section "set1" and key_name "1" valid:
939 * section key_name def_val res buffer
940 * "set1" "1" "x" 43 [data]
941 * "set1" "1 " "x" 43 [data] (!)
942 * "set1" " 1 "' "x" 43 [data] (!)
943 * "set1" "" "x" 1 "x"
944 * "set1" "" "x " 1 "x" (!)
945 * "set1" "" " x " 3 " x" (!)
946 * "set1" NULL "x" 6 "1\02\03\0\0"
947 * "set1" "" "x" 1 "x"
948 * NULL "1" "x" 0 "" (!)
954 static INT
PROFILE_GetString( LPCWSTR section
, LPCWSTR key_name
,
955 LPCWSTR def_val
, LPWSTR buffer
, UINT len
, BOOL win32
)
957 PROFILEKEY
*key
= NULL
;
958 static const WCHAR empty_strW
[] = { 0 };
960 if(!buffer
|| !len
) return 0;
962 if (!def_val
) def_val
= empty_strW
;
967 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
968 return strlenW(buffer
);
970 key
= PROFILE_Find( &CurProfile
->section
, section
, key_name
, FALSE
, FALSE
);
971 PROFILE_CopyEntry( buffer
, (key
&& key
->value
) ? key
->value
: def_val
,
973 TRACE("(%s,%s,%s): returning %s\n",
974 debugstr_w(section
), debugstr_w(key_name
),
975 debugstr_w(def_val
), debugstr_w(buffer
) );
976 return strlenW( buffer
);
978 /* no "else" here ! */
979 if (section
&& section
[0])
981 INT ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, FALSE
, !win32
);
982 if (!buffer
[0]) /* no luck -> def_val */
984 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
985 ret
= strlenW(buffer
);
994 /***********************************************************************
997 * Set a profile string.
999 static BOOL
PROFILE_SetString( LPCWSTR section_name
, LPCWSTR key_name
,
1000 LPCWSTR value
, BOOL create_always
)
1002 if (!key_name
) /* Delete a whole section */
1004 TRACE("(%s)\n", debugstr_w(section_name
));
1005 CurProfile
->changed
|= PROFILE_DeleteSection( &CurProfile
->section
,
1007 return TRUE
; /* Even if PROFILE_DeleteSection() has failed,
1008 this is not an error on application's level.*/
1010 else if (!value
) /* Delete a key */
1012 TRACE("(%s,%s)\n", debugstr_w(section_name
), debugstr_w(key_name
) );
1013 CurProfile
->changed
|= PROFILE_DeleteKey( &CurProfile
->section
,
1014 section_name
, key_name
);
1015 return TRUE
; /* same error handling as above */
1017 else /* Set the key value */
1019 PROFILEKEY
*key
= PROFILE_Find(&CurProfile
->section
, section_name
,
1020 key_name
, TRUE
, create_always
);
1021 TRACE("(%s,%s,%s):\n",
1022 debugstr_w(section_name
), debugstr_w(key_name
), debugstr_w(value
) );
1023 if (!key
) return FALSE
;
1025 /* strip the leading spaces. We can safely strip \n\r and
1026 * friends too, they should not happen here anyway. */
1027 while (PROFILE_isspaceW(*value
)) value
++;
1031 if (!strcmpW( key
->value
, value
))
1033 TRACE(" no change needed\n" );
1034 return TRUE
; /* No change needed */
1036 TRACE(" replacing %s\n", debugstr_w(key
->value
) );
1037 HeapFree( GetProcessHeap(), 0, key
->value
);
1039 else TRACE(" creating key\n" );
1040 key
->value
= HeapAlloc( GetProcessHeap(), 0, (strlenW(value
)+1) * sizeof(WCHAR
) );
1041 strcpyW( key
->value
, value
);
1042 CurProfile
->changed
= TRUE
;
1048 /********************* API functions **********************************/
1051 /***********************************************************************
1052 * GetProfileIntA (KERNEL32.@)
1054 UINT WINAPI
GetProfileIntA( LPCSTR section
, LPCSTR entry
, INT def_val
)
1056 return GetPrivateProfileIntA( section
, entry
, def_val
, "win.ini" );
1059 /***********************************************************************
1060 * GetProfileIntW (KERNEL32.@)
1062 UINT WINAPI
GetProfileIntW( LPCWSTR section
, LPCWSTR entry
, INT def_val
)
1064 return GetPrivateProfileIntW( section
, entry
, def_val
, wininiW
);
1069 * - Section names if 'section' is NULL
1070 * - Keys in a Section if 'entry' is NULL
1071 * (see MSDN doc for GetPrivateProfileString)
1073 static int PROFILE_GetPrivateProfileString( LPCWSTR section
, LPCWSTR entry
,
1074 LPCWSTR def_val
, LPWSTR buffer
,
1075 UINT len
, LPCWSTR filename
,
1079 LPWSTR defval_tmp
= NULL
;
1081 TRACE("%s,%s,%s,%p,%u,%s\n", debugstr_w(section
), debugstr_w(entry
),
1082 debugstr_w(def_val
), buffer
, len
, debugstr_w(filename
));
1084 /* strip any trailing ' ' of def_val. */
1087 LPCWSTR p
= &def_val
[strlenW(def_val
) - 1];
1089 while (p
> def_val
&& *p
== ' ')
1094 int len
= (int)(p
- def_val
) + 1;
1096 defval_tmp
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1097 memcpy(defval_tmp
, def_val
, len
* sizeof(WCHAR
));
1098 defval_tmp
[len
] = '\0';
1099 def_val
= defval_tmp
;
1103 RtlEnterCriticalSection( &PROFILE_CritSect
);
1105 if (PROFILE_Open( filename
, FALSE
)) {
1106 if (win32
&& (section
== NULL
))
1107 ret
= PROFILE_GetSectionNames(buffer
, len
);
1109 /* PROFILE_GetString can handle the 'entry == NULL' case */
1110 ret
= PROFILE_GetString( section
, entry
, def_val
, buffer
, len
, win32
);
1111 } else if (buffer
&& def_val
) {
1112 lstrcpynW( buffer
, def_val
, len
);
1113 ret
= strlenW( buffer
);
1118 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1120 HeapFree(GetProcessHeap(), 0, defval_tmp
);
1122 TRACE("returning %s, %d\n", debugstr_w(buffer
), ret
);
1127 /***********************************************************************
1128 * GetPrivateProfileString (KERNEL.128)
1130 INT16 WINAPI
GetPrivateProfileString16( LPCSTR section
, LPCSTR entry
,
1131 LPCSTR def_val
, LPSTR buffer
,
1132 UINT16 len
, LPCSTR filename
)
1134 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1136 INT16 retW
, ret
= 0;
1138 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1139 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1140 else sectionW
.Buffer
= NULL
;
1141 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1142 else entryW
.Buffer
= NULL
;
1143 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1144 else def_valW
.Buffer
= NULL
;
1145 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1146 else filenameW
.Buffer
= NULL
;
1148 retW
= PROFILE_GetPrivateProfileString( sectionW
.Buffer
, entryW
.Buffer
,
1149 def_valW
.Buffer
, bufferW
, len
,
1150 filenameW
.Buffer
, FALSE
);
1153 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1160 ret
--; /* strip terminating 0 */
1163 RtlFreeUnicodeString(§ionW
);
1164 RtlFreeUnicodeString(&entryW
);
1165 RtlFreeUnicodeString(&def_valW
);
1166 RtlFreeUnicodeString(&filenameW
);
1167 HeapFree(GetProcessHeap(), 0, bufferW
);
1171 /***********************************************************************
1172 * GetPrivateProfileStringA (KERNEL32.@)
1174 INT WINAPI
GetPrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1175 LPCSTR def_val
, LPSTR buffer
,
1176 UINT len
, LPCSTR filename
)
1178 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1182 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1183 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1184 else sectionW
.Buffer
= NULL
;
1185 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1186 else entryW
.Buffer
= NULL
;
1187 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1188 else def_valW
.Buffer
= NULL
;
1189 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1190 else filenameW
.Buffer
= NULL
;
1192 retW
= GetPrivateProfileStringW( sectionW
.Buffer
, entryW
.Buffer
,
1193 def_valW
.Buffer
, bufferW
, len
,
1197 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1204 ret
--; /* strip terminating 0 */
1207 RtlFreeUnicodeString(§ionW
);
1208 RtlFreeUnicodeString(&entryW
);
1209 RtlFreeUnicodeString(&def_valW
);
1210 RtlFreeUnicodeString(&filenameW
);
1211 HeapFree(GetProcessHeap(), 0, bufferW
);
1215 /***********************************************************************
1216 * GetPrivateProfileStringW (KERNEL32.@)
1218 INT WINAPI
GetPrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1219 LPCWSTR def_val
, LPWSTR buffer
,
1220 UINT len
, LPCWSTR filename
)
1222 TRACE("(%s, %s, %s, %p, %d, %s)\n", debugstr_w(section
), debugstr_w(entry
), debugstr_w(def_val
), buffer
, len
, debugstr_w(filename
));
1224 return PROFILE_GetPrivateProfileString( section
, entry
, def_val
,
1225 buffer
, len
, filename
, TRUE
);
1228 /***********************************************************************
1229 * GetProfileStringA (KERNEL32.@)
1231 INT WINAPI
GetProfileStringA( LPCSTR section
, LPCSTR entry
, LPCSTR def_val
,
1232 LPSTR buffer
, UINT len
)
1234 return GetPrivateProfileStringA( section
, entry
, def_val
,
1235 buffer
, len
, "win.ini" );
1238 /***********************************************************************
1239 * GetProfileStringW (KERNEL32.@)
1241 INT WINAPI
GetProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1242 LPCWSTR def_val
, LPWSTR buffer
, UINT len
)
1244 return GetPrivateProfileStringW( section
, entry
, def_val
,
1245 buffer
, len
, wininiW
);
1248 /***********************************************************************
1249 * WriteProfileStringA (KERNEL32.@)
1251 BOOL WINAPI
WriteProfileStringA( LPCSTR section
, LPCSTR entry
,
1254 return WritePrivateProfileStringA( section
, entry
, string
, "win.ini" );
1257 /***********************************************************************
1258 * WriteProfileStringW (KERNEL32.@)
1260 BOOL WINAPI
WriteProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1263 return WritePrivateProfileStringW( section
, entry
, string
, wininiW
);
1267 /***********************************************************************
1268 * GetPrivateProfileIntW (KERNEL32.@)
1270 UINT WINAPI
GetPrivateProfileIntW( LPCWSTR section
, LPCWSTR entry
,
1271 INT def_val
, LPCWSTR filename
)
1274 UNICODE_STRING bufferW
;
1278 if (!(len
= GetPrivateProfileStringW( section
, entry
, emptystringW
,
1279 buffer
, sizeof(buffer
)/sizeof(WCHAR
),
1283 /* FIXME: if entry can be found but it's empty, then Win16 is
1284 * supposed to return 0 instead of def_val ! Difficult/problematic
1285 * to implement (every other failure also returns zero buffer),
1286 * thus wait until testing framework avail for making sure nothing
1287 * else gets broken that way. */
1288 if (!buffer
[0]) return (UINT
)def_val
;
1290 RtlInitUnicodeString( &bufferW
, buffer
);
1291 RtlUnicodeStringToInteger( &bufferW
, 0, &result
);
1295 /***********************************************************************
1296 * GetPrivateProfileIntA (KERNEL32.@)
1298 * FIXME: rewrite using unicode
1300 UINT WINAPI
GetPrivateProfileIntA( LPCSTR section
, LPCSTR entry
,
1301 INT def_val
, LPCSTR filename
)
1303 UNICODE_STRING entryW
, filenameW
, sectionW
;
1305 if(entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1306 else entryW
.Buffer
= NULL
;
1307 if(filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1308 else filenameW
.Buffer
= NULL
;
1309 if(section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1310 else sectionW
.Buffer
= NULL
;
1311 res
= GetPrivateProfileIntW(sectionW
.Buffer
, entryW
.Buffer
, def_val
,
1313 RtlFreeUnicodeString(§ionW
);
1314 RtlFreeUnicodeString(&filenameW
);
1315 RtlFreeUnicodeString(&entryW
);
1319 /***********************************************************************
1320 * GetPrivateProfileSectionW (KERNEL32.@)
1322 INT WINAPI
GetPrivateProfileSectionW( LPCWSTR section
, LPWSTR buffer
,
1323 DWORD len
, LPCWSTR filename
)
1327 if (!section
|| !buffer
)
1329 SetLastError(ERROR_INVALID_PARAMETER
);
1333 TRACE("(%s, %p, %d, %s)\n", debugstr_w(section
), buffer
, len
, debugstr_w(filename
));
1335 RtlEnterCriticalSection( &PROFILE_CritSect
);
1337 if (PROFILE_Open( filename
, FALSE
))
1338 ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, TRUE
, FALSE
);
1340 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1345 /***********************************************************************
1346 * GetPrivateProfileSectionA (KERNEL32.@)
1348 INT WINAPI
GetPrivateProfileSectionA( LPCSTR section
, LPSTR buffer
,
1349 DWORD len
, LPCSTR filename
)
1351 UNICODE_STRING sectionW
, filenameW
;
1355 if (!section
|| !buffer
)
1357 SetLastError(ERROR_INVALID_PARAMETER
);
1361 bufferW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1362 RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1363 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1364 else filenameW
.Buffer
= NULL
;
1366 retW
= GetPrivateProfileSectionW(sectionW
.Buffer
, bufferW
, len
, filenameW
.Buffer
);
1369 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1385 RtlFreeUnicodeString(§ionW
);
1386 RtlFreeUnicodeString(&filenameW
);
1387 HeapFree(GetProcessHeap(), 0, bufferW
);
1391 /***********************************************************************
1392 * GetProfileSectionA (KERNEL32.@)
1394 INT WINAPI
GetProfileSectionA( LPCSTR section
, LPSTR buffer
, DWORD len
)
1396 return GetPrivateProfileSectionA( section
, buffer
, len
, "win.ini" );
1399 /***********************************************************************
1400 * GetProfileSectionW (KERNEL32.@)
1402 INT WINAPI
GetProfileSectionW( LPCWSTR section
, LPWSTR buffer
, DWORD len
)
1404 return GetPrivateProfileSectionW( section
, buffer
, len
, wininiW
);
1408 /***********************************************************************
1409 * WritePrivateProfileStringW (KERNEL32.@)
1411 BOOL WINAPI
WritePrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1412 LPCWSTR string
, LPCWSTR filename
)
1416 RtlEnterCriticalSection( &PROFILE_CritSect
);
1418 if (!section
&& !entry
&& !string
) /* documented "file flush" case */
1420 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1422 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1425 else if (PROFILE_Open( filename
, TRUE
))
1428 FIXME("(NULL?,%s,%s,%s)?\n",
1429 debugstr_w(entry
), debugstr_w(string
), debugstr_w(filename
));
1431 ret
= PROFILE_SetString( section
, entry
, string
, FALSE
);
1432 PROFILE_FlushFile();
1436 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1440 /***********************************************************************
1441 * WritePrivateProfileStringA (KERNEL32.@)
1443 BOOL WINAPI
WritePrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1444 LPCSTR string
, LPCSTR filename
)
1446 UNICODE_STRING sectionW
, entryW
, stringW
, filenameW
;
1449 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1450 else sectionW
.Buffer
= NULL
;
1451 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1452 else entryW
.Buffer
= NULL
;
1453 if (string
) RtlCreateUnicodeStringFromAsciiz(&stringW
, string
);
1454 else stringW
.Buffer
= NULL
;
1455 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1456 else filenameW
.Buffer
= NULL
;
1458 ret
= WritePrivateProfileStringW(sectionW
.Buffer
, entryW
.Buffer
,
1459 stringW
.Buffer
, filenameW
.Buffer
);
1460 RtlFreeUnicodeString(§ionW
);
1461 RtlFreeUnicodeString(&entryW
);
1462 RtlFreeUnicodeString(&stringW
);
1463 RtlFreeUnicodeString(&filenameW
);
1467 /***********************************************************************
1468 * WritePrivateProfileSectionW (KERNEL32.@)
1470 BOOL WINAPI
WritePrivateProfileSectionW( LPCWSTR section
,
1471 LPCWSTR string
, LPCWSTR filename
)
1476 RtlEnterCriticalSection( &PROFILE_CritSect
);
1478 if (!section
&& !string
)
1480 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1482 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1485 else if (PROFILE_Open( filename
, TRUE
)) {
1486 if (!string
) {/* delete the named section*/
1487 ret
= PROFILE_SetString(section
,NULL
,NULL
, FALSE
);
1488 PROFILE_FlushFile();
1490 PROFILE_DeleteAllKeys(section
);
1493 LPWSTR buf
= HeapAlloc( GetProcessHeap(), 0, (strlenW(string
)+1) * sizeof(WCHAR
) );
1494 strcpyW( buf
, string
);
1495 if((p
= strchrW( buf
, '='))) {
1497 ret
= PROFILE_SetString( section
, buf
, p
+1, TRUE
);
1499 HeapFree( GetProcessHeap(), 0, buf
);
1500 string
+= strlenW(string
)+1;
1502 PROFILE_FlushFile();
1506 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1510 /***********************************************************************
1511 * WritePrivateProfileSectionA (KERNEL32.@)
1513 BOOL WINAPI
WritePrivateProfileSectionA( LPCSTR section
,
1514 LPCSTR string
, LPCSTR filename
)
1517 UNICODE_STRING sectionW
, filenameW
;
1526 while(*p
) p
+= strlen(p
) + 1;
1527 lenA
= p
- string
+ 1;
1528 lenW
= MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, NULL
, 0);
1529 if ((stringW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
))))
1530 MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, stringW
, lenW
);
1532 else stringW
= NULL
;
1533 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1534 else sectionW
.Buffer
= NULL
;
1535 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1536 else filenameW
.Buffer
= NULL
;
1538 ret
= WritePrivateProfileSectionW(sectionW
.Buffer
, stringW
, filenameW
.Buffer
);
1540 HeapFree(GetProcessHeap(), 0, stringW
);
1541 RtlFreeUnicodeString(§ionW
);
1542 RtlFreeUnicodeString(&filenameW
);
1546 /***********************************************************************
1547 * WriteProfileSectionA (KERNEL32.@)
1549 BOOL WINAPI
WriteProfileSectionA( LPCSTR section
, LPCSTR keys_n_values
)
1552 return WritePrivateProfileSectionA( section
, keys_n_values
, "win.ini");
1555 /***********************************************************************
1556 * WriteProfileSectionW (KERNEL32.@)
1558 BOOL WINAPI
WriteProfileSectionW( LPCWSTR section
, LPCWSTR keys_n_values
)
1560 return WritePrivateProfileSectionW(section
, keys_n_values
, wininiW
);
1564 /***********************************************************************
1565 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1567 * Returns the section names contained in the specified file.
1568 * FIXME: Where do we find this file when the path is relative?
1569 * The section names are returned as a list of strings with an extra
1570 * '\0' to mark the end of the list. Except for that the behavior
1571 * depends on the Windows version.
1574 * - if the buffer is 0 or 1 character long then it is as if it was of
1576 * - otherwise, if the buffer is too small only the section names that fit
1578 * - note that this means if the buffer was too small to return even just
1579 * the first section name then a single '\0' will be returned.
1580 * - the return value is the number of characters written in the buffer,
1581 * except if the buffer was too small in which case len-2 is returned
1584 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1585 * '\0' and the return value is 0
1586 * - otherwise if the buffer is too small then the first section name that
1587 * does not fit is truncated so that the string list can be terminated
1588 * correctly (double '\0')
1589 * - the return value is the number of characters written in the buffer
1590 * except for the trailing '\0'. If the buffer is too small, then the
1591 * return value is len-2
1592 * - Win2000 has a bug that triggers when the section names and the
1593 * trailing '\0' fit exactly in the buffer. In that case the trailing
1596 * Wine implements the observed Win2000 behavior (except for the bug).
1598 * Note that when the buffer is big enough then the return value may be any
1599 * value between 1 and len-1 (or len in Win95), including len-2.
1601 DWORD WINAPI
GetPrivateProfileSectionNamesW( LPWSTR buffer
, DWORD size
,
1606 RtlEnterCriticalSection( &PROFILE_CritSect
);
1608 if (PROFILE_Open( filename
, FALSE
))
1609 ret
= PROFILE_GetSectionNames(buffer
, size
);
1611 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1617 /***********************************************************************
1618 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1620 DWORD WINAPI
GetPrivateProfileSectionNamesA( LPSTR buffer
, DWORD size
,
1623 UNICODE_STRING filenameW
;
1627 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)) : NULL
;
1628 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1629 else filenameW
.Buffer
= NULL
;
1631 retW
= GetPrivateProfileSectionNamesW(bufferW
, size
, filenameW
.Buffer
);
1634 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+1, buffer
, size
-1, NULL
, NULL
);
1646 RtlFreeUnicodeString(&filenameW
);
1647 HeapFree(GetProcessHeap(), 0, bufferW
);
1651 /***********************************************************************
1652 * GetPrivateProfileStructW (KERNEL32.@)
1654 * Should match Win95's behaviour pretty much
1656 BOOL WINAPI
GetPrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1657 LPVOID buf
, UINT len
, LPCWSTR filename
)
1661 RtlEnterCriticalSection( &PROFILE_CritSect
);
1663 if (PROFILE_Open( filename
, FALSE
)) {
1664 PROFILEKEY
*k
= PROFILE_Find ( &CurProfile
->section
, section
, key
, FALSE
, FALSE
);
1666 TRACE("value (at %p): %s\n", k
->value
, debugstr_w(k
->value
));
1667 if (((strlenW(k
->value
) - 2) / 2) == len
)
1674 end
= k
->value
+ strlenW(k
->value
); /* -> '\0' */
1675 /* check for invalid chars in ASCII coded hex string */
1676 for (p
=k
->value
; p
< end
; p
++)
1680 WARN("invalid char '%x' in file %s->[%s]->%s !\n",
1681 *p
, debugstr_w(filename
), debugstr_w(section
), debugstr_w(key
));
1688 BOOL highnibble
= TRUE
;
1690 LPBYTE binbuf
= (LPBYTE
)buf
;
1692 end
-= 2; /* don't include checksum in output data */
1693 /* translate ASCII hex format into binary data */
1694 for (p
=k
->value
; p
< end
; p
++)
1698 (c
- 'A' + 10) : (c
- '0');
1705 *binbuf
++ = b
; /* feed binary data into output */
1706 chksum
+= b
; /* calculate checksum */
1708 highnibble
^= 1; /* toggle */
1710 /* retrieve stored checksum value */
1712 b
= ( (c
> '9') ? (c
- 'A' + 10) : (c
- '0') ) << 4;
1714 b
+= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1715 if (b
== (chksum
& 0xff)) /* checksums match ? */
1721 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1726 /***********************************************************************
1727 * GetPrivateProfileStructA (KERNEL32.@)
1729 BOOL WINAPI
GetPrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1730 LPVOID buffer
, UINT len
, LPCSTR filename
)
1732 UNICODE_STRING sectionW
, keyW
, filenameW
;
1735 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1736 else sectionW
.Buffer
= NULL
;
1737 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1738 else keyW
.Buffer
= NULL
;
1739 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1740 else filenameW
.Buffer
= NULL
;
1742 ret
= GetPrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buffer
, len
,
1744 /* Do not translate binary data. */
1746 RtlFreeUnicodeString(§ionW
);
1747 RtlFreeUnicodeString(&keyW
);
1748 RtlFreeUnicodeString(&filenameW
);
1754 /***********************************************************************
1755 * WritePrivateProfileStructW (KERNEL32.@)
1757 BOOL WINAPI
WritePrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1758 LPVOID buf
, UINT bufsize
, LPCWSTR filename
)
1762 LPWSTR outstring
, p
;
1765 if (!section
&& !key
&& !buf
) /* flush the cache */
1766 return WritePrivateProfileStringW( NULL
, NULL
, NULL
, filename
);
1768 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1769 outstring
= HeapAlloc( GetProcessHeap(), 0, (bufsize
*2 + 2 + 1) * sizeof(WCHAR
) );
1771 for (binbuf
= (LPBYTE
)buf
; binbuf
< (LPBYTE
)buf
+bufsize
; binbuf
++) {
1772 *p
++ = hex
[*binbuf
>> 4];
1773 *p
++ = hex
[*binbuf
& 0xf];
1776 /* checksum is sum & 0xff */
1777 *p
++ = hex
[(sum
& 0xf0) >> 4];
1778 *p
++ = hex
[sum
& 0xf];
1781 RtlEnterCriticalSection( &PROFILE_CritSect
);
1783 if (PROFILE_Open( filename
, TRUE
)) {
1784 ret
= PROFILE_SetString( section
, key
, outstring
, FALSE
);
1785 PROFILE_FlushFile();
1788 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1790 HeapFree( GetProcessHeap(), 0, outstring
);
1795 /***********************************************************************
1796 * WritePrivateProfileStructA (KERNEL32.@)
1798 BOOL WINAPI
WritePrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1799 LPVOID buf
, UINT bufsize
, LPCSTR filename
)
1801 UNICODE_STRING sectionW
, keyW
, filenameW
;
1804 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1805 else sectionW
.Buffer
= NULL
;
1806 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1807 else keyW
.Buffer
= NULL
;
1808 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1809 else filenameW
.Buffer
= NULL
;
1811 /* Do not translate binary data. */
1812 ret
= WritePrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buf
, bufsize
,
1815 RtlFreeUnicodeString(§ionW
);
1816 RtlFreeUnicodeString(&keyW
);
1817 RtlFreeUnicodeString(&filenameW
);
1822 /***********************************************************************
1823 * WriteOutProfiles (KERNEL.315)
1825 void WINAPI
WriteOutProfiles16(void)
1827 RtlEnterCriticalSection( &PROFILE_CritSect
);
1828 PROFILE_FlushFile();
1829 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1832 /***********************************************************************
1833 * CloseProfileUserMapping (KERNEL32.@)
1835 BOOL WINAPI
CloseProfileUserMapping(void) {
1836 FIXME("(), stub!\n");
1837 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);