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
23 #include "wine/port.h"
34 #include "wine/winbase16.h"
35 #include "wine/unicode.h"
36 #include "wine/server.h"
37 #include "wine/library.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(profile);
42 static const char bom_utf8[] = {0xEF,0xBB,0xBF};
52 typedef struct tagPROFILEKEY
55 struct tagPROFILEKEY *next;
59 typedef struct tagPROFILESECTION
61 struct tagPROFILEKEY *key;
62 struct tagPROFILESECTION *next;
70 PROFILESECTION *section;
72 FILETIME LastWriteTime;
77 #define N_CACHED_PROFILES 10
79 /* Cached profile files */
80 static PROFILE *MRUProfile[N_CACHED_PROFILES]={NULL};
82 #define CurProfile (MRUProfile[0])
84 /* Check for comments in profile */
85 #define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
87 static const WCHAR emptystringW[] = {0};
88 static const WCHAR wininiW[] = { 'w','i','n','.','i','n','i',0 };
90 static CRITICAL_SECTION PROFILE_CritSect;
91 static CRITICAL_SECTION_DEBUG critsect_debug =
93 0, 0, &PROFILE_CritSect,
94 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
95 0, 0, { 0, (DWORD)(__FILE__ ": PROFILE_CritSect") }
97 static CRITICAL_SECTION PROFILE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
99 static const char hex[16] = "0123456789ABCDEF";
101 /***********************************************************************
104 * Copy the content of an entry into a buffer, removing quotes, and possibly
105 * translating environment variables.
107 static void PROFILE_CopyEntry( LPWSTR buffer, LPCWSTR value, int len,
114 if (strip_quote && ((*value == '\'') || (*value == '\"')))
116 if (value[1] && (value[strlenW(value)-1] == *value)) quote = *value++;
119 lstrcpynW( buffer, value, len );
120 if (quote && (len >= strlenW(value))) buffer[strlenW(buffer)-1] = '\0';
123 /* byte-swaps shorts in-place in a buffer. len is in WCHARs */
124 static inline void PROFILE_ByteSwapShortBuffer(WCHAR * buffer, int len)
127 USHORT * shortbuffer = (USHORT *)buffer;
128 for (i = 0; i < len; i++)
129 shortbuffer[i] = RtlUshortByteSwap(shortbuffer[i]);
132 /* writes any necessary encoding marker to the file */
133 static inline void PROFILE_WriteMarker(HANDLE hFile, ENCODING encoding)
135 DWORD dwBytesWritten;
142 WriteFile(hFile, bom_utf8, sizeof(bom_utf8), &dwBytesWritten, NULL);
144 case ENCODING_UTF16LE:
146 WriteFile(hFile, &bom, sizeof(bom), &dwBytesWritten, NULL);
148 case ENCODING_UTF16BE:
150 WriteFile(hFile, &bom, sizeof(bom), &dwBytesWritten, NULL);
155 static void PROFILE_WriteLine( HANDLE hFile, WCHAR * szLine, int len, ENCODING encoding)
158 int write_buffer_len;
159 DWORD dwBytesWritten;
161 TRACE("writing: %s\n", debugstr_wn(szLine, len));
166 write_buffer_len = WideCharToMultiByte(CP_ACP, 0, szLine, len, NULL, 0, NULL, NULL);
167 write_buffer = HeapAlloc(GetProcessHeap(), 0, write_buffer_len);
168 if (!write_buffer) return;
169 len = WideCharToMultiByte(CP_ACP, 0, szLine, len, write_buffer, write_buffer_len, NULL, NULL);
170 WriteFile(hFile, write_buffer, len, &dwBytesWritten, NULL);
171 HeapFree(GetProcessHeap(), 0, write_buffer);
174 write_buffer_len = WideCharToMultiByte(CP_UTF8, 0, szLine, len, NULL, 0, NULL, NULL);
175 write_buffer = HeapAlloc(GetProcessHeap(), 0, write_buffer_len);
176 if (!write_buffer) return;
177 len = WideCharToMultiByte(CP_UTF8, 0, szLine, len, write_buffer, write_buffer_len, NULL, NULL);
178 WriteFile(hFile, write_buffer, len, &dwBytesWritten, NULL);
179 HeapFree(GetProcessHeap(), 0, write_buffer);
181 case ENCODING_UTF16LE:
182 WriteFile(hFile, szLine, len * sizeof(WCHAR), &dwBytesWritten, NULL);
184 case ENCODING_UTF16BE:
185 PROFILE_ByteSwapShortBuffer(szLine, len);
186 WriteFile(hFile, szLine, len * sizeof(WCHAR), &dwBytesWritten, NULL);
189 FIXME("encoding type %d not implemented\n", encoding);
193 /***********************************************************************
196 * Save a profile tree to a file.
198 static void PROFILE_Save( HANDLE hFile, const PROFILESECTION *section, ENCODING encoding )
203 PROFILE_WriteMarker(hFile, encoding);
205 for ( ; section; section = section->next)
209 if (section->name[0]) len += strlenW(section->name) + 6;
211 for (key = section->key; key; key = key->next)
213 len += strlenW(key->name) + 2;
214 if (key->value) len += strlenW(key->value) + 1;
217 buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
221 if (section->name[0])
226 strcpyW( p, section->name );
232 for (key = section->key; key; key = key->next)
234 strcpyW( p, key->name );
239 strcpyW( p, key->value );
245 PROFILE_WriteLine( hFile, buffer, len, encoding );
246 HeapFree(GetProcessHeap(), 0, buffer);
251 /***********************************************************************
254 * Free a profile tree.
256 static void PROFILE_Free( PROFILESECTION *section )
258 PROFILESECTION *next_section;
259 PROFILEKEY *key, *next_key;
261 for ( ; section; section = next_section)
263 for (key = section->key; key; key = next_key)
265 next_key = key->next;
266 HeapFree( GetProcessHeap(), 0, key->value );
267 HeapFree( GetProcessHeap(), 0, key );
269 next_section = section->next;
270 HeapFree( GetProcessHeap(), 0, section );
274 /* returns 1 if a character white space else 0 */
275 static inline int PROFILE_isspaceW(WCHAR c)
277 if (isspaceW(c)) return 1;
278 if (c=='\r' || c==0x1a) return 1;
279 /* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */
283 static inline ENCODING PROFILE_DetectTextEncoding(const void * buffer, int * len)
285 DWORD flags = IS_TEXT_UNICODE_SIGNATURE |
286 IS_TEXT_UNICODE_REVERSE_SIGNATURE |
287 IS_TEXT_UNICODE_ODD_LENGTH;
288 if (*len >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
290 *len = sizeof(bom_utf8);
291 return ENCODING_UTF8;
293 RtlIsTextUnicode((void *)buffer, *len, &flags);
294 if (flags & IS_TEXT_UNICODE_SIGNATURE)
296 *len = sizeof(WCHAR);
297 return ENCODING_UTF16LE;
299 if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
301 *len = sizeof(WCHAR);
302 return ENCODING_UTF16BE;
305 return ENCODING_ANSI;
308 static const WCHAR * PROFILE_GetLine(const WCHAR * szStart, const WCHAR * szEnd)
310 return memchrW(szStart, '\n', szEnd - szStart);
313 /***********************************************************************
316 * Load a profile tree from a file.
318 static PROFILESECTION *PROFILE_Load(HANDLE hFile, ENCODING * pEncoding)
322 const WCHAR *szLineStart, *szLineEnd;
323 const WCHAR *szValueStart, *szNameEnd, *szEnd;
325 PROFILESECTION *section, *first_section;
326 PROFILESECTION **next_section;
327 PROFILEKEY *key, *prev_key, **next_key;
330 TRACE("%p\n", hFile);
332 dwFileSize = GetFileSize(hFile, NULL);
333 if (dwFileSize == INVALID_FILE_SIZE)
336 pBuffer = HeapAlloc(GetProcessHeap(), 0 , dwFileSize);
337 if (!pBuffer) return NULL;
339 if (!ReadFile(hFile, pBuffer, dwFileSize, &dwFileSize, NULL))
341 HeapFree(GetProcessHeap(), 0, pBuffer);
342 WARN("Error %ld reading file\n", GetLastError());
346 *pEncoding = PROFILE_DetectTextEncoding(pBuffer, &len);
347 /* len is set to the number of bytes in the character marker.
348 * we want to skip these bytes */
349 pBuffer = (char *)pBuffer + len;
354 TRACE("ANSI encoding\n");
356 len = MultiByteToWideChar(CP_ACP, 0, (char *)pBuffer, dwFileSize, NULL, 0);
357 szFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
360 HeapFree(GetProcessHeap(), 0, pBuffer);
363 MultiByteToWideChar(CP_ACP, 0, (char *)pBuffer, dwFileSize, szFile, len);
364 szEnd = szFile + len;
367 TRACE("UTF8 encoding\n");
369 len = MultiByteToWideChar(CP_UTF8, 0, (char *)pBuffer, dwFileSize, NULL, 0);
370 szFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
373 HeapFree(GetProcessHeap(), 0, pBuffer);
376 MultiByteToWideChar(CP_UTF8, 0, (char *)pBuffer, dwFileSize, szFile, len);
377 szEnd = szFile + len;
379 case ENCODING_UTF16LE:
380 TRACE("UTF16 Little Endian encoding\n");
381 szFile = (WCHAR *)pBuffer + 1;
382 szEnd = (WCHAR *)((char *)pBuffer + dwFileSize);
384 case ENCODING_UTF16BE:
385 TRACE("UTF16 Big Endian encoding\n");
386 szFile = (WCHAR *)pBuffer + 1;
387 szEnd = (WCHAR *)((char *)pBuffer + dwFileSize);
388 PROFILE_ByteSwapShortBuffer(szFile, dwFileSize / sizeof(WCHAR));
391 FIXME("encoding type %d not implemented\n", *pEncoding);
392 HeapFree(GetProcessHeap(), 0, pBuffer);
396 first_section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) );
397 if(first_section == NULL)
399 if (szFile != pBuffer)
400 HeapFree(GetProcessHeap(), 0, szFile);
401 HeapFree(GetProcessHeap(), 0, pBuffer);
404 first_section->name[0] = 0;
405 first_section->key = NULL;
406 first_section->next = NULL;
407 next_section = &first_section->next;
408 next_key = &first_section->key;
410 szLineEnd = szFile - 1; /* will be increased to correct value in loop */
414 szLineStart = szLineEnd + 1;
415 if (szLineStart >= szEnd)
417 szLineEnd = PROFILE_GetLine(szLineStart, szEnd);
422 while (szLineStart < szLineEnd && PROFILE_isspaceW(*szLineStart)) szLineStart++;
424 if (szLineStart >= szLineEnd) continue;
426 if (*szLineStart == '[') /* section start */
428 const WCHAR * szSectionEnd;
429 if (!(szSectionEnd = memrchrW( szLineStart, ']', szLineEnd - szLineStart )))
431 WARN("Invalid section header at line %d: %s\n",
432 line, debugstr_wn(szLineStart, (int)(szLineEnd - szLineStart)) );
437 len = (int)(szSectionEnd - szLineStart);
438 /* no need to allocate +1 for NULL terminating character as
439 * already included in structure */
440 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) + len * sizeof(WCHAR) )))
442 memcpy(section->name, szLineStart, len * sizeof(WCHAR));
443 section->name[len] = '\0';
445 section->next = NULL;
446 *next_section = section;
447 next_section = §ion->next;
448 next_key = §ion->key;
451 TRACE("New section: %s\n", debugstr_w(section->name));
457 /* get rid of white space at the end of the line */
458 while ((szLineEnd > szLineStart) && ((*szLineEnd == '\n') || PROFILE_isspaceW(*szLineEnd))) szLineEnd--;
460 /* line end should be pointing to character *after* the last wanted character */
463 /* get rid of white space after the name and before the start
465 if ((szNameEnd = szValueStart = memchrW( szLineStart, '=', szLineEnd - szLineStart )) != NULL)
467 szNameEnd = szValueStart - 1;
468 while ((szNameEnd > szLineStart) && PROFILE_isspaceW(*szNameEnd)) szNameEnd--;
470 while (szValueStart < szLineEnd && PROFILE_isspaceW(*szValueStart)) szValueStart++;
473 szNameEnd = szLineEnd - 1;
474 /* name end should be pointing to character *after* the last wanted character */
477 len = (int)(szNameEnd - szLineStart);
479 if (len || !prev_key || *prev_key->name)
481 /* no need to allocate +1 for NULL terminating character as
482 * already included in structure */
483 if (!(key = HeapAlloc( GetProcessHeap(), 0, sizeof(*key) + len * sizeof(WCHAR) ))) break;
484 memcpy(key->name, szLineStart, len * sizeof(WCHAR));
485 key->name[len] = '\0';
488 len = (int)(szLineEnd - szValueStart);
489 key->value = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
490 memcpy(key->value, szValueStart, len * sizeof(WCHAR));
491 key->value[len] = '\0';
493 else key->value = NULL;
497 next_key = &key->next;
500 TRACE("New key: name=%s, value=%s\n",
501 debugstr_w(key->name), key->value ? debugstr_w(key->value) : "(none)");
504 if (szFile != pBuffer)
505 HeapFree(GetProcessHeap(), 0, szFile);
506 HeapFree(GetProcessHeap(), 0, pBuffer);
507 return first_section;
511 /***********************************************************************
512 * PROFILE_DeleteSection
514 * Delete a section from a profile tree.
516 static BOOL PROFILE_DeleteSection( PROFILESECTION **section, LPCWSTR name )
520 if ((*section)->name[0] && !strcmpiW( (*section)->name, name ))
522 PROFILESECTION *to_del = *section;
523 *section = to_del->next;
525 PROFILE_Free( to_del );
528 section = &(*section)->next;
534 /***********************************************************************
537 * Delete a key from a profile tree.
539 static BOOL PROFILE_DeleteKey( PROFILESECTION **section,
540 LPCWSTR section_name, LPCWSTR key_name )
544 if ((*section)->name[0] && !strcmpiW( (*section)->name, section_name ))
546 PROFILEKEY **key = &(*section)->key;
549 if (!strcmpiW( (*key)->name, key_name ))
551 PROFILEKEY *to_del = *key;
553 HeapFree( GetProcessHeap(), 0, to_del->value);
554 HeapFree( GetProcessHeap(), 0, to_del );
560 section = &(*section)->next;
566 /***********************************************************************
567 * PROFILE_DeleteAllKeys
569 * Delete all keys from a profile tree.
571 void PROFILE_DeleteAllKeys( LPCWSTR section_name)
573 PROFILESECTION **section= &CurProfile->section;
576 if ((*section)->name[0] && !strcmpiW( (*section)->name, section_name ))
578 PROFILEKEY **key = &(*section)->key;
581 PROFILEKEY *to_del = *key;
583 HeapFree( GetProcessHeap(), 0, to_del->value);
584 HeapFree( GetProcessHeap(), 0, to_del );
585 CurProfile->changed =TRUE;
588 section = &(*section)->next;
593 /***********************************************************************
596 * Find a key in a profile tree, optionally creating it.
598 static PROFILEKEY *PROFILE_Find( PROFILESECTION **section, LPCWSTR section_name,
599 LPCWSTR key_name, BOOL create, BOOL create_always )
604 while (PROFILE_isspaceW(*section_name)) section_name++;
605 p = section_name + strlenW(section_name) - 1;
606 while ((p > section_name) && PROFILE_isspaceW(*p)) p--;
607 seclen = p - section_name + 1;
609 while (PROFILE_isspaceW(*key_name)) key_name++;
610 p = key_name + strlenW(key_name) - 1;
611 while ((p > key_name) && PROFILE_isspaceW(*p)) p--;
612 keylen = p - key_name + 1;
616 if ( ((*section)->name[0])
617 && (!(strncmpiW( (*section)->name, section_name, seclen )))
618 && (((*section)->name)[seclen] == '\0') )
620 PROFILEKEY **key = &(*section)->key;
624 /* If create_always is FALSE then we check if the keyname
625 * already exists. Otherwise we add it regardless of its
626 * existence, to allow keys to be added more than once in
631 if ( (!(strncmpiW( (*key)->name, key_name, keylen )))
632 && (((*key)->name)[keylen] == '\0') )
637 if (!create) return NULL;
638 if (!(*key = HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY) + strlenW(key_name) * sizeof(WCHAR) )))
640 strcpyW( (*key)->name, key_name );
641 (*key)->value = NULL;
645 section = &(*section)->next;
647 if (!create) return NULL;
648 *section = HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION) + strlenW(section_name) * sizeof(WCHAR) );
649 if(*section == NULL) return NULL;
650 strcpyW( (*section)->name, section_name );
651 (*section)->next = NULL;
652 if (!((*section)->key = HeapAlloc( GetProcessHeap(), 0,
653 sizeof(PROFILEKEY) + strlenW(key_name) * sizeof(WCHAR) )))
655 HeapFree(GetProcessHeap(), 0, *section);
658 strcpyW( (*section)->key->name, key_name );
659 (*section)->key->value = NULL;
660 (*section)->key->next = NULL;
661 return (*section)->key;
665 /***********************************************************************
668 * Flush the current profile to disk if changed.
670 static BOOL PROFILE_FlushFile(void)
673 FILETIME LastWriteTime;
677 WARN("No current profile!\n");
681 if (!CurProfile->changed) return TRUE;
683 hFile = CreateFileW(CurProfile->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
685 if (hFile == INVALID_HANDLE_VALUE)
687 WARN("could not save profile file %s (error was %ld)\n", debugstr_w(CurProfile->filename), GetLastError());
691 TRACE("Saving %s\n", debugstr_w(CurProfile->filename));
692 PROFILE_Save( hFile, CurProfile->section, CurProfile->encoding );
693 if(GetFileTime(hFile, NULL, NULL, &LastWriteTime))
694 CurProfile->LastWriteTime=LastWriteTime;
695 CloseHandle( hFile );
696 CurProfile->changed = FALSE;
701 /***********************************************************************
702 * PROFILE_ReleaseFile
704 * Flush the current profile to disk and remove it from the cache.
706 static void PROFILE_ReleaseFile(void)
709 PROFILE_Free( CurProfile->section );
710 HeapFree( GetProcessHeap(), 0, CurProfile->filename );
711 CurProfile->changed = FALSE;
712 CurProfile->section = NULL;
713 CurProfile->filename = NULL;
714 CurProfile->encoding = ENCODING_ANSI;
715 ZeroMemory(&CurProfile->LastWriteTime, sizeof(CurProfile->LastWriteTime));
719 /***********************************************************************
722 * Open a profile file, checking the cached file first.
724 static BOOL PROFILE_Open( LPCWSTR filename )
726 WCHAR windirW[MAX_PATH];
727 WCHAR buffer[MAX_PATH];
728 HANDLE hFile = INVALID_HANDLE_VALUE;
729 FILETIME LastWriteTime;
731 PROFILE *tempProfile;
733 ZeroMemory(&LastWriteTime, sizeof(LastWriteTime));
735 /* First time around */
738 for(i=0;i<N_CACHED_PROFILES;i++)
740 MRUProfile[i]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE) );
741 if(MRUProfile[i] == NULL) break;
742 MRUProfile[i]->changed=FALSE;
743 MRUProfile[i]->section=NULL;
744 MRUProfile[i]->filename=NULL;
745 MRUProfile[i]->encoding=ENCODING_ANSI;
746 ZeroMemory(&MRUProfile[i]->LastWriteTime, sizeof(FILETIME));
749 GetWindowsDirectoryW( windirW, MAX_PATH );
751 if ((RtlDetermineDosPathNameType_U(filename) == RELATIVE_PATH) &&
752 !strchrW(filename, '\\') && !strchrW(filename, '/'))
754 static const WCHAR wszSeparator[] = {'\\', 0};
755 strcpyW(buffer, windirW);
756 strcatW(buffer, wszSeparator);
757 strcatW(buffer, filename);
762 GetFullPathNameW(filename, sizeof(buffer)/sizeof(buffer[0]), buffer, &dummy);
765 TRACE("path: %s\n", debugstr_w(buffer));
767 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
769 if ((hFile == INVALID_HANDLE_VALUE) && (GetLastError() != ERROR_FILE_NOT_FOUND))
771 WARN("Error %ld opening file %s\n", GetLastError(), debugstr_w(buffer));
775 for(i=0;i<N_CACHED_PROFILES;i++)
777 if ((MRUProfile[i]->filename && !strcmpiW( buffer, MRUProfile[i]->filename )))
779 TRACE("MRU Filename: %s, new filename: %s\n", debugstr_w(MRUProfile[i]->filename), debugstr_w(buffer));
783 tempProfile=MRUProfile[i];
785 MRUProfile[j]=MRUProfile[j-1];
786 CurProfile=tempProfile;
789 if (hFile != INVALID_HANDLE_VALUE)
791 if (TRACE_ON(profile))
793 GetFileTime(hFile, NULL, NULL, &LastWriteTime);
794 if (memcmp(&CurProfile->LastWriteTime, &LastWriteTime, sizeof(FILETIME)))
795 TRACE("(%s): already opened (mru=%d)\n",
796 debugstr_w(buffer), i);
798 TRACE("(%s): already opened, needs refreshing (mru=%d)\n",
799 debugstr_w(buffer), i);
803 else TRACE("(%s): already opened, not yet created (mru=%d)\n",
804 debugstr_w(buffer), i);
809 /* Flush the old current profile */
812 /* Make the oldest profile the current one only in order to get rid of it */
813 if(i==N_CACHED_PROFILES)
815 tempProfile=MRUProfile[N_CACHED_PROFILES-1];
816 for(i=N_CACHED_PROFILES-1;i>0;i--)
817 MRUProfile[i]=MRUProfile[i-1];
818 CurProfile=tempProfile;
820 if(CurProfile->filename) PROFILE_ReleaseFile();
822 /* OK, now that CurProfile is definitely free we assign it our new file */
823 CurProfile->filename = HeapAlloc( GetProcessHeap(), 0, (strlenW(buffer)+1) * sizeof(WCHAR) );
824 strcpyW( CurProfile->filename, buffer );
826 if (hFile != INVALID_HANDLE_VALUE)
828 CurProfile->section = PROFILE_Load(hFile, &CurProfile->encoding);
829 GetFileTime(hFile, NULL, NULL, &CurProfile->LastWriteTime);
834 /* Does not exist yet, we will create it in PROFILE_FlushFile */
835 WARN("profile file %s not found\n", debugstr_w(buffer) );
841 /***********************************************************************
844 * Returns all keys of a section.
845 * If return_values is TRUE, also include the corresponding values.
847 static INT PROFILE_GetSection( PROFILESECTION *section, LPCWSTR section_name,
848 LPWSTR buffer, UINT len, BOOL return_values )
852 if(!buffer) return 0;
854 TRACE("%s,%p,%u\n", debugstr_w(section_name), buffer, len);
858 if (section->name[0] && !strcmpiW( section->name, section_name ))
861 for (key = section->key; key; key = key->next)
864 if (!*key->name) continue; /* Skip empty lines */
865 if (IS_ENTRY_COMMENT(key->name)) continue; /* Skip comments */
866 PROFILE_CopyEntry( buffer, key->name, len - 1, 0 );
867 len -= strlenW(buffer) + 1;
868 buffer += strlenW(buffer) + 1;
871 if (return_values && key->value) {
873 PROFILE_CopyEntry ( buffer, key->value, len - 1, 0 );
874 len -= strlenW(buffer) + 1;
875 buffer += strlenW(buffer) + 1;
880 /*If either lpszSection or lpszKey is NULL and the supplied
881 destination buffer is too small to hold all the strings,
882 the last string is truncated and followed by two null characters.
883 In this case, the return value is equal to cchReturnBuffer
891 section = section->next;
893 buffer[0] = buffer[1] = '\0';
897 /* See GetPrivateProfileSectionNamesA for documentation */
898 static INT PROFILE_GetSectionNames( LPWSTR buffer, UINT len )
902 PROFILESECTION *section;
904 TRACE("(%p, %d)\n", buffer, len);
915 section = CurProfile->section;
916 while ((section!=NULL)) {
917 if (section->name[0]) {
918 l = strlenW(section->name)+1;
921 strncpyW(buf, section->name, f-1);
928 strcpyW(buf, section->name);
932 section = section->next;
939 /***********************************************************************
942 * Get a profile string.
944 * Tests with GetPrivateProfileString16, W95a,
945 * with filled buffer ("****...") and section "set1" and key_name "1" valid:
946 * section key_name def_val res buffer
947 * "set1" "1" "x" 43 [data]
948 * "set1" "1 " "x" 43 [data] (!)
949 * "set1" " 1 "' "x" 43 [data] (!)
950 * "set1" "" "x" 1 "x"
951 * "set1" "" "x " 1 "x" (!)
952 * "set1" "" " x " 3 " x" (!)
953 * "set1" NULL "x" 6 "1\02\03\0\0"
954 * "set1" "" "x" 1 "x"
955 * NULL "1" "x" 0 "" (!)
961 static INT PROFILE_GetString( LPCWSTR section, LPCWSTR key_name,
962 LPCWSTR def_val, LPWSTR buffer, UINT len )
964 PROFILEKEY *key = NULL;
965 static const WCHAR empty_strW[] = { 0 };
967 if(!buffer) return 0;
969 if (!def_val) def_val = empty_strW;
974 /* Win95 returns 0 on keyname "". Tested with Likse32 bon 000227 */
977 key = PROFILE_Find( &CurProfile->section, section, key_name, FALSE, FALSE);
978 PROFILE_CopyEntry( buffer, (key && key->value) ? key->value : def_val,
980 TRACE("(%s,%s,%s): returning %s\n",
981 debugstr_w(section), debugstr_w(key_name),
982 debugstr_w(def_val), debugstr_w(buffer) );
983 return strlenW( buffer );
985 /* no "else" here ! */
986 if (section && section[0])
988 INT ret = PROFILE_GetSection(CurProfile->section, section, buffer, len, FALSE);
989 if (!buffer[0]) /* no luck -> def_val */
991 PROFILE_CopyEntry(buffer, def_val, len, TRUE);
992 ret = strlenW(buffer);
1001 /***********************************************************************
1004 * Set a profile string.
1006 static BOOL PROFILE_SetString( LPCWSTR section_name, LPCWSTR key_name,
1007 LPCWSTR value, BOOL create_always )
1009 if (!key_name) /* Delete a whole section */
1011 TRACE("(%s)\n", debugstr_w(section_name));
1012 CurProfile->changed |= PROFILE_DeleteSection( &CurProfile->section,
1014 return TRUE; /* Even if PROFILE_DeleteSection() has failed,
1015 this is not an error on application's level.*/
1017 else if (!value) /* Delete a key */
1019 TRACE("(%s,%s)\n", debugstr_w(section_name), debugstr_w(key_name) );
1020 CurProfile->changed |= PROFILE_DeleteKey( &CurProfile->section,
1021 section_name, key_name );
1022 return TRUE; /* same error handling as above */
1024 else /* Set the key value */
1026 PROFILEKEY *key = PROFILE_Find(&CurProfile->section, section_name,
1027 key_name, TRUE, create_always );
1028 TRACE("(%s,%s,%s):\n",
1029 debugstr_w(section_name), debugstr_w(key_name), debugstr_w(value) );
1030 if (!key) return FALSE;
1032 /* strip the leading spaces. We can safely strip \n\r and
1033 * friends too, they should not happen here anyway. */
1034 while (PROFILE_isspaceW(*value)) value++;
1038 if (!strcmpW( key->value, value ))
1040 TRACE(" no change needed\n" );
1041 return TRUE; /* No change needed */
1043 TRACE(" replacing %s\n", debugstr_w(key->value) );
1044 HeapFree( GetProcessHeap(), 0, key->value );
1046 else TRACE(" creating key\n" );
1047 key->value = HeapAlloc( GetProcessHeap(), 0, (strlenW(value)+1) * sizeof(WCHAR) );
1048 strcpyW( key->value, value );
1049 CurProfile->changed = TRUE;
1055 /********************* API functions **********************************/
1058 /***********************************************************************
1059 * GetProfileIntA (KERNEL32.@)
1061 UINT WINAPI GetProfileIntA( LPCSTR section, LPCSTR entry, INT def_val )
1063 return GetPrivateProfileIntA( section, entry, def_val, "win.ini" );
1066 /***********************************************************************
1067 * GetProfileIntW (KERNEL32.@)
1069 UINT WINAPI GetProfileIntW( LPCWSTR section, LPCWSTR entry, INT def_val )
1071 return GetPrivateProfileIntW( section, entry, def_val, wininiW );
1075 * if allow_section_name_copy is TRUE, allow the copying :
1076 * - of Section names if 'section' is NULL
1077 * - of Keys in a Section if 'entry' is NULL
1078 * (see MSDN doc for GetPrivateProfileString)
1080 static int PROFILE_GetPrivateProfileString( LPCWSTR section, LPCWSTR entry,
1081 LPCWSTR def_val, LPWSTR buffer,
1082 UINT len, LPCWSTR filename,
1083 BOOL allow_section_name_copy )
1086 LPCWSTR pDefVal = NULL;
1091 TRACE("%s,%s,%s,%p,%u,%s\n", debugstr_w(section), debugstr_w(entry),
1092 debugstr_w(def_val), buffer, len, debugstr_w(filename));
1094 /* strip any trailing ' ' of def_val. */
1097 LPCWSTR p = &def_val[strlenW(def_val)]; /* even "" works ! */
1105 if (*p == ' ') /* ouch, contained trailing ' ' */
1107 int len = (int)(p - def_val);
1110 p = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1111 strncpyW(p, def_val, len);
1117 pDefVal = (LPCWSTR)def_val;
1119 RtlEnterCriticalSection( &PROFILE_CritSect );
1121 if (PROFILE_Open( filename )) {
1122 if ((allow_section_name_copy) && (section == NULL))
1123 ret = PROFILE_GetSectionNames(buffer, len);
1125 /* PROFILE_GetString already handles the 'entry == NULL' case */
1126 ret = PROFILE_GetString( section, entry, pDefVal, buffer, len );
1128 lstrcpynW( buffer, pDefVal, len );
1129 ret = strlenW( buffer );
1132 RtlLeaveCriticalSection( &PROFILE_CritSect );
1134 if (pDefVal != def_val) /* allocated */
1135 HeapFree(GetProcessHeap(), 0, (void*)pDefVal);
1137 TRACE("returning %s, %d\n", debugstr_w(buffer), ret);
1142 /***********************************************************************
1143 * GetPrivateProfileString (KERNEL.128)
1145 INT16 WINAPI GetPrivateProfileString16( LPCSTR section, LPCSTR entry,
1146 LPCSTR def_val, LPSTR buffer,
1147 UINT16 len, LPCSTR filename )
1149 UNICODE_STRING sectionW, entryW, def_valW, filenameW;
1151 INT16 retW, ret = 0;
1153 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
1154 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1155 else sectionW.Buffer = NULL;
1156 if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1157 else entryW.Buffer = NULL;
1158 if (def_val) RtlCreateUnicodeStringFromAsciiz(&def_valW, def_val);
1159 else def_valW.Buffer = NULL;
1160 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1161 else filenameW.Buffer = NULL;
1163 retW = PROFILE_GetPrivateProfileString( sectionW.Buffer, entryW.Buffer,
1164 def_valW.Buffer, bufferW, len,
1165 filenameW.Buffer, FALSE );
1168 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
1175 ret--; /* strip terminating 0 */
1178 RtlFreeUnicodeString(§ionW);
1179 RtlFreeUnicodeString(&entryW);
1180 RtlFreeUnicodeString(&def_valW);
1181 RtlFreeUnicodeString(&filenameW);
1182 HeapFree(GetProcessHeap(), 0, bufferW);
1186 /***********************************************************************
1187 * GetPrivateProfileStringA (KERNEL32.@)
1189 INT WINAPI GetPrivateProfileStringA( LPCSTR section, LPCSTR entry,
1190 LPCSTR def_val, LPSTR buffer,
1191 UINT len, LPCSTR filename )
1193 UNICODE_STRING sectionW, entryW, def_valW, filenameW;
1197 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
1198 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1199 else sectionW.Buffer = NULL;
1200 if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1201 else entryW.Buffer = NULL;
1202 if (def_val) RtlCreateUnicodeStringFromAsciiz(&def_valW, def_val);
1203 else def_valW.Buffer = NULL;
1204 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1205 else filenameW.Buffer = NULL;
1207 retW = GetPrivateProfileStringW( sectionW.Buffer, entryW.Buffer,
1208 def_valW.Buffer, bufferW, len,
1212 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
1219 ret--; /* strip terminating 0 */
1222 RtlFreeUnicodeString(§ionW);
1223 RtlFreeUnicodeString(&entryW);
1224 RtlFreeUnicodeString(&def_valW);
1225 RtlFreeUnicodeString(&filenameW);
1226 HeapFree(GetProcessHeap(), 0, bufferW);
1230 /***********************************************************************
1231 * GetPrivateProfileStringW (KERNEL32.@)
1233 INT WINAPI GetPrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
1234 LPCWSTR def_val, LPWSTR buffer,
1235 UINT len, LPCWSTR filename )
1237 TRACE("(%s, %s, %s, %p, %d, %s)\n", debugstr_w(section), debugstr_w(entry), debugstr_w(def_val), buffer, len, debugstr_w(filename));
1239 return PROFILE_GetPrivateProfileString( section, entry, def_val,
1240 buffer, len, filename, TRUE );
1243 /***********************************************************************
1244 * GetProfileStringA (KERNEL32.@)
1246 INT WINAPI GetProfileStringA( LPCSTR section, LPCSTR entry, LPCSTR def_val,
1247 LPSTR buffer, UINT len )
1249 return GetPrivateProfileStringA( section, entry, def_val,
1250 buffer, len, "win.ini" );
1253 /***********************************************************************
1254 * GetProfileStringW (KERNEL32.@)
1256 INT WINAPI GetProfileStringW( LPCWSTR section, LPCWSTR entry,
1257 LPCWSTR def_val, LPWSTR buffer, UINT len )
1259 return GetPrivateProfileStringW( section, entry, def_val,
1260 buffer, len, wininiW );
1263 /***********************************************************************
1264 * WriteProfileStringA (KERNEL32.@)
1266 BOOL WINAPI WriteProfileStringA( LPCSTR section, LPCSTR entry,
1269 return WritePrivateProfileStringA( section, entry, string, "win.ini" );
1272 /***********************************************************************
1273 * WriteProfileStringW (KERNEL32.@)
1275 BOOL WINAPI WriteProfileStringW( LPCWSTR section, LPCWSTR entry,
1278 return WritePrivateProfileStringW( section, entry, string, wininiW );
1282 /***********************************************************************
1283 * GetPrivateProfileIntW (KERNEL32.@)
1285 UINT WINAPI GetPrivateProfileIntW( LPCWSTR section, LPCWSTR entry,
1286 INT def_val, LPCWSTR filename )
1289 UNICODE_STRING bufferW;
1293 if (!(len = GetPrivateProfileStringW( section, entry, emptystringW,
1294 buffer, sizeof(buffer)/sizeof(WCHAR),
1298 if (len+1 == sizeof(buffer)/sizeof(WCHAR)) FIXME("result may be wrong!\n");
1300 /* FIXME: if entry can be found but it's empty, then Win16 is
1301 * supposed to return 0 instead of def_val ! Difficult/problematic
1302 * to implement (every other failure also returns zero buffer),
1303 * thus wait until testing framework avail for making sure nothing
1304 * else gets broken that way. */
1305 if (!buffer[0]) return (UINT)def_val;
1307 RtlInitUnicodeString( &bufferW, buffer );
1308 RtlUnicodeStringToInteger( &bufferW, 10, &result);
1312 /***********************************************************************
1313 * GetPrivateProfileIntA (KERNEL32.@)
1315 * FIXME: rewrite using unicode
1317 UINT WINAPI GetPrivateProfileIntA( LPCSTR section, LPCSTR entry,
1318 INT def_val, LPCSTR filename )
1320 UNICODE_STRING entryW, filenameW, sectionW;
1322 if(entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1323 else entryW.Buffer = NULL;
1324 if(filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1325 else filenameW.Buffer = NULL;
1326 if(section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1327 else sectionW.Buffer = NULL;
1328 res = GetPrivateProfileIntW(sectionW.Buffer, entryW.Buffer, def_val,
1330 RtlFreeUnicodeString(§ionW);
1331 RtlFreeUnicodeString(&filenameW);
1332 RtlFreeUnicodeString(&entryW);
1336 /***********************************************************************
1337 * GetPrivateProfileSectionW (KERNEL32.@)
1339 INT WINAPI GetPrivateProfileSectionW( LPCWSTR section, LPWSTR buffer,
1340 DWORD len, LPCWSTR filename )
1344 TRACE("(%s, %p, %ld, %s)\n", debugstr_w(section), buffer, len, debugstr_w(filename));
1346 RtlEnterCriticalSection( &PROFILE_CritSect );
1348 if (PROFILE_Open( filename ))
1349 ret = PROFILE_GetSection(CurProfile->section, section, buffer, len, TRUE);
1351 RtlLeaveCriticalSection( &PROFILE_CritSect );
1356 /***********************************************************************
1357 * GetPrivateProfileSectionA (KERNEL32.@)
1359 INT WINAPI GetPrivateProfileSectionA( LPCSTR section, LPSTR buffer,
1360 DWORD len, LPCSTR filename )
1362 UNICODE_STRING sectionW, filenameW;
1366 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
1367 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1368 else sectionW.Buffer = NULL;
1369 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1370 else filenameW.Buffer = NULL;
1372 retW = GetPrivateProfileSectionW(sectionW.Buffer, bufferW, len, filenameW.Buffer);
1375 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 2, buffer, len, NULL, NULL);
1391 RtlFreeUnicodeString(§ionW);
1392 RtlFreeUnicodeString(&filenameW);
1393 HeapFree(GetProcessHeap(), 0, bufferW);
1397 /***********************************************************************
1398 * GetProfileSectionA (KERNEL32.@)
1400 INT WINAPI GetProfileSectionA( LPCSTR section, LPSTR buffer, DWORD len )
1402 return GetPrivateProfileSectionA( section, buffer, len, "win.ini" );
1405 /***********************************************************************
1406 * GetProfileSectionW (KERNEL32.@)
1408 INT WINAPI GetProfileSectionW( LPCWSTR section, LPWSTR buffer, DWORD len )
1410 return GetPrivateProfileSectionW( section, buffer, len, wininiW );
1414 /***********************************************************************
1415 * WritePrivateProfileStringW (KERNEL32.@)
1417 BOOL WINAPI WritePrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
1418 LPCWSTR string, LPCWSTR filename )
1422 RtlEnterCriticalSection( &PROFILE_CritSect );
1424 if (PROFILE_Open( filename ))
1426 if (!section && !entry && !string) /* documented "file flush" case */
1428 PROFILE_FlushFile();
1429 PROFILE_ReleaseFile(); /* always return FALSE in this case */
1433 FIXME("(NULL?,%s,%s,%s)?\n",
1434 debugstr_w(entry), debugstr_w(string), debugstr_w(filename));
1436 ret = PROFILE_SetString( section, entry, string, FALSE);
1437 PROFILE_FlushFile();
1442 RtlLeaveCriticalSection( &PROFILE_CritSect );
1446 /***********************************************************************
1447 * WritePrivateProfileStringA (KERNEL32.@)
1449 BOOL WINAPI WritePrivateProfileStringA( LPCSTR section, LPCSTR entry,
1450 LPCSTR string, LPCSTR filename )
1452 UNICODE_STRING sectionW, entryW, stringW, filenameW;
1455 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1456 else sectionW.Buffer = NULL;
1457 if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1458 else entryW.Buffer = NULL;
1459 if (string) RtlCreateUnicodeStringFromAsciiz(&stringW, string);
1460 else stringW.Buffer = NULL;
1461 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1462 else filenameW.Buffer = NULL;
1464 ret = WritePrivateProfileStringW(sectionW.Buffer, entryW.Buffer,
1465 stringW.Buffer, filenameW.Buffer);
1466 RtlFreeUnicodeString(§ionW);
1467 RtlFreeUnicodeString(&entryW);
1468 RtlFreeUnicodeString(&stringW);
1469 RtlFreeUnicodeString(&filenameW);
1473 /***********************************************************************
1474 * WritePrivateProfileSectionW (KERNEL32.@)
1476 BOOL WINAPI WritePrivateProfileSectionW( LPCWSTR section,
1477 LPCWSTR string, LPCWSTR filename )
1482 RtlEnterCriticalSection( &PROFILE_CritSect );
1484 if (PROFILE_Open( filename )) {
1485 if (!section && !string)
1486 PROFILE_ReleaseFile(); /* always return FALSE in this case */
1487 else if (!string) {/* delete the named section*/
1488 ret = PROFILE_SetString(section,NULL,NULL, FALSE);
1489 PROFILE_FlushFile();
1491 PROFILE_DeleteAllKeys(section);
1494 LPWSTR buf = HeapAlloc( GetProcessHeap(), 0, (strlenW(string)+1) * sizeof(WCHAR) );
1495 strcpyW( buf, string );
1496 if((p = strchrW( buf, '='))) {
1498 ret = PROFILE_SetString( section, buf, p+1, TRUE);
1500 HeapFree( GetProcessHeap(), 0, buf );
1501 string += strlenW(string)+1;
1503 PROFILE_FlushFile();
1507 RtlLeaveCriticalSection( &PROFILE_CritSect );
1511 /***********************************************************************
1512 * WritePrivateProfileSectionA (KERNEL32.@)
1514 BOOL WINAPI WritePrivateProfileSectionA( LPCSTR section,
1515 LPCSTR string, LPCSTR filename)
1518 UNICODE_STRING sectionW, filenameW;
1527 while(*p) p += strlen(p) + 1;
1528 lenA = p - string + 1;
1529 lenW = MultiByteToWideChar(CP_ACP, 0, string, lenA, NULL, 0);
1530 if ((stringW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR))))
1531 MultiByteToWideChar(CP_ACP, 0, string, lenA, stringW, lenW);
1533 else stringW = NULL;
1534 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1535 else sectionW.Buffer = NULL;
1536 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1537 else filenameW.Buffer = NULL;
1539 ret = WritePrivateProfileSectionW(sectionW.Buffer, stringW, filenameW.Buffer);
1541 HeapFree(GetProcessHeap(), 0, stringW);
1542 RtlFreeUnicodeString(§ionW);
1543 RtlFreeUnicodeString(&filenameW);
1547 /***********************************************************************
1548 * WriteProfileSectionA (KERNEL32.@)
1550 BOOL WINAPI WriteProfileSectionA( LPCSTR section, LPCSTR keys_n_values)
1553 return WritePrivateProfileSectionA( section, keys_n_values, "win.ini");
1556 /***********************************************************************
1557 * WriteProfileSectionW (KERNEL32.@)
1559 BOOL WINAPI WriteProfileSectionW( LPCWSTR section, LPCWSTR keys_n_values)
1561 return WritePrivateProfileSectionW(section, keys_n_values, wininiW);
1565 /***********************************************************************
1566 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1568 * Returns the section names contained in the specified file.
1569 * FIXME: Where do we find this file when the path is relative?
1570 * The section names are returned as a list of strings with an extra
1571 * '\0' to mark the end of the list. Except for that the behavior
1572 * depends on the Windows version.
1575 * - if the buffer is 0 or 1 character long then it is as if it was of
1577 * - otherwise, if the buffer is to small only the section names that fit
1579 * - note that this means if the buffer was to small to return even just
1580 * the first section name then a single '\0' will be returned.
1581 * - the return value is the number of characters written in the buffer,
1582 * except if the buffer was too smal in which case len-2 is returned
1585 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1586 * '\0' and the return value is 0
1587 * - otherwise if the buffer is too small then the first section name that
1588 * does not fit is truncated so that the string list can be terminated
1589 * correctly (double '\0')
1590 * - the return value is the number of characters written in the buffer
1591 * except for the trailing '\0'. If the buffer is too small, then the
1592 * return value is len-2
1593 * - Win2000 has a bug that triggers when the section names and the
1594 * trailing '\0' fit exactly in the buffer. In that case the trailing
1597 * Wine implements the observed Win2000 behavior (except for the bug).
1599 * Note that when the buffer is big enough then the return value may be any
1600 * value between 1 and len-1 (or len in Win95), including len-2.
1602 DWORD WINAPI GetPrivateProfileSectionNamesW( LPWSTR buffer, DWORD size,
1607 RtlEnterCriticalSection( &PROFILE_CritSect );
1609 if (PROFILE_Open( filename ))
1610 ret = PROFILE_GetSectionNames(buffer, size);
1612 RtlLeaveCriticalSection( &PROFILE_CritSect );
1618 /***********************************************************************
1619 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1621 DWORD WINAPI GetPrivateProfileSectionNamesA( LPSTR buffer, DWORD size,
1624 UNICODE_STRING filenameW;
1628 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR)) : NULL;
1629 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1630 else filenameW.Buffer = NULL;
1632 retW = GetPrivateProfileSectionNamesW(bufferW, size, filenameW.Buffer);
1635 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW, buffer, size, NULL, NULL);
1643 RtlFreeUnicodeString(&filenameW);
1644 HeapFree(GetProcessHeap(), 0, bufferW);
1648 /***********************************************************************
1649 * GetPrivateProfileStructW (KERNEL32.@)
1651 * Should match Win95's behaviour pretty much
1653 BOOL WINAPI GetPrivateProfileStructW (LPCWSTR section, LPCWSTR key,
1654 LPVOID buf, UINT len, LPCWSTR filename)
1658 RtlEnterCriticalSection( &PROFILE_CritSect );
1660 if (PROFILE_Open( filename )) {
1661 PROFILEKEY *k = PROFILE_Find ( &CurProfile->section, section, key, FALSE, FALSE);
1663 TRACE("value (at %p): %s\n", k->value, debugstr_w(k->value));
1664 if (((strlenW(k->value) - 2) / 2) == len)
1671 end = k->value + strlenW(k->value); /* -> '\0' */
1672 /* check for invalid chars in ASCII coded hex string */
1673 for (p=k->value; p < end; p++)
1677 WARN("invalid char '%x' in file %s->[%s]->%s !\n",
1678 *p, debugstr_w(filename), debugstr_w(section), debugstr_w(key));
1685 BOOL highnibble = TRUE;
1687 LPBYTE binbuf = (LPBYTE)buf;
1689 end -= 2; /* don't include checksum in output data */
1690 /* translate ASCII hex format into binary data */
1691 for (p=k->value; p < end; p++)
1695 (c - 'A' + 10) : (c - '0');
1702 *binbuf++ = b; /* feed binary data into output */
1703 chksum += b; /* calculate checksum */
1705 highnibble ^= 1; /* toggle */
1707 /* retrieve stored checksum value */
1709 b = ( (c > '9') ? (c - 'A' + 10) : (c - '0') ) << 4;
1711 b += (c > '9') ? (c - 'A' + 10) : (c - '0');
1712 if (b == (chksum & 0xff)) /* checksums match ? */
1718 RtlLeaveCriticalSection( &PROFILE_CritSect );
1723 /***********************************************************************
1724 * GetPrivateProfileStructA (KERNEL32.@)
1726 BOOL WINAPI GetPrivateProfileStructA (LPCSTR section, LPCSTR key,
1727 LPVOID buffer, UINT len, LPCSTR filename)
1729 UNICODE_STRING sectionW, keyW, filenameW;
1732 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1733 else sectionW.Buffer = NULL;
1734 if (key) RtlCreateUnicodeStringFromAsciiz(&keyW, key);
1735 else keyW.Buffer = NULL;
1736 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1737 else filenameW.Buffer = NULL;
1739 ret = GetPrivateProfileStructW(sectionW.Buffer, keyW.Buffer, buffer, len,
1741 /* Do not translate binary data. */
1743 RtlFreeUnicodeString(§ionW);
1744 RtlFreeUnicodeString(&keyW);
1745 RtlFreeUnicodeString(&filenameW);
1751 /***********************************************************************
1752 * WritePrivateProfileStructW (KERNEL32.@)
1754 BOOL WINAPI WritePrivateProfileStructW (LPCWSTR section, LPCWSTR key,
1755 LPVOID buf, UINT bufsize, LPCWSTR filename)
1759 LPWSTR outstring, p;
1762 if (!section && !key && !buf) /* flush the cache */
1763 return WritePrivateProfileStringW( NULL, NULL, NULL, filename );
1765 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1766 outstring = HeapAlloc( GetProcessHeap(), 0, (bufsize*2 + 2 + 1) * sizeof(WCHAR) );
1768 for (binbuf = (LPBYTE)buf; binbuf < (LPBYTE)buf+bufsize; binbuf++) {
1769 *p++ = hex[*binbuf >> 4];
1770 *p++ = hex[*binbuf & 0xf];
1773 /* checksum is sum & 0xff */
1774 *p++ = hex[(sum & 0xf0) >> 4];
1775 *p++ = hex[sum & 0xf];
1778 RtlEnterCriticalSection( &PROFILE_CritSect );
1780 if (PROFILE_Open( filename )) {
1781 ret = PROFILE_SetString( section, key, outstring, FALSE);
1782 PROFILE_FlushFile();
1785 RtlLeaveCriticalSection( &PROFILE_CritSect );
1787 HeapFree( GetProcessHeap(), 0, outstring );
1792 /***********************************************************************
1793 * WritePrivateProfileStructA (KERNEL32.@)
1795 BOOL WINAPI WritePrivateProfileStructA (LPCSTR section, LPCSTR key,
1796 LPVOID buf, UINT bufsize, LPCSTR filename)
1798 UNICODE_STRING sectionW, keyW, filenameW;
1801 if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
1802 else sectionW.Buffer = NULL;
1803 if (key) RtlCreateUnicodeStringFromAsciiz(&keyW, key);
1804 else keyW.Buffer = NULL;
1805 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1806 else filenameW.Buffer = NULL;
1808 /* Do not translate binary data. */
1809 ret = WritePrivateProfileStructW(sectionW.Buffer, keyW.Buffer, buf, bufsize,
1812 RtlFreeUnicodeString(§ionW);
1813 RtlFreeUnicodeString(&keyW);
1814 RtlFreeUnicodeString(&filenameW);
1819 /***********************************************************************
1820 * WriteOutProfiles (KERNEL.315)
1822 void WINAPI WriteOutProfiles16(void)
1824 RtlEnterCriticalSection( &PROFILE_CritSect );
1825 PROFILE_FlushFile();
1826 RtlLeaveCriticalSection( &PROFILE_CritSect );
1829 /***********************************************************************
1830 * CloseProfileUserMapping (KERNEL32.@)
1832 BOOL WINAPI CloseProfileUserMapping(void) {
1833 FIXME("(), stub!\n");
1834 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);