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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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};
74 static RTL_CRITICAL_SECTION PROFILE_CritSect
;
75 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
77 0, 0, &PROFILE_CritSect
,
78 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
81 static RTL_CRITICAL_SECTION PROFILE_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
83 static const char hex
[16] = "0123456789ABCDEF";
86 static __inline WCHAR
*memchrW( const WCHAR
*ptr
, WCHAR ch
, size_t n
)
89 for (end
= ptr
+ n
; ptr
< end
; ptr
++)
95 static __inline WCHAR
*memrchrW( const WCHAR
*ptr
, WCHAR ch
, size_t n
)
97 const WCHAR
*end
, *ret
= NULL
;
98 for (end
= ptr
+ n
; ptr
< end
; ptr
++)
104 /***********************************************************************
107 * Copy the content of an entry into a buffer, removing quotes, and possibly
108 * translating environment variables.
110 static void PROFILE_CopyEntry( LPWSTR buffer
, LPCWSTR value
, int len
,
117 if (strip_quote
&& ((*value
== '\'') || (*value
== '\"')))
119 if (value
[1] && (value
[wcslen(value
)-1] == *value
))
123 lstrcpynW( buffer
, value
, len
);
124 if (quote
&& (len
>= (int)wcslen(value
))) buffer
[wcslen(buffer
)-1] = '\0';
128 /* byte-swaps shorts in-place in a buffer. len is in WCHARs */
129 static __inline
void PROFILE_ByteSwapShortBuffer(WCHAR
* buffer
, int len
)
132 USHORT
* shortbuffer
= (USHORT
*)buffer
;
133 for (i
= 0; i
< len
; i
++)
134 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
);
162 static void PROFILE_WriteLine( HANDLE hFile
, WCHAR
* szLine
, int len
, ENCODING encoding
)
165 int write_buffer_len
;
166 DWORD dwBytesWritten
;
168 DPRINT("writing: %.*S\n", len
, szLine
);
173 write_buffer_len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
174 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
175 if (!write_buffer
) return;
176 len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
177 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
178 HeapFree(GetProcessHeap(), 0, write_buffer
);
181 write_buffer_len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
182 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
183 if (!write_buffer
) return;
184 len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
185 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
186 HeapFree(GetProcessHeap(), 0, write_buffer
);
188 case ENCODING_UTF16LE
:
189 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
191 case ENCODING_UTF16BE
:
192 PROFILE_ByteSwapShortBuffer(szLine
, len
);
193 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
196 DPRINT1("encoding type %d not implemented\n", encoding
);
201 /***********************************************************************
204 * Save a profile tree to a file.
206 static void PROFILE_Save( HANDLE hFile
, const PROFILESECTION
*section
, ENCODING encoding
)
211 PROFILE_WriteMarker(hFile
, encoding
);
213 for ( ; section
; section
= section
->next
)
217 if (section
->name
[0]) len
+= wcslen(section
->name
) + 6;
219 for (key
= section
->key
; key
; key
= key
->next
)
221 len
+= wcslen(key
->name
) + 2;
222 if (key
->value
) len
+= wcslen(key
->value
) + 1;
225 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
229 if (section
->name
[0])
234 wcscpy( p
, section
->name
);
240 for (key
= section
->key
; key
; key
= key
->next
)
242 wcscpy( p
, key
->name
);
247 wcscpy( p
, key
->value
);
253 PROFILE_WriteLine( hFile
, buffer
, len
, encoding
);
254 HeapFree(GetProcessHeap(), 0, buffer
);
259 /***********************************************************************
262 * Free a profile tree.
264 static void PROFILE_Free( PROFILESECTION
*section
)
266 PROFILESECTION
*next_section
;
267 PROFILEKEY
*key
, *next_key
;
269 for ( ; section
; section
= next_section
)
271 for (key
= section
->key
; key
; key
= next_key
)
273 next_key
= key
->next
;
274 HeapFree( GetProcessHeap(), 0, key
->value
);
275 HeapFree( GetProcessHeap(), 0, key
);
277 next_section
= section
->next
;
278 HeapFree( GetProcessHeap(), 0, section
);
283 /* returns 1 if a character white space else 0 */
284 static __inline
int PROFILE_isspaceW(WCHAR c
)
288 if (c
=='\r' || c
==0x1a)
290 /* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */
295 static __inline ENCODING
PROFILE_DetectTextEncoding(const void * buffer
, int * len
)
297 INT flags
= IS_TEXT_UNICODE_SIGNATURE
|
298 IS_TEXT_UNICODE_REVERSE_SIGNATURE
|
299 IS_TEXT_UNICODE_ODD_LENGTH
;
300 if (*len
>= (int)sizeof(bom_utf8
) && !memcmp(buffer
, bom_utf8
, sizeof(bom_utf8
)))
302 *len
= sizeof(bom_utf8
);
303 return ENCODING_UTF8
;
305 RtlIsTextUnicode((void *)buffer
, *len
, &flags
);
306 if (flags
& IS_TEXT_UNICODE_SIGNATURE
)
308 *len
= sizeof(WCHAR
);
309 return ENCODING_UTF16LE
;
311 if (flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
313 *len
= sizeof(WCHAR
);
314 return ENCODING_UTF16BE
;
317 return ENCODING_ANSI
;
321 /***********************************************************************
324 * Load a profile tree from a file.
326 static PROFILESECTION
*PROFILE_Load(HANDLE hFile
, ENCODING
* pEncoding
)
330 const WCHAR
*szLineStart
, *szLineEnd
;
331 const WCHAR
*szValueStart
, *szEnd
, *next_line
;
333 PROFILESECTION
*section
, *first_section
;
334 PROFILESECTION
**next_section
;
335 PROFILEKEY
*key
, *prev_key
, **next_key
;
338 DPRINT("%p\n", hFile
);
340 dwFileSize
= GetFileSize(hFile
, NULL
);
341 if (dwFileSize
== INVALID_FILE_SIZE
)
344 pBuffer
= HeapAlloc(GetProcessHeap(), 0 , dwFileSize
);
348 if (!ReadFile(hFile
, pBuffer
, dwFileSize
, &dwFileSize
, NULL
))
350 HeapFree(GetProcessHeap(), 0, pBuffer
);
351 DPRINT("Error %ld reading file\n", GetLastError());
356 *pEncoding
= PROFILE_DetectTextEncoding(pBuffer
, &len
);
357 /* len is set to the number of bytes in the character marker.
358 * we want to skip these bytes */
359 pBuffer
= (char *)pBuffer
+ len
;
364 DPRINT("ANSI encoding\n");
366 len
= MultiByteToWideChar(CP_ACP
, 0, (char *)pBuffer
, dwFileSize
, NULL
, 0);
367 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
370 HeapFree(GetProcessHeap(), 0, pBuffer
);
373 MultiByteToWideChar(CP_ACP
, 0, (char *)pBuffer
, dwFileSize
, szFile
, len
);
374 szEnd
= szFile
+ len
;
378 DPRINT("UTF8 encoding\n");
380 len
= MultiByteToWideChar(CP_UTF8
, 0, (char *)pBuffer
, dwFileSize
, NULL
, 0);
381 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
384 HeapFree(GetProcessHeap(), 0, pBuffer
);
387 MultiByteToWideChar(CP_UTF8
, 0, (char *)pBuffer
, dwFileSize
, szFile
, len
);
388 szEnd
= szFile
+ len
;
391 case ENCODING_UTF16LE
:
392 DPRINT("UTF16 Little Endian encoding\n");
393 szFile
= (WCHAR
*)pBuffer
+ 1;
394 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
397 case ENCODING_UTF16BE
:
398 DPRINT("UTF16 Big Endian encoding\n");
399 szFile
= (WCHAR
*)pBuffer
+ 1;
400 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
401 PROFILE_ByteSwapShortBuffer(szFile
, dwFileSize
/ sizeof(WCHAR
));
405 DPRINT("encoding type %d not implemented\n", *pEncoding
);
406 HeapFree(GetProcessHeap(), 0, pBuffer
);
410 first_section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) );
411 if (first_section
== NULL
)
413 if (szFile
!= pBuffer
)
414 HeapFree(GetProcessHeap(), 0, szFile
);
415 HeapFree(GetProcessHeap(), 0, pBuffer
);
418 first_section
->name
[0] = 0;
419 first_section
->key
= NULL
;
420 first_section
->next
= NULL
;
421 next_section
= &first_section
->next
;
422 next_key
= &first_section
->key
;
426 while (next_line
< szEnd
)
428 szLineStart
= next_line
;
429 next_line
= memchrW(szLineStart
, '\n', szEnd
- szLineStart
);
430 if (!next_line
) next_line
= szEnd
;
432 szLineEnd
= next_line
;
436 /* get rid of white space */
437 while (szLineStart
< szLineEnd
&& PROFILE_isspaceW(*szLineStart
)) szLineStart
++;
438 while ((szLineEnd
> szLineStart
) && ((szLineEnd
[-1] == '\n') || (PROFILE_isspaceW(szLineEnd
[-1]) && szLineEnd
[-1] != ' '))) szLineEnd
--;
440 if (szLineStart
>= szLineEnd
)
443 if (*szLineStart
== '[') /* section start */
445 const WCHAR
* szSectionEnd
;
446 if (!(szSectionEnd
= memrchrW( szLineStart
, ']', szLineEnd
- szLineStart
)))
448 DPRINT("Invalid section header at line %d: %.*S\n",
449 line
, (int)(szLineEnd
- szLineStart
), szLineStart
);
454 len
= (int)(szSectionEnd
- szLineStart
);
455 /* no need to allocate +1 for NULL terminating character as
456 * already included in structure */
457 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) + len
* sizeof(WCHAR
) )))
459 memcpy(section
->name
, szLineStart
, len
* sizeof(WCHAR
));
460 section
->name
[len
] = '\0';
462 section
->next
= NULL
;
463 *next_section
= section
;
464 next_section
= §ion
->next
;
465 next_key
= §ion
->key
;
468 DPRINT("New section: %S\n", section
->name
);
474 /* get rid of white space after the name and before the start
476 len
= szLineEnd
- szLineStart
;
477 if ((szValueStart
= memchrW( szLineStart
, '=', szLineEnd
- szLineStart
)) != NULL
)
479 const WCHAR
*szNameEnd
= szValueStart
;
480 while ((szNameEnd
> szLineStart
) && PROFILE_isspaceW(szNameEnd
[-1])) szNameEnd
--;
481 len
= szNameEnd
- szLineStart
;
483 while (szValueStart
< szLineEnd
&& PROFILE_isspaceW(*szValueStart
)) szValueStart
++;
486 if (len
|| !prev_key
|| *prev_key
->name
)
488 /* no need to allocate +1 for NULL terminating character as
489 * already included in structure */
490 if (!(key
= HeapAlloc( GetProcessHeap(), 0, sizeof(*key
) + len
* sizeof(WCHAR
) ))) break;
491 memcpy(key
->name
, szLineStart
, len
* sizeof(WCHAR
));
492 key
->name
[len
] = '\0';
495 len
= (int)(szLineEnd
- szValueStart
);
496 key
->value
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
497 memcpy(key
->value
, szValueStart
, len
* sizeof(WCHAR
));
498 key
->value
[len
] = '\0';
500 else key
->value
= NULL
;
504 next_key
= &key
->next
;
507 DPRINT("New key: name=%S, value=%S\n",
508 key
->name
, key
->value
?key
->value
: L
"(none)");
511 if (szFile
!= pBuffer
)
512 HeapFree(GetProcessHeap(), 0, szFile
);
513 HeapFree(GetProcessHeap(), 0, pBuffer
);
514 return first_section
;
518 /***********************************************************************
519 * PROFILE_DeleteSection
521 * Delete a section from a profile tree.
523 static BOOL
PROFILE_DeleteSection( PROFILESECTION
**section
, LPCWSTR name
)
527 if ((*section
)->name
[0] && !_wcsicmp( (*section
)->name
, name
))
529 PROFILESECTION
*to_del
= *section
;
530 *section
= to_del
->next
;
532 PROFILE_Free( to_del
);
535 section
= &(*section
)->next
;
541 /***********************************************************************
544 * Delete a key from a profile tree.
546 static BOOL
PROFILE_DeleteKey( PROFILESECTION
**section
,
547 LPCWSTR section_name
, LPCWSTR key_name
)
551 if ((*section
)->name
[0] && !_wcsicmp( (*section
)->name
, section_name
))
553 PROFILEKEY
**key
= &(*section
)->key
;
556 if (!_wcsicmp( (*key
)->name
, key_name
))
558 PROFILEKEY
*to_del
= *key
;
560 HeapFree( GetProcessHeap(), 0, to_del
->value
);
561 HeapFree( GetProcessHeap(), 0, to_del
);
567 section
= &(*section
)->next
;
573 /***********************************************************************
574 * PROFILE_DeleteAllKeys
576 * Delete all keys from a profile tree.
578 static void PROFILE_DeleteAllKeys( LPCWSTR section_name
)
580 PROFILESECTION
**section
= &CurProfile
->section
;
583 if ((*section
)->name
[0] && !_wcsicmp( (*section
)->name
, section_name
))
585 PROFILEKEY
**key
= &(*section
)->key
;
588 PROFILEKEY
*to_del
= *key
;
590 HeapFree( GetProcessHeap(), 0, to_del
->value
);
591 HeapFree( GetProcessHeap(), 0, to_del
);
592 CurProfile
->changed
=TRUE
;
595 section
= &(*section
)->next
;
600 /***********************************************************************
603 * Find a key in a profile tree, optionally creating it.
605 static PROFILEKEY
*PROFILE_Find( PROFILESECTION
**section
, LPCWSTR section_name
,
606 LPCWSTR key_name
, BOOL create
, BOOL create_always
)
611 while (PROFILE_isspaceW(*section_name
)) section_name
++;
612 p
= section_name
+ wcslen(section_name
) - 1;
613 while ((p
> section_name
) && PROFILE_isspaceW(*p
)) p
--;
614 seclen
= p
- section_name
+ 1;
616 while (PROFILE_isspaceW(*key_name
)) key_name
++;
617 p
= key_name
+ wcslen(key_name
) - 1;
618 while ((p
> key_name
) && PROFILE_isspaceW(*p
)) p
--;
619 keylen
= p
- key_name
+ 1;
623 if ( ((*section
)->name
[0])
624 && (!(_wcsnicmp( (*section
)->name
, section_name
, seclen
)))
625 && (((*section
)->name
)[seclen
] == '\0') )
627 PROFILEKEY
**key
= &(*section
)->key
;
631 /* If create_always is FALSE then we check if the keyname
632 * already exists. Otherwise we add it regardless of its
633 * existence, to allow keys to be added more than once in
638 if ( (!(_wcsnicmp( (*key
)->name
, key_name
, keylen
)))
639 && (((*key
)->name
)[keylen
] == '\0') )
646 if (!(*key
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY
) + wcslen(key_name
) * sizeof(WCHAR
) )))
648 wcscpy( (*key
)->name
, key_name
);
649 (*key
)->value
= NULL
;
653 section
= &(*section
)->next
;
655 if (!create
) return NULL
;
656 *section
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION
) + wcslen(section_name
) * sizeof(WCHAR
) );
657 if(*section
== NULL
) return NULL
;
658 wcscpy( (*section
)->name
, section_name
);
659 (*section
)->next
= NULL
;
660 if (!((*section
)->key
= HeapAlloc( GetProcessHeap(), 0,
661 sizeof(PROFILEKEY
) + wcslen(key_name
) * sizeof(WCHAR
) )))
663 HeapFree(GetProcessHeap(), 0, *section
);
666 wcscpy( (*section
)->key
->name
, key_name
);
667 (*section
)->key
->value
= NULL
;
668 (*section
)->key
->next
= NULL
;
669 return (*section
)->key
;
673 /***********************************************************************
676 * Flush the current profile to disk if changed.
678 static BOOL
PROFILE_FlushFile(void)
681 FILETIME LastWriteTime
;
685 DPRINT("No current profile!\n");
689 if (!CurProfile
->changed
) return TRUE
;
691 hFile
= CreateFileW(CurProfile
->filename
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
693 if (hFile
== INVALID_HANDLE_VALUE
)
695 DPRINT("could not save profile file %S (error was %ld)\n", CurProfile
->filename
, GetLastError());
699 DPRINT("Saving %S\n", CurProfile
->filename
);
700 PROFILE_Save( hFile
, CurProfile
->section
, CurProfile
->encoding
);
701 if(GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
))
702 CurProfile
->LastWriteTime
=LastWriteTime
;
703 CloseHandle( hFile
);
704 CurProfile
->changed
= FALSE
;
709 /***********************************************************************
710 * PROFILE_ReleaseFile
712 * Flush the current profile to disk and remove it from the cache.
714 static void PROFILE_ReleaseFile(void)
717 PROFILE_Free( CurProfile
->section
);
718 HeapFree( GetProcessHeap(), 0, CurProfile
->filename
);
719 CurProfile
->changed
= FALSE
;
720 CurProfile
->section
= NULL
;
721 CurProfile
->filename
= NULL
;
722 CurProfile
->encoding
= ENCODING_ANSI
;
723 ZeroMemory(&CurProfile
->LastWriteTime
, sizeof(CurProfile
->LastWriteTime
));
727 /***********************************************************************
730 * Open a profile file, checking the cached file first.
732 static BOOL
PROFILE_Open( LPCWSTR filename
)
734 WCHAR windirW
[MAX_PATH
];
735 WCHAR buffer
[MAX_PATH
];
736 HANDLE hFile
= INVALID_HANDLE_VALUE
;
737 FILETIME LastWriteTime
;
739 PROFILE
*tempProfile
;
741 ZeroMemory(&LastWriteTime
, sizeof(LastWriteTime
));
743 /* First time around */
746 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
748 MRUProfile
[i
]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE
) );
749 if(MRUProfile
[i
] == NULL
) break;
750 MRUProfile
[i
]->changed
=FALSE
;
751 MRUProfile
[i
]->section
=NULL
;
752 MRUProfile
[i
]->filename
=NULL
;
753 MRUProfile
[i
]->encoding
=ENCODING_ANSI
;
754 ZeroMemory(&MRUProfile
[i
]->LastWriteTime
, sizeof(FILETIME
));
757 GetWindowsDirectoryW( windirW
, MAX_PATH
);
760 filename
= L
"win.ini";
762 if ((RtlDetermineDosPathNameType_U(filename
) == RtlPathTypeRelative
) &&
763 !wcschr(filename
, '\\') && !wcschr(filename
, '/'))
765 static const WCHAR wszSeparator
[] = {'\\', 0};
766 wcscpy(buffer
, windirW
);
767 wcscat(buffer
, wszSeparator
);
768 wcscat(buffer
, filename
);
773 GetFullPathNameW(filename
, sizeof(buffer
)/sizeof(buffer
[0]), buffer
, &dummy
);
776 DPRINT("path: %S\n", buffer
);
778 hFile
= CreateFileW(buffer
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
780 if ((hFile
== INVALID_HANDLE_VALUE
) && (GetLastError() != ERROR_FILE_NOT_FOUND
))
782 DPRINT("Error %ld opening file %S\n", GetLastError(), buffer
);
786 for(i
= 0; i
< N_CACHED_PROFILES
; i
++)
788 if ((MRUProfile
[i
]->filename
&& !wcscmp( buffer
, MRUProfile
[i
]->filename
)))
790 DPRINT("MRU Filename: %S, new filename: %S\n", MRUProfile
[i
]->filename
, buffer
);
794 tempProfile
=MRUProfile
[i
];
795 for (j
= i
; j
> 0; j
--)
796 MRUProfile
[j
] = MRUProfile
[j
-1];
797 CurProfile
=tempProfile
;
799 if (hFile
!= INVALID_HANDLE_VALUE
)
800 GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
);
802 LastWriteTime
.dwHighDateTime
= LastWriteTime
.dwLowDateTime
= 0;
803 if (memcmp(&CurProfile
->LastWriteTime
, &LastWriteTime
, sizeof(FILETIME
)))
805 DPRINT("(%S): already opened (mru = %d)\n",
810 DPRINT("(%S): already opened, needs refreshing (mru = %d)\n",
813 if (hFile
!= INVALID_HANDLE_VALUE
)
819 /* Flush the old current profile */
822 /* Make the oldest profile the current one only in order to get rid of it */
823 if(i
== N_CACHED_PROFILES
)
825 tempProfile
= MRUProfile
[N_CACHED_PROFILES
-1];
826 for (i
= N_CACHED_PROFILES
- 1; i
> 0; i
--)
827 MRUProfile
[i
] = MRUProfile
[i
-1];
828 CurProfile
=tempProfile
;
831 if (CurProfile
->filename
)
832 PROFILE_ReleaseFile();
834 /* OK, now that CurProfile is definitely free we assign it our new file */
835 CurProfile
->filename
= HeapAlloc( GetProcessHeap(), 0, (wcslen(buffer
)+1) * sizeof(WCHAR
) );
836 if(CurProfile
->filename
== NULL
)
838 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
841 wcscpy( CurProfile
->filename
, buffer
);
843 if (hFile
!= INVALID_HANDLE_VALUE
)
845 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
846 GetFileTime(hFile
, NULL
, NULL
, &CurProfile
->LastWriteTime
);
851 /* Does not exist yet, we will create it in PROFILE_FlushFile */
852 DPRINT("profile file %S not found\n", buffer
);
858 /***********************************************************************
861 * Returns all keys of a section.
862 * If return_values is TRUE, also include the corresponding values.
864 static INT
PROFILE_GetSection( PROFILESECTION
*section
, LPCWSTR section_name
,
865 LPWSTR buffer
, UINT len
, BOOL return_values
, BOOL return_noequalkeys
)
872 DPRINT("%S,%p,%u\n", section_name
, buffer
, len
);
876 if (section
->name
[0] && !_wcsicmp( section
->name
, section_name
))
879 for (key
= section
->key
; key
; key
= key
->next
)
882 if (!*key
->name
) continue; /* Skip empty lines */
883 if (IS_ENTRY_COMMENT(key
->name
)) continue; /* Skip comments */
884 if (!return_noequalkeys
&& !return_values
&& !key
->value
) continue; /* Skip lines w.o. '=' */
885 PROFILE_CopyEntry( buffer
, key
->name
, len
- 1, 0 );
886 len
-= wcslen(buffer
) + 1;
887 buffer
+= wcslen(buffer
) + 1;
890 if (return_values
&& key
->value
)
893 PROFILE_CopyEntry ( buffer
, key
->value
, len
- 1, 0 );
894 len
-= wcslen(buffer
) + 1;
895 buffer
+= wcslen(buffer
) + 1;
901 /*If either lpszSection or lpszKey is NULL and the supplied
902 destination buffer is too small to hold all the strings,
903 the last string is truncated and followed by two null characters.
904 In this case, the return value is equal to cchReturnBuffer
911 section
= section
->next
;
913 buffer
[0] = buffer
[1] = '\0';
917 /* See GetPrivateProfileSectionNamesA for documentation */
918 static INT
PROFILE_GetSectionNames( LPWSTR buffer
, UINT len
)
922 PROFILESECTION
*section
;
924 DPRINT("(%p, %d)\n", buffer
, len
);
935 section
= CurProfile
->section
;
936 while ((section
!=NULL
)) {
937 if (section
->name
[0]) {
938 tmplen
= wcslen(section
->name
)+1;
939 if (tmplen
>= buflen
) {
941 memcpy(buf
, section
->name
, (buflen
- 1) * sizeof(WCHAR
));
948 memcpy(buf
, section
->name
, tmplen
* sizeof(WCHAR
));
952 section
= section
->next
;
959 /***********************************************************************
962 * Get a profile string.
964 * Tests with GetPrivateProfileString16, W95a,
965 * with filled buffer ("****...") and section "set1" and key_name "1" valid:
966 * section key_name def_val res buffer
967 * "set1" "1" "x" 43 [data]
968 * "set1" "1 " "x" 43 [data] (!)
969 * "set1" " 1 "' "x" 43 [data] (!)
970 * "set1" "" "x" 1 "x"
971 * "set1" "" "x " 1 "x" (!)
972 * "set1" "" " x " 3 " x" (!)
973 * "set1" NULL "x" 6 "1\02\03\0\0"
974 * "set1" "" "x" 1 "x"
975 * NULL "1" "x" 0 "" (!)
981 static INT
PROFILE_GetString( LPCWSTR section
, LPCWSTR key_name
,
982 LPCWSTR def_val
, LPWSTR buffer
, UINT len
, BOOL win32
)
984 PROFILEKEY
*key
= NULL
;
985 static const WCHAR empty_strW
[] = { 0 };
987 if (!buffer
) return 0;
989 if (!def_val
) def_val
= empty_strW
;
994 /* Win95 returns 0 on keyname "". Tested with Likse32 bon 000227 */
997 key
= PROFILE_Find( &CurProfile
->section
, section
, key_name
, FALSE
, FALSE
);
998 PROFILE_CopyEntry( buffer
, (key
&& key
->value
) ? key
->value
: def_val
,
1000 DPRINT("(%S, %S, %S): returning %S\n",
1003 return wcslen(buffer
);
1006 /* no "else" here ! */
1007 if (section
&& section
[0])
1009 INT ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, FALSE
, !win32
);
1010 if (!buffer
[0]) /* no luck -> def_val */
1012 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
1013 ret
= wcslen(buffer
);
1022 /***********************************************************************
1025 * Set a profile string.
1027 static BOOL
PROFILE_SetString( LPCWSTR section_name
, LPCWSTR key_name
,
1028 LPCWSTR value
, BOOL create_always
)
1030 if (!key_name
) /* Delete a whole section */
1032 DPRINT("(%S)\n", section_name
);
1033 CurProfile
->changed
|= PROFILE_DeleteSection( &CurProfile
->section
,
1035 return TRUE
; /* Even if PROFILE_DeleteSection() has failed,
1036 this is not an error on application's level.*/
1038 else if (!value
) /* Delete a key */
1040 DPRINT("(%S, %S)\n", section_name
, key_name
);
1041 CurProfile
->changed
|= PROFILE_DeleteKey( &CurProfile
->section
,
1042 section_name
, key_name
);
1043 return TRUE
; /* same error handling as above */
1045 else /* Set the key value */
1047 PROFILEKEY
*key
= PROFILE_Find(&CurProfile
->section
, section_name
,
1048 key_name
, TRUE
, create_always
);
1049 DPRINT("(%S, %S, %S):\n",
1050 section_name
, key_name
, value
);
1051 if (!key
) return FALSE
;
1053 /* strip the leading spaces. We can safely strip \n\r and
1054 * friends too, they should not happen here anyway. */
1055 while (PROFILE_isspaceW(*value
)) value
++;
1059 if (!wcscmp( key
->value
, value
))
1061 DPRINT(" no change needed\n" );
1062 return TRUE
; /* No change needed */
1064 DPRINT(" replacing %S\n", key
->value
);
1065 HeapFree( GetProcessHeap(), 0, key
->value
);
1069 DPRINT(" creating key\n");
1071 key
->value
= HeapAlloc( GetProcessHeap(), 0, (wcslen(value
) + 1) * sizeof(WCHAR
) );
1072 if(key
->value
== NULL
)
1074 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1077 wcscpy( key
->value
, value
);
1078 CurProfile
->changed
= TRUE
;
1084 /********************* API functions **********************************/
1087 /***********************************************************************
1088 * GetProfileIntA (KERNEL32.@)
1090 UINT WINAPI
GetProfileIntA( LPCSTR section
, LPCSTR entry
, INT def_val
)
1092 return GetPrivateProfileIntA( section
, entry
, def_val
, "win.ini" );
1095 /***********************************************************************
1096 * GetProfileIntW (KERNEL32.@)
1098 UINT WINAPI
GetProfileIntW( LPCWSTR section
, LPCWSTR entry
, INT def_val
)
1100 return GetPrivateProfileIntW( section
, entry
, def_val
, L
"win.ini" );
1105 * - Section names if 'section' is NULL
1106 * - Keys in a Section if 'entry' is NULL
1107 * (see MSDN doc for GetPrivateProfileString)
1109 static int PROFILE_GetPrivateProfileString( LPCWSTR section
, LPCWSTR entry
,
1110 LPCWSTR def_val
, LPWSTR buffer
,
1111 UINT len
, LPCWSTR filename
,
1115 LPCWSTR pDefVal
= NULL
;
1117 DPRINT("%S, %S, %S, %p, %u, %S\n",
1119 def_val
, buffer
, len
, filename
);
1121 /* strip any trailing ' ' of def_val. */
1124 LPCWSTR p
= &def_val
[wcslen(def_val
)]; /* even "" works ! */
1133 if (*p
== ' ') /* ouch, contained trailing ' ' */
1135 int len
= (int)(p
- def_val
);
1138 p
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1141 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1144 memcpy(p
, def_val
, len
* sizeof(WCHAR
));
1151 pDefVal
= (LPCWSTR
)def_val
;
1153 RtlEnterCriticalSection( &PROFILE_CritSect
);
1155 if (PROFILE_Open( filename
)) {
1156 if (win32
&& (section
== NULL
))
1157 ret
= PROFILE_GetSectionNames(buffer
, len
);
1159 /* PROFILE_GetString can handle the 'entry == NULL' case */
1160 ret
= PROFILE_GetString( section
, entry
, pDefVal
, buffer
, len
, win32
);
1162 lstrcpynW( buffer
, pDefVal
, len
);
1163 ret
= wcslen( buffer
);
1166 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1168 if (pDefVal
!= def_val
) /* allocated */
1169 HeapFree(GetProcessHeap(), 0, (void*)pDefVal
);
1171 DPRINT("returning %S, %d\n", buffer
, ret
);
1177 /***********************************************************************
1178 * GetPrivateProfileStringA (KERNEL32.@)
1180 DWORD WINAPI
GetPrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1181 LPCSTR def_val
, LPSTR buffer
,
1182 DWORD len
, LPCSTR filename
)
1184 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1188 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1189 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1190 else sectionW
.Buffer
= NULL
;
1191 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1192 else entryW
.Buffer
= NULL
;
1193 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1194 else def_valW
.Buffer
= NULL
;
1195 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1196 else filenameW
.Buffer
= NULL
;
1198 retW
= GetPrivateProfileStringW( sectionW
.Buffer
, entryW
.Buffer
,
1199 def_valW
.Buffer
, bufferW
, len
,
1203 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1210 ret
--; /* strip terminating 0 */
1213 RtlFreeUnicodeString(§ionW
);
1214 RtlFreeUnicodeString(&entryW
);
1215 RtlFreeUnicodeString(&def_valW
);
1216 RtlFreeUnicodeString(&filenameW
);
1217 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 if (len
+1 == sizeof(buffer
)/sizeof(WCHAR
))
1294 DPRINT1("result may be wrong!\n");
1296 /* FIXME: if entry can be found but it's empty, then Win16 is
1297 * supposed to return 0 instead of def_val ! Difficult/problematic
1298 * to implement (every other failure also returns zero buffer),
1299 * thus wait until testing framework avail for making sure nothing
1300 * else gets broken that way. */
1302 return (UINT
)def_val
;
1304 RtlInitUnicodeString( &bufferW
, buffer
);
1305 RtlUnicodeStringToInteger( &bufferW
, 10, &result
);
1309 /***********************************************************************
1310 * GetPrivateProfileIntA (KERNEL32.@)
1312 * FIXME: rewrite using unicode
1314 UINT WINAPI
GetPrivateProfileIntA( LPCSTR section
, LPCSTR entry
,
1315 INT def_val
, LPCSTR filename
)
1317 UNICODE_STRING entryW
, filenameW
, sectionW
;
1319 if(entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1320 else entryW
.Buffer
= NULL
;
1321 if(filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1322 else filenameW
.Buffer
= NULL
;
1323 if(section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1324 else sectionW
.Buffer
= NULL
;
1325 res
= GetPrivateProfileIntW(sectionW
.Buffer
, entryW
.Buffer
, def_val
,
1327 RtlFreeUnicodeString(§ionW
);
1328 RtlFreeUnicodeString(&filenameW
);
1329 RtlFreeUnicodeString(&entryW
);
1334 /***********************************************************************
1335 * GetPrivateProfileSectionW (KERNEL32.@)
1337 DWORD WINAPI
GetPrivateProfileSectionW( LPCWSTR section
, LPWSTR buffer
,
1338 DWORD len
, LPCWSTR filename
)
1342 if (!section
|| !buffer
)
1344 SetLastError(ERROR_INVALID_PARAMETER
);
1348 DPRINT("(%S, %p, %ld, %S)\n",
1349 section
, buffer
, len
, filename
);
1351 RtlEnterCriticalSection( &PROFILE_CritSect
);
1353 if (PROFILE_Open( filename
))
1354 ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, TRUE
, FALSE
);
1356 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1362 /***********************************************************************
1363 * GetPrivateProfileSectionA (KERNEL32.@)
1365 DWORD WINAPI
GetPrivateProfileSectionA( LPCSTR section
, LPSTR buffer
,
1366 DWORD len
, LPCSTR filename
)
1368 UNICODE_STRING sectionW
, filenameW
;
1372 if (!section
|| !buffer
)
1374 SetLastError(ERROR_INVALID_PARAMETER
);
1378 bufferW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1379 RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1380 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1381 else filenameW
.Buffer
= NULL
;
1383 retW
= GetPrivateProfileSectionW(sectionW
.Buffer
, bufferW
, len
, filenameW
.Buffer
);
1386 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1402 RtlFreeUnicodeString(§ionW
);
1403 RtlFreeUnicodeString(&filenameW
);
1404 HeapFree(GetProcessHeap(), 0, bufferW
);
1408 /***********************************************************************
1409 * GetProfileSectionA (KERNEL32.@)
1411 DWORD WINAPI
GetProfileSectionA( LPCSTR section
, LPSTR buffer
, DWORD len
)
1413 return GetPrivateProfileSectionA( section
, buffer
, len
, "win.ini" );
1416 /***********************************************************************
1417 * GetProfileSectionW (KERNEL32.@)
1419 DWORD WINAPI
GetProfileSectionW( LPCWSTR section
, LPWSTR buffer
, DWORD len
)
1421 return GetPrivateProfileSectionW( section
, buffer
, len
, L
"win.ini" );
1425 /***********************************************************************
1426 * WritePrivateProfileStringW (KERNEL32.@)
1428 BOOL WINAPI
WritePrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1429 LPCWSTR string
, LPCWSTR filename
)
1433 RtlEnterCriticalSection( &PROFILE_CritSect
);
1435 if (!section
&& !entry
&& !string
) /* documented "file flush" case */
1437 if (!filename
|| PROFILE_Open( filename
))
1439 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1442 else if (PROFILE_Open( filename
))
1445 DPRINT1("(NULL?, %S, %S, %S)?\n",
1446 entry
, string
, filename
);
1448 ret
= PROFILE_SetString( section
, entry
, string
, FALSE
);
1449 PROFILE_FlushFile();
1453 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1457 /***********************************************************************
1458 * WritePrivateProfileStringA (KERNEL32.@)
1460 BOOL WINAPI
WritePrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1461 LPCSTR string
, LPCSTR filename
)
1463 UNICODE_STRING sectionW
, entryW
, stringW
, filenameW
;
1466 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1467 else sectionW
.Buffer
= NULL
;
1468 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1469 else entryW
.Buffer
= NULL
;
1470 if (string
) RtlCreateUnicodeStringFromAsciiz(&stringW
, string
);
1471 else stringW
.Buffer
= NULL
;
1472 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1473 else filenameW
.Buffer
= NULL
;
1475 ret
= WritePrivateProfileStringW(sectionW
.Buffer
, entryW
.Buffer
,
1476 stringW
.Buffer
, filenameW
.Buffer
);
1477 RtlFreeUnicodeString(§ionW
);
1478 RtlFreeUnicodeString(&entryW
);
1479 RtlFreeUnicodeString(&stringW
);
1480 RtlFreeUnicodeString(&filenameW
);
1484 /***********************************************************************
1485 * WritePrivateProfileSectionW (KERNEL32.@)
1487 BOOL WINAPI
WritePrivateProfileSectionW( LPCWSTR section
,
1488 LPCWSTR string
, LPCWSTR filename
)
1493 RtlEnterCriticalSection( &PROFILE_CritSect
);
1495 if (!section
&& !string
)
1497 if (!filename
|| PROFILE_Open( filename
))
1499 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1502 else if (PROFILE_Open( filename
)) {
1503 if (!string
) {/* delete the named section*/
1504 ret
= PROFILE_SetString(section
,NULL
,NULL
, FALSE
);
1505 PROFILE_FlushFile();
1507 PROFILE_DeleteAllKeys(section
);
1510 LPWSTR buf
= HeapAlloc( GetProcessHeap(), 0, (wcslen(string
)+1) * sizeof(WCHAR
) );
1513 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1514 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1517 wcscpy( buf
, string
);
1518 if((p
= wcschr( buf
, '='))) {
1520 ret
= PROFILE_SetString( section
, buf
, p
+ 1, TRUE
);
1522 HeapFree( GetProcessHeap(), 0, buf
);
1523 string
+= wcslen(string
) + 1;
1525 PROFILE_FlushFile();
1529 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1533 /***********************************************************************
1534 * WritePrivateProfileSectionA (KERNEL32.@)
1536 BOOL WINAPI
WritePrivateProfileSectionA( LPCSTR section
,
1537 LPCSTR string
, LPCSTR filename
)
1540 UNICODE_STRING sectionW
, filenameW
;
1549 while(*p
) p
+= strlen(p
) + 1;
1550 lenA
= p
- string
+ 1;
1551 lenW
= MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, NULL
, 0);
1552 if ((stringW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
))))
1553 MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, stringW
, lenW
);
1555 else stringW
= NULL
;
1556 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1557 else sectionW
.Buffer
= NULL
;
1558 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1559 else filenameW
.Buffer
= NULL
;
1561 ret
= WritePrivateProfileSectionW(sectionW
.Buffer
, stringW
, filenameW
.Buffer
);
1563 HeapFree(GetProcessHeap(), 0, stringW
);
1564 RtlFreeUnicodeString(§ionW
);
1565 RtlFreeUnicodeString(&filenameW
);
1569 /***********************************************************************
1570 * WriteProfileSectionA (KERNEL32.@)
1572 BOOL WINAPI
WriteProfileSectionA( LPCSTR section
, LPCSTR keys_n_values
)
1575 return WritePrivateProfileSectionA(section
, keys_n_values
, "win.ini");
1578 /***********************************************************************
1579 * WriteProfileSectionW (KERNEL32.@)
1581 BOOL WINAPI
WriteProfileSectionW( LPCWSTR section
, LPCWSTR keys_n_values
)
1583 return WritePrivateProfileSectionW(section
, keys_n_values
, L
"win.ini");
1587 /***********************************************************************
1588 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1590 * Returns the section names contained in the specified file.
1591 * FIXME: Where do we find this file when the path is relative?
1592 * The section names are returned as a list of strings with an extra
1593 * '\0' to mark the end of the list. Except for that the behavior
1594 * depends on the Windows version.
1597 * - if the buffer is 0 or 1 character long then it is as if it was of
1599 * - otherwise, if the buffer is to small only the section names that fit
1601 * - note that this means if the buffer was to small to return even just
1602 * the first section name then a single '\0' will be returned.
1603 * - the return value is the number of characters written in the buffer,
1604 * except if the buffer was too smal in which case len-2 is returned
1607 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1608 * '\0' and the return value is 0
1609 * - otherwise if the buffer is too small then the first section name that
1610 * does not fit is truncated so that the string list can be terminated
1611 * correctly (double '\0')
1612 * - the return value is the number of characters written in the buffer
1613 * except for the trailing '\0'. If the buffer is too small, then the
1614 * return value is len-2
1615 * - Win2000 has a bug that triggers when the section names and the
1616 * trailing '\0' fit exactly in the buffer. In that case the trailing
1619 * Wine implements the observed Win2000 behavior (except for the bug).
1621 * Note that when the buffer is big enough then the return value may be any
1622 * value between 1 and len-1 (or len in Win95), including len-2.
1624 DWORD WINAPI
GetPrivateProfileSectionNamesW( LPWSTR buffer
, DWORD size
,
1629 RtlEnterCriticalSection( &PROFILE_CritSect
);
1631 if (PROFILE_Open( filename
))
1632 ret
= PROFILE_GetSectionNames(buffer
, size
);
1634 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1640 /***********************************************************************
1641 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1643 DWORD WINAPI
GetPrivateProfileSectionNamesA( LPSTR buffer
, DWORD size
,
1646 UNICODE_STRING filenameW
;
1650 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)) : NULL
;
1652 RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1654 filenameW
.Buffer
= NULL
;
1656 retW
= GetPrivateProfileSectionNamesW(bufferW
, size
, filenameW
.Buffer
);
1659 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+1, buffer
, size
-1, NULL
, NULL
);
1671 RtlFreeUnicodeString(&filenameW
);
1672 HeapFree(GetProcessHeap(), 0, bufferW
);
1676 /***********************************************************************
1677 * GetPrivateProfileStructW (KERNEL32.@)
1679 * Should match Win95's behaviour pretty much
1681 BOOL WINAPI
GetPrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1682 LPVOID buf
, UINT len
, LPCWSTR filename
)
1686 RtlEnterCriticalSection( &PROFILE_CritSect
);
1688 if (PROFILE_Open( filename
))
1690 PROFILEKEY
*k
= PROFILE_Find ( &CurProfile
->section
, section
, key
, FALSE
, FALSE
);
1693 DPRINT("value (at %p): %S\n", k
->value
, k
->value
);
1694 if (((wcslen(k
->value
) - 2) / 2) == len
)
1701 end
= k
->value
+ wcslen(k
->value
); /* -> '\0' */
1703 /* check for invalid chars in ASCII coded hex string */
1704 for (p
= k
->value
; p
< end
; p
++)
1708 DPRINT("invalid char '%x' in file %S->[%S]->%S !\n",
1709 *p
, filename
, section
, key
);
1717 BOOL highnibble
= TRUE
;
1719 LPBYTE binbuf
= (LPBYTE
)buf
;
1721 end
-= 2; /* don't include checksum in output data */
1722 /* translate ASCII hex format into binary data */
1723 for (p
= k
->value
; p
< end
; p
++)
1726 val
= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1733 *binbuf
++ = b
; /* feed binary data into output */
1734 chksum
+= b
; /* calculate checksum */
1736 highnibble
^= 1; /* toggle */
1739 /* retrieve stored checksum value */
1741 b
= ( (c
> '9') ? (c
- 'A' + 10) : (c
- '0') ) << 4;
1743 b
+= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1744 if (b
== (chksum
& 0xff)) /* checksums match ? */
1750 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1755 /***********************************************************************
1756 * GetPrivateProfileStructA (KERNEL32.@)
1758 BOOL WINAPI
GetPrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1759 LPVOID buffer
, UINT len
, LPCSTR filename
)
1761 UNICODE_STRING sectionW
, keyW
, filenameW
;
1764 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1765 else sectionW
.Buffer
= NULL
;
1766 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1767 else keyW
.Buffer
= NULL
;
1768 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1769 else filenameW
.Buffer
= NULL
;
1771 ret
= GetPrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buffer
, len
,
1773 /* Do not translate binary data. */
1775 RtlFreeUnicodeString(§ionW
);
1776 RtlFreeUnicodeString(&keyW
);
1777 RtlFreeUnicodeString(&filenameW
);
1782 /***********************************************************************
1783 * WritePrivateProfileStructW (KERNEL32.@)
1785 BOOL WINAPI
WritePrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1786 LPVOID buf
, UINT bufsize
, LPCWSTR filename
)
1790 LPWSTR outstring
, p
;
1793 if (!section
&& !key
&& !buf
) /* flush the cache */
1794 return WritePrivateProfileStringW( NULL
, NULL
, NULL
, filename
);
1796 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1797 outstring
= HeapAlloc( GetProcessHeap(), 0, (bufsize
*2 + 2 + 1) * sizeof(WCHAR
) );
1798 if(outstring
== NULL
)
1800 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1804 for (binbuf
= (LPBYTE
)buf
; binbuf
< (LPBYTE
)buf
+bufsize
; binbuf
++)
1806 *p
++ = hex
[*binbuf
>> 4];
1807 *p
++ = hex
[*binbuf
& 0xf];
1810 /* checksum is sum & 0xff */
1811 *p
++ = hex
[(sum
& 0xf0) >> 4];
1812 *p
++ = hex
[sum
& 0xf];
1815 RtlEnterCriticalSection( &PROFILE_CritSect
);
1817 if (PROFILE_Open( filename
))
1819 ret
= PROFILE_SetString( section
, key
, outstring
, FALSE
);
1820 PROFILE_FlushFile();
1823 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1825 HeapFree( GetProcessHeap(), 0, outstring
);
1831 /***********************************************************************
1832 * WritePrivateProfileStructA (KERNEL32.@)
1835 WritePrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1836 LPVOID buf
, UINT bufsize
, LPCSTR filename
)
1838 UNICODE_STRING sectionW
, keyW
, filenameW
;
1841 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1842 else sectionW
.Buffer
= NULL
;
1843 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1844 else keyW
.Buffer
= NULL
;
1845 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1846 else filenameW
.Buffer
= NULL
;
1848 /* Do not translate binary data. */
1849 ret
= WritePrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buf
, bufsize
,
1852 RtlFreeUnicodeString(§ionW
);
1853 RtlFreeUnicodeString(&keyW
);
1854 RtlFreeUnicodeString(&filenameW
);
1859 /***********************************************************************
1860 * CloseProfileUserMapping
1863 CloseProfileUserMapping(VOID
)
1865 DPRINT1("(), stub!\n");
1866 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1875 QueryWin31IniFilesMappedToRegistry(DWORD Unknown0
,
1880 DPRINT1("QueryWin31IniFilesMappedToRegistry not implemented\n");
1881 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);