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
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(profile
);
36 static const char bom_utf8
[] = {0xEF,0xBB,0xBF};
46 typedef struct tagPROFILEKEY
49 struct tagPROFILEKEY
*next
;
53 typedef struct tagPROFILESECTION
55 struct tagPROFILEKEY
*key
;
56 struct tagPROFILESECTION
*next
;
64 PROFILESECTION
*section
;
66 FILETIME LastWriteTime
;
71 #define N_CACHED_PROFILES 10
73 /* Cached profile files */
74 static PROFILE
*MRUProfile
[N_CACHED_PROFILES
]={NULL
};
76 #define CurProfile (MRUProfile[0])
78 /* Check for comments in profile */
79 #define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
81 static CRITICAL_SECTION PROFILE_CritSect
;
82 static CRITICAL_SECTION_DEBUG critsect_debug
=
84 0, 0, &PROFILE_CritSect
,
85 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
86 0, 0, { (DWORD_PTR
)(__FILE__
": PROFILE_CritSect") }
88 static CRITICAL_SECTION PROFILE_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
90 static const char hex
[16] = "0123456789ABCDEF";
92 /***********************************************************************
95 * Copy the content of an entry into a buffer, removing quotes, and possibly
96 * translating environment variables.
98 static void PROFILE_CopyEntry( LPWSTR buffer
, LPCWSTR value
, int len
)
104 if (*value
== '\'' || *value
== '\"')
106 if (value
[1] && (value
[lstrlenW(value
)-1] == *value
)) quote
= *value
++;
109 lstrcpynW( buffer
, value
, len
);
110 if (quote
&& (len
>= lstrlenW(value
))) buffer
[lstrlenW(buffer
)-1] = '\0';
113 /* byte-swaps shorts in-place in a buffer. len is in WCHARs */
114 static inline void PROFILE_ByteSwapShortBuffer(WCHAR
* buffer
, int len
)
117 USHORT
* shortbuffer
= buffer
;
118 for (i
= 0; i
< len
; i
++)
119 shortbuffer
[i
] = RtlUshortByteSwap(shortbuffer
[i
]);
122 /* writes any necessary encoding marker to the file */
123 static inline void PROFILE_WriteMarker(HANDLE hFile
, ENCODING encoding
)
125 DWORD dwBytesWritten
;
132 WriteFile(hFile
, bom_utf8
, sizeof(bom_utf8
), &dwBytesWritten
, NULL
);
134 case ENCODING_UTF16LE
:
136 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
138 case ENCODING_UTF16BE
:
140 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
145 static void PROFILE_WriteLine( HANDLE hFile
, WCHAR
* szLine
, int len
, ENCODING encoding
)
148 int write_buffer_len
;
149 DWORD dwBytesWritten
;
151 TRACE("writing: %s\n", debugstr_wn(szLine
, len
));
156 write_buffer_len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
157 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
158 if (!write_buffer
) return;
159 len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
160 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
161 HeapFree(GetProcessHeap(), 0, write_buffer
);
164 write_buffer_len
= WideCharToMultiByte(CP_UTF8
, 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_UTF8
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
168 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
169 HeapFree(GetProcessHeap(), 0, write_buffer
);
171 case ENCODING_UTF16LE
:
172 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
174 case ENCODING_UTF16BE
:
175 PROFILE_ByteSwapShortBuffer(szLine
, len
);
176 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
179 FIXME("encoding type %d not implemented\n", encoding
);
183 /***********************************************************************
186 * Save a profile tree to a file.
188 static void PROFILE_Save( HANDLE hFile
, const PROFILESECTION
*section
, ENCODING encoding
)
193 PROFILE_WriteMarker(hFile
, encoding
);
195 for ( ; section
; section
= section
->next
)
199 if (section
->name
[0]) len
+= lstrlenW(section
->name
) + 4;
201 for (key
= section
->key
; key
; key
= key
->next
)
203 len
+= lstrlenW(key
->name
) + 2;
204 if (key
->value
) len
+= lstrlenW(key
->value
) + 1;
207 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
211 if (section
->name
[0])
214 lstrcpyW( p
, section
->name
);
221 for (key
= section
->key
; key
; key
= key
->next
)
223 lstrcpyW( p
, key
->name
);
228 lstrcpyW( p
, key
->value
);
234 PROFILE_WriteLine( hFile
, buffer
, len
, encoding
);
235 HeapFree(GetProcessHeap(), 0, buffer
);
240 /***********************************************************************
243 * Free a profile tree.
245 static void PROFILE_Free( PROFILESECTION
*section
)
247 PROFILESECTION
*next_section
;
248 PROFILEKEY
*key
, *next_key
;
250 for ( ; section
; section
= next_section
)
252 for (key
= section
->key
; key
; key
= next_key
)
254 next_key
= key
->next
;
255 HeapFree( GetProcessHeap(), 0, key
->value
);
256 HeapFree( GetProcessHeap(), 0, key
);
258 next_section
= section
->next
;
259 HeapFree( GetProcessHeap(), 0, section
);
263 /* returns TRUE if a whitespace character, else FALSE */
264 static inline BOOL
PROFILE_isspaceW(WCHAR c
)
266 /* ^Z (DOS EOF) is a space too (found on CD-ROMs) */
267 return (c
>= 0x09 && c
<= 0x0d) || c
== 0x1a || c
== 0x20;
270 static inline ENCODING
PROFILE_DetectTextEncoding(const void * buffer
, int * len
)
272 int flags
= IS_TEXT_UNICODE_SIGNATURE
|
273 IS_TEXT_UNICODE_REVERSE_SIGNATURE
|
274 IS_TEXT_UNICODE_ODD_LENGTH
;
275 if (*len
>= sizeof(bom_utf8
) && !memcmp(buffer
, bom_utf8
, sizeof(bom_utf8
)))
277 *len
= sizeof(bom_utf8
);
278 return ENCODING_UTF8
;
280 RtlIsTextUnicode(buffer
, *len
, &flags
);
281 if (flags
& IS_TEXT_UNICODE_SIGNATURE
)
283 *len
= sizeof(WCHAR
);
284 return ENCODING_UTF16LE
;
286 if (flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
288 *len
= sizeof(WCHAR
);
289 return ENCODING_UTF16BE
;
292 return ENCODING_ANSI
;
295 static void profile_trim_spaces(WCHAR
**start
, WCHAR
**end
)
297 WCHAR
*s
= *start
, *e
= *end
;
299 while (s
< e
&& PROFILE_isspaceW(*s
)) s
++;
300 while ((e
> s
) && PROFILE_isspaceW(e
[-1])) e
--;
306 /***********************************************************************
309 * Load a profile tree from a file.
311 static PROFILESECTION
*PROFILE_Load(HANDLE hFile
, ENCODING
* pEncoding
)
313 void *buffer_base
, *pBuffer
;
315 WCHAR
*szLineStart
, *szLineEnd
, *next_line
;
316 const WCHAR
*szValueStart
, *szEnd
;
318 PROFILESECTION
*section
, *first_section
;
319 PROFILESECTION
**next_section
;
320 PROFILEKEY
*key
, *prev_key
, **next_key
;
323 TRACE("%p\n", hFile
);
325 dwFileSize
= GetFileSize(hFile
, NULL
);
326 if (dwFileSize
== INVALID_FILE_SIZE
|| dwFileSize
== 0)
329 buffer_base
= HeapAlloc(GetProcessHeap(), 0 , dwFileSize
);
330 if (!buffer_base
) return NULL
;
332 if (!ReadFile(hFile
, buffer_base
, dwFileSize
, &dwFileSize
, NULL
))
334 HeapFree(GetProcessHeap(), 0, buffer_base
);
335 WARN("Error %ld reading file\n", GetLastError());
339 *pEncoding
= PROFILE_DetectTextEncoding(buffer_base
, &len
);
340 /* len is set to the number of bytes in the character marker.
341 * we want to skip these bytes */
342 pBuffer
= (char *)buffer_base
+ len
;
347 TRACE("ANSI encoding\n");
349 len
= MultiByteToWideChar(CP_ACP
, 0, pBuffer
, dwFileSize
, NULL
, 0);
350 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
353 HeapFree(GetProcessHeap(), 0, buffer_base
);
356 MultiByteToWideChar(CP_ACP
, 0, pBuffer
, dwFileSize
, szFile
, len
);
357 szEnd
= szFile
+ len
;
360 TRACE("UTF8 encoding\n");
362 len
= MultiByteToWideChar(CP_UTF8
, 0, pBuffer
, dwFileSize
, NULL
, 0);
363 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
366 HeapFree(GetProcessHeap(), 0, buffer_base
);
369 MultiByteToWideChar(CP_UTF8
, 0, pBuffer
, dwFileSize
, szFile
, len
);
370 szEnd
= szFile
+ len
;
372 case ENCODING_UTF16LE
:
373 TRACE("UTF16 Little Endian encoding\n");
375 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
377 case ENCODING_UTF16BE
:
378 TRACE("UTF16 Big Endian encoding\n");
380 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
381 PROFILE_ByteSwapShortBuffer(szFile
, dwFileSize
/ sizeof(WCHAR
));
384 FIXME("encoding type %d not implemented\n", *pEncoding
);
385 HeapFree(GetProcessHeap(), 0, buffer_base
);
389 first_section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) );
390 if(first_section
== NULL
)
392 if (szFile
!= pBuffer
)
393 HeapFree(GetProcessHeap(), 0, szFile
);
394 HeapFree(GetProcessHeap(), 0, buffer_base
);
397 first_section
->name
[0] = 0;
398 first_section
->key
= NULL
;
399 first_section
->next
= NULL
;
400 next_section
= &first_section
->next
;
401 next_key
= &first_section
->key
;
405 while (next_line
< szEnd
)
407 szLineStart
= next_line
;
408 while (next_line
< szEnd
&& *next_line
!= '\n' && *next_line
!= '\r') next_line
++;
409 while (next_line
< szEnd
&& (*next_line
== '\n' || *next_line
== '\r')) next_line
++;
410 szLineEnd
= next_line
;
412 /* get rid of white space */
413 profile_trim_spaces(&szLineStart
, &szLineEnd
);
415 if (szLineStart
>= szLineEnd
) continue;
417 if (*szLineStart
== '[') /* section start */
419 for (len
= szLineEnd
- szLineStart
; len
> 0; len
--) if (szLineStart
[len
- 1] == ']') break;
422 WARN("Invalid section header: %s\n",
423 debugstr_wn(szLineStart
, (int)(szLineEnd
- szLineStart
)) );
430 szLineEnd
= szLineStart
+ len
;
431 profile_trim_spaces(&szLineStart
, &szLineEnd
);
432 len
= szLineEnd
- 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 for (szValueStart
= szLineStart
; szValueStart
< szLineEnd
; szValueStart
++) if (*szValueStart
== '=') break;
457 if (szValueStart
< szLineEnd
)
459 const WCHAR
*szNameEnd
= szValueStart
;
460 while ((szNameEnd
> szLineStart
) && PROFILE_isspaceW(szNameEnd
[-1])) szNameEnd
--;
461 len
= szNameEnd
- szLineStart
;
463 while (szValueStart
< szLineEnd
&& PROFILE_isspaceW(*szValueStart
)) szValueStart
++;
465 else szValueStart
= NULL
;
467 if (len
|| !prev_key
|| *prev_key
->name
)
469 /* no need to allocate +1 for NULL terminating character as
470 * already included in structure */
471 if (!(key
= HeapAlloc( GetProcessHeap(), 0, sizeof(*key
) + len
* sizeof(WCHAR
) ))) break;
472 memcpy(key
->name
, szLineStart
, len
* sizeof(WCHAR
));
473 key
->name
[len
] = '\0';
476 len
= (int)(szLineEnd
- szValueStart
);
477 key
->value
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
478 memcpy(key
->value
, szValueStart
, len
* sizeof(WCHAR
));
479 key
->value
[len
] = '\0';
481 else key
->value
= NULL
;
485 next_key
= &key
->next
;
488 TRACE("New key: name=%s, value=%s\n",
489 debugstr_w(key
->name
), key
->value
? debugstr_w(key
->value
) : "(none)");
492 if (szFile
!= pBuffer
)
493 HeapFree(GetProcessHeap(), 0, szFile
);
494 HeapFree(GetProcessHeap(), 0, buffer_base
);
495 return first_section
;
499 /***********************************************************************
502 * Delete a key from a profile tree.
504 static BOOL
PROFILE_DeleteKey( PROFILESECTION
**section
,
505 LPCWSTR section_name
, LPCWSTR key_name
)
509 if (!wcsicmp( (*section
)->name
, section_name
))
511 PROFILEKEY
**key
= &(*section
)->key
;
514 if (!wcsicmp( (*key
)->name
, key_name
))
516 PROFILEKEY
*to_del
= *key
;
518 HeapFree( GetProcessHeap(), 0, to_del
->value
);
519 HeapFree( GetProcessHeap(), 0, to_del
);
525 section
= &(*section
)->next
;
531 /***********************************************************************
532 * PROFILE_DeleteAllKeys
534 * Delete all keys from a profile tree.
536 static void PROFILE_DeleteAllKeys( LPCWSTR section_name
)
538 PROFILESECTION
**section
= &CurProfile
->section
;
541 if (!wcsicmp( (*section
)->name
, section_name
))
543 PROFILEKEY
**key
= &(*section
)->key
;
546 PROFILEKEY
*to_del
= *key
;
548 HeapFree( GetProcessHeap(), 0, to_del
->value
);
549 HeapFree( GetProcessHeap(), 0, to_del
);
550 CurProfile
->changed
=TRUE
;
553 section
= &(*section
)->next
;
558 /***********************************************************************
561 * Find a key in a profile tree, optionally creating it.
563 static PROFILEKEY
*PROFILE_Find( PROFILESECTION
**section
, LPCWSTR section_name
,
564 LPCWSTR key_name
, BOOL create
, BOOL create_always
)
567 int seclen
= 0, keylen
= 0;
569 while (PROFILE_isspaceW(*section_name
)) section_name
++;
572 p
= section_name
+ lstrlenW(section_name
) - 1;
573 while ((p
> section_name
) && PROFILE_isspaceW(*p
)) p
--;
574 seclen
= p
- section_name
+ 1;
577 while (PROFILE_isspaceW(*key_name
)) key_name
++;
580 p
= key_name
+ lstrlenW(key_name
) - 1;
581 while ((p
> key_name
) && PROFILE_isspaceW(*p
)) p
--;
582 keylen
= p
- key_name
+ 1;
587 if (!wcsnicmp((*section
)->name
, section_name
, seclen
) &&
588 ((*section
)->name
)[seclen
] == '\0')
590 PROFILEKEY
**key
= &(*section
)->key
;
594 /* If create_always is FALSE then we check if the keyname
595 * already exists. Otherwise we add it regardless of its
596 * existence, to allow keys to be added more than once in
601 if ( (!(wcsnicmp( (*key
)->name
, key_name
, keylen
)))
602 && (((*key
)->name
)[keylen
] == '\0') )
607 if (!create
) return NULL
;
608 if (!(*key
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY
) + lstrlenW(key_name
) * sizeof(WCHAR
) )))
610 lstrcpyW( (*key
)->name
, key_name
);
611 (*key
)->value
= NULL
;
615 section
= &(*section
)->next
;
617 if (!create
) return NULL
;
618 *section
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION
) + lstrlenW(section_name
) * sizeof(WCHAR
) );
619 if(*section
== NULL
) return NULL
;
620 lstrcpyW( (*section
)->name
, section_name
);
621 (*section
)->next
= NULL
;
622 if (!((*section
)->key
= HeapAlloc( GetProcessHeap(), 0,
623 sizeof(PROFILEKEY
) + lstrlenW(key_name
) * sizeof(WCHAR
) )))
625 HeapFree(GetProcessHeap(), 0, *section
);
628 lstrcpyW( (*section
)->key
->name
, key_name
);
629 (*section
)->key
->value
= NULL
;
630 (*section
)->key
->next
= NULL
;
631 return (*section
)->key
;
635 /***********************************************************************
638 * Flush the current profile to disk if changed.
640 static BOOL
PROFILE_FlushFile(void)
643 FILETIME LastWriteTime
;
647 WARN("No current profile!\n");
651 if (!CurProfile
->changed
) return TRUE
;
653 hFile
= CreateFileW(CurProfile
->filename
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
654 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
656 if (hFile
== INVALID_HANDLE_VALUE
)
658 WARN("could not save profile file %s (error was %ld)\n", debugstr_w(CurProfile
->filename
), GetLastError());
662 TRACE("Saving %s\n", debugstr_w(CurProfile
->filename
));
663 PROFILE_Save( hFile
, CurProfile
->section
, CurProfile
->encoding
);
664 if(GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
))
665 CurProfile
->LastWriteTime
=LastWriteTime
;
666 CloseHandle( hFile
);
667 CurProfile
->changed
= FALSE
;
672 /***********************************************************************
673 * PROFILE_ReleaseFile
675 * Flush the current profile to disk and remove it from the cache.
677 static void PROFILE_ReleaseFile(void)
680 PROFILE_Free( CurProfile
->section
);
681 HeapFree( GetProcessHeap(), 0, CurProfile
->filename
);
682 CurProfile
->changed
= FALSE
;
683 CurProfile
->section
= NULL
;
684 CurProfile
->filename
= NULL
;
685 CurProfile
->encoding
= ENCODING_ANSI
;
686 ZeroMemory(&CurProfile
->LastWriteTime
, sizeof(CurProfile
->LastWriteTime
));
689 /***********************************************************************
691 * Compares a file time with the current time. If the file time is
692 * at least 2.1 seconds in the past, return true.
694 * Intended as cache safety measure: The time resolution on FAT is
695 * two seconds, so files that are not at least two seconds old might
696 * keep their time even on modification, so don't cache them.
698 static BOOL
is_not_current(FILETIME
*ft
)
703 NtQuerySystemTime( &now
);
704 ftll
= ((LONGLONG
)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
705 TRACE("%s; %s\n", wine_dbgstr_longlong(ftll
), wine_dbgstr_longlong(now
.QuadPart
));
706 return ftll
+ 21000000 < now
.QuadPart
;
709 /***********************************************************************
712 * Open a profile file, checking the cached file first.
714 static BOOL
PROFILE_Open( LPCWSTR filename
, BOOL write_access
)
716 WCHAR buffer
[MAX_PATH
];
717 HANDLE hFile
= INVALID_HANDLE_VALUE
;
718 FILETIME LastWriteTime
;
720 PROFILE
*tempProfile
;
722 ZeroMemory(&LastWriteTime
, sizeof(LastWriteTime
));
724 /* First time around */
727 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
729 MRUProfile
[i
]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE
) );
730 if(MRUProfile
[i
] == NULL
) break;
731 MRUProfile
[i
]->changed
=FALSE
;
732 MRUProfile
[i
]->section
=NULL
;
733 MRUProfile
[i
]->filename
=NULL
;
734 MRUProfile
[i
]->encoding
=ENCODING_ANSI
;
735 ZeroMemory(&MRUProfile
[i
]->LastWriteTime
, sizeof(FILETIME
));
739 filename
= L
"win.ini";
741 if ((RtlDetermineDosPathNameType_U(filename
) == RELATIVE_PATH
) &&
742 !wcschr(filename
, '\\') && !wcschr(filename
, '/'))
744 WCHAR windirW
[MAX_PATH
];
745 GetWindowsDirectoryW( windirW
, MAX_PATH
);
746 lstrcpyW(buffer
, windirW
);
747 lstrcatW(buffer
, L
"\\");
748 lstrcatW(buffer
, filename
);
753 GetFullPathNameW(filename
, ARRAY_SIZE(buffer
), buffer
, &dummy
);
756 TRACE("path: %s\n", debugstr_w(buffer
));
758 hFile
= CreateFileW(buffer
, GENERIC_READ
| (write_access
? GENERIC_WRITE
: 0),
759 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
,
760 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
762 if ((hFile
== INVALID_HANDLE_VALUE
) && (GetLastError() != ERROR_FILE_NOT_FOUND
))
764 WARN("Error %ld opening file %s\n", GetLastError(), debugstr_w(buffer
));
768 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
770 if ((MRUProfile
[i
]->filename
&& !wcsicmp( buffer
, MRUProfile
[i
]->filename
)))
772 TRACE("MRU Filename: %s, new filename: %s\n", debugstr_w(MRUProfile
[i
]->filename
), debugstr_w(buffer
));
776 tempProfile
=MRUProfile
[i
];
778 MRUProfile
[j
]=MRUProfile
[j
-1];
779 CurProfile
=tempProfile
;
782 if (hFile
!= INVALID_HANDLE_VALUE
)
784 GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
);
785 if (!memcmp( &CurProfile
->LastWriteTime
, &LastWriteTime
, sizeof(FILETIME
) ) &&
786 is_not_current(&LastWriteTime
))
787 TRACE("(%s): already opened (mru=%d)\n",
788 debugstr_w(buffer
), i
);
791 TRACE("(%s): already opened, needs refreshing (mru=%d)\n",
792 debugstr_w(buffer
), i
);
793 PROFILE_Free(CurProfile
->section
);
794 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
795 CurProfile
->LastWriteTime
= LastWriteTime
;
800 else TRACE("(%s): already opened, not yet created (mru=%d)\n",
801 debugstr_w(buffer
), i
);
805 /* Flush the old current profile */
808 /* Make the oldest profile the current one only in order to get rid of it */
809 if(i
==N_CACHED_PROFILES
)
811 tempProfile
=MRUProfile
[N_CACHED_PROFILES
-1];
812 for(i
=N_CACHED_PROFILES
-1;i
>0;i
--)
813 MRUProfile
[i
]=MRUProfile
[i
-1];
814 CurProfile
=tempProfile
;
816 if(CurProfile
->filename
) PROFILE_ReleaseFile();
818 /* OK, now that CurProfile is definitely free we assign it our new file */
819 CurProfile
->filename
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW(buffer
)+1) * sizeof(WCHAR
) );
820 lstrcpyW( CurProfile
->filename
, buffer
);
822 if (hFile
!= INVALID_HANDLE_VALUE
)
824 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
825 GetFileTime(hFile
, NULL
, NULL
, &CurProfile
->LastWriteTime
);
830 /* Does not exist yet, we will create it in PROFILE_FlushFile */
831 WARN("profile file %s not found\n", debugstr_w(buffer
) );
837 /***********************************************************************
840 * Returns all keys of a section.
841 * If return_values is TRUE, also include the corresponding values.
843 static INT
PROFILE_GetSection( const WCHAR
*filename
, LPCWSTR section_name
,
844 LPWSTR buffer
, UINT len
, BOOL return_values
)
846 PROFILESECTION
*section
;
849 if(!buffer
) return 0;
851 TRACE("%s,%p,%u\n", debugstr_w(section_name
), buffer
, len
);
853 EnterCriticalSection( &PROFILE_CritSect
);
855 if (!PROFILE_Open( filename
, FALSE
))
857 LeaveCriticalSection( &PROFILE_CritSect
);
862 for (section
= CurProfile
->section
; section
; section
= section
->next
)
864 if (!wcsicmp( section
->name
, section_name
))
867 for (key
= section
->key
; key
; key
= key
->next
)
870 if (!*key
->name
&& !key
->value
) continue; /* Skip empty lines */
871 if (IS_ENTRY_COMMENT(key
->name
)) continue; /* Skip comments */
872 if (!return_values
&& !key
->value
) continue; /* Skip lines w.o. '=' */
873 lstrcpynW( buffer
, key
->name
, len
- 1 );
874 len
-= lstrlenW(buffer
) + 1;
875 buffer
+= lstrlenW(buffer
) + 1;
878 if (return_values
&& key
->value
) {
880 lstrcpynW( buffer
, key
->value
, len
- 1 );
881 len
-= lstrlenW(buffer
) + 1;
882 buffer
+= lstrlenW(buffer
) + 1;
887 LeaveCriticalSection( &PROFILE_CritSect
);
890 /*If either lpszSection or lpszKey is NULL and the supplied
891 destination buffer is too small to hold all the strings,
892 the last string is truncated and followed by two null characters.
893 In this case, the return value is equal to cchReturnBuffer
902 buffer
[0] = buffer
[1] = '\0';
904 LeaveCriticalSection( &PROFILE_CritSect
);
909 static BOOL
PROFILE_DeleteSection( const WCHAR
*filename
, const WCHAR
*name
)
911 PROFILESECTION
**section
;
913 EnterCriticalSection( &PROFILE_CritSect
);
915 if (!PROFILE_Open( filename
, TRUE
))
917 LeaveCriticalSection( &PROFILE_CritSect
);
921 for (section
= &CurProfile
->section
; *section
; section
= &(*section
)->next
)
923 if (!wcsicmp( (*section
)->name
, name
))
925 PROFILESECTION
*to_del
= *section
;
926 *section
= to_del
->next
;
928 PROFILE_Free( to_del
);
929 CurProfile
->changed
= TRUE
;
935 LeaveCriticalSection( &PROFILE_CritSect
);
940 /* See GetPrivateProfileSectionNamesA for documentation */
941 static INT
PROFILE_GetSectionNames( LPWSTR buffer
, UINT len
)
945 PROFILESECTION
*section
;
947 TRACE("(%p, %d)\n", buffer
, len
);
958 section
= CurProfile
->section
;
959 while ((section
!=NULL
)) {
960 if (section
->name
[0]) {
961 tmplen
= lstrlenW(section
->name
)+1;
962 if (tmplen
>= buflen
) {
964 memcpy(buf
, section
->name
, (buflen
-1) * sizeof(WCHAR
));
971 memcpy(buf
, section
->name
, tmplen
* sizeof(WCHAR
));
975 section
= section
->next
;
981 /***********************************************************************
984 * Set a profile string.
986 static BOOL
PROFILE_SetString( LPCWSTR section_name
, LPCWSTR key_name
,
987 LPCWSTR value
, BOOL create_always
)
989 if (!value
) /* Delete a key */
991 TRACE("(%s,%s)\n", debugstr_w(section_name
), debugstr_w(key_name
) );
992 CurProfile
->changed
|= PROFILE_DeleteKey( &CurProfile
->section
,
993 section_name
, key_name
);
994 return TRUE
; /* same error handling as above */
996 else /* Set the key value */
998 PROFILEKEY
*key
= PROFILE_Find(&CurProfile
->section
, section_name
,
999 key_name
, TRUE
, create_always
);
1000 TRACE("(%s,%s,%s):\n",
1001 debugstr_w(section_name
), debugstr_w(key_name
), debugstr_w(value
) );
1002 if (!key
) return FALSE
;
1004 /* strip the leading spaces. We can safely strip \n\r and
1005 * friends too, they should not happen here anyway. */
1006 while (PROFILE_isspaceW(*value
)) value
++;
1010 if (!wcscmp( key
->value
, value
))
1012 TRACE(" no change needed\n" );
1013 return TRUE
; /* No change needed */
1015 TRACE(" replacing %s\n", debugstr_w(key
->value
) );
1016 HeapFree( GetProcessHeap(), 0, key
->value
);
1018 else TRACE(" creating key\n" );
1019 key
->value
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW(value
)+1) * sizeof(WCHAR
) );
1020 lstrcpyW( key
->value
, value
);
1021 CurProfile
->changed
= TRUE
;
1026 static HKEY
open_file_mapping_key( const WCHAR
*filename
)
1028 static HKEY mapping_key
;
1031 EnterCriticalSection( &PROFILE_CritSect
);
1033 if (!mapping_key
&& RegOpenKeyExW( HKEY_LOCAL_MACHINE
,
1034 L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping",
1035 0, KEY_WOW64_64KEY
, &mapping_key
))
1038 LeaveCriticalSection( &PROFILE_CritSect
);
1040 if (mapping_key
&& !RegOpenKeyExW( mapping_key
, PathFindFileNameW( filename
), 0, KEY_READ
, &key
))
1045 static WCHAR
*enum_key( HKEY key
, DWORD i
)
1047 WCHAR
*value
, *new_value
;
1048 DWORD max
= 256, len
;
1051 if (!(value
= HeapAlloc( GetProcessHeap(), 0, max
* sizeof(WCHAR
) ))) return NULL
;
1053 while ((res
= RegEnumValueW( key
, i
, value
, &len
, NULL
, NULL
, NULL
, NULL
)) == ERROR_MORE_DATA
)
1056 if (!(new_value
= HeapReAlloc( GetProcessHeap(), 0, value
, max
* sizeof(WCHAR
) )))
1058 HeapFree( GetProcessHeap(), 0, value
);
1064 if (!res
) return value
;
1065 HeapFree( GetProcessHeap(), 0, value
);
1069 static WCHAR
*get_key_value( HKEY key
, const WCHAR
*value
)
1074 if (RegGetValueW( key
, NULL
, value
, RRF_RT_REG_SZ
| RRF_NOEXPAND
, NULL
, NULL
, &size
)) return NULL
;
1075 if (!(data
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
1076 if (!RegGetValueW( key
, NULL
, value
, RRF_RT_REG_SZ
| RRF_NOEXPAND
, NULL
, (BYTE
*)data
, &size
)) return data
;
1077 HeapFree( GetProcessHeap(), 0, data
);
1081 static HKEY
open_mapped_key( const WCHAR
*path
, BOOL write
)
1083 static const WCHAR usrW
[] = {'U','S','R',':'};
1084 static const WCHAR sysW
[] = {'S','Y','S',':'};
1085 WCHAR
*combined_path
;
1090 TRACE("%s\n", debugstr_w( path
));
1092 for (p
= path
; strchr("!#@", *p
); p
++)
1093 FIXME("ignoring %c modifier\n", *p
);
1095 if (!wcsncmp( p
, usrW
, ARRAY_SIZE( usrW
) ))
1098 res
= RegCreateKeyExW( HKEY_CURRENT_USER
, p
+ 4, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
1100 res
= RegOpenKeyExW( HKEY_CURRENT_USER
, p
+ 4, 0, KEY_READ
, &key
);
1101 return res
? NULL
: key
;
1104 if (!wcsncmp( p
, sysW
, ARRAY_SIZE( sysW
) ))
1107 if (!(combined_path
= HeapAlloc( GetProcessHeap(), 0,
1108 (ARRAY_SIZE( L
"Software\\" ) + lstrlenW( p
)) * sizeof(WCHAR
) )))
1110 lstrcpyW( combined_path
, L
"Software\\" );
1111 lstrcatW( combined_path
, p
);
1113 res
= RegCreateKeyExW( HKEY_LOCAL_MACHINE
, combined_path
, 0, NULL
,
1114 0, KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
1116 res
= RegOpenKeyExW( HKEY_LOCAL_MACHINE
, combined_path
, 0, KEY_READ
, &key
);
1117 HeapFree( GetProcessHeap(), 0, combined_path
);
1118 return res
? NULL
: key
;
1121 FIXME("unhandled path syntax %s\n", debugstr_w( path
));
1125 /* returns TRUE if the given section + name is mapped */
1126 static BOOL
get_mapped_section_key( const WCHAR
*filename
, const WCHAR
*section
,
1127 const WCHAR
*name
, BOOL write
, HKEY
*ret_key
)
1129 WCHAR
*path
= NULL
, *combined_path
;
1130 HKEY key
, subkey
= NULL
;
1132 if (!(key
= open_file_mapping_key( filename
)))
1135 if (!RegOpenKeyExW( key
, section
, 0, KEY_READ
, &subkey
))
1137 if (!(path
= get_key_value( subkey
, name
)))
1138 path
= get_key_value( subkey
, NULL
);
1139 RegCloseKey( subkey
);
1141 if (!path
) return FALSE
;
1145 if (!(path
= get_key_value( key
, section
)))
1147 if ((path
= get_key_value( key
, NULL
)))
1149 if ((combined_path
= HeapAlloc( GetProcessHeap(), 0,
1150 (lstrlenW( path
) + lstrlenW( section
) + 2) * sizeof(WCHAR
) )))
1152 lstrcpyW( combined_path
, path
);
1153 lstrcatW( combined_path
, L
"\\" );
1154 lstrcatW( combined_path
, section
);
1156 HeapFree( GetProcessHeap(), 0, path
);
1157 path
= combined_path
;
1161 if (!path
) return FALSE
;
1164 *ret_key
= open_mapped_key( path
, write
);
1165 HeapFree( GetProcessHeap(), 0, path
);
1169 static DWORD
get_mapped_section( HKEY key
, WCHAR
*buffer
, DWORD size
, BOOL return_values
)
1171 WCHAR
*entry
, *value
;
1174 for (i
= 0; (entry
= enum_key( key
, i
)); ++i
)
1176 lstrcpynW( buffer
+ ret
, entry
, size
- ret
- 1 );
1177 ret
= min( ret
+ lstrlenW( entry
) + 1, size
- 1 );
1178 if (return_values
&& ret
< size
- 1 && (value
= get_key_value( key
, entry
)))
1180 buffer
[ret
- 1] = '=';
1181 lstrcpynW( buffer
+ ret
, value
, size
- ret
- 1 );
1182 ret
= min( ret
+ lstrlenW( value
) + 1, size
- 1 );
1183 HeapFree( GetProcessHeap(), 0, value
);
1185 HeapFree( GetProcessHeap(), 0, entry
);
1191 static DWORD
get_section( const WCHAR
*filename
, const WCHAR
*section
,
1192 WCHAR
*buffer
, DWORD size
, BOOL return_values
)
1194 HKEY key
, subkey
, section_key
;
1195 BOOL use_ini
= TRUE
;
1199 if ((key
= open_file_mapping_key( filename
)))
1201 if (!RegOpenKeyExW( key
, section
, 0, KEY_READ
, &subkey
))
1203 WCHAR
*entry
, *value
;
1207 for (i
= 0; (entry
= enum_key( subkey
, i
)); ++i
)
1209 if (!(path
= get_key_value( subkey
, entry
)))
1211 HeapFree( GetProcessHeap(), 0, entry
);
1215 entry_key
= open_mapped_key( path
, FALSE
);
1216 HeapFree( GetProcessHeap(), 0, path
);
1219 HeapFree( GetProcessHeap(), 0, entry
);
1225 if ((value
= get_key_value( entry_key
, entry
)))
1227 lstrcpynW( buffer
+ ret
, entry
, size
- ret
- 1 );
1228 ret
= min( ret
+ lstrlenW( entry
) + 1, size
- 1 );
1229 if (return_values
&& ret
< size
- 1)
1231 buffer
[ret
- 1] = '=';
1232 lstrcpynW( buffer
+ ret
, value
, size
- ret
- 1 );
1233 ret
= min( ret
+ lstrlenW( value
) + 1, size
- 1 );
1235 HeapFree( GetProcessHeap(), 0, value
);
1240 ret
= get_mapped_section( entry_key
, buffer
, size
, return_values
);
1244 HeapFree( GetProcessHeap(), 0, entry
);
1245 RegCloseKey( entry_key
);
1248 RegCloseKey( subkey
);
1250 else if (get_mapped_section_key( filename
, section
, NULL
, FALSE
, §ion_key
))
1252 ret
= get_mapped_section( section_key
, buffer
, size
, return_values
);
1254 RegCloseKey( section_key
);
1261 ret
+= PROFILE_GetSection( filename
, section
, buffer
+ ret
, size
- ret
, return_values
);
1266 static void delete_key_values( HKEY key
)
1270 while ((entry
= enum_key( key
, 0 )))
1272 RegDeleteValueW( key
, entry
);
1273 HeapFree( GetProcessHeap(), 0, entry
);
1277 static BOOL
delete_section( const WCHAR
*filename
, const WCHAR
*section
)
1279 HKEY key
, subkey
, section_key
;
1281 if ((key
= open_file_mapping_key( filename
)))
1283 if (!RegOpenKeyExW( key
, section
, 0, KEY_READ
, &subkey
))
1285 WCHAR
*entry
, *path
;
1289 for (i
= 0; (entry
= enum_key( subkey
, i
)); ++i
)
1291 if (!(path
= get_key_value( subkey
, entry
)))
1293 HeapFree( GetProcessHeap(), 0, entry
);
1297 entry_key
= open_mapped_key( path
, TRUE
);
1298 HeapFree( GetProcessHeap(), 0, path
);
1301 HeapFree( GetProcessHeap(), 0, entry
);
1306 RegDeleteValueW( entry_key
, entry
);
1308 delete_key_values( entry_key
);
1310 HeapFree( GetProcessHeap(), 0, entry
);
1311 RegCloseKey( entry_key
);
1314 RegCloseKey( subkey
);
1316 else if (get_mapped_section_key( filename
, section
, NULL
, TRUE
, §ion_key
))
1318 delete_key_values( section_key
);
1319 RegCloseKey( section_key
);
1325 return PROFILE_DeleteSection( filename
, section
);
1328 /********************* API functions **********************************/
1331 /***********************************************************************
1332 * GetProfileIntA (KERNEL32.@)
1334 UINT WINAPI
GetProfileIntA( LPCSTR section
, LPCSTR entry
, INT def_val
)
1336 return GetPrivateProfileIntA( section
, entry
, def_val
, "win.ini" );
1339 /***********************************************************************
1340 * GetProfileIntW (KERNEL32.@)
1342 UINT WINAPI
GetProfileIntW( LPCWSTR section
, LPCWSTR entry
, INT def_val
)
1344 return GetPrivateProfileIntW( section
, entry
, def_val
, L
"win.ini" );
1347 /***********************************************************************
1348 * GetPrivateProfileStringW (KERNEL32.@)
1350 INT WINAPI
GetPrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1351 LPCWSTR def_val
, LPWSTR buffer
,
1352 UINT len
, LPCWSTR filename
)
1355 LPWSTR defval_tmp
= NULL
;
1359 TRACE("%s,%s,%s,%p,%u,%s\n", debugstr_w(section
), debugstr_w(entry
),
1360 debugstr_w(def_val
), buffer
, len
, debugstr_w(filename
));
1362 if (!buffer
|| !len
) return 0;
1363 if (!def_val
) def_val
= L
"";
1364 if (!section
) return GetPrivateProfileSectionNamesW( buffer
, len
, filename
);
1367 ret
= get_section( filename
, section
, buffer
, len
, FALSE
);
1370 PROFILE_CopyEntry( buffer
, def_val
, len
);
1371 ret
= lstrlenW( buffer
);
1376 /* strip any trailing ' ' of def_val. */
1377 p
= def_val
+ lstrlenW(def_val
) - 1;
1379 while (p
> def_val
&& *p
== ' ') p
--;
1383 int vlen
= (int)(p
- def_val
) + 1;
1385 defval_tmp
= HeapAlloc(GetProcessHeap(), 0, (vlen
+ 1) * sizeof(WCHAR
));
1386 memcpy(defval_tmp
, def_val
, vlen
* sizeof(WCHAR
));
1387 defval_tmp
[vlen
] = '\0';
1388 def_val
= defval_tmp
;
1391 if (get_mapped_section_key( filename
, section
, entry
, FALSE
, &key
))
1397 if ((value
= get_key_value( key
, entry
)))
1399 lstrcpynW( buffer
, value
, len
);
1400 HeapFree( GetProcessHeap(), 0, value
);
1403 lstrcpynW( buffer
, def_val
, len
);
1408 lstrcpynW( buffer
, def_val
, len
);
1410 ret
= lstrlenW( buffer
);
1414 EnterCriticalSection( &PROFILE_CritSect
);
1416 if (PROFILE_Open( filename
, FALSE
))
1418 PROFILEKEY
*key
= PROFILE_Find( &CurProfile
->section
, section
, entry
, FALSE
, FALSE
);
1419 PROFILE_CopyEntry( buffer
, (key
&& key
->value
) ? key
->value
: def_val
, len
);
1420 TRACE("-> %s\n", debugstr_w( buffer
));
1421 ret
= lstrlenW( buffer
);
1425 lstrcpynW( buffer
, def_val
, len
);
1426 ret
= lstrlenW( buffer
);
1429 LeaveCriticalSection( &PROFILE_CritSect
);
1432 HeapFree(GetProcessHeap(), 0, defval_tmp
);
1434 TRACE("returning %s, %d\n", debugstr_w(buffer
), ret
);
1439 /***********************************************************************
1440 * GetPrivateProfileStringA (KERNEL32.@)
1442 INT WINAPI
GetPrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1443 LPCSTR def_val
, LPSTR buffer
,
1444 UINT len
, LPCSTR filename
)
1446 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1450 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1451 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1452 else sectionW
.Buffer
= NULL
;
1453 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1454 else entryW
.Buffer
= NULL
;
1455 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1456 else def_valW
.Buffer
= NULL
;
1457 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1458 else filenameW
.Buffer
= NULL
;
1460 retW
= GetPrivateProfileStringW( sectionW
.Buffer
, entryW
.Buffer
,
1461 def_valW
.Buffer
, bufferW
, len
,
1467 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
, buffer
, len
- 1, NULL
, NULL
);
1474 RtlFreeUnicodeString(§ionW
);
1475 RtlFreeUnicodeString(&entryW
);
1476 RtlFreeUnicodeString(&def_valW
);
1477 RtlFreeUnicodeString(&filenameW
);
1478 HeapFree(GetProcessHeap(), 0, bufferW
);
1482 /***********************************************************************
1483 * GetProfileStringA (KERNEL32.@)
1485 INT WINAPI
GetProfileStringA( LPCSTR section
, LPCSTR entry
, LPCSTR def_val
,
1486 LPSTR buffer
, UINT len
)
1488 return GetPrivateProfileStringA( section
, entry
, def_val
,
1489 buffer
, len
, "win.ini" );
1492 /***********************************************************************
1493 * GetProfileStringW (KERNEL32.@)
1495 INT WINAPI
GetProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1496 LPCWSTR def_val
, LPWSTR buffer
, UINT len
)
1498 return GetPrivateProfileStringW( section
, entry
, def_val
, buffer
, len
, L
"win.ini" );
1501 /***********************************************************************
1502 * WriteProfileStringA (KERNEL32.@)
1504 BOOL WINAPI
WriteProfileStringA( LPCSTR section
, LPCSTR entry
,
1507 return WritePrivateProfileStringA( section
, entry
, string
, "win.ini" );
1510 /***********************************************************************
1511 * WriteProfileStringW (KERNEL32.@)
1513 BOOL WINAPI
WriteProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1516 return WritePrivateProfileStringW( section
, entry
, string
, L
"win.ini" );
1520 /***********************************************************************
1521 * GetPrivateProfileIntW (KERNEL32.@)
1523 UINT WINAPI
GetPrivateProfileIntW( LPCWSTR section
, LPCWSTR entry
,
1524 INT def_val
, LPCWSTR filename
)
1527 UNICODE_STRING bufferW
;
1530 if (GetPrivateProfileStringW( section
, entry
, L
"", buffer
, ARRAY_SIZE( buffer
),
1534 /* FIXME: if entry can be found but it's empty, then Win16 is
1535 * supposed to return 0 instead of def_val ! Difficult/problematic
1536 * to implement (every other failure also returns zero buffer),
1537 * thus wait until testing framework avail for making sure nothing
1538 * else gets broken that way. */
1539 if (!buffer
[0]) return (UINT
)def_val
;
1541 RtlInitUnicodeString( &bufferW
, buffer
);
1542 RtlUnicodeStringToInteger( &bufferW
, 0, &result
);
1546 /***********************************************************************
1547 * GetPrivateProfileIntA (KERNEL32.@)
1549 UINT WINAPI
GetPrivateProfileIntA( LPCSTR section
, LPCSTR entry
,
1550 INT def_val
, LPCSTR filename
)
1552 UNICODE_STRING entryW
, filenameW
, sectionW
;
1554 if(entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1555 else entryW
.Buffer
= NULL
;
1556 if(filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1557 else filenameW
.Buffer
= NULL
;
1558 if(section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1559 else sectionW
.Buffer
= NULL
;
1560 res
= GetPrivateProfileIntW(sectionW
.Buffer
, entryW
.Buffer
, def_val
,
1562 RtlFreeUnicodeString(§ionW
);
1563 RtlFreeUnicodeString(&filenameW
);
1564 RtlFreeUnicodeString(&entryW
);
1568 /***********************************************************************
1569 * GetPrivateProfileSectionW (KERNEL32.@)
1571 INT WINAPI
GetPrivateProfileSectionW( LPCWSTR section
, LPWSTR buffer
,
1572 DWORD len
, LPCWSTR filename
)
1574 if (!section
|| !buffer
)
1576 SetLastError(ERROR_INVALID_PARAMETER
);
1580 TRACE("(%s, %p, %ld, %s)\n", debugstr_w(section
), buffer
, len
, debugstr_w(filename
));
1582 return get_section( filename
, section
, buffer
, len
, TRUE
);
1585 /***********************************************************************
1586 * GetPrivateProfileSectionA (KERNEL32.@)
1588 INT WINAPI
GetPrivateProfileSectionA( LPCSTR section
, LPSTR buffer
,
1589 DWORD len
, LPCSTR filename
)
1591 UNICODE_STRING sectionW
, filenameW
;
1595 if (!section
|| !buffer
)
1597 SetLastError(ERROR_INVALID_PARAMETER
);
1601 bufferW
= HeapAlloc(GetProcessHeap(), 0, len
* 2 * sizeof(WCHAR
));
1602 RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1603 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1604 else filenameW
.Buffer
= NULL
;
1606 retW
= GetPrivateProfileSectionW(sectionW
.Buffer
, bufferW
, len
* 2, filenameW
.Buffer
);
1609 if (retW
== len
* 2 - 2) retW
++; /* overflow */
1610 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1611 if (!ret
|| ret
== len
) /* overflow */
1625 RtlFreeUnicodeString(§ionW
);
1626 RtlFreeUnicodeString(&filenameW
);
1627 HeapFree(GetProcessHeap(), 0, bufferW
);
1631 /***********************************************************************
1632 * GetProfileSectionA (KERNEL32.@)
1634 INT WINAPI
GetProfileSectionA( LPCSTR section
, LPSTR buffer
, DWORD len
)
1636 return GetPrivateProfileSectionA( section
, buffer
, len
, "win.ini" );
1639 /***********************************************************************
1640 * GetProfileSectionW (KERNEL32.@)
1642 INT WINAPI
GetProfileSectionW( LPCWSTR section
, LPWSTR buffer
, DWORD len
)
1644 return GetPrivateProfileSectionW( section
, buffer
, len
, L
"win.ini" );
1648 /***********************************************************************
1649 * WritePrivateProfileStringW (KERNEL32.@)
1651 BOOL WINAPI
WritePrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1652 LPCWSTR string
, LPCWSTR filename
)
1657 TRACE("(%s, %s, %s, %s)\n", debugstr_w(section
), debugstr_w(entry
), debugstr_w(string
), debugstr_w(filename
));
1659 if (!section
&& !entry
&& !string
) /* documented "file flush" case */
1661 EnterCriticalSection( &PROFILE_CritSect
);
1662 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1664 if (CurProfile
) PROFILE_ReleaseFile();
1666 LeaveCriticalSection( &PROFILE_CritSect
);
1669 if (!entry
) return delete_section( filename
, section
);
1671 if (get_mapped_section_key( filename
, section
, entry
, TRUE
, &key
))
1676 res
= RegSetValueExW( key
, entry
, 0, REG_SZ
, (const BYTE
*)string
,
1677 (lstrlenW( string
) + 1) * sizeof(WCHAR
) );
1679 res
= RegDeleteValueW( key
, entry
);
1681 if (res
) SetLastError( res
);
1685 EnterCriticalSection( &PROFILE_CritSect
);
1687 if (PROFILE_Open( filename
, TRUE
))
1690 SetLastError(ERROR_FILE_NOT_FOUND
);
1692 ret
= PROFILE_SetString( section
, entry
, string
, FALSE
);
1693 if (ret
) ret
= PROFILE_FlushFile();
1696 LeaveCriticalSection( &PROFILE_CritSect
);
1700 /***********************************************************************
1701 * WritePrivateProfileStringA (KERNEL32.@)
1703 BOOL WINAPI DECLSPEC_HOTPATCH
WritePrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1704 LPCSTR string
, LPCSTR filename
)
1706 UNICODE_STRING sectionW
, entryW
, stringW
, filenameW
;
1709 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1710 else sectionW
.Buffer
= NULL
;
1711 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1712 else entryW
.Buffer
= NULL
;
1713 if (string
) RtlCreateUnicodeStringFromAsciiz(&stringW
, string
);
1714 else stringW
.Buffer
= NULL
;
1715 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1716 else filenameW
.Buffer
= NULL
;
1718 ret
= WritePrivateProfileStringW(sectionW
.Buffer
, entryW
.Buffer
,
1719 stringW
.Buffer
, filenameW
.Buffer
);
1720 RtlFreeUnicodeString(§ionW
);
1721 RtlFreeUnicodeString(&entryW
);
1722 RtlFreeUnicodeString(&stringW
);
1723 RtlFreeUnicodeString(&filenameW
);
1727 /***********************************************************************
1728 * WritePrivateProfileSectionW (KERNEL32.@)
1730 BOOL WINAPI
WritePrivateProfileSectionW( LPCWSTR section
,
1731 LPCWSTR string
, LPCWSTR filename
)
1735 HKEY key
, section_key
;
1737 if (!section
&& !string
)
1739 EnterCriticalSection( &PROFILE_CritSect
);
1740 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1742 if (CurProfile
) PROFILE_ReleaseFile();
1744 LeaveCriticalSection( &PROFILE_CritSect
);
1747 if (!string
) return delete_section( filename
, section
);
1749 if ((key
= open_file_mapping_key( filename
)))
1751 /* replace existing entries, but only if they are mapped, and do not
1752 * delete any keys */
1754 const WCHAR
*entry
, *p
;
1756 for (entry
= string
; *entry
; entry
+= lstrlenW( entry
) + 1)
1758 if ((p
= wcschr( entry
, '=' )))
1762 if (!(entry_copy
= HeapAlloc( GetProcessHeap(), 0, (p
- entry
) * sizeof(WCHAR
) )))
1764 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1768 lstrcpynW( entry_copy
, entry
, p
- entry
);
1769 if (get_mapped_section_key( filename
, section
, entry_copy
, TRUE
, §ion_key
))
1771 LSTATUS res
= RegSetValueExW( section_key
, entry_copy
, 0, REG_SZ
, (const BYTE
*)p
,
1772 (lstrlenW( p
) + 1) * sizeof(WCHAR
) );
1773 RegCloseKey( section_key
);
1776 HeapFree( GetProcessHeap(), 0, entry_copy
);
1777 SetLastError( res
);
1782 HeapFree( GetProcessHeap(), 0, entry_copy
);
1789 EnterCriticalSection( &PROFILE_CritSect
);
1791 if (PROFILE_Open( filename
, TRUE
))
1793 PROFILE_DeleteAllKeys(section
);
1795 while (*string
&& ret
)
1797 WCHAR
*buf
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW( string
) + 1) * sizeof(WCHAR
) );
1798 lstrcpyW( buf
, string
);
1799 if ((p
= wcschr( buf
, '=')))
1802 ret
= PROFILE_SetString( section
, buf
, p
+1, TRUE
);
1804 HeapFree( GetProcessHeap(), 0, buf
);
1805 string
+= lstrlenW( string
) + 1;
1807 if (ret
) ret
= PROFILE_FlushFile();
1810 LeaveCriticalSection( &PROFILE_CritSect
);
1814 /***********************************************************************
1815 * WritePrivateProfileSectionA (KERNEL32.@)
1817 BOOL WINAPI
WritePrivateProfileSectionA( LPCSTR section
,
1818 LPCSTR string
, LPCSTR filename
)
1821 UNICODE_STRING sectionW
, filenameW
;
1830 while(*p
) p
+= strlen(p
) + 1;
1831 lenA
= p
- string
+ 1;
1832 lenW
= MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, NULL
, 0);
1833 if ((stringW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
))))
1834 MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, stringW
, lenW
);
1836 else stringW
= NULL
;
1837 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1838 else sectionW
.Buffer
= NULL
;
1839 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1840 else filenameW
.Buffer
= NULL
;
1842 ret
= WritePrivateProfileSectionW(sectionW
.Buffer
, stringW
, filenameW
.Buffer
);
1844 HeapFree(GetProcessHeap(), 0, stringW
);
1845 RtlFreeUnicodeString(§ionW
);
1846 RtlFreeUnicodeString(&filenameW
);
1850 /***********************************************************************
1851 * WriteProfileSectionA (KERNEL32.@)
1853 BOOL WINAPI
WriteProfileSectionA( LPCSTR section
, LPCSTR keys_n_values
)
1856 return WritePrivateProfileSectionA( section
, keys_n_values
, "win.ini");
1859 /***********************************************************************
1860 * WriteProfileSectionW (KERNEL32.@)
1862 BOOL WINAPI
WriteProfileSectionW( LPCWSTR section
, LPCWSTR keys_n_values
)
1864 return WritePrivateProfileSectionW(section
, keys_n_values
, L
"win.ini");
1868 /***********************************************************************
1869 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1871 * Returns the section names contained in the specified file.
1872 * FIXME: Where do we find this file when the path is relative?
1873 * The section names are returned as a list of strings with an extra
1874 * '\0' to mark the end of the list. Except for that the behavior
1875 * depends on the Windows version.
1878 * - if the buffer is 0 or 1 character long then it is as if it was of
1880 * - otherwise, if the buffer is too small only the section names that fit
1882 * - note that this means if the buffer was too small to return even just
1883 * the first section name then a single '\0' will be returned.
1884 * - the return value is the number of characters written in the buffer,
1885 * except if the buffer was too small in which case len-2 is returned
1888 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1889 * '\0' and the return value is 0
1890 * - otherwise if the buffer is too small then the first section name that
1891 * does not fit is truncated so that the string list can be terminated
1892 * correctly (double '\0')
1893 * - the return value is the number of characters written in the buffer
1894 * except for the trailing '\0'. If the buffer is too small, then the
1895 * return value is len-2
1896 * - Win2000 has a bug that triggers when the section names and the
1897 * trailing '\0' fit exactly in the buffer. In that case the trailing
1900 * Wine implements the observed Win2000 behavior (except for the bug).
1902 * Note that when the buffer is big enough then the return value may be any
1903 * value between 1 and len-1 (or len in Win95), including len-2.
1905 DWORD WINAPI
GetPrivateProfileSectionNamesW( LPWSTR buffer
, DWORD size
,
1911 if ((key
= open_file_mapping_key( filename
)))
1916 for (i
= 0; (section
= enum_key( key
, i
)); ++i
)
1918 lstrcpynW( buffer
+ ret
, section
, size
- ret
- 1 );
1919 ret
= min( ret
+ lstrlenW( section
) + 1, size
- 1 );
1920 HeapFree( GetProcessHeap(), 0, section
);
1926 RtlEnterCriticalSection( &PROFILE_CritSect
);
1928 if (PROFILE_Open( filename
, FALSE
))
1929 ret
+= PROFILE_GetSectionNames( buffer
+ ret
, size
- ret
);
1931 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1937 /***********************************************************************
1938 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1940 DWORD WINAPI
GetPrivateProfileSectionNamesA( LPSTR buffer
, DWORD size
,
1943 UNICODE_STRING filenameW
;
1947 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)) : NULL
;
1948 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1949 else filenameW
.Buffer
= NULL
;
1951 retW
= GetPrivateProfileSectionNamesW(bufferW
, size
, filenameW
.Buffer
);
1954 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+1, buffer
, size
-1, NULL
, NULL
);
1966 RtlFreeUnicodeString(&filenameW
);
1967 HeapFree(GetProcessHeap(), 0, bufferW
);
1971 static int get_hex_byte( const WCHAR
*p
)
1975 if (*p
>= '0' && *p
<= '9') val
= *p
- '0';
1976 else if (*p
>= 'A' && *p
<= 'Z') val
= *p
- 'A' + 10;
1977 else if (*p
>= 'a' && *p
<= 'z') val
= *p
- 'a' + 10;
1981 if (*p
>= '0' && *p
<= '9') val
+= *p
- '0';
1982 else if (*p
>= 'A' && *p
<= 'Z') val
+= *p
- 'A' + 10;
1983 else if (*p
>= 'a' && *p
<= 'z') val
+= *p
- 'a' + 10;
1988 /***********************************************************************
1989 * GetPrivateProfileStructW (KERNEL32.@)
1991 * Should match Win95's behaviour pretty much
1993 BOOL WINAPI
GetPrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1994 LPVOID buf
, UINT len
, LPCWSTR filename
)
2002 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (2 * len
+ 3) * sizeof(WCHAR
) ))) return FALSE
;
2004 if (GetPrivateProfileStringW( section
, key
, NULL
, buffer
, 2 * len
+ 3, filename
) != 2 * len
+ 2)
2007 for (p
= buffer
; len
; p
+= 2, len
--)
2009 if ((val
= get_hex_byte( p
)) == -1) goto done
;
2013 /* retrieve stored checksum value */
2014 if ((val
= get_hex_byte( p
)) == -1) goto done
;
2015 ret
= ((BYTE
)val
== chksum
);
2018 HeapFree( GetProcessHeap(), 0, buffer
);
2022 /***********************************************************************
2023 * GetPrivateProfileStructA (KERNEL32.@)
2025 BOOL WINAPI
GetPrivateProfileStructA (LPCSTR section
, LPCSTR key
,
2026 LPVOID buffer
, UINT len
, LPCSTR filename
)
2028 UNICODE_STRING sectionW
, keyW
, filenameW
;
2031 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
2032 else sectionW
.Buffer
= NULL
;
2033 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
2034 else keyW
.Buffer
= NULL
;
2035 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
2036 else filenameW
.Buffer
= NULL
;
2038 ret
= GetPrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buffer
, len
,
2040 /* Do not translate binary data. */
2042 RtlFreeUnicodeString(§ionW
);
2043 RtlFreeUnicodeString(&keyW
);
2044 RtlFreeUnicodeString(&filenameW
);
2050 /***********************************************************************
2051 * WritePrivateProfileStructW (KERNEL32.@)
2053 BOOL WINAPI
WritePrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
2054 LPVOID buf
, UINT bufsize
, LPCWSTR filename
)
2058 LPWSTR outstring
, p
;
2061 TRACE("(%s %s %p %u %s)\n", debugstr_w(section
), debugstr_w(key
), buf
, bufsize
, debugstr_w(filename
));
2063 if (!section
&& !key
&& !buf
) /* flush the cache */
2064 return WritePrivateProfileStringW( NULL
, NULL
, NULL
, filename
);
2067 return WritePrivateProfileStringW(section
, key
, NULL
, filename
);
2069 /* allocate string buffer for hex chars + checksum hex char + '\0' */
2070 outstring
= HeapAlloc( GetProcessHeap(), 0, (bufsize
*2 + 2 + 1) * sizeof(WCHAR
) );
2072 for (binbuf
= (LPBYTE
)buf
; binbuf
< (LPBYTE
)buf
+bufsize
; binbuf
++) {
2073 *p
++ = hex
[*binbuf
>> 4];
2074 *p
++ = hex
[*binbuf
& 0xf];
2077 /* checksum is sum & 0xff */
2078 *p
++ = hex
[(sum
& 0xf0) >> 4];
2079 *p
++ = hex
[sum
& 0xf];
2082 ret
= WritePrivateProfileStringW( section
, key
, outstring
, filename
);
2083 HeapFree( GetProcessHeap(), 0, outstring
);
2087 /***********************************************************************
2088 * WritePrivateProfileStructA (KERNEL32.@)
2090 BOOL WINAPI
WritePrivateProfileStructA (LPCSTR section
, LPCSTR key
,
2091 LPVOID buf
, UINT bufsize
, LPCSTR filename
)
2093 UNICODE_STRING sectionW
, keyW
, filenameW
;
2096 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
2097 else sectionW
.Buffer
= NULL
;
2098 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
2099 else keyW
.Buffer
= NULL
;
2100 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
2101 else filenameW
.Buffer
= NULL
;
2103 /* Do not translate binary data. */
2104 ret
= WritePrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buf
, bufsize
,
2107 RtlFreeUnicodeString(§ionW
);
2108 RtlFreeUnicodeString(&keyW
);
2109 RtlFreeUnicodeString(&filenameW
);
2114 /***********************************************************************
2115 * OpenProfileUserMapping (KERNEL32.@)
2117 BOOL WINAPI
OpenProfileUserMapping(void) {
2118 FIXME("(), stub!\n");
2119 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2123 /***********************************************************************
2124 * CloseProfileUserMapping (KERNEL32.@)
2126 BOOL WINAPI
CloseProfileUserMapping(void) {
2127 FIXME("(), stub!\n");
2128 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);