4 * Copyright 1993 Miguel de Icaza
5 * Copyright 1996 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
33 #include "wine/winbase16.h"
34 #include "wine/unicode.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(profile
);
40 static const char bom_utf8
[] = {0xEF,0xBB,0xBF};
50 typedef struct tagPROFILEKEY
53 struct tagPROFILEKEY
*next
;
57 typedef struct tagPROFILESECTION
59 struct tagPROFILEKEY
*key
;
60 struct tagPROFILESECTION
*next
;
68 PROFILESECTION
*section
;
70 FILETIME LastWriteTime
;
75 #define N_CACHED_PROFILES 10
77 /* Cached profile files */
78 static PROFILE
*MRUProfile
[N_CACHED_PROFILES
]={NULL
};
80 #define CurProfile (MRUProfile[0])
82 /* Check for comments in profile */
83 #define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
85 static const WCHAR emptystringW
[] = {0};
86 static const WCHAR wininiW
[] = { 'w','i','n','.','i','n','i',0 };
88 static CRITICAL_SECTION PROFILE_CritSect
;
89 static CRITICAL_SECTION_DEBUG critsect_debug
=
91 0, 0, &PROFILE_CritSect
,
92 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
93 0, 0, { (DWORD_PTR
)(__FILE__
": PROFILE_CritSect") }
95 static CRITICAL_SECTION PROFILE_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
97 static const char hex
[16] = "0123456789ABCDEF";
99 /***********************************************************************
102 * Copy the content of an entry into a buffer, removing quotes, and possibly
103 * translating environment variables.
105 static void PROFILE_CopyEntry( LPWSTR buffer
, LPCWSTR value
, int len
,
112 if (strip_quote
&& ((*value
== '\'') || (*value
== '\"')))
114 if (value
[1] && (value
[strlenW(value
)-1] == *value
)) quote
= *value
++;
117 lstrcpynW( buffer
, value
, len
);
118 if (quote
&& (len
>= strlenW(value
))) buffer
[strlenW(buffer
)-1] = '\0';
121 /* byte-swaps shorts in-place in a buffer. len is in WCHARs */
122 static inline void PROFILE_ByteSwapShortBuffer(WCHAR
* buffer
, int len
)
125 USHORT
* shortbuffer
= (USHORT
*)buffer
;
126 for (i
= 0; i
< len
; i
++)
127 shortbuffer
[i
] = RtlUshortByteSwap(shortbuffer
[i
]);
130 /* writes any necessary encoding marker to the file */
131 static inline void PROFILE_WriteMarker(HANDLE hFile
, ENCODING encoding
)
133 DWORD dwBytesWritten
;
140 WriteFile(hFile
, bom_utf8
, sizeof(bom_utf8
), &dwBytesWritten
, NULL
);
142 case ENCODING_UTF16LE
:
144 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
146 case ENCODING_UTF16BE
:
148 WriteFile(hFile
, &bom
, sizeof(bom
), &dwBytesWritten
, NULL
);
153 static void PROFILE_WriteLine( HANDLE hFile
, WCHAR
* szLine
, int len
, ENCODING encoding
)
156 int write_buffer_len
;
157 DWORD dwBytesWritten
;
159 TRACE("writing: %s\n", debugstr_wn(szLine
, len
));
164 write_buffer_len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
165 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
166 if (!write_buffer
) return;
167 len
= WideCharToMultiByte(CP_ACP
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
168 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
169 HeapFree(GetProcessHeap(), 0, write_buffer
);
172 write_buffer_len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, NULL
, 0, NULL
, NULL
);
173 write_buffer
= HeapAlloc(GetProcessHeap(), 0, write_buffer_len
);
174 if (!write_buffer
) return;
175 len
= WideCharToMultiByte(CP_UTF8
, 0, szLine
, len
, write_buffer
, write_buffer_len
, NULL
, NULL
);
176 WriteFile(hFile
, write_buffer
, len
, &dwBytesWritten
, NULL
);
177 HeapFree(GetProcessHeap(), 0, write_buffer
);
179 case ENCODING_UTF16LE
:
180 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
182 case ENCODING_UTF16BE
:
183 PROFILE_ByteSwapShortBuffer(szLine
, len
);
184 WriteFile(hFile
, szLine
, len
* sizeof(WCHAR
), &dwBytesWritten
, NULL
);
187 FIXME("encoding type %d not implemented\n", encoding
);
191 /***********************************************************************
194 * Save a profile tree to a file.
196 static void PROFILE_Save( HANDLE hFile
, const PROFILESECTION
*section
, ENCODING encoding
)
201 PROFILE_WriteMarker(hFile
, encoding
);
203 for ( ; section
; section
= section
->next
)
207 if (section
->name
[0]) len
+= strlenW(section
->name
) + 6;
209 for (key
= section
->key
; key
; key
= key
->next
)
211 len
+= strlenW(key
->name
) + 2;
212 if (key
->value
) len
+= strlenW(key
->value
) + 1;
215 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
219 if (section
->name
[0])
224 strcpyW( p
, section
->name
);
230 for (key
= section
->key
; key
; key
= key
->next
)
232 strcpyW( p
, key
->name
);
237 strcpyW( p
, key
->value
);
243 PROFILE_WriteLine( hFile
, buffer
, len
, encoding
);
244 HeapFree(GetProcessHeap(), 0, buffer
);
249 /***********************************************************************
252 * Free a profile tree.
254 static void PROFILE_Free( PROFILESECTION
*section
)
256 PROFILESECTION
*next_section
;
257 PROFILEKEY
*key
, *next_key
;
259 for ( ; section
; section
= next_section
)
261 for (key
= section
->key
; key
; key
= next_key
)
263 next_key
= key
->next
;
264 HeapFree( GetProcessHeap(), 0, key
->value
);
265 HeapFree( GetProcessHeap(), 0, key
);
267 next_section
= section
->next
;
268 HeapFree( GetProcessHeap(), 0, section
);
272 /* returns 1 if a character white space else 0 */
273 static inline int PROFILE_isspaceW(WCHAR c
)
275 /* ^Z (DOS EOF) is a space too (found on CD-ROMs) */
276 return isspaceW(c
) || c
== 0x1a;
279 static inline ENCODING
PROFILE_DetectTextEncoding(const void * buffer
, int * len
)
281 int flags
= IS_TEXT_UNICODE_SIGNATURE
|
282 IS_TEXT_UNICODE_REVERSE_SIGNATURE
|
283 IS_TEXT_UNICODE_ODD_LENGTH
;
284 if (*len
>= sizeof(bom_utf8
) && !memcmp(buffer
, bom_utf8
, sizeof(bom_utf8
)))
286 *len
= sizeof(bom_utf8
);
287 return ENCODING_UTF8
;
289 RtlIsTextUnicode(buffer
, *len
, &flags
);
290 if (flags
& IS_TEXT_UNICODE_SIGNATURE
)
292 *len
= sizeof(WCHAR
);
293 return ENCODING_UTF16LE
;
295 if (flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
297 *len
= sizeof(WCHAR
);
298 return ENCODING_UTF16BE
;
301 return ENCODING_ANSI
;
305 /***********************************************************************
308 * Load a profile tree from a file.
310 static PROFILESECTION
*PROFILE_Load(HANDLE hFile
, ENCODING
* pEncoding
)
312 void *buffer_base
, *pBuffer
;
314 const WCHAR
*szLineStart
, *szLineEnd
;
315 const WCHAR
*szValueStart
, *szEnd
, *next_line
;
317 PROFILESECTION
*section
, *first_section
;
318 PROFILESECTION
**next_section
;
319 PROFILEKEY
*key
, *prev_key
, **next_key
;
322 TRACE("%p\n", hFile
);
324 dwFileSize
= GetFileSize(hFile
, NULL
);
325 if (dwFileSize
== INVALID_FILE_SIZE
)
328 buffer_base
= HeapAlloc(GetProcessHeap(), 0 , dwFileSize
);
329 if (!buffer_base
) return NULL
;
331 if (!ReadFile(hFile
, buffer_base
, dwFileSize
, &dwFileSize
, NULL
))
333 HeapFree(GetProcessHeap(), 0, buffer_base
);
334 WARN("Error %d reading file\n", GetLastError());
338 *pEncoding
= PROFILE_DetectTextEncoding(buffer_base
, &len
);
339 /* len is set to the number of bytes in the character marker.
340 * we want to skip these bytes */
341 pBuffer
= (char *)buffer_base
+ len
;
346 TRACE("ANSI encoding\n");
348 len
= MultiByteToWideChar(CP_ACP
, 0, (char *)pBuffer
, dwFileSize
, NULL
, 0);
349 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
352 HeapFree(GetProcessHeap(), 0, buffer_base
);
355 MultiByteToWideChar(CP_ACP
, 0, (char *)pBuffer
, dwFileSize
, szFile
, len
);
356 szEnd
= szFile
+ len
;
359 TRACE("UTF8 encoding\n");
361 len
= MultiByteToWideChar(CP_UTF8
, 0, (char *)pBuffer
, dwFileSize
, NULL
, 0);
362 szFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
365 HeapFree(GetProcessHeap(), 0, buffer_base
);
368 MultiByteToWideChar(CP_UTF8
, 0, (char *)pBuffer
, dwFileSize
, szFile
, len
);
369 szEnd
= szFile
+ len
;
371 case ENCODING_UTF16LE
:
372 TRACE("UTF16 Little Endian encoding\n");
373 szFile
= (WCHAR
*)pBuffer
;
374 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
376 case ENCODING_UTF16BE
:
377 TRACE("UTF16 Big Endian encoding\n");
378 szFile
= (WCHAR
*)pBuffer
;
379 szEnd
= (WCHAR
*)((char *)pBuffer
+ dwFileSize
);
380 PROFILE_ByteSwapShortBuffer(szFile
, dwFileSize
/ sizeof(WCHAR
));
383 FIXME("encoding type %d not implemented\n", *pEncoding
);
384 HeapFree(GetProcessHeap(), 0, buffer_base
);
388 first_section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) );
389 if(first_section
== NULL
)
391 if (szFile
!= pBuffer
)
392 HeapFree(GetProcessHeap(), 0, szFile
);
393 HeapFree(GetProcessHeap(), 0, buffer_base
);
396 first_section
->name
[0] = 0;
397 first_section
->key
= NULL
;
398 first_section
->next
= NULL
;
399 next_section
= &first_section
->next
;
400 next_key
= &first_section
->key
;
404 while (next_line
< szEnd
)
406 szLineStart
= next_line
;
407 next_line
= memchrW(szLineStart
, '\n', szEnd
- szLineStart
);
408 if (!next_line
) next_line
= szEnd
;
410 szLineEnd
= next_line
;
414 /* get rid of white space */
415 while (szLineStart
< szLineEnd
&& PROFILE_isspaceW(*szLineStart
)) szLineStart
++;
416 while ((szLineEnd
> szLineStart
) && PROFILE_isspaceW(szLineEnd
[-1])) szLineEnd
--;
418 if (szLineStart
>= szLineEnd
) continue;
420 if (*szLineStart
== '[') /* section start */
422 const WCHAR
* szSectionEnd
;
423 if (!(szSectionEnd
= memrchrW( szLineStart
, ']', szLineEnd
- szLineStart
)))
425 WARN("Invalid section header at line %d: %s\n",
426 line
, debugstr_wn(szLineStart
, (int)(szLineEnd
- szLineStart
)) );
431 len
= (int)(szSectionEnd
- szLineStart
);
432 /* no need to allocate +1 for NULL terminating character as
433 * already included in structure */
434 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) + len
* sizeof(WCHAR
) )))
436 memcpy(section
->name
, szLineStart
, len
* sizeof(WCHAR
));
437 section
->name
[len
] = '\0';
439 section
->next
= NULL
;
440 *next_section
= section
;
441 next_section
= §ion
->next
;
442 next_key
= §ion
->key
;
445 TRACE("New section: %s\n", debugstr_w(section
->name
));
451 /* get rid of white space after the name and before the start
453 len
= szLineEnd
- szLineStart
;
454 if ((szValueStart
= memchrW( szLineStart
, '=', szLineEnd
- szLineStart
)) != NULL
)
456 const WCHAR
*szNameEnd
= szValueStart
;
457 while ((szNameEnd
> szLineStart
) && PROFILE_isspaceW(szNameEnd
[-1])) szNameEnd
--;
458 len
= szNameEnd
- szLineStart
;
460 while (szValueStart
< szLineEnd
&& PROFILE_isspaceW(*szValueStart
)) szValueStart
++;
463 if (len
|| !prev_key
|| *prev_key
->name
)
465 /* no need to allocate +1 for NULL terminating character as
466 * already included in structure */
467 if (!(key
= HeapAlloc( GetProcessHeap(), 0, sizeof(*key
) + len
* sizeof(WCHAR
) ))) break;
468 memcpy(key
->name
, szLineStart
, len
* sizeof(WCHAR
));
469 key
->name
[len
] = '\0';
472 len
= (int)(szLineEnd
- szValueStart
);
473 key
->value
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
474 memcpy(key
->value
, szValueStart
, len
* sizeof(WCHAR
));
475 key
->value
[len
] = '\0';
477 else key
->value
= NULL
;
481 next_key
= &key
->next
;
484 TRACE("New key: name=%s, value=%s\n",
485 debugstr_w(key
->name
), key
->value
? debugstr_w(key
->value
) : "(none)");
488 if (szFile
!= pBuffer
)
489 HeapFree(GetProcessHeap(), 0, szFile
);
490 HeapFree(GetProcessHeap(), 0, buffer_base
);
491 return first_section
;
495 /***********************************************************************
496 * PROFILE_DeleteSection
498 * Delete a section from a profile tree.
500 static BOOL
PROFILE_DeleteSection( PROFILESECTION
**section
, LPCWSTR name
)
504 if ((*section
)->name
[0] && !strcmpiW( (*section
)->name
, name
))
506 PROFILESECTION
*to_del
= *section
;
507 *section
= to_del
->next
;
509 PROFILE_Free( to_del
);
512 section
= &(*section
)->next
;
518 /***********************************************************************
521 * Delete a key from a profile tree.
523 static BOOL
PROFILE_DeleteKey( PROFILESECTION
**section
,
524 LPCWSTR section_name
, LPCWSTR key_name
)
528 if ((*section
)->name
[0] && !strcmpiW( (*section
)->name
, section_name
))
530 PROFILEKEY
**key
= &(*section
)->key
;
533 if (!strcmpiW( (*key
)->name
, key_name
))
535 PROFILEKEY
*to_del
= *key
;
537 HeapFree( GetProcessHeap(), 0, to_del
->value
);
538 HeapFree( GetProcessHeap(), 0, to_del
);
544 section
= &(*section
)->next
;
550 /***********************************************************************
551 * PROFILE_DeleteAllKeys
553 * Delete all keys from a profile tree.
555 static void PROFILE_DeleteAllKeys( LPCWSTR section_name
)
557 PROFILESECTION
**section
= &CurProfile
->section
;
560 if ((*section
)->name
[0] && !strcmpiW( (*section
)->name
, section_name
))
562 PROFILEKEY
**key
= &(*section
)->key
;
565 PROFILEKEY
*to_del
= *key
;
567 HeapFree( GetProcessHeap(), 0, to_del
->value
);
568 HeapFree( GetProcessHeap(), 0, to_del
);
569 CurProfile
->changed
=TRUE
;
572 section
= &(*section
)->next
;
577 /***********************************************************************
580 * Find a key in a profile tree, optionally creating it.
582 static PROFILEKEY
*PROFILE_Find( PROFILESECTION
**section
, LPCWSTR section_name
,
583 LPCWSTR key_name
, BOOL create
, BOOL create_always
)
588 while (PROFILE_isspaceW(*section_name
)) section_name
++;
589 p
= section_name
+ strlenW(section_name
) - 1;
590 while ((p
> section_name
) && PROFILE_isspaceW(*p
)) p
--;
591 seclen
= p
- section_name
+ 1;
593 while (PROFILE_isspaceW(*key_name
)) key_name
++;
594 p
= key_name
+ strlenW(key_name
) - 1;
595 while ((p
> key_name
) && PROFILE_isspaceW(*p
)) p
--;
596 keylen
= p
- key_name
+ 1;
600 if ( ((*section
)->name
[0])
601 && (!(strncmpiW( (*section
)->name
, section_name
, seclen
)))
602 && (((*section
)->name
)[seclen
] == '\0') )
604 PROFILEKEY
**key
= &(*section
)->key
;
608 /* If create_always is FALSE then we check if the keyname
609 * already exists. Otherwise we add it regardless of its
610 * existence, to allow keys to be added more than once in
615 if ( (!(strncmpiW( (*key
)->name
, key_name
, keylen
)))
616 && (((*key
)->name
)[keylen
] == '\0') )
621 if (!create
) return NULL
;
622 if (!(*key
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY
) + strlenW(key_name
) * sizeof(WCHAR
) )))
624 strcpyW( (*key
)->name
, key_name
);
625 (*key
)->value
= NULL
;
629 section
= &(*section
)->next
;
631 if (!create
) return NULL
;
632 *section
= HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION
) + strlenW(section_name
) * sizeof(WCHAR
) );
633 if(*section
== NULL
) return NULL
;
634 strcpyW( (*section
)->name
, section_name
);
635 (*section
)->next
= NULL
;
636 if (!((*section
)->key
= HeapAlloc( GetProcessHeap(), 0,
637 sizeof(PROFILEKEY
) + strlenW(key_name
) * sizeof(WCHAR
) )))
639 HeapFree(GetProcessHeap(), 0, *section
);
642 strcpyW( (*section
)->key
->name
, key_name
);
643 (*section
)->key
->value
= NULL
;
644 (*section
)->key
->next
= NULL
;
645 return (*section
)->key
;
649 /***********************************************************************
652 * Flush the current profile to disk if changed.
654 static BOOL
PROFILE_FlushFile(void)
657 FILETIME LastWriteTime
;
661 WARN("No current profile!\n");
665 if (!CurProfile
->changed
) return TRUE
;
667 hFile
= CreateFileW(CurProfile
->filename
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
668 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
670 if (hFile
== INVALID_HANDLE_VALUE
)
672 WARN("could not save profile file %s (error was %d)\n", debugstr_w(CurProfile
->filename
), GetLastError());
676 TRACE("Saving %s\n", debugstr_w(CurProfile
->filename
));
677 PROFILE_Save( hFile
, CurProfile
->section
, CurProfile
->encoding
);
678 if(GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
))
679 CurProfile
->LastWriteTime
=LastWriteTime
;
680 CloseHandle( hFile
);
681 CurProfile
->changed
= FALSE
;
686 /***********************************************************************
687 * PROFILE_ReleaseFile
689 * Flush the current profile to disk and remove it from the cache.
691 static void PROFILE_ReleaseFile(void)
694 PROFILE_Free( CurProfile
->section
);
695 HeapFree( GetProcessHeap(), 0, CurProfile
->filename
);
696 CurProfile
->changed
= FALSE
;
697 CurProfile
->section
= NULL
;
698 CurProfile
->filename
= NULL
;
699 CurProfile
->encoding
= ENCODING_ANSI
;
700 ZeroMemory(&CurProfile
->LastWriteTime
, sizeof(CurProfile
->LastWriteTime
));
703 /***********************************************************************
705 * Compares a file time with the current time. If the file time is
706 * at least 2.1 seconds in the past, return true.
708 * Intended as cache safety measure: The time resolution on FAT is
709 * two seconds, so files that are not at least two seconds old might
710 * keep their time even on modification, so don't cache them.
712 static BOOL
is_not_current(FILETIME
* ft
)
715 LONGLONG ftll
, nowll
;
716 GetSystemTimeAsFileTime(&Now
);
717 ftll
= ((LONGLONG
)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
718 nowll
= ((LONGLONG
)Now
.dwHighDateTime
<< 32) + Now
.dwLowDateTime
;
719 TRACE("%08x;%08x\n",(unsigned)ftll
+21000000,(unsigned)nowll
);
720 return ftll
+ 21000000 < nowll
;
723 /***********************************************************************
726 * Open a profile file, checking the cached file first.
728 static BOOL
PROFILE_Open( LPCWSTR filename
, BOOL write_access
)
730 WCHAR windirW
[MAX_PATH
];
731 WCHAR buffer
[MAX_PATH
];
732 HANDLE hFile
= INVALID_HANDLE_VALUE
;
733 FILETIME LastWriteTime
;
735 PROFILE
*tempProfile
;
737 ZeroMemory(&LastWriteTime
, sizeof(LastWriteTime
));
739 /* First time around */
742 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
744 MRUProfile
[i
]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE
) );
745 if(MRUProfile
[i
] == NULL
) break;
746 MRUProfile
[i
]->changed
=FALSE
;
747 MRUProfile
[i
]->section
=NULL
;
748 MRUProfile
[i
]->filename
=NULL
;
749 MRUProfile
[i
]->encoding
=ENCODING_ANSI
;
750 ZeroMemory(&MRUProfile
[i
]->LastWriteTime
, sizeof(FILETIME
));
753 GetWindowsDirectoryW( windirW
, MAX_PATH
);
758 if ((RtlDetermineDosPathNameType_U(filename
) == RELATIVE_PATH
) &&
759 !strchrW(filename
, '\\') && !strchrW(filename
, '/'))
761 static const WCHAR wszSeparator
[] = {'\\', 0};
762 strcpyW(buffer
, windirW
);
763 strcatW(buffer
, wszSeparator
);
764 strcatW(buffer
, filename
);
769 GetFullPathNameW(filename
, sizeof(buffer
)/sizeof(buffer
[0]), buffer
, &dummy
);
772 TRACE("path: %s\n", debugstr_w(buffer
));
774 hFile
= CreateFileW(buffer
, GENERIC_READ
| (write_access
? GENERIC_WRITE
: 0),
775 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
,
776 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
778 if ((hFile
== INVALID_HANDLE_VALUE
) && (GetLastError() != ERROR_FILE_NOT_FOUND
))
780 WARN("Error %d opening file %s\n", GetLastError(), debugstr_w(buffer
));
784 for(i
=0;i
<N_CACHED_PROFILES
;i
++)
786 if ((MRUProfile
[i
]->filename
&& !strcmpiW( buffer
, MRUProfile
[i
]->filename
)))
788 TRACE("MRU Filename: %s, new filename: %s\n", debugstr_w(MRUProfile
[i
]->filename
), debugstr_w(buffer
));
792 tempProfile
=MRUProfile
[i
];
794 MRUProfile
[j
]=MRUProfile
[j
-1];
795 CurProfile
=tempProfile
;
798 if (hFile
!= INVALID_HANDLE_VALUE
)
800 GetFileTime(hFile
, NULL
, NULL
, &LastWriteTime
);
801 if (!memcmp( &CurProfile
->LastWriteTime
, &LastWriteTime
, sizeof(FILETIME
) ) &&
802 is_not_current(&LastWriteTime
))
803 TRACE("(%s): already opened (mru=%d)\n",
804 debugstr_w(buffer
), i
);
807 TRACE("(%s): already opened, needs refreshing (mru=%d)\n",
808 debugstr_w(buffer
), i
);
809 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
810 CurProfile
->LastWriteTime
= LastWriteTime
;
814 else TRACE("(%s): already opened, not yet created (mru=%d)\n",
815 debugstr_w(buffer
), i
);
820 /* Flush the old current profile */
823 /* Make the oldest profile the current one only in order to get rid of it */
824 if(i
==N_CACHED_PROFILES
)
826 tempProfile
=MRUProfile
[N_CACHED_PROFILES
-1];
827 for(i
=N_CACHED_PROFILES
-1;i
>0;i
--)
828 MRUProfile
[i
]=MRUProfile
[i
-1];
829 CurProfile
=tempProfile
;
831 if(CurProfile
->filename
) PROFILE_ReleaseFile();
833 /* OK, now that CurProfile is definitely free we assign it our new file */
834 CurProfile
->filename
= HeapAlloc( GetProcessHeap(), 0, (strlenW(buffer
)+1) * sizeof(WCHAR
) );
835 strcpyW( CurProfile
->filename
, buffer
);
837 if (hFile
!= INVALID_HANDLE_VALUE
)
839 CurProfile
->section
= PROFILE_Load(hFile
, &CurProfile
->encoding
);
840 GetFileTime(hFile
, NULL
, NULL
, &CurProfile
->LastWriteTime
);
845 /* Does not exist yet, we will create it in PROFILE_FlushFile */
846 WARN("profile file %s not found\n", debugstr_w(buffer
) );
852 /***********************************************************************
855 * Returns all keys of a section.
856 * If return_values is TRUE, also include the corresponding values.
858 static INT
PROFILE_GetSection( PROFILESECTION
*section
, LPCWSTR section_name
,
859 LPWSTR buffer
, UINT len
, BOOL return_values
, BOOL return_noequalkeys
)
863 if(!buffer
) return 0;
865 TRACE("%s,%p,%u\n", debugstr_w(section_name
), buffer
, len
);
869 if (section
->name
[0] && !strcmpiW( section
->name
, section_name
))
872 for (key
= section
->key
; key
; key
= key
->next
)
875 if (!*key
->name
) continue; /* Skip empty lines */
876 if (IS_ENTRY_COMMENT(key
->name
)) continue; /* Skip comments */
877 if (!return_noequalkeys
&& !return_values
&& !key
->value
) continue; /* Skip lines w.o. '=' */
878 PROFILE_CopyEntry( buffer
, key
->name
, len
- 1, 0 );
879 len
-= strlenW(buffer
) + 1;
880 buffer
+= strlenW(buffer
) + 1;
883 if (return_values
&& key
->value
) {
885 PROFILE_CopyEntry ( buffer
, key
->value
, len
- 1, 0 );
886 len
-= strlenW(buffer
) + 1;
887 buffer
+= strlenW(buffer
) + 1;
892 /*If either lpszSection or lpszKey is NULL and the supplied
893 destination buffer is too small to hold all the strings,
894 the last string is truncated and followed by two null characters.
895 In this case, the return value is equal to cchReturnBuffer
903 section
= section
->next
;
905 buffer
[0] = buffer
[1] = '\0';
909 /* See GetPrivateProfileSectionNamesA for documentation */
910 static INT
PROFILE_GetSectionNames( LPWSTR buffer
, UINT len
)
914 PROFILESECTION
*section
;
916 TRACE("(%p, %d)\n", buffer
, len
);
927 section
= CurProfile
->section
;
928 while ((section
!=NULL
)) {
929 if (section
->name
[0]) {
930 tmplen
= strlenW(section
->name
)+1;
931 if (tmplen
>= buflen
) {
933 memcpy(buf
, section
->name
, (buflen
-1) * sizeof(WCHAR
));
940 memcpy(buf
, section
->name
, tmplen
* sizeof(WCHAR
));
944 section
= section
->next
;
951 /***********************************************************************
954 * Get a profile string.
956 * Tests with GetPrivateProfileString16, W95a,
957 * with filled buffer ("****...") and section "set1" and key_name "1" valid:
958 * section key_name def_val res buffer
959 * "set1" "1" "x" 43 [data]
960 * "set1" "1 " "x" 43 [data] (!)
961 * "set1" " 1 "' "x" 43 [data] (!)
962 * "set1" "" "x" 1 "x"
963 * "set1" "" "x " 1 "x" (!)
964 * "set1" "" " x " 3 " x" (!)
965 * "set1" NULL "x" 6 "1\02\03\0\0"
966 * "set1" "" "x" 1 "x"
967 * NULL "1" "x" 0 "" (!)
973 static INT
PROFILE_GetString( LPCWSTR section
, LPCWSTR key_name
,
974 LPCWSTR def_val
, LPWSTR buffer
, UINT len
, BOOL win32
)
976 PROFILEKEY
*key
= NULL
;
977 static const WCHAR empty_strW
[] = { 0 };
979 if(!buffer
|| !len
) return 0;
981 if (!def_val
) def_val
= empty_strW
;
986 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
987 return strlenW(buffer
);
989 key
= PROFILE_Find( &CurProfile
->section
, section
, key_name
, FALSE
, FALSE
);
990 PROFILE_CopyEntry( buffer
, (key
&& key
->value
) ? key
->value
: def_val
,
992 TRACE("(%s,%s,%s): returning %s\n",
993 debugstr_w(section
), debugstr_w(key_name
),
994 debugstr_w(def_val
), debugstr_w(buffer
) );
995 return strlenW( buffer
);
997 /* no "else" here ! */
998 if (section
&& section
[0])
1000 INT ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, FALSE
, !win32
);
1001 if (!buffer
[0]) /* no luck -> def_val */
1003 PROFILE_CopyEntry(buffer
, def_val
, len
, TRUE
);
1004 ret
= strlenW(buffer
);
1013 /***********************************************************************
1016 * Set a profile string.
1018 static BOOL
PROFILE_SetString( LPCWSTR section_name
, LPCWSTR key_name
,
1019 LPCWSTR value
, BOOL create_always
)
1021 if (!key_name
) /* Delete a whole section */
1023 TRACE("(%s)\n", debugstr_w(section_name
));
1024 CurProfile
->changed
|= PROFILE_DeleteSection( &CurProfile
->section
,
1026 return TRUE
; /* Even if PROFILE_DeleteSection() has failed,
1027 this is not an error on application's level.*/
1029 else if (!value
) /* Delete a key */
1031 TRACE("(%s,%s)\n", debugstr_w(section_name
), debugstr_w(key_name
) );
1032 CurProfile
->changed
|= PROFILE_DeleteKey( &CurProfile
->section
,
1033 section_name
, key_name
);
1034 return TRUE
; /* same error handling as above */
1036 else /* Set the key value */
1038 PROFILEKEY
*key
= PROFILE_Find(&CurProfile
->section
, section_name
,
1039 key_name
, TRUE
, create_always
);
1040 TRACE("(%s,%s,%s):\n",
1041 debugstr_w(section_name
), debugstr_w(key_name
), debugstr_w(value
) );
1042 if (!key
) return FALSE
;
1044 /* strip the leading spaces. We can safely strip \n\r and
1045 * friends too, they should not happen here anyway. */
1046 while (PROFILE_isspaceW(*value
)) value
++;
1050 if (!strcmpW( key
->value
, value
))
1052 TRACE(" no change needed\n" );
1053 return TRUE
; /* No change needed */
1055 TRACE(" replacing %s\n", debugstr_w(key
->value
) );
1056 HeapFree( GetProcessHeap(), 0, key
->value
);
1058 else TRACE(" creating key\n" );
1059 key
->value
= HeapAlloc( GetProcessHeap(), 0, (strlenW(value
)+1) * sizeof(WCHAR
) );
1060 strcpyW( key
->value
, value
);
1061 CurProfile
->changed
= TRUE
;
1067 /********************* API functions **********************************/
1070 /***********************************************************************
1071 * GetProfileIntA (KERNEL32.@)
1073 UINT WINAPI
GetProfileIntA( LPCSTR section
, LPCSTR entry
, INT def_val
)
1075 return GetPrivateProfileIntA( section
, entry
, def_val
, "win.ini" );
1078 /***********************************************************************
1079 * GetProfileIntW (KERNEL32.@)
1081 UINT WINAPI
GetProfileIntW( LPCWSTR section
, LPCWSTR entry
, INT def_val
)
1083 return GetPrivateProfileIntW( section
, entry
, def_val
, wininiW
);
1088 * - Section names if 'section' is NULL
1089 * - Keys in a Section if 'entry' is NULL
1090 * (see MSDN doc for GetPrivateProfileString)
1092 static int PROFILE_GetPrivateProfileString( LPCWSTR section
, LPCWSTR entry
,
1093 LPCWSTR def_val
, LPWSTR buffer
,
1094 UINT len
, LPCWSTR filename
,
1098 LPWSTR defval_tmp
= NULL
;
1100 TRACE("%s,%s,%s,%p,%u,%s\n", debugstr_w(section
), debugstr_w(entry
),
1101 debugstr_w(def_val
), buffer
, len
, debugstr_w(filename
));
1103 /* strip any trailing ' ' of def_val. */
1106 LPCWSTR p
= &def_val
[strlenW(def_val
) - 1];
1108 while (p
> def_val
&& *p
== ' ')
1113 int len
= (int)(p
- def_val
) + 1;
1115 defval_tmp
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1116 memcpy(defval_tmp
, def_val
, len
* sizeof(WCHAR
));
1117 defval_tmp
[len
] = '\0';
1118 def_val
= defval_tmp
;
1122 RtlEnterCriticalSection( &PROFILE_CritSect
);
1124 if (PROFILE_Open( filename
, FALSE
)) {
1125 if (win32
&& (section
== NULL
))
1126 ret
= PROFILE_GetSectionNames(buffer
, len
);
1128 /* PROFILE_GetString can handle the 'entry == NULL' case */
1129 ret
= PROFILE_GetString( section
, entry
, def_val
, buffer
, len
, win32
);
1130 } else if (buffer
&& def_val
) {
1131 lstrcpynW( buffer
, def_val
, len
);
1132 ret
= strlenW( buffer
);
1137 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1139 HeapFree(GetProcessHeap(), 0, defval_tmp
);
1141 TRACE("returning %s, %d\n", debugstr_w(buffer
), ret
);
1146 /***********************************************************************
1147 * GetPrivateProfileString (KERNEL.128)
1149 INT16 WINAPI
GetPrivateProfileString16( LPCSTR section
, LPCSTR entry
,
1150 LPCSTR def_val
, LPSTR buffer
,
1151 UINT16 len
, LPCSTR filename
)
1153 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1155 INT16 retW
, ret
= 0;
1157 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1158 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1159 else sectionW
.Buffer
= NULL
;
1160 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1161 else entryW
.Buffer
= NULL
;
1162 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1163 else def_valW
.Buffer
= NULL
;
1164 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1165 else filenameW
.Buffer
= NULL
;
1167 retW
= PROFILE_GetPrivateProfileString( sectionW
.Buffer
, entryW
.Buffer
,
1168 def_valW
.Buffer
, bufferW
, len
,
1169 filenameW
.Buffer
, FALSE
);
1172 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1179 ret
--; /* strip terminating 0 */
1182 RtlFreeUnicodeString(§ionW
);
1183 RtlFreeUnicodeString(&entryW
);
1184 RtlFreeUnicodeString(&def_valW
);
1185 RtlFreeUnicodeString(&filenameW
);
1186 HeapFree(GetProcessHeap(), 0, bufferW
);
1190 /***********************************************************************
1191 * GetPrivateProfileStringA (KERNEL32.@)
1193 INT WINAPI
GetPrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1194 LPCSTR def_val
, LPSTR buffer
,
1195 UINT len
, LPCSTR filename
)
1197 UNICODE_STRING sectionW
, entryW
, def_valW
, filenameW
;
1201 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)) : NULL
;
1202 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1203 else sectionW
.Buffer
= NULL
;
1204 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1205 else entryW
.Buffer
= NULL
;
1206 if (def_val
) RtlCreateUnicodeStringFromAsciiz(&def_valW
, def_val
);
1207 else def_valW
.Buffer
= NULL
;
1208 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1209 else filenameW
.Buffer
= NULL
;
1211 retW
= GetPrivateProfileStringW( sectionW
.Buffer
, entryW
.Buffer
,
1212 def_valW
.Buffer
, bufferW
, len
,
1216 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1223 ret
--; /* strip terminating 0 */
1226 RtlFreeUnicodeString(§ionW
);
1227 RtlFreeUnicodeString(&entryW
);
1228 RtlFreeUnicodeString(&def_valW
);
1229 RtlFreeUnicodeString(&filenameW
);
1230 HeapFree(GetProcessHeap(), 0, bufferW
);
1234 /***********************************************************************
1235 * GetPrivateProfileStringW (KERNEL32.@)
1237 INT WINAPI
GetPrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1238 LPCWSTR def_val
, LPWSTR buffer
,
1239 UINT len
, LPCWSTR filename
)
1241 TRACE("(%s, %s, %s, %p, %d, %s)\n", debugstr_w(section
), debugstr_w(entry
), debugstr_w(def_val
), buffer
, len
, debugstr_w(filename
));
1243 return PROFILE_GetPrivateProfileString( section
, entry
, def_val
,
1244 buffer
, len
, filename
, TRUE
);
1247 /***********************************************************************
1248 * GetProfileStringA (KERNEL32.@)
1250 INT WINAPI
GetProfileStringA( LPCSTR section
, LPCSTR entry
, LPCSTR def_val
,
1251 LPSTR buffer
, UINT len
)
1253 return GetPrivateProfileStringA( section
, entry
, def_val
,
1254 buffer
, len
, "win.ini" );
1257 /***********************************************************************
1258 * GetProfileStringW (KERNEL32.@)
1260 INT WINAPI
GetProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1261 LPCWSTR def_val
, LPWSTR buffer
, UINT len
)
1263 return GetPrivateProfileStringW( section
, entry
, def_val
,
1264 buffer
, len
, wininiW
);
1267 /***********************************************************************
1268 * WriteProfileStringA (KERNEL32.@)
1270 BOOL WINAPI
WriteProfileStringA( LPCSTR section
, LPCSTR entry
,
1273 return WritePrivateProfileStringA( section
, entry
, string
, "win.ini" );
1276 /***********************************************************************
1277 * WriteProfileStringW (KERNEL32.@)
1279 BOOL WINAPI
WriteProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1282 return WritePrivateProfileStringW( section
, entry
, string
, wininiW
);
1286 /***********************************************************************
1287 * GetPrivateProfileIntW (KERNEL32.@)
1289 UINT WINAPI
GetPrivateProfileIntW( LPCWSTR section
, LPCWSTR entry
,
1290 INT def_val
, LPCWSTR filename
)
1293 UNICODE_STRING bufferW
;
1297 if (!(len
= GetPrivateProfileStringW( section
, entry
, emptystringW
,
1298 buffer
, sizeof(buffer
)/sizeof(WCHAR
),
1302 /* FIXME: if entry can be found but it's empty, then Win16 is
1303 * supposed to return 0 instead of def_val ! Difficult/problematic
1304 * to implement (every other failure also returns zero buffer),
1305 * thus wait until testing framework avail for making sure nothing
1306 * else gets broken that way. */
1307 if (!buffer
[0]) return (UINT
)def_val
;
1309 RtlInitUnicodeString( &bufferW
, buffer
);
1310 RtlUnicodeStringToInteger( &bufferW
, 0, &result
);
1314 /***********************************************************************
1315 * GetPrivateProfileIntA (KERNEL32.@)
1317 * FIXME: rewrite using unicode
1319 UINT WINAPI
GetPrivateProfileIntA( LPCSTR section
, LPCSTR entry
,
1320 INT def_val
, LPCSTR filename
)
1322 UNICODE_STRING entryW
, filenameW
, sectionW
;
1324 if(entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1325 else entryW
.Buffer
= NULL
;
1326 if(filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1327 else filenameW
.Buffer
= NULL
;
1328 if(section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1329 else sectionW
.Buffer
= NULL
;
1330 res
= GetPrivateProfileIntW(sectionW
.Buffer
, entryW
.Buffer
, def_val
,
1332 RtlFreeUnicodeString(§ionW
);
1333 RtlFreeUnicodeString(&filenameW
);
1334 RtlFreeUnicodeString(&entryW
);
1338 /***********************************************************************
1339 * GetPrivateProfileSectionW (KERNEL32.@)
1341 INT WINAPI
GetPrivateProfileSectionW( LPCWSTR section
, LPWSTR buffer
,
1342 DWORD len
, LPCWSTR filename
)
1346 if (!section
|| !buffer
)
1348 SetLastError(ERROR_INVALID_PARAMETER
);
1352 TRACE("(%s, %p, %d, %s)\n", debugstr_w(section
), buffer
, len
, debugstr_w(filename
));
1354 RtlEnterCriticalSection( &PROFILE_CritSect
);
1356 if (PROFILE_Open( filename
, FALSE
))
1357 ret
= PROFILE_GetSection(CurProfile
->section
, section
, buffer
, len
, TRUE
, FALSE
);
1359 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1364 /***********************************************************************
1365 * GetPrivateProfileSectionA (KERNEL32.@)
1367 INT WINAPI
GetPrivateProfileSectionA( LPCSTR section
, LPSTR buffer
,
1368 DWORD len
, LPCSTR filename
)
1370 UNICODE_STRING sectionW
, filenameW
;
1374 if (!section
|| !buffer
)
1376 SetLastError(ERROR_INVALID_PARAMETER
);
1380 bufferW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1381 RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1382 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1383 else filenameW
.Buffer
= NULL
;
1385 retW
= GetPrivateProfileSectionW(sectionW
.Buffer
, bufferW
, len
, filenameW
.Buffer
);
1388 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+ 1, buffer
, len
, NULL
, NULL
);
1404 RtlFreeUnicodeString(§ionW
);
1405 RtlFreeUnicodeString(&filenameW
);
1406 HeapFree(GetProcessHeap(), 0, bufferW
);
1410 /***********************************************************************
1411 * GetProfileSectionA (KERNEL32.@)
1413 INT WINAPI
GetProfileSectionA( LPCSTR section
, LPSTR buffer
, DWORD len
)
1415 return GetPrivateProfileSectionA( section
, buffer
, len
, "win.ini" );
1418 /***********************************************************************
1419 * GetProfileSectionW (KERNEL32.@)
1421 INT WINAPI
GetProfileSectionW( LPCWSTR section
, LPWSTR buffer
, DWORD len
)
1423 return GetPrivateProfileSectionW( section
, buffer
, len
, wininiW
);
1427 /***********************************************************************
1428 * WritePrivateProfileStringW (KERNEL32.@)
1430 BOOL WINAPI
WritePrivateProfileStringW( LPCWSTR section
, LPCWSTR entry
,
1431 LPCWSTR string
, LPCWSTR filename
)
1435 RtlEnterCriticalSection( &PROFILE_CritSect
);
1437 if (!section
&& !entry
&& !string
) /* documented "file flush" case */
1439 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1441 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1444 else if (PROFILE_Open( filename
, TRUE
))
1447 FIXME("(NULL?,%s,%s,%s)?\n",
1448 debugstr_w(entry
), debugstr_w(string
), debugstr_w(filename
));
1450 ret
= PROFILE_SetString( section
, entry
, string
, FALSE
);
1451 PROFILE_FlushFile();
1455 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1459 /***********************************************************************
1460 * WritePrivateProfileStringA (KERNEL32.@)
1462 BOOL WINAPI
WritePrivateProfileStringA( LPCSTR section
, LPCSTR entry
,
1463 LPCSTR string
, LPCSTR filename
)
1465 UNICODE_STRING sectionW
, entryW
, stringW
, filenameW
;
1468 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1469 else sectionW
.Buffer
= NULL
;
1470 if (entry
) RtlCreateUnicodeStringFromAsciiz(&entryW
, entry
);
1471 else entryW
.Buffer
= NULL
;
1472 if (string
) RtlCreateUnicodeStringFromAsciiz(&stringW
, string
);
1473 else stringW
.Buffer
= NULL
;
1474 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1475 else filenameW
.Buffer
= NULL
;
1477 ret
= WritePrivateProfileStringW(sectionW
.Buffer
, entryW
.Buffer
,
1478 stringW
.Buffer
, filenameW
.Buffer
);
1479 RtlFreeUnicodeString(§ionW
);
1480 RtlFreeUnicodeString(&entryW
);
1481 RtlFreeUnicodeString(&stringW
);
1482 RtlFreeUnicodeString(&filenameW
);
1486 /***********************************************************************
1487 * WritePrivateProfileSectionW (KERNEL32.@)
1489 BOOL WINAPI
WritePrivateProfileSectionW( LPCWSTR section
,
1490 LPCWSTR string
, LPCWSTR filename
)
1495 RtlEnterCriticalSection( &PROFILE_CritSect
);
1497 if (!section
&& !string
)
1499 if (!filename
|| PROFILE_Open( filename
, TRUE
))
1501 if (CurProfile
) PROFILE_ReleaseFile(); /* always return FALSE in this case */
1504 else if (PROFILE_Open( filename
, TRUE
)) {
1505 if (!string
) {/* delete the named section*/
1506 ret
= PROFILE_SetString(section
,NULL
,NULL
, FALSE
);
1507 PROFILE_FlushFile();
1509 PROFILE_DeleteAllKeys(section
);
1512 LPWSTR buf
= HeapAlloc( GetProcessHeap(), 0, (strlenW(string
)+1) * sizeof(WCHAR
) );
1513 strcpyW( buf
, string
);
1514 if((p
= strchrW( buf
, '='))) {
1516 ret
= PROFILE_SetString( section
, buf
, p
+1, TRUE
);
1518 HeapFree( GetProcessHeap(), 0, buf
);
1519 string
+= strlenW(string
)+1;
1521 PROFILE_FlushFile();
1525 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1529 /***********************************************************************
1530 * WritePrivateProfileSectionA (KERNEL32.@)
1532 BOOL WINAPI
WritePrivateProfileSectionA( LPCSTR section
,
1533 LPCSTR string
, LPCSTR filename
)
1536 UNICODE_STRING sectionW
, filenameW
;
1545 while(*p
) p
+= strlen(p
) + 1;
1546 lenA
= p
- string
+ 1;
1547 lenW
= MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, NULL
, 0);
1548 if ((stringW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
))))
1549 MultiByteToWideChar(CP_ACP
, 0, string
, lenA
, stringW
, lenW
);
1551 else stringW
= NULL
;
1552 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1553 else sectionW
.Buffer
= NULL
;
1554 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1555 else filenameW
.Buffer
= NULL
;
1557 ret
= WritePrivateProfileSectionW(sectionW
.Buffer
, stringW
, filenameW
.Buffer
);
1559 HeapFree(GetProcessHeap(), 0, stringW
);
1560 RtlFreeUnicodeString(§ionW
);
1561 RtlFreeUnicodeString(&filenameW
);
1565 /***********************************************************************
1566 * WriteProfileSectionA (KERNEL32.@)
1568 BOOL WINAPI
WriteProfileSectionA( LPCSTR section
, LPCSTR keys_n_values
)
1571 return WritePrivateProfileSectionA( section
, keys_n_values
, "win.ini");
1574 /***********************************************************************
1575 * WriteProfileSectionW (KERNEL32.@)
1577 BOOL WINAPI
WriteProfileSectionW( LPCWSTR section
, LPCWSTR keys_n_values
)
1579 return WritePrivateProfileSectionW(section
, keys_n_values
, wininiW
);
1583 /***********************************************************************
1584 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1586 * Returns the section names contained in the specified file.
1587 * FIXME: Where do we find this file when the path is relative?
1588 * The section names are returned as a list of strings with an extra
1589 * '\0' to mark the end of the list. Except for that the behavior
1590 * depends on the Windows version.
1593 * - if the buffer is 0 or 1 character long then it is as if it was of
1595 * - otherwise, if the buffer is too small only the section names that fit
1597 * - note that this means if the buffer was too small to return even just
1598 * the first section name then a single '\0' will be returned.
1599 * - the return value is the number of characters written in the buffer,
1600 * except if the buffer was too small in which case len-2 is returned
1603 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1604 * '\0' and the return value is 0
1605 * - otherwise if the buffer is too small then the first section name that
1606 * does not fit is truncated so that the string list can be terminated
1607 * correctly (double '\0')
1608 * - the return value is the number of characters written in the buffer
1609 * except for the trailing '\0'. If the buffer is too small, then the
1610 * return value is len-2
1611 * - Win2000 has a bug that triggers when the section names and the
1612 * trailing '\0' fit exactly in the buffer. In that case the trailing
1615 * Wine implements the observed Win2000 behavior (except for the bug).
1617 * Note that when the buffer is big enough then the return value may be any
1618 * value between 1 and len-1 (or len in Win95), including len-2.
1620 DWORD WINAPI
GetPrivateProfileSectionNamesW( LPWSTR buffer
, DWORD size
,
1625 RtlEnterCriticalSection( &PROFILE_CritSect
);
1627 if (PROFILE_Open( filename
, FALSE
))
1628 ret
= PROFILE_GetSectionNames(buffer
, size
);
1630 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1636 /***********************************************************************
1637 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1639 DWORD WINAPI
GetPrivateProfileSectionNamesA( LPSTR buffer
, DWORD size
,
1642 UNICODE_STRING filenameW
;
1646 bufferW
= buffer
? HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)) : NULL
;
1647 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1648 else filenameW
.Buffer
= NULL
;
1650 retW
= GetPrivateProfileSectionNamesW(bufferW
, size
, filenameW
.Buffer
);
1653 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, retW
+1, buffer
, size
-1, NULL
, NULL
);
1665 RtlFreeUnicodeString(&filenameW
);
1666 HeapFree(GetProcessHeap(), 0, bufferW
);
1670 /***********************************************************************
1671 * GetPrivateProfileStructW (KERNEL32.@)
1673 * Should match Win95's behaviour pretty much
1675 BOOL WINAPI
GetPrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1676 LPVOID buf
, UINT len
, LPCWSTR filename
)
1680 RtlEnterCriticalSection( &PROFILE_CritSect
);
1682 if (PROFILE_Open( filename
, FALSE
)) {
1683 PROFILEKEY
*k
= PROFILE_Find ( &CurProfile
->section
, section
, key
, FALSE
, FALSE
);
1685 TRACE("value (at %p): %s\n", k
->value
, debugstr_w(k
->value
));
1686 if (((strlenW(k
->value
) - 2) / 2) == len
)
1693 end
= k
->value
+ strlenW(k
->value
); /* -> '\0' */
1694 /* check for invalid chars in ASCII coded hex string */
1695 for (p
=k
->value
; p
< end
; p
++)
1699 WARN("invalid char '%x' in file %s->[%s]->%s !\n",
1700 *p
, debugstr_w(filename
), debugstr_w(section
), debugstr_w(key
));
1707 BOOL highnibble
= TRUE
;
1709 LPBYTE binbuf
= (LPBYTE
)buf
;
1711 end
-= 2; /* don't include checksum in output data */
1712 /* translate ASCII hex format into binary data */
1713 for (p
=k
->value
; p
< end
; p
++)
1717 (c
- 'A' + 10) : (c
- '0');
1724 *binbuf
++ = b
; /* feed binary data into output */
1725 chksum
+= b
; /* calculate checksum */
1727 highnibble
^= 1; /* toggle */
1729 /* retrieve stored checksum value */
1731 b
= ( (c
> '9') ? (c
- 'A' + 10) : (c
- '0') ) << 4;
1733 b
+= (c
> '9') ? (c
- 'A' + 10) : (c
- '0');
1734 if (b
== (chksum
& 0xff)) /* checksums match ? */
1740 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1745 /***********************************************************************
1746 * GetPrivateProfileStructA (KERNEL32.@)
1748 BOOL WINAPI
GetPrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1749 LPVOID buffer
, UINT len
, LPCSTR filename
)
1751 UNICODE_STRING sectionW
, keyW
, filenameW
;
1754 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1755 else sectionW
.Buffer
= NULL
;
1756 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1757 else keyW
.Buffer
= NULL
;
1758 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1759 else filenameW
.Buffer
= NULL
;
1761 ret
= GetPrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buffer
, len
,
1763 /* Do not translate binary data. */
1765 RtlFreeUnicodeString(§ionW
);
1766 RtlFreeUnicodeString(&keyW
);
1767 RtlFreeUnicodeString(&filenameW
);
1773 /***********************************************************************
1774 * WritePrivateProfileStructW (KERNEL32.@)
1776 BOOL WINAPI
WritePrivateProfileStructW (LPCWSTR section
, LPCWSTR key
,
1777 LPVOID buf
, UINT bufsize
, LPCWSTR filename
)
1781 LPWSTR outstring
, p
;
1784 if (!section
&& !key
&& !buf
) /* flush the cache */
1785 return WritePrivateProfileStringW( NULL
, NULL
, NULL
, filename
);
1787 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1788 outstring
= HeapAlloc( GetProcessHeap(), 0, (bufsize
*2 + 2 + 1) * sizeof(WCHAR
) );
1790 for (binbuf
= (LPBYTE
)buf
; binbuf
< (LPBYTE
)buf
+bufsize
; binbuf
++) {
1791 *p
++ = hex
[*binbuf
>> 4];
1792 *p
++ = hex
[*binbuf
& 0xf];
1795 /* checksum is sum & 0xff */
1796 *p
++ = hex
[(sum
& 0xf0) >> 4];
1797 *p
++ = hex
[sum
& 0xf];
1800 RtlEnterCriticalSection( &PROFILE_CritSect
);
1802 if (PROFILE_Open( filename
, TRUE
)) {
1803 ret
= PROFILE_SetString( section
, key
, outstring
, FALSE
);
1804 PROFILE_FlushFile();
1807 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1809 HeapFree( GetProcessHeap(), 0, outstring
);
1814 /***********************************************************************
1815 * WritePrivateProfileStructA (KERNEL32.@)
1817 BOOL WINAPI
WritePrivateProfileStructA (LPCSTR section
, LPCSTR key
,
1818 LPVOID buf
, UINT bufsize
, LPCSTR filename
)
1820 UNICODE_STRING sectionW
, keyW
, filenameW
;
1823 if (section
) RtlCreateUnicodeStringFromAsciiz(§ionW
, section
);
1824 else sectionW
.Buffer
= NULL
;
1825 if (key
) RtlCreateUnicodeStringFromAsciiz(&keyW
, key
);
1826 else keyW
.Buffer
= NULL
;
1827 if (filename
) RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
1828 else filenameW
.Buffer
= NULL
;
1830 /* Do not translate binary data. */
1831 ret
= WritePrivateProfileStructW(sectionW
.Buffer
, keyW
.Buffer
, buf
, bufsize
,
1834 RtlFreeUnicodeString(§ionW
);
1835 RtlFreeUnicodeString(&keyW
);
1836 RtlFreeUnicodeString(&filenameW
);
1841 /***********************************************************************
1842 * WriteOutProfiles (KERNEL.315)
1844 void WINAPI
WriteOutProfiles16(void)
1846 RtlEnterCriticalSection( &PROFILE_CritSect
);
1847 PROFILE_FlushFile();
1848 RtlLeaveCriticalSection( &PROFILE_CritSect
);
1851 /***********************************************************************
1852 * CloseProfileUserMapping (KERNEL32.@)
1854 BOOL WINAPI
CloseProfileUserMapping(void) {
1855 FIXME("(), stub!\n");
1856 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);