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
27 static const char bom_utf8
[] = {0xEF,0xBB,0xBF};
37 typedef struct tagPROFILEKEY
40 struct tagPROFILEKEY
*next
;
44 typedef struct tagPROFILESECTION
46 struct tagPROFILEKEY
*key
;
47 struct tagPROFILESECTION
*next
;
55 PROFILESECTION
*section
;
57 FILETIME LastWriteTime
;
62 #define N_CACHED_PROFILES 10
64 /* Cached profile files */
65 static PROFILE
*MRUProfile
[N_CACHED_PROFILES
]={NULL
};
67 #define CurProfile (MRUProfile[0])
69 /* Check for comments in profile */
70 #define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
72 static const WCHAR emptystringW
[] = {0};
73 static const WCHAR wininiW
[] = { 'w','i','n','.','i','n','i',0 };
75 static RTL_CRITICAL_SECTION PROFILE_CritSect
;
76 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
78 0, 0, &PROFILE_CritSect
,
79 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
82 static RTL_CRITICAL_SECTION PROFILE_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
84 static const char hex
[16] = "0123456789ABCDEF";
87 static __inline WCHAR
*memchrW( const WCHAR
*ptr
, WCHAR ch
, size_t n
)
90 for (end
= ptr
+ n
; ptr
< end
; ptr
++)
92 return (WCHAR
*)(ULONG_PTR
)ptr
;
96 static __inline WCHAR
*memrchrW( const WCHAR
*ptr
, WCHAR ch
, size_t n
)
100 for (end
= ptr
+ n
; ptr
< end
; ptr
++)
102 ret
= (WCHAR
*)(ULONG_PTR
)ptr
;
106 /***********************************************************************
109 * Copy the content of an entry into a buffer, removing quotes, and possibly
110 * translating environment variables.
112 static void PROFILE_CopyEntry( LPWSTR buffer
, LPCWSTR value
, int len
,
119 if (strip_quote
&& ((*value
== '\'') || (*value
== '\"')))
121 if (value
[1] && (value
[wcslen(value
)-1] == *value
))
125 lstrcpynW( buffer
, value
, len
);
126 if (quote
&& (len
>= (int)wcslen(value
))) buffer
[wcslen(buffer
)-1] = '\0';
129 /* byte-swaps shorts in-place in a buffer. len is in WCHARs */
130 static __inline
void PROFILE_ByteSwapShortBuffer(WCHAR
* buffer
, int len
)
133 USHORT
* shortbuffer
= buffer
;
134 for (i
= 0; i
< len
; i
++)
135 shortbuffer
[i
] = RtlUshortByteSwap(shortbuffer
[i
]);
138 /* writes any necessary encoding marker to the file */
139 static __inline
void PROFILE_WriteMarker(HANDLE hFile
, ENCODING encoding
)
141 DWORD dwBytesWritten
;
148 WriteFile(hFile
, bom_utf8
, sizeof(bom_utf8
), &dwBytesWritten
, NULL
);
150 case ENCODING_UTF16LE
:
152 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
154 case ENCODING_UTF16BE
:
156 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
161 static void PROFILE_WriteLine( HANDLE hFile
, WCHAR
* szLine
, int len
, ENCODING encoding
)
164 int write_buffer_len
;
165 DWORD dwBytesWritten
;
167 DPRINT("writing: %.*S\n", len
, szLine
);
172 write_buffer_len
= WideCharToMultiByte(CP_ACP
, 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_ACP
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
176 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
177 HeapFree(GetProcessHeap(), 0, write_buffer
);
180 write_buffer_len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
181 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
182 if (!write_buffer
) return;
183 len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
184 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
185 HeapFree(GetProcessHeap(), 0, write_buffer
);
187 case ENCODING_UTF16LE
:
188 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
190 case ENCODING_UTF16BE
:
191 PROFILE_ByteSwapShortBuffer(szLine
, len
);
192 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
195 DPRINT1("encoding type %d not implemented\n", encoding
);
199 /***********************************************************************
202 * Save a profile tree to a file.
204 static void PROFILE_Save( HANDLE hFile
, const PROFILESECTION
*section
, ENCODING encoding
)
209 PROFILE_WriteMarker(hFile
, encoding
);
211 for ( ; section
; section
= section
->next
)
215 if (section
->name
[0]) len
+= wcslen(section
->name
) + 4;
217 for (key
= section
->key
; key
; key
= key
->next
)
219 len
+= wcslen(key
->name
) + 2;
220 if (key
->value
) len
+= wcslen(key
->value
) + 1;
223 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
227 if (section
->name
[0])
230 wcscpy( p
, section
->name
);
237 for (key
= section
->key
; key
; key
= key
->next
)
239 wcscpy( p
, key
->name
);
244 wcscpy( p
, key
->value
);
250 PROFILE_WriteLine( hFile
, buffer
, len
, encoding
);
251 HeapFree(GetProcessHeap(), 0, buffer
);
256 /***********************************************************************
259 * Free a profile tree.
261 static void PROFILE_Free( PROFILESECTION
*section
)
263 PROFILESECTION
*next_section
;
264 PROFILEKEY
*key
, *next_key
;
266 for ( ; section
; section
= next_section
)
268 for (key
= section
->key
; key
; key
= next_key
)
270 next_key
= key
->next
;
271 HeapFree( GetProcessHeap(), 0, key
->value
);
272 HeapFree( GetProcessHeap(), 0, key
);
274 next_section
= section
->next
;
275 HeapFree( GetProcessHeap(), 0, section
);
279 /* returns 1 if a character white space else 0 */
280 static __inline
int PROFILE_isspaceW(WCHAR c
)
282 /* ^Z (DOS EOF) is a space too (found on CD-ROMs) */
283 return isspace(c
) || c
== 0x1a;
286 static __inline ENCODING
PROFILE_DetectTextEncoding(const void * buffer
, int * len
)
288 INT flags
= IS_TEXT_UNICODE_SIGNATURE
|
289 IS_TEXT_UNICODE_REVERSE_SIGNATURE
|
290 IS_TEXT_UNICODE_ODD_LENGTH
;
291 if (*len
>= sizeof(bom_utf8
) && !memcmp(buffer
, bom_utf8
, sizeof(bom_utf8
)))
293 *len
= sizeof(bom_utf8
);
294 return ENCODING_UTF8
;
296 RtlIsTextUnicode((void *)buffer
, *len
, &flags
);
297 if (flags
& IS_TEXT_UNICODE_SIGNATURE
)
299 *len
= sizeof(WCHAR
);
300 return ENCODING_UTF16LE
;
302 if (flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
304 *len
= sizeof(WCHAR
);
305 return ENCODING_UTF16BE
;
308 return ENCODING_ANSI
;
312 /***********************************************************************
315 * Load a profile tree from a file.
317 static PROFILESECTION
*PROFILE_Load(HANDLE hFile
, ENCODING
* pEncoding
)
319 void *buffer_base
, *pBuffer
;
321 const WCHAR
*szLineStart
, *szLineEnd
;
322 const WCHAR
*szValueStart
, *szEnd
, *next_line
;
324 PROFILESECTION
*section
, *first_section
;
325 PROFILESECTION
**next_section
;
326 PROFILEKEY
*key
, *prev_key
, **next_key
;
329 DPRINT("%p\n", hFile
);
331 dwFileSize
= GetFileSize(hFile
, NULL
);
332 if (dwFileSize
== INVALID_FILE_SIZE
|| dwFileSize
== 0)
335 buffer_base
= HeapAlloc(GetProcessHeap(), 0 , dwFileSize
);
336 if (!buffer_base
) return NULL
;
338 if (!ReadFile(hFile
, buffer_base
, dwFileSize
, &dwFileSize
, NULL
))
340 HeapFree(GetProcessHeap(), 0, buffer_base
);
341 DPRINT("Error %ld reading file\n", GetLastError());
345 *pEncoding
= PROFILE_DetectTextEncoding(buffer_base
, &len
);
346 /* len is set to the number of bytes in the character marker.
347 * we want to skip these bytes */
348 pBuffer
= (char *)buffer_base
+ len
;
353 DPRINT("ANSI encoding\n");
355 len
= MultiByteToWideChar(CP_ACP
, 0, pBuffer
, dwFileSize
, NULL
, 0);
356 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
359 HeapFree(GetProcessHeap(), 0, buffer_base
);
362 MultiByteToWideChar(CP_ACP
, 0, pBuffer
, dwFileSize
, szFile
, len
);
363 szEnd
= szFile
+ len
;
366 DPRINT("UTF8 encoding\n");
368 len
= MultiByteToWideChar(CP_UTF8
, 0, pBuffer
, dwFileSize
, NULL
, 0);
369 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
372 HeapFree(GetProcessHeap(), 0, buffer_base
);
375 MultiByteToWideChar(CP_UTF8
, 0, pBuffer
, dwFileSize
, szFile
, len
);
376 szEnd
= szFile
+ len
;
378 case ENCODING_UTF16LE
:
379 DPRINT("UTF16 Little Endian encoding\n");
381 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
383 case ENCODING_UTF16BE
:
384 DPRINT("UTF16 Big Endian encoding\n");
386 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
387 PROFILE_ByteSwapShortBuffer(szFile
, dwFileSize
/ sizeof(WCHAR
));
390 DPRINT("encoding type %d not implemented\n", *pEncoding
);
391 HeapFree(GetProcessHeap(), 0, buffer_base
);
395 first_section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) );
396 if(first_section
== NULL
)
398 if (szFile
!= pBuffer
)
399 HeapFree(GetProcessHeap(), 0, szFile
);
400 HeapFree(GetProcessHeap(), 0, buffer_base
);
403 first_section
->name
[0] = 0;
404 first_section
->key
= NULL
;
405 first_section
->next
= NULL
;
406 next_section
= &first_section
->next
;
407 next_key
= &first_section
->key
;
411 while (next_line
< szEnd
)
413 szLineStart
= next_line
;
414 next_line
= memchrW(szLineStart
, '\n', szEnd
- szLineStart
);
415 if (!next_line
) next_line
= memchrW(szLineStart
, '\r', szEnd
- szLineStart
);
416 if (!next_line
) next_line
= szEnd
;
418 szLineEnd
= next_line
;
422 /* get rid of white space */
423 while (szLineStart
< szLineEnd
&& PROFILE_isspaceW(*szLineStart
)) szLineStart
++;
424 while ((szLineEnd
> szLineStart
) && PROFILE_isspaceW(szLineEnd
[-1])) szLineEnd
--;
426 if (szLineStart
>= szLineEnd
) continue;
428 if (*szLineStart
== '[') /* section start */
430 const WCHAR
* szSectionEnd
;
431 if (!(szSectionEnd
= memrchrW( szLineStart
, ']', szLineEnd
- szLineStart
)))
433 DPRINT("Invalid section header at line %d: %.*S\n",
434 line
, (int)(szLineEnd
- szLineStart
), szLineStart
);
439 len
= (int)(szSectionEnd
- szLineStart
);
440 /* no need to allocate +1 for NULL terminating character as
441 * already included in structure */
442 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) + len
* sizeof(WCHAR
) )))
444 memcpy(section
->name
, szLineStart
, len
* sizeof(WCHAR
));
445 section
->name
[len
] = '\0';
447 section
->next
= NULL
;
448 *next_section
= section
;
449 next_section
= §ion
->next
;
450 next_key
= §ion
->key
;
453 DPRINT("New section: %S\n", section
->name
);
459 /* get rid of white space after the name and before the start
461 len
= szLineEnd
- szLineStart
;
462 if ((szValueStart
= memchrW( szLineStart
, '=', szLineEnd
- szLineStart
)) != NULL
)
464 const WCHAR
*szNameEnd
= szValueStart
;
465 while ((szNameEnd
> szLineStart
) && PROFILE_isspaceW(szNameEnd
[-1])) szNameEnd
--;
466 len
= szNameEnd
- szLineStart
;
468 while (szValueStart
< szLineEnd
&& PROFILE_isspaceW(*szValueStart
)) szValueStart
++;
471 if (len
|| !prev_key
|| *prev_key
->name
)
473 /* no need to allocate +1 for NULL terminating character as
474 * already included in structure */
475 if (!(key
= HeapAlloc( GetProcessHeap(), 0, sizeof(*key
) + len
* sizeof(WCHAR
) ))) break;
476 memcpy(key
->name
, szLineStart
, len
* sizeof(WCHAR
));
477 key
->name
[len
] = '\0';
480 len
= (int)(szLineEnd
- szValueStart
);
481 key
->value
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
482 if (!key
->value
) break;
483 memcpy(key
->value
, szValueStart
, len
* sizeof(WCHAR
));
484 key
->value
[len
] = '\0';
486 else key
->value
= NULL
;
490 next_key
= &key
->next
;
493 DPRINT("New key: name=%S, value=%S\n",
494 key
->name
, key
->value
?key
->value
: L
"(none)");
497 if (szFile
!= pBuffer
)
498 HeapFree(GetProcessHeap(), 0, szFile
);
499 HeapFree(GetProcessHeap(), 0, buffer_base
);
500 return first_section
;
504 /***********************************************************************
505 * PROFILE_DeleteSection
507 * Delete a section from a profile tree.
509 static BOOL
PROFILE_DeleteSection( PROFILESECTION
**section
, LPCWSTR name
)
513 if ((*section
)->name
[0] && !_wcsicmp( (*section
)->name
, name
))
515 PROFILESECTION
*to_del
= *section
;
516 *section
= to_del
->next
;
518 PROFILE_Free( to_del
);
521 section
= &(*section
)->next
;
527 /***********************************************************************
530 * Delete a key from a profile tree.
532 static BOOL
PROFILE_DeleteKey( PROFILESECTION
**section
,
533 LPCWSTR section_name
, LPCWSTR key_name
)
537 if ((*section
)->name
[0] && !_wcsicmp( (*section
)->name
, section_name
))
539 PROFILEKEY
**key
= &(*section
)->key
;
542 if (!_wcsicmp( (*key
)->name
, key_name
))
544 PROFILEKEY
*to_del
= *key
;
546 HeapFree( GetProcessHeap(), 0, to_del
->value
);
547 HeapFree( GetProcessHeap(), 0, to_del
);
553 section
= &(*section
)->next
;
559 /***********************************************************************
560 * PROFILE_DeleteAllKeys
562 * Delete all keys from a profile tree.
564 static void PROFILE_DeleteAllKeys( LPCWSTR section_name
)
566 PROFILESECTION
**section
= &CurProfile
->section
;
569 if ((*section
)->name
[0] && !_wcsicmp( (*section
)->name
, section_name
))
571 PROFILEKEY
**key
= &(*section
)->key
;
574 PROFILEKEY
*to_del
= *key
;
576 HeapFree( GetProcessHeap(), 0, to_del
->value
);
577 HeapFree( GetProcessHeap(), 0, to_del
);
578 CurProfile
->changed
=TRUE
;
581 section
= &(*section
)->next
;
586 /***********************************************************************
589 * Find a key in a profile tree, optionally creating it.
591 static PROFILEKEY
*PROFILE_Find( PROFILESECTION
**section
, LPCWSTR section_name
,
592 LPCWSTR key_name
, BOOL create
, BOOL create_always
)
597 while (PROFILE_isspaceW(*section_name
)) section_name
++;
599 p
= section_name
+ wcslen(section_name
) - 1;
603 while ((p
> section_name
) && PROFILE_isspaceW(*p
)) p
--;
604 seclen
= p
- section_name
+ 1;
606 while (PROFILE_isspaceW(*key_name
)) key_name
++;
608 p
= key_name
+ wcslen(key_name
) - 1;
612 while ((p
> key_name
) && PROFILE_isspaceW(*p
)) p
--;
613 keylen
= p
- key_name
+ 1;
617 if ( ((*section
)->name
[0])
618 && (!(_wcsnicmp( (*section
)->name
, section_name
, seclen
)))
619 && (((*section
)->name
)[seclen
] == '\0') )
621 PROFILEKEY
**key
= &(*section
)->key
;
625 /* If create_always is FALSE then we check if the keyname
626 * already exists. Otherwise we add it regardless of its
627 * existence, to allow keys to be added more than once in
632 if ( (!(_wcsnicmp( (*key
)->name
, key_name
, keylen
)))
633 && (((*key
)->name
)[keylen
] == '\0') )
640 if (!(*key
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY
) + wcslen(key_name
) * sizeof(WCHAR
) )))
642 wcscpy( (*key
)->name
, key_name
);
643 (*key
)->value
= NULL
;
647 section
= &(*section
)->next
;
649 if (!create
) return NULL
;
650 *section
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION
) + wcslen(section_name
) * sizeof(WCHAR
) );
651 if(*section
== NULL
) return NULL
;
652 wcscpy( (*section
)->name
, section_name
);
653 (*section
)->next
= NULL
;
654 if (!((*section
)->key
= HeapAlloc( GetProcessHeap(), 0,
655 sizeof(PROFILEKEY
) + wcslen(key_name
) * sizeof(WCHAR
) )))
657 HeapFree(GetProcessHeap(), 0, *section
);
660 wcscpy( (*section
)->key
->name
, key_name
);
661 (*section
)->key
->value
= NULL
;
662 (*section
)->key
->next
= NULL
;
663 return (*section
)->key
;
667 /***********************************************************************
670 * Flush the current profile to disk if changed.
672 static BOOL
PROFILE_FlushFile(void)
675 FILETIME LastWriteTime
;
679 DPRINT("No current profile!\n");
683 if (!CurProfile
->changed
) return TRUE
;
685 hFile
= CreateFileW(CurProfile
->filename
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
686 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
688 if (hFile
== INVALID_HANDLE_VALUE
)
690 DPRINT("could not save profile file %S (error was %ld)\n", CurProfile
->filename
, GetLastError());
694 DPRINT("Saving %S\n", CurProfile
->filename
);
695 PROFILE_Save( hFile
, CurProfile
->section
, CurProfile
->encoding
);
696 if(GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
))
697 CurProfile
->LastWriteTime
=LastWriteTime
;
698 CloseHandle( hFile
);
699 CurProfile
->changed
= FALSE
;
704 /***********************************************************************
705 * PROFILE_ReleaseFile
707 * Flush the current profile to disk and remove it from the cache.
709 static void PROFILE_ReleaseFile(void)
712 PROFILE_Free( CurProfile
->section
);
713 HeapFree( GetProcessHeap(), 0, CurProfile
->filename
);
714 CurProfile
->changed
= FALSE
;
715 CurProfile
->section
= NULL
;
716 CurProfile
->filename
= NULL
;
717 CurProfile
->encoding
= ENCODING_ANSI
;
718 ZeroMemory(&CurProfile
->LastWriteTime
, sizeof(CurProfile
->LastWriteTime
));
721 /***********************************************************************
723 * Compares a file time with the current time. If the file time is
724 * at least 2.1 seconds in the past, return true.
726 * Intended as cache safety measure: The time resolution on FAT is
727 * two seconds, so files that are not at least two seconds old might
728 * keep their time even on modification, so don't cache them.
730 static BOOL
is_not_current(FILETIME
* ft
)
733 LONGLONG ftll
, nowll
;
734 GetSystemTimeAsFileTime(&Now
);
735 ftll
= ((LONGLONG
)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
736 nowll
= ((LONGLONG
)Now
.dwHighDateTime
<< 32) + Now
.dwLowDateTime
;
737 DPRINT("%08x;%08x\n",(unsigned)ftll
+21000000,(unsigned)nowll
);
738 return ftll
+ 21000000 < nowll
;
741 /***********************************************************************
744 * Open a profile file, checking the cached file first.
746 static BOOL
PROFILE_Open( LPCWSTR filename
, BOOL write_access
)
748 WCHAR windirW
[MAX_PATH
];
749 WCHAR buffer
[MAX_PATH
];
750 HANDLE hFile
= INVALID_HANDLE_VALUE
;
751 FILETIME LastWriteTime
;
753 PROFILE
*tempProfile
;
755 ZeroMemory(&LastWriteTime
, sizeof(LastWriteTime
));
757 /* First time around */
760 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
762 MRUProfile
[i
]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE
) );
763 if(MRUProfile
[i
] == NULL
) break;
764 MRUProfile
[i
]->changed
=FALSE
;
765 MRUProfile
[i
]->section
=NULL
;
766 MRUProfile
[i
]->filename
=NULL
;
767 MRUProfile
[i
]->encoding
=ENCODING_ANSI
;
768 ZeroMemory(&MRUProfile
[i
]->LastWriteTime
, sizeof(FILETIME
));
771 GetWindowsDirectoryW( windirW
, MAX_PATH
);
776 if ((RtlDetermineDosPathNameType_U(filename
) == RtlPathTypeRelative
) &&
777 !wcschr(filename
, '\\') && !wcschr(filename
, '/'))
779 static const WCHAR wszSeparator
[] = {'\\', 0};
780 wcscpy(buffer
, windirW
);
781 wcscat(buffer
, wszSeparator
);
782 wcscat(buffer
, filename
);
787 GetFullPathNameW(filename
, sizeof(buffer
)/sizeof(buffer
[0]), buffer
, &dummy
);
790 DPRINT("path: %S\n", buffer
);
792 hFile
= CreateFileW(buffer
, GENERIC_READ
| (write_access
? GENERIC_WRITE
: 0),
793 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
,
794 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
796 if ((hFile
== INVALID_HANDLE_VALUE
) && (GetLastError() != ERROR_FILE_NOT_FOUND
))
798 DPRINT("Error %ld opening file %S\n", GetLastError(), buffer
);
802 for(i
= 0; i
< N_CACHED_PROFILES
; i
++)
804 if ((MRUProfile
[i
]->filename
&& !wcscmp( buffer
, MRUProfile
[i
]->filename
)))
806 DPRINT("MRU Filename: %S, new filename: %S\n", MRUProfile
[i
]->filename
, buffer
);
810 tempProfile
=MRUProfile
[i
];
811 for (j
= i
; j
> 0; j
--)
812 MRUProfile
[j
] = MRUProfile
[j
-1];
813 CurProfile
=tempProfile
;
815 if (hFile
!= INVALID_HANDLE_VALUE
)
817 GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
);
818 if (!memcmp( &CurProfile
->LastWriteTime
, &LastWriteTime
, sizeof(FILETIME
) ) &&
819 is_not_current(&LastWriteTime
))
820 DPRINT("(%S): already opened (mru=%d)\n", buffer
, i
);
823 DPRINT("(%S): already opened, needs refreshing (mru=%d)\n", buffer
, i
);
824 PROFILE_Free(CurProfile
->section
);
825 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
826 CurProfile
->LastWriteTime
= LastWriteTime
;
830 else DPRINT("(%S): already opened (mru = %d)\n", buffer
, i
);
836 /* Flush the old current profile */
839 /* Make the oldest profile the current one only in order to get rid of it */
840 if(i
== N_CACHED_PROFILES
)
842 tempProfile
= MRUProfile
[N_CACHED_PROFILES
-1];
843 for (i
= N_CACHED_PROFILES
- 1; i
> 0; i
--)
844 MRUProfile
[i
] = MRUProfile
[i
-1];
845 CurProfile
=tempProfile
;
847 if(CurProfile
->filename
) PROFILE_ReleaseFile();
849 /* OK, now that CurProfile is definitely free we assign it our new file */
850 CurProfile
->filename
= HeapAlloc( GetProcessHeap(), 0, (wcslen(buffer
)+1) * sizeof(WCHAR
) );
851 if(CurProfile
->filename
== NULL
)
853 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
856 wcscpy( CurProfile
->filename
, buffer
);
858 if (hFile
!= INVALID_HANDLE_VALUE
)
860 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
861 GetFileTime(hFile
, NULL
, NULL
, &CurProfile
->LastWriteTime
);
866 /* Does not exist yet, we will create it in PROFILE_FlushFile */
867 DPRINT("profile file %S not found\n", buffer
);
873 /***********************************************************************
876 * Returns all keys of a section.
877 * If return_values is TRUE, also include the corresponding values.
879 static INT
PROFILE_GetSection( PROFILESECTION
*section
, LPCWSTR section_name
,
880 LPWSTR buffer
, UINT len
, BOOL return_values
)
884 if(!buffer
) return 0;
886 DPRINT("%S,%p,%u\n", section_name
, buffer
, len
);
890 if (section
->name
[0] && !_wcsicmp( section
->name
, section_name
))
893 for (key
= section
->key
; key
; key
= key
->next
)
896 if (!*key
->name
) continue; /* Skip empty lines */
897 if (IS_ENTRY_COMMENT(key
->name
)) continue; /* Skip comments */
898 if (!return_values
&& !key
->value
) continue; /* Skip lines w.o. '=' */
899 PROFILE_CopyEntry( buffer
, key
->name
, len
- 1, 0 );
900 len
-= wcslen(buffer
) + 1;
901 buffer
+= wcslen(buffer
) + 1;
904 if (return_values
&& key
->value
)
907 PROFILE_CopyEntry ( buffer
, key
->value
, len
- 1, 0 );
908 len
-= wcslen(buffer
) + 1;
909 buffer
+= wcslen(buffer
) + 1;
915 /*If either lpszSection or lpszKey is NULL and the supplied
916 destination buffer is too small to hold all the strings,
917 the last string is truncated and followed by two null characters.
918 In this case, the return value is equal to cchReturnBuffer
925 section
= section
->next
;
927 buffer
[0] = buffer
[1] = '\0';
931 /* See GetPrivateProfileSectionNamesA for documentation */
932 static INT
PROFILE_GetSectionNames( LPWSTR buffer
, UINT len
)
936 PROFILESECTION
*section
;
938 DPRINT("(%p, %d)\n", buffer
, len
);
949 section
= CurProfile
->section
;
950 while ((section
!=NULL
)) {
951 if (section
->name
[0]) {
952 tmplen
= wcslen(section
->name
)+1;
953 if (tmplen
>= buflen
) {
955 memcpy(buf
, section
->name
, (buflen
- 1) * sizeof(WCHAR
));
962 memcpy(buf
, section
->name
, tmplen
* sizeof(WCHAR
));
966 section
= section
->next
;
973 /***********************************************************************
976 * Get a profile string.
978 * Tests with GetPrivateProfileString16, W95a,
979 * with filled buffer ("****...") and section "set1" and key_name "1" valid:
980 * section key_name def_val res buffer
981 * "set1" "1" "x" 43 [data]
982 * "set1" "1 " "x" 43 [data] (!)
983 * "set1" " 1 "' "x" 43 [data] (!)
984 * "set1" "" "x" 1 "x"
985 * "set1" "" "x " 1 "x" (!)
986 * "set1" "" " x " 3 " x" (!)
987 * "set1" NULL "x" 6 "1\02\03\0\0"
988 * "set1" "" "x" 1 "x"
989 * NULL "1" "x" 0 "" (!)
995 static INT
PROFILE_GetString( LPCWSTR section
, LPCWSTR key_name
,
996 LPCWSTR def_val
, LPWSTR buffer
, UINT len
, BOOL win32
)
998 PROFILEKEY
*key
= NULL
;
999 static const WCHAR empty_strW
[] = { 0 };
1001 if (!buffer
|| !len
) return 0;
1003 if (!def_val
) def_val
= empty_strW
;
1008 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
1009 return wcslen(buffer
);
1011 key
= PROFILE_Find( &CurProfile
->section
, section
, key_name
, FALSE
, FALSE
);
1012 PROFILE_CopyEntry( buffer
, (key
&& key
->value
) ? key
->value
: def_val
,
1014 DPRINT("(%S, %S, %S): returning %S\n",
1017 return wcslen(buffer
);
1020 /* no "else" here ! */
1021 if (section
&& section
[0])
1023 INT ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, FALSE
);
1024 if (!buffer
[0]) /* no luck -> def_val */
1026 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
1027 ret
= wcslen(buffer
);
1036 /***********************************************************************
1039 * Set a profile string.
1041 static BOOL
PROFILE_SetString( LPCWSTR section_name
, LPCWSTR key_name
,
1042 LPCWSTR value
, BOOL create_always
)
1044 if (!key_name
) /* Delete a whole section */
1046 DPRINT("(%S)\n", section_name
);
1047 CurProfile
->changed
|= PROFILE_DeleteSection( &CurProfile
->section
,
1049 return TRUE
; /* Even if PROFILE_DeleteSection() has failed,
1050 this is not an error on application's level.*/
1052 else if (!value
) /* Delete a key */
1054 DPRINT("(%S, %S)\n", section_name
, key_name
);
1055 CurProfile
->changed
|= PROFILE_DeleteKey( &CurProfile
->section
,
1056 section_name
, key_name
);
1057 return TRUE
; /* same error handling as above */
1059 else /* Set the key value */
1061 PROFILEKEY
*key
= PROFILE_Find(&CurProfile
->section
, section_name
,
1062 key_name
, TRUE
, create_always
);
1063 DPRINT("(%S, %S, %S):\n",
1064 section_name
, key_name
, value
);
1065 if (!key
) return FALSE
;
1067 /* strip the leading spaces. We can safely strip \n\r and
1068 * friends too, they should not happen here anyway. */
1069 while (PROFILE_isspaceW(*value
)) value
++;
1073 if (!wcscmp( key
->value
, value
))
1075 DPRINT(" no change needed\n" );
1076 return TRUE
; /* No change needed */
1078 DPRINT(" replacing %S\n", key
->value
);
1079 HeapFree( GetProcessHeap(), 0, key
->value
);
1083 DPRINT(" creating key\n");
1085 key
->value
= HeapAlloc( GetProcessHeap(), 0, (wcslen(value
) + 1) * sizeof(WCHAR
) );
1086 if(key
->value
== NULL
)
1088 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1091 wcscpy( key
->value
, value
);
1092 CurProfile
->changed
= TRUE
;
1098 /********************* API functions **********************************/
1101 /***********************************************************************
1102 * GetProfileIntA (KERNEL32.@)
1104 UINT WINAPI
GetProfileIntA( LPCSTR section
, LPCSTR entry
, INT def_val
)
1106 return GetPrivateProfileIntA( section
, entry
, def_val
, "win.ini" );
1109 /***********************************************************************
1110 * GetProfileIntW (KERNEL32.@)
1112 UINT WINAPI
GetProfileIntW( LPCWSTR section
, LPCWSTR entry
, INT def_val
)
1114 return GetPrivateProfileIntW( section
, entry
, def_val
, L
"win.ini" );
1119 * - Section names if 'section' is NULL
1120 * - Keys in a Section if 'entry' is NULL
1121 * (see MSDN doc for GetPrivateProfileString)
1123 static int PROFILE_GetPrivateProfileString( LPCWSTR section
, LPCWSTR entry
,
1124 LPCWSTR def_val
, LPWSTR buffer
,
1125 UINT len
, LPCWSTR filename
,
1129 LPWSTR defval_tmp
= NULL
;
1131 DPRINT("%S, %S, %S, %p, %u, %S\n",
1132 section
, entry
, def_val
, buffer
, len
, filename
);
1134 /* strip any trailing ' ' of def_val. */
1137 LPCWSTR p
= def_val
+ wcslen(def_val
) - 1;
1139 while (p
> def_val
&& *p
== ' ')
1144 int len
= (int)(p
- def_val
) + 1;
1146 defval_tmp
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1147 if (!defval_tmp
) return 0;
1148 memcpy(defval_tmp
, def_val
, len
* sizeof(WCHAR
));
1149 defval_tmp
[len
] = '\0';
1150 def_val
= defval_tmp
;
1154 RtlEnterCriticalSection( &PROFILE_CritSect
);
1156 if (PROFILE_Open( filename
, FALSE
)) {
1157 if (win32
&& (section
== NULL
))
1158 ret
= PROFILE_GetSectionNames(buffer
, len
);
1160 /* PROFILE_GetString can handle the 'entry == NULL' case */
1161 ret
= PROFILE_GetString( section
, entry
, def_val
, buffer
, len
, win32
);
1162 } else if (buffer
&& def_val
) {
1163 lstrcpynW( buffer
, def_val
, len
);
1164 ret
= wcslen( buffer
);
1169 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1171 HeapFree(GetProcessHeap(), 0, defval_tmp
);
1173 DPRINT("returning %S, %d\n", buffer
, ret
);
1179 /***********************************************************************
1180 * GetPrivateProfileStringA (KERNEL32.@)
1182 DWORD WINAPI
GetPrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1183 LPCSTR def_val
, LPSTR buffer
,
1184 DWORD len
, LPCSTR filename
)
1186 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1190 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1191 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1192 else sectionW
.Buffer
= NULL
;
1193 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1194 else entryW
.Buffer
= NULL
;
1195 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1196 else def_valW
.Buffer
= NULL
;
1197 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1198 else filenameW
.Buffer
= NULL
;
1200 retW
= GetPrivateProfileStringW( sectionW
.Buffer
, entryW
.Buffer
,
1201 def_valW
.Buffer
, bufferW
, len
,
1207 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
, buffer
, len
- 1, NULL
, NULL
);
1214 RtlFreeUnicodeString(§ionW
);
1215 RtlFreeUnicodeString(&entryW
);
1216 RtlFreeUnicodeString(&def_valW
);
1217 RtlFreeUnicodeString(&filenameW
);
1218 HeapFree(GetProcessHeap(), 0, bufferW
);
1222 /***********************************************************************
1223 * GetPrivateProfileStringW (KERNEL32.@)
1225 DWORD WINAPI
GetPrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1226 LPCWSTR def_val
, LPWSTR buffer
,
1227 DWORD len
, LPCWSTR filename
)
1229 DPRINT("(%S, %S, %S, %p, %d, %S)\n",
1230 section
, entry
, def_val
, buffer
, len
, filename
);
1232 return PROFILE_GetPrivateProfileString( section
, entry
, def_val
,
1233 buffer
, len
, filename
, TRUE
);
1237 /***********************************************************************
1238 * GetProfileStringA (KERNEL32.@)
1240 DWORD WINAPI
GetProfileStringA( LPCSTR section
, LPCSTR entry
, LPCSTR def_val
,
1241 LPSTR buffer
, DWORD len
)
1243 return GetPrivateProfileStringA( section
, entry
, def_val
,
1244 buffer
, len
, "win.ini" );
1248 /***********************************************************************
1249 * GetProfileStringW (KERNEL32.@)
1251 DWORD WINAPI
GetProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1252 LPCWSTR def_val
, LPWSTR buffer
, DWORD len
)
1254 return GetPrivateProfileStringW( section
, entry
, def_val
,
1255 buffer
, len
, L
"win.ini" );
1258 /***********************************************************************
1259 * WriteProfileStringA (KERNEL32.@)
1261 BOOL WINAPI
WriteProfileStringA( LPCSTR section
, LPCSTR entry
,
1264 return WritePrivateProfileStringA( section
, entry
, string
, "win.ini" );
1267 /***********************************************************************
1268 * WriteProfileStringW (KERNEL32.@)
1270 BOOL WINAPI
WriteProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1273 return WritePrivateProfileStringW( section
, entry
, string
, L
"win.ini" );
1277 /***********************************************************************
1278 * GetPrivateProfileIntW (KERNEL32.@)
1280 UINT WINAPI
GetPrivateProfileIntW( LPCWSTR section
, LPCWSTR entry
,
1281 INT def_val
, LPCWSTR filename
)
1284 UNICODE_STRING bufferW
;
1288 if (!(len
= GetPrivateProfileStringW( section
, entry
, emptystringW
,
1289 buffer
, sizeof(buffer
)/sizeof(WCHAR
),
1293 /* FIXME: if entry can be found but it's empty, then Win16 is
1294 * supposed to return 0 instead of def_val ! Difficult/problematic
1295 * to implement (every other failure also returns zero buffer),
1296 * thus wait until testing framework avail for making sure nothing
1297 * else gets broken that way. */
1298 if (!buffer
[0]) return (UINT
)def_val
;
1300 RtlInitUnicodeString( &bufferW
, buffer
);
1301 RtlUnicodeStringToInteger( &bufferW
, 0, &result
);
1305 /***********************************************************************
1306 * GetPrivateProfileIntA (KERNEL32.@)
1308 * FIXME: rewrite using unicode
1310 UINT WINAPI
GetPrivateProfileIntA( LPCSTR section
, LPCSTR entry
,
1311 INT def_val
, LPCSTR filename
)
1313 UNICODE_STRING entryW
, filenameW
, sectionW
;
1315 if(entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1316 else entryW
.Buffer
= NULL
;
1317 if(filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1318 else filenameW
.Buffer
= NULL
;
1319 if(section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1320 else sectionW
.Buffer
= NULL
;
1321 res
= GetPrivateProfileIntW(sectionW
.Buffer
, entryW
.Buffer
, def_val
,
1323 RtlFreeUnicodeString(§ionW
);
1324 RtlFreeUnicodeString(&filenameW
);
1325 RtlFreeUnicodeString(&entryW
);
1329 /***********************************************************************
1330 * GetPrivateProfileSectionW (KERNEL32.@)
1332 DWORD WINAPI
GetPrivateProfileSectionW( LPCWSTR section
, LPWSTR buffer
,
1333 DWORD len
, LPCWSTR filename
)
1337 if (!section
|| !buffer
)
1339 SetLastError(ERROR_INVALID_PARAMETER
);
1343 DPRINT("(%S, %p, %ld, %S)\n", section
, buffer
, len
, filename
);
1345 RtlEnterCriticalSection( &PROFILE_CritSect
);
1347 if (PROFILE_Open( filename
, FALSE
))
1348 ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, TRUE
);
1350 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1355 /***********************************************************************
1356 * GetPrivateProfileSectionA (KERNEL32.@)
1358 DWORD WINAPI
GetPrivateProfileSectionA( LPCSTR section
, LPSTR buffer
,
1359 DWORD len
, LPCSTR filename
)
1361 UNICODE_STRING sectionW
, filenameW
;
1365 if (!section
|| !buffer
)
1367 SetLastError(ERROR_INVALID_PARAMETER
);
1371 bufferW
= HeapAlloc(GetProcessHeap(), 0, len
* 2 * sizeof(WCHAR
));
1374 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1378 RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1379 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1380 else filenameW
.Buffer
= NULL
;
1382 retW
= GetPrivateProfileSectionW(sectionW
.Buffer
, bufferW
, len
* 2, filenameW
.Buffer
);
1385 if (retW
== len
* 2 - 2) retW
++; /* overflow */
1386 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1387 if (!ret
|| ret
== len
) /* overflow */
1401 RtlFreeUnicodeString(§ionW
);
1402 RtlFreeUnicodeString(&filenameW
);
1403 HeapFree(GetProcessHeap(), 0, bufferW
);
1407 /***********************************************************************
1408 * GetProfileSectionA (KERNEL32.@)
1410 DWORD WINAPI
GetProfileSectionA( LPCSTR section
, LPSTR buffer
, DWORD len
)
1412 return GetPrivateProfileSectionA( section
, buffer
, len
, "win.ini" );
1415 /***********************************************************************
1416 * GetProfileSectionW (KERNEL32.@)
1418 DWORD WINAPI
GetProfileSectionW( LPCWSTR section
, LPWSTR buffer
, DWORD len
)
1420 return GetPrivateProfileSectionW( section
, buffer
, len
, L
"win.ini" );
1424 /***********************************************************************
1425 * WritePrivateProfileStringW (KERNEL32.@)
1427 BOOL WINAPI
WritePrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1428 LPCWSTR string
, LPCWSTR filename
)
1432 RtlEnterCriticalSection( &PROFILE_CritSect
);
1434 if (!section
&& !entry
&& !string
) /* documented "file flush" case */
1436 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1438 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1441 else if (PROFILE_Open( filename
, TRUE
))
1444 SetLastError(ERROR_FILE_NOT_FOUND
);
1446 ret
= PROFILE_SetString( section
, entry
, string
, FALSE
);
1447 PROFILE_FlushFile();
1451 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1455 /***********************************************************************
1456 * WritePrivateProfileStringA (KERNEL32.@)
1458 BOOL WINAPI
WritePrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1459 LPCSTR string
, LPCSTR filename
)
1461 UNICODE_STRING sectionW
, entryW
, stringW
, filenameW
;
1464 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1465 else sectionW
.Buffer
= NULL
;
1466 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1467 else entryW
.Buffer
= NULL
;
1468 if (string
) RtlCreateUnicodeStringFromAsciiz(&stringW
, string
);
1469 else stringW
.Buffer
= NULL
;
1470 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1471 else filenameW
.Buffer
= NULL
;
1473 ret
= WritePrivateProfileStringW(sectionW
.Buffer
, entryW
.Buffer
,
1474 stringW
.Buffer
, filenameW
.Buffer
);
1475 RtlFreeUnicodeString(§ionW
);
1476 RtlFreeUnicodeString(&entryW
);
1477 RtlFreeUnicodeString(&stringW
);
1478 RtlFreeUnicodeString(&filenameW
);
1482 /***********************************************************************
1483 * WritePrivateProfileSectionW (KERNEL32.@)
1485 BOOL WINAPI
WritePrivateProfileSectionW( LPCWSTR section
,
1486 LPCWSTR string
, LPCWSTR filename
)
1491 RtlEnterCriticalSection( &PROFILE_CritSect
);
1493 if (!section
&& !string
)
1495 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1497 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1500 else if (PROFILE_Open( filename
, TRUE
)) {
1501 if (!string
) {/* delete the named section*/
1502 ret
= PROFILE_SetString(section
,NULL
,NULL
, FALSE
);
1503 PROFILE_FlushFile();
1505 PROFILE_DeleteAllKeys(section
);
1508 LPWSTR buf
= HeapAlloc( GetProcessHeap(), 0, (wcslen(string
)+1) * sizeof(WCHAR
) );
1511 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1512 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1515 wcscpy( buf
, string
);
1516 if((p
= wcschr( buf
, '='))) {
1518 ret
= PROFILE_SetString( section
, buf
, p
+ 1, TRUE
);
1520 HeapFree( GetProcessHeap(), 0, buf
);
1521 string
+= wcslen(string
) + 1;
1523 PROFILE_FlushFile();
1527 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1531 /***********************************************************************
1532 * WritePrivateProfileSectionA (KERNEL32.@)
1534 BOOL WINAPI
WritePrivateProfileSectionA( LPCSTR section
,
1535 LPCSTR string
, LPCSTR filename
)
1538 UNICODE_STRING sectionW
, filenameW
;
1547 while(*p
) p
+= strlen(p
) + 1;
1548 lenA
= p
- string
+ 1;
1549 lenW
= MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, NULL
, 0);
1550 if ((stringW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
))))
1551 MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, stringW
, lenW
);
1553 else stringW
= NULL
;
1554 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1555 else sectionW
.Buffer
= NULL
;
1556 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1557 else filenameW
.Buffer
= NULL
;
1559 ret
= WritePrivateProfileSectionW(sectionW
.Buffer
, stringW
, filenameW
.Buffer
);
1561 HeapFree(GetProcessHeap(), 0, stringW
);
1562 RtlFreeUnicodeString(§ionW
);
1563 RtlFreeUnicodeString(&filenameW
);
1567 /***********************************************************************
1568 * WriteProfileSectionA (KERNEL32.@)
1570 BOOL WINAPI
WriteProfileSectionA( LPCSTR section
, LPCSTR keys_n_values
)
1573 return WritePrivateProfileSectionA(section
, keys_n_values
, "win.ini");
1576 /***********************************************************************
1577 * WriteProfileSectionW (KERNEL32.@)
1579 BOOL WINAPI
WriteProfileSectionW( LPCWSTR section
, LPCWSTR keys_n_values
)
1581 return WritePrivateProfileSectionW(section
, keys_n_values
, wininiW
);
1585 /***********************************************************************
1586 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1588 * Returns the section names contained in the specified file.
1589 * FIXME: Where do we find this file when the path is relative?
1590 * The section names are returned as a list of strings with an extra
1591 * '\0' to mark the end of the list. Except for that the behavior
1592 * depends on the Windows version.
1595 * - if the buffer is 0 or 1 character long then it is as if it was of
1597 * - otherwise, if the buffer is too small only the section names that fit
1599 * - note that this means if the buffer was too small to return even just
1600 * the first section name then a single '\0' will be returned.
1601 * - the return value is the number of characters written in the buffer,
1602 * except if the buffer was too small in which case len-2 is returned
1605 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1606 * '\0' and the return value is 0
1607 * - otherwise if the buffer is too small then the first section name that
1608 * does not fit is truncated so that the string list can be terminated
1609 * correctly (double '\0')
1610 * - the return value is the number of characters written in the buffer
1611 * except for the trailing '\0'. If the buffer is too small, then the
1612 * return value is len-2
1613 * - Win2000 has a bug that triggers when the section names and the
1614 * trailing '\0' fit exactly in the buffer. In that case the trailing
1617 * Wine implements the observed Win2000 behavior (except for the bug).
1619 * Note that when the buffer is big enough then the return value may be any
1620 * value between 1 and len-1 (or len in Win95), including len-2.
1622 DWORD WINAPI
GetPrivateProfileSectionNamesW( LPWSTR buffer
, DWORD size
,
1627 RtlEnterCriticalSection( &PROFILE_CritSect
);
1629 if (PROFILE_Open( filename
, FALSE
))
1630 ret
= PROFILE_GetSectionNames(buffer
, size
);
1632 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1638 /***********************************************************************
1639 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1641 DWORD WINAPI
GetPrivateProfileSectionNamesA( LPSTR buffer
, DWORD size
,
1644 UNICODE_STRING filenameW
;
1648 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)) : NULL
;
1649 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1650 else filenameW
.Buffer
= NULL
;
1652 retW
= GetPrivateProfileSectionNamesW(bufferW
, size
, filenameW
.Buffer
);
1655 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+1, buffer
, size
-1, NULL
, NULL
);
1667 RtlFreeUnicodeString(&filenameW
);
1668 HeapFree(GetProcessHeap(), 0, bufferW
);
1672 /***********************************************************************
1673 * GetPrivateProfileStructW (KERNEL32.@)
1675 * Should match Win95's behaviour pretty much
1677 BOOL WINAPI
GetPrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1678 LPVOID buf
, UINT len
, LPCWSTR filename
)
1682 RtlEnterCriticalSection( &PROFILE_CritSect
);
1684 if (PROFILE_Open( filename
, FALSE
))
1686 PROFILEKEY
*k
= PROFILE_Find ( &CurProfile
->section
, section
, key
, FALSE
, FALSE
);
1689 DPRINT("value (at %p): %S\n", k
->value
, k
->value
);
1690 if (((wcslen(k
->value
) - 2) / 2) == len
)
1697 end
= k
->value
+ wcslen(k
->value
); /* -> '\0' */
1699 /* check for invalid chars in ASCII coded hex string */
1700 for (p
= k
->value
; p
< end
; p
++)
1704 DPRINT("invalid char '%x' in file %S->[%S]->%S !\n",
1705 *p
, filename
, section
, key
);
1713 BOOL highnibble
= TRUE
;
1715 LPBYTE binbuf
= buf
;
1717 end
-= 2; /* don't include checksum in output data */
1718 /* translate ASCII hex format into binary data */
1719 for (p
= k
->value
; p
< end
; p
++)
1722 val
= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1729 *binbuf
++ = b
; /* feed binary data into output */
1730 chksum
+= b
; /* calculate checksum */
1732 highnibble
^= 1; /* toggle */
1735 /* retrieve stored checksum value */
1737 b
= ( (c
> '9') ? (c
- 'A' + 10) : (c
- '0') ) << 4;
1739 b
+= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1740 if (b
== (chksum
& 0xff)) /* checksums match ? */
1746 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1751 /***********************************************************************
1752 * GetPrivateProfileStructA (KERNEL32.@)
1754 BOOL WINAPI
GetPrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1755 LPVOID buffer
, UINT len
, LPCSTR filename
)
1757 UNICODE_STRING sectionW
, keyW
, filenameW
;
1760 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1761 else sectionW
.Buffer
= NULL
;
1762 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1763 else keyW
.Buffer
= NULL
;
1764 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1765 else filenameW
.Buffer
= NULL
;
1767 ret
= GetPrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buffer
, len
,
1769 /* Do not translate binary data. */
1771 RtlFreeUnicodeString(§ionW
);
1772 RtlFreeUnicodeString(&keyW
);
1773 RtlFreeUnicodeString(&filenameW
);
1779 /***********************************************************************
1780 * WritePrivateProfileStructW (KERNEL32.@)
1782 BOOL WINAPI
WritePrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1783 LPVOID buf
, UINT bufsize
, LPCWSTR filename
)
1787 LPWSTR outstring
, p
;
1790 if (!section
&& !key
&& !buf
) /* flush the cache */
1791 return WritePrivateProfileStringW( NULL
, NULL
, NULL
, filename
);
1793 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1794 outstring
= HeapAlloc( GetProcessHeap(), 0, (bufsize
*2 + 2 + 1) * sizeof(WCHAR
) );
1795 if(outstring
== NULL
)
1797 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1801 for (binbuf
= (LPBYTE
)buf
; binbuf
< (LPBYTE
)buf
+bufsize
; binbuf
++)
1803 *p
++ = hex
[*binbuf
>> 4];
1804 *p
++ = hex
[*binbuf
& 0xf];
1807 /* checksum is sum & 0xff */
1808 *p
++ = hex
[(sum
& 0xf0) >> 4];
1809 *p
++ = hex
[sum
& 0xf];
1812 RtlEnterCriticalSection( &PROFILE_CritSect
);
1814 if (PROFILE_Open( filename
, TRUE
))
1816 ret
= PROFILE_SetString( section
, key
, outstring
, FALSE
);
1817 PROFILE_FlushFile();
1820 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1822 HeapFree( GetProcessHeap(), 0, outstring
);
1827 /***********************************************************************
1828 * WritePrivateProfileStructA (KERNEL32.@)
1831 WritePrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1832 LPVOID buf
, UINT bufsize
, LPCSTR filename
)
1834 UNICODE_STRING sectionW
, keyW
, filenameW
;
1837 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1838 else sectionW
.Buffer
= NULL
;
1839 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1840 else keyW
.Buffer
= NULL
;
1841 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1842 else filenameW
.Buffer
= NULL
;
1844 /* Do not translate binary data. */
1845 ret
= WritePrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buf
, bufsize
,
1848 RtlFreeUnicodeString(§ionW
);
1849 RtlFreeUnicodeString(&keyW
);
1850 RtlFreeUnicodeString(&filenameW
);
1855 /***********************************************************************
1856 * CloseProfileUserMapping
1859 CloseProfileUserMapping(VOID
)
1861 DPRINT1("(), stub!\n");
1862 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1871 QueryWin31IniFilesMappedToRegistry(DWORD Unknown0
,
1876 DPRINT1("QueryWin31IniFilesMappedToRegistry not implemented\n");
1877 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);