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
)
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" );
1117 /***********************************************************************
1118 * GetPrivateProfileStringW (KERNEL32.@)
1120 DWORD WINAPI
GetPrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1121 LPCWSTR def_val
, LPWSTR buffer
,
1122 DWORD len
, LPCWSTR filename
)
1125 LPWSTR defval_tmp
= NULL
;
1127 DPRINT("%S, %S, %S, %p, %u, %S\n",
1128 section
, entry
, def_val
, buffer
, len
, filename
);
1130 /* strip any trailing ' ' of def_val. */
1133 LPCWSTR p
= def_val
+ wcslen(def_val
) - 1;
1135 while (p
> def_val
&& *p
== ' ')
1140 int len
= (int)(p
- def_val
) + 1;
1142 defval_tmp
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1143 if (!defval_tmp
) return 0;
1144 memcpy(defval_tmp
, def_val
, len
* sizeof(WCHAR
));
1145 defval_tmp
[len
] = '\0';
1146 def_val
= defval_tmp
;
1150 RtlEnterCriticalSection( &PROFILE_CritSect
);
1152 if (PROFILE_Open( filename
, FALSE
)) {
1153 if (section
== NULL
)
1154 ret
= PROFILE_GetSectionNames(buffer
, len
);
1156 /* PROFILE_GetString can handle the 'entry == NULL' case */
1157 ret
= PROFILE_GetString( section
, entry
, def_val
, buffer
, len
);
1158 } else if (buffer
&& def_val
) {
1159 lstrcpynW( buffer
, def_val
, len
);
1160 ret
= wcslen( buffer
);
1165 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1167 HeapFree(GetProcessHeap(), 0, defval_tmp
);
1169 DPRINT("returning %S, %d\n", buffer
, ret
);
1174 /***********************************************************************
1175 * GetPrivateProfileStringA (KERNEL32.@)
1177 DWORD WINAPI
GetPrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1178 LPCSTR def_val
, LPSTR buffer
,
1179 DWORD len
, LPCSTR filename
)
1181 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1185 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1186 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1187 else sectionW
.Buffer
= NULL
;
1188 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1189 else entryW
.Buffer
= NULL
;
1190 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1191 else def_valW
.Buffer
= NULL
;
1192 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1193 else filenameW
.Buffer
= NULL
;
1195 retW
= GetPrivateProfileStringW( sectionW
.Buffer
, entryW
.Buffer
,
1196 def_valW
.Buffer
, bufferW
, len
,
1202 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
, buffer
, len
- 1, NULL
, NULL
);
1209 RtlFreeUnicodeString(§ionW
);
1210 RtlFreeUnicodeString(&entryW
);
1211 RtlFreeUnicodeString(&def_valW
);
1212 RtlFreeUnicodeString(&filenameW
);
1213 HeapFree(GetProcessHeap(), 0, bufferW
);
1217 /***********************************************************************
1218 * GetProfileStringA (KERNEL32.@)
1220 DWORD WINAPI
GetProfileStringA( LPCSTR section
, LPCSTR entry
, LPCSTR def_val
,
1221 LPSTR buffer
, DWORD len
)
1223 return GetPrivateProfileStringA( section
, entry
, def_val
,
1224 buffer
, len
, "win.ini" );
1227 /***********************************************************************
1228 * GetProfileStringW (KERNEL32.@)
1230 DWORD WINAPI
GetProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1231 LPCWSTR def_val
, LPWSTR buffer
, DWORD len
)
1233 return GetPrivateProfileStringW( section
, entry
, def_val
,
1234 buffer
, len
, L
"win.ini" );
1237 /***********************************************************************
1238 * WriteProfileStringA (KERNEL32.@)
1240 BOOL WINAPI
WriteProfileStringA( LPCSTR section
, LPCSTR entry
,
1243 return WritePrivateProfileStringA( section
, entry
, string
, "win.ini" );
1246 /***********************************************************************
1247 * WriteProfileStringW (KERNEL32.@)
1249 BOOL WINAPI
WriteProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1252 return WritePrivateProfileStringW( section
, entry
, string
, L
"win.ini" );
1256 /***********************************************************************
1257 * GetPrivateProfileIntW (KERNEL32.@)
1259 UINT WINAPI
GetPrivateProfileIntW( LPCWSTR section
, LPCWSTR entry
,
1260 INT def_val
, LPCWSTR filename
)
1263 UNICODE_STRING bufferW
;
1267 if (!(len
= GetPrivateProfileStringW( section
, entry
, emptystringW
,
1268 buffer
, sizeof(buffer
)/sizeof(WCHAR
),
1272 /* FIXME: if entry can be found but it's empty, then Win16 is
1273 * supposed to return 0 instead of def_val ! Difficult/problematic
1274 * to implement (every other failure also returns zero buffer),
1275 * thus wait until testing framework avail for making sure nothing
1276 * else gets broken that way. */
1277 if (!buffer
[0]) return (UINT
)def_val
;
1279 RtlInitUnicodeString( &bufferW
, buffer
);
1280 RtlUnicodeStringToInteger( &bufferW
, 0, &result
);
1284 /***********************************************************************
1285 * GetPrivateProfileIntA (KERNEL32.@)
1287 * FIXME: rewrite using unicode
1289 UINT WINAPI
GetPrivateProfileIntA( LPCSTR section
, LPCSTR entry
,
1290 INT def_val
, LPCSTR filename
)
1292 UNICODE_STRING entryW
, filenameW
, sectionW
;
1294 if(entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1295 else entryW
.Buffer
= NULL
;
1296 if(filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1297 else filenameW
.Buffer
= NULL
;
1298 if(section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1299 else sectionW
.Buffer
= NULL
;
1300 res
= GetPrivateProfileIntW(sectionW
.Buffer
, entryW
.Buffer
, def_val
,
1302 RtlFreeUnicodeString(§ionW
);
1303 RtlFreeUnicodeString(&filenameW
);
1304 RtlFreeUnicodeString(&entryW
);
1308 /***********************************************************************
1309 * GetPrivateProfileSectionW (KERNEL32.@)
1311 DWORD WINAPI
GetPrivateProfileSectionW( LPCWSTR section
, LPWSTR buffer
,
1312 DWORD len
, LPCWSTR filename
)
1316 if (!section
|| !buffer
)
1318 SetLastError(ERROR_INVALID_PARAMETER
);
1322 DPRINT("(%S, %p, %ld, %S)\n", section
, buffer
, len
, filename
);
1324 RtlEnterCriticalSection( &PROFILE_CritSect
);
1326 if (PROFILE_Open( filename
, FALSE
))
1327 ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, TRUE
);
1329 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1334 /***********************************************************************
1335 * GetPrivateProfileSectionA (KERNEL32.@)
1337 DWORD WINAPI
GetPrivateProfileSectionA( LPCSTR section
, LPSTR buffer
,
1338 DWORD len
, LPCSTR filename
)
1340 UNICODE_STRING sectionW
, filenameW
;
1344 if (!section
|| !buffer
)
1346 SetLastError(ERROR_INVALID_PARAMETER
);
1350 bufferW
= HeapAlloc(GetProcessHeap(), 0, len
* 2 * sizeof(WCHAR
));
1353 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1357 RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1358 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1359 else filenameW
.Buffer
= NULL
;
1361 retW
= GetPrivateProfileSectionW(sectionW
.Buffer
, bufferW
, len
* 2, filenameW
.Buffer
);
1364 if (retW
== len
* 2 - 2) retW
++; /* overflow */
1365 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1366 if (!ret
|| ret
== len
) /* overflow */
1380 RtlFreeUnicodeString(§ionW
);
1381 RtlFreeUnicodeString(&filenameW
);
1382 HeapFree(GetProcessHeap(), 0, bufferW
);
1386 /***********************************************************************
1387 * GetProfileSectionA (KERNEL32.@)
1389 DWORD WINAPI
GetProfileSectionA( LPCSTR section
, LPSTR buffer
, DWORD len
)
1391 return GetPrivateProfileSectionA( section
, buffer
, len
, "win.ini" );
1394 /***********************************************************************
1395 * GetProfileSectionW (KERNEL32.@)
1397 DWORD WINAPI
GetProfileSectionW( LPCWSTR section
, LPWSTR buffer
, DWORD len
)
1399 return GetPrivateProfileSectionW( section
, buffer
, len
, L
"win.ini" );
1403 /***********************************************************************
1404 * WritePrivateProfileStringW (KERNEL32.@)
1406 BOOL WINAPI
WritePrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1407 LPCWSTR string
, LPCWSTR filename
)
1411 RtlEnterCriticalSection( &PROFILE_CritSect
);
1413 if (!section
&& !entry
&& !string
) /* documented "file flush" case */
1415 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1417 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1420 else if (PROFILE_Open( filename
, TRUE
))
1423 SetLastError(ERROR_FILE_NOT_FOUND
);
1425 ret
= PROFILE_SetString( section
, entry
, string
, FALSE
);
1426 PROFILE_FlushFile();
1430 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1434 /***********************************************************************
1435 * WritePrivateProfileStringA (KERNEL32.@)
1437 BOOL WINAPI
WritePrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1438 LPCSTR string
, LPCSTR filename
)
1440 UNICODE_STRING sectionW
, entryW
, stringW
, filenameW
;
1443 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1444 else sectionW
.Buffer
= NULL
;
1445 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1446 else entryW
.Buffer
= NULL
;
1447 if (string
) RtlCreateUnicodeStringFromAsciiz(&stringW
, string
);
1448 else stringW
.Buffer
= NULL
;
1449 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1450 else filenameW
.Buffer
= NULL
;
1452 ret
= WritePrivateProfileStringW(sectionW
.Buffer
, entryW
.Buffer
,
1453 stringW
.Buffer
, filenameW
.Buffer
);
1454 RtlFreeUnicodeString(§ionW
);
1455 RtlFreeUnicodeString(&entryW
);
1456 RtlFreeUnicodeString(&stringW
);
1457 RtlFreeUnicodeString(&filenameW
);
1461 /***********************************************************************
1462 * WritePrivateProfileSectionW (KERNEL32.@)
1464 BOOL WINAPI
WritePrivateProfileSectionW( LPCWSTR section
,
1465 LPCWSTR string
, LPCWSTR filename
)
1470 RtlEnterCriticalSection( &PROFILE_CritSect
);
1472 if (!section
&& !string
)
1474 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1476 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1479 else if (PROFILE_Open( filename
, TRUE
)) {
1480 if (!string
) {/* delete the named section*/
1481 ret
= PROFILE_SetString(section
,NULL
,NULL
, FALSE
);
1482 PROFILE_FlushFile();
1484 PROFILE_DeleteAllKeys(section
);
1487 LPWSTR buf
= HeapAlloc( GetProcessHeap(), 0, (wcslen(string
)+1) * sizeof(WCHAR
) );
1490 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1491 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1494 wcscpy( buf
, string
);
1495 if((p
= wcschr( buf
, '='))) {
1497 ret
= PROFILE_SetString( section
, buf
, p
+ 1, TRUE
);
1499 HeapFree( GetProcessHeap(), 0, buf
);
1500 string
+= wcslen(string
) + 1;
1502 PROFILE_FlushFile();
1506 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1510 /***********************************************************************
1511 * WritePrivateProfileSectionA (KERNEL32.@)
1513 BOOL WINAPI
WritePrivateProfileSectionA( LPCSTR section
,
1514 LPCSTR string
, LPCSTR filename
)
1517 UNICODE_STRING sectionW
, filenameW
;
1526 while(*p
) p
+= strlen(p
) + 1;
1527 lenA
= p
- string
+ 1;
1528 lenW
= MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, NULL
, 0);
1529 if ((stringW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
))))
1530 MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, stringW
, lenW
);
1532 else stringW
= NULL
;
1533 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1534 else sectionW
.Buffer
= NULL
;
1535 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1536 else filenameW
.Buffer
= NULL
;
1538 ret
= WritePrivateProfileSectionW(sectionW
.Buffer
, stringW
, filenameW
.Buffer
);
1540 HeapFree(GetProcessHeap(), 0, stringW
);
1541 RtlFreeUnicodeString(§ionW
);
1542 RtlFreeUnicodeString(&filenameW
);
1546 /***********************************************************************
1547 * WriteProfileSectionA (KERNEL32.@)
1549 BOOL WINAPI
WriteProfileSectionA( LPCSTR section
, LPCSTR keys_n_values
)
1552 return WritePrivateProfileSectionA(section
, keys_n_values
, "win.ini");
1555 /***********************************************************************
1556 * WriteProfileSectionW (KERNEL32.@)
1558 BOOL WINAPI
WriteProfileSectionW( LPCWSTR section
, LPCWSTR keys_n_values
)
1560 return WritePrivateProfileSectionW(section
, keys_n_values
, wininiW
);
1564 /***********************************************************************
1565 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1567 * Returns the section names contained in the specified file.
1568 * FIXME: Where do we find this file when the path is relative?
1569 * The section names are returned as a list of strings with an extra
1570 * '\0' to mark the end of the list. Except for that the behavior
1571 * depends on the Windows version.
1574 * - if the buffer is 0 or 1 character long then it is as if it was of
1576 * - otherwise, if the buffer is too small only the section names that fit
1578 * - note that this means if the buffer was too small to return even just
1579 * the first section name then a single '\0' will be returned.
1580 * - the return value is the number of characters written in the buffer,
1581 * except if the buffer was too small in which case len-2 is returned
1584 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1585 * '\0' and the return value is 0
1586 * - otherwise if the buffer is too small then the first section name that
1587 * does not fit is truncated so that the string list can be terminated
1588 * correctly (double '\0')
1589 * - the return value is the number of characters written in the buffer
1590 * except for the trailing '\0'. If the buffer is too small, then the
1591 * return value is len-2
1592 * - Win2000 has a bug that triggers when the section names and the
1593 * trailing '\0' fit exactly in the buffer. In that case the trailing
1596 * Wine implements the observed Win2000 behavior (except for the bug).
1598 * Note that when the buffer is big enough then the return value may be any
1599 * value between 1 and len-1 (or len in Win95), including len-2.
1601 DWORD WINAPI
GetPrivateProfileSectionNamesW( LPWSTR buffer
, DWORD size
,
1606 RtlEnterCriticalSection( &PROFILE_CritSect
);
1608 if (PROFILE_Open( filename
, FALSE
))
1609 ret
= PROFILE_GetSectionNames(buffer
, size
);
1611 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1617 /***********************************************************************
1618 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1620 DWORD WINAPI
GetPrivateProfileSectionNamesA( LPSTR buffer
, DWORD size
,
1623 UNICODE_STRING filenameW
;
1627 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)) : NULL
;
1628 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1629 else filenameW
.Buffer
= NULL
;
1631 retW
= GetPrivateProfileSectionNamesW(bufferW
, size
, filenameW
.Buffer
);
1634 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+1, buffer
, size
-1, NULL
, NULL
);
1646 RtlFreeUnicodeString(&filenameW
);
1647 HeapFree(GetProcessHeap(), 0, bufferW
);
1651 /***********************************************************************
1652 * GetPrivateProfileStructW (KERNEL32.@)
1654 * Should match Win95's behaviour pretty much
1656 BOOL WINAPI
GetPrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1657 LPVOID buf
, UINT len
, LPCWSTR filename
)
1661 RtlEnterCriticalSection( &PROFILE_CritSect
);
1663 if (PROFILE_Open( filename
, FALSE
))
1665 PROFILEKEY
*k
= PROFILE_Find ( &CurProfile
->section
, section
, key
, FALSE
, FALSE
);
1668 DPRINT("value (at %p): %S\n", k
->value
, k
->value
);
1669 if (((wcslen(k
->value
) - 2) / 2) == len
)
1676 end
= k
->value
+ wcslen(k
->value
); /* -> '\0' */
1678 /* check for invalid chars in ASCII coded hex string */
1679 for (p
= k
->value
; p
< end
; p
++)
1683 DPRINT("invalid char '%x' in file %S->[%S]->%S !\n",
1684 *p
, filename
, section
, key
);
1692 BOOL highnibble
= TRUE
;
1694 LPBYTE binbuf
= buf
;
1696 end
-= 2; /* don't include checksum in output data */
1697 /* translate ASCII hex format into binary data */
1698 for (p
= k
->value
; p
< end
; p
++)
1701 val
= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1708 *binbuf
++ = b
; /* feed binary data into output */
1709 chksum
+= b
; /* calculate checksum */
1711 highnibble
^= 1; /* toggle */
1714 /* retrieve stored checksum value */
1716 b
= ( (c
> '9') ? (c
- 'A' + 10) : (c
- '0') ) << 4;
1718 b
+= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1719 if (b
== (chksum
& 0xff)) /* checksums match ? */
1725 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1730 /***********************************************************************
1731 * GetPrivateProfileStructA (KERNEL32.@)
1733 BOOL WINAPI
GetPrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1734 LPVOID buffer
, UINT len
, LPCSTR filename
)
1736 UNICODE_STRING sectionW
, keyW
, filenameW
;
1739 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1740 else sectionW
.Buffer
= NULL
;
1741 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1742 else keyW
.Buffer
= NULL
;
1743 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1744 else filenameW
.Buffer
= NULL
;
1746 ret
= GetPrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buffer
, len
,
1748 /* Do not translate binary data. */
1750 RtlFreeUnicodeString(§ionW
);
1751 RtlFreeUnicodeString(&keyW
);
1752 RtlFreeUnicodeString(&filenameW
);
1758 /***********************************************************************
1759 * WritePrivateProfileStructW (KERNEL32.@)
1761 BOOL WINAPI
WritePrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1762 LPVOID buf
, UINT bufsize
, LPCWSTR filename
)
1766 LPWSTR outstring
, p
;
1769 if (!section
&& !key
&& !buf
) /* flush the cache */
1770 return WritePrivateProfileStringW( NULL
, NULL
, NULL
, filename
);
1772 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1773 outstring
= HeapAlloc( GetProcessHeap(), 0, (bufsize
*2 + 2 + 1) * sizeof(WCHAR
) );
1774 if(outstring
== NULL
)
1776 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1780 for (binbuf
= (LPBYTE
)buf
; binbuf
< (LPBYTE
)buf
+bufsize
; binbuf
++)
1782 *p
++ = hex
[*binbuf
>> 4];
1783 *p
++ = hex
[*binbuf
& 0xf];
1786 /* checksum is sum & 0xff */
1787 *p
++ = hex
[(sum
& 0xf0) >> 4];
1788 *p
++ = hex
[sum
& 0xf];
1791 RtlEnterCriticalSection( &PROFILE_CritSect
);
1793 if (PROFILE_Open( filename
, TRUE
))
1795 ret
= PROFILE_SetString( section
, key
, outstring
, FALSE
);
1796 PROFILE_FlushFile();
1799 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1801 HeapFree( GetProcessHeap(), 0, outstring
);
1806 /***********************************************************************
1807 * WritePrivateProfileStructA (KERNEL32.@)
1810 WritePrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1811 LPVOID buf
, UINT bufsize
, LPCSTR filename
)
1813 UNICODE_STRING sectionW
, keyW
, filenameW
;
1816 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1817 else sectionW
.Buffer
= NULL
;
1818 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1819 else keyW
.Buffer
= NULL
;
1820 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1821 else filenameW
.Buffer
= NULL
;
1823 /* Do not translate binary data. */
1824 ret
= WritePrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buf
, bufsize
,
1827 RtlFreeUnicodeString(§ionW
);
1828 RtlFreeUnicodeString(&keyW
);
1829 RtlFreeUnicodeString(&filenameW
);
1834 /***********************************************************************
1835 * OpenProfileUserMapping (KERNEL32.@)
1837 BOOL WINAPI
OpenProfileUserMapping(VOID
)
1839 DPRINT1("(), stub!\n");
1840 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1844 /***********************************************************************
1845 * CloseProfileUserMapping (KERNEL32.@)
1847 BOOL WINAPI
CloseProfileUserMapping(VOID
)
1849 DPRINT1("(), stub!\n");
1850 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1859 QueryWin31IniFilesMappedToRegistry(DWORD Unknown0
,
1864 DPRINT1("QueryWin31IniFilesMappedToRegistry not implemented\n");
1865 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);