Added line wrapping for a FIXME.
[wine/multimedia.git] / files / profile.c
blob77eca2c0085cf82029a6176e0f012afa2277cafc
1 /*
2 * Profile functions
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winnls.h"
39 #include "winerror.h"
40 #include "winternl.h"
41 #include "wine/winbase16.h"
42 #include "drive.h"
43 #include "file.h"
44 #include "heap.h"
45 #include "wine/unicode.h"
46 #include "wine/server.h"
47 #include "wine/library.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(profile);
52 typedef struct tagPROFILEKEY
54 WCHAR *value;
55 struct tagPROFILEKEY *next;
56 WCHAR name[1];
57 } PROFILEKEY;
59 typedef struct tagPROFILESECTION
61 struct tagPROFILEKEY *key;
62 struct tagPROFILESECTION *next;
63 WCHAR name[1];
64 } PROFILESECTION;
67 typedef struct
69 BOOL changed;
70 PROFILESECTION *section;
71 WCHAR *dos_name;
72 char *unix_name;
73 WCHAR *filename;
74 time_t mtime;
75 } PROFILE;
78 #define N_CACHED_PROFILES 10
80 /* Cached profile files */
81 static PROFILE *MRUProfile[N_CACHED_PROFILES]={NULL};
83 #define CurProfile (MRUProfile[0])
85 #define PROFILE_MAX_LINE_LEN 1024
87 /* Check for comments in profile */
88 #define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
90 static const WCHAR emptystringW[] = {0};
91 static const WCHAR wininiW[] = { 'w','i','n','.','i','n','i',0 };
93 static CRITICAL_SECTION PROFILE_CritSect = CRITICAL_SECTION_INIT("PROFILE_CritSect");
95 static const char hex[16] = "0123456789ABCDEF";
97 /***********************************************************************
98 * PROFILE_CopyEntry
100 * Copy the content of an entry into a buffer, removing quotes, and possibly
101 * translating environment variables.
103 static void PROFILE_CopyEntry( LPWSTR buffer, LPCWSTR value, int len,
104 int handle_env, BOOL strip_quote )
106 WCHAR quote = '\0';
107 LPCWSTR p;
109 if(!buffer) return;
111 if (strip_quote && ((*value == '\'') || (*value == '\"')))
113 if (value[1] && (value[strlenW(value)-1] == *value)) quote = *value++;
116 if (!handle_env)
118 lstrcpynW( buffer, value, len );
119 if (quote && (len >= strlenW(value))) buffer[strlenW(buffer)-1] = '\0';
120 return;
123 p = value;
124 while (*p && (len > 1))
126 if ((*p == '$') && (p[1] == '{'))
128 WCHAR env_val[1024];
129 LPCWSTR p2 = strchrW( p, '}' );
130 int copy_len;
131 if (!p2) continue; /* ignore it */
132 copy_len = min( 1024, (int)(p2-p)-1 );
133 strncpyW(env_val, p + 2, copy_len );
134 env_val[copy_len - 1] = 0; /* ensure 0 termination */
135 *buffer = 0;
136 if (GetEnvironmentVariableW( env_val, buffer, len))
138 copy_len = strlenW( buffer );
139 buffer += copy_len;
140 len -= copy_len;
142 p = p2 + 1;
144 else
146 *buffer++ = *p++;
147 len--;
150 if (quote && (len > 1)) buffer--;
151 *buffer = '\0';
155 /***********************************************************************
156 * PROFILE_Save
158 * Save a profile tree to a file.
160 static void PROFILE_Save( FILE *file, PROFILESECTION *section )
162 PROFILEKEY *key;
163 char buffer[PROFILE_MAX_LINE_LEN];
165 for ( ; section; section = section->next)
167 if (section->name[0])
169 WideCharToMultiByte(CP_ACP, 0, section->name, -1, buffer, sizeof(buffer), NULL, NULL);
170 fprintf( file, "\r\n[%s]\r\n", buffer );
172 for (key = section->key; key; key = key->next)
174 WideCharToMultiByte(CP_ACP, 0, key->name, -1, buffer, sizeof(buffer), NULL, NULL);
175 fprintf( file, "%s", buffer );
176 if (key->value)
178 WideCharToMultiByte(CP_ACP, 0, key->value, -1, buffer, sizeof(buffer), NULL, NULL);
179 fprintf( file, "=%s", buffer );
181 fprintf( file, "\r\n" );
187 /***********************************************************************
188 * PROFILE_Free
190 * Free a profile tree.
192 static void PROFILE_Free( PROFILESECTION *section )
194 PROFILESECTION *next_section;
195 PROFILEKEY *key, *next_key;
197 for ( ; section; section = next_section)
199 for (key = section->key; key; key = next_key)
201 next_key = key->next;
202 if (key->value) HeapFree( GetProcessHeap(), 0, key->value );
203 HeapFree( GetProcessHeap(), 0, key );
205 next_section = section->next;
206 HeapFree( GetProcessHeap(), 0, section );
210 static inline int PROFILE_isspace(char c)
212 if (isspace(c)) return 1;
213 if (c=='\r' || c==0x1a) return 1;
214 /* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */
215 return 0;
219 /***********************************************************************
220 * PROFILE_Load
222 * Load a profile tree from a file.
224 static PROFILESECTION *PROFILE_Load( FILE *file )
226 char buffer[PROFILE_MAX_LINE_LEN];
227 char *p, *p2;
228 int line = 0, len;
229 PROFILESECTION *section, *first_section;
230 PROFILESECTION **next_section;
231 PROFILEKEY *key, *prev_key, **next_key;
233 first_section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) );
234 if(first_section == NULL) return NULL;
235 first_section->name[0] = 0;
236 first_section->key = NULL;
237 first_section->next = NULL;
238 next_section = &first_section->next;
239 next_key = &first_section->key;
240 prev_key = NULL;
242 while (fgets( buffer, PROFILE_MAX_LINE_LEN, file ))
244 line++;
245 p = buffer;
246 while (*p && PROFILE_isspace(*p)) p++;
247 if (*p == '[') /* section start */
249 if (!(p2 = strrchr( p, ']' )))
251 WARN("Invalid section header at line %d: '%s'\n",
252 line, p );
254 else
256 *p2 = '\0';
257 p++;
258 len = strlen(p);
259 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) + len * sizeof(WCHAR) )))
260 break;
261 MultiByteToWideChar(CP_ACP, 0, p, -1, section->name, len + 1);
262 section->key = NULL;
263 section->next = NULL;
264 *next_section = section;
265 next_section = &section->next;
266 next_key = &section->key;
267 prev_key = NULL;
269 TRACE("New section: %s\n", debugstr_w(section->name));
271 continue;
275 p2=p+strlen(p) - 1;
276 while ((p2 > p) && ((*p2 == '\n') || PROFILE_isspace(*p2))) *p2--='\0';
278 if ((p2 = strchr( p, '=' )) != NULL)
280 char *p3 = p2 - 1;
281 while ((p3 > p) && PROFILE_isspace(*p3)) *p3-- = '\0';
282 *p2++ = '\0';
283 while (*p2 && PROFILE_isspace(*p2)) p2++;
286 if(*p || !prev_key || *prev_key->name)
288 len = strlen(p);
289 if (!(key = HeapAlloc( GetProcessHeap(), 0, sizeof(*key) + len * sizeof(WCHAR) ))) break;
290 MultiByteToWideChar(CP_ACP, 0, p, -1, key->name, len + 1);
291 if (p2)
293 len = strlen(p2) + 1;
294 key->value = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
295 MultiByteToWideChar(CP_ACP, 0, p2, -1, key->value, len);
297 else key->value = NULL;
299 key->next = NULL;
300 *next_key = key;
301 next_key = &key->next;
302 prev_key = key;
304 TRACE("New key: name=%s, value=%s\n",
305 debugstr_w(key->name), key->value ? debugstr_w(key->value) : "(none)");
308 return first_section;
312 /***********************************************************************
313 * PROFILE_DeleteSection
315 * Delete a section from a profile tree.
317 static BOOL PROFILE_DeleteSection( PROFILESECTION **section, LPCWSTR name )
319 while (*section)
321 if ((*section)->name[0] && !strcmpiW( (*section)->name, name ))
323 PROFILESECTION *to_del = *section;
324 *section = to_del->next;
325 to_del->next = NULL;
326 PROFILE_Free( to_del );
327 return TRUE;
329 section = &(*section)->next;
331 return FALSE;
335 /***********************************************************************
336 * PROFILE_DeleteKey
338 * Delete a key from a profile tree.
340 static BOOL PROFILE_DeleteKey( PROFILESECTION **section,
341 LPCWSTR section_name, LPCWSTR key_name )
343 while (*section)
345 if ((*section)->name[0] && !strcmpiW( (*section)->name, section_name ))
347 PROFILEKEY **key = &(*section)->key;
348 while (*key)
350 if (!strcmpiW( (*key)->name, key_name ))
352 PROFILEKEY *to_del = *key;
353 *key = to_del->next;
354 if (to_del->value) HeapFree( GetProcessHeap(), 0, to_del->value);
355 HeapFree( GetProcessHeap(), 0, to_del );
356 return TRUE;
358 key = &(*key)->next;
361 section = &(*section)->next;
363 return FALSE;
367 /***********************************************************************
368 * PROFILE_DeleteAllKeys
370 * Delete all keys from a profile tree.
372 void PROFILE_DeleteAllKeys( LPCWSTR section_name)
374 PROFILESECTION **section= &CurProfile->section;
375 while (*section)
377 if ((*section)->name[0] && !strcmpiW( (*section)->name, section_name ))
379 PROFILEKEY **key = &(*section)->key;
380 while (*key)
382 PROFILEKEY *to_del = *key;
383 *key = to_del->next;
384 if (to_del->value) HeapFree( GetProcessHeap(), 0, to_del->value);
385 HeapFree( GetProcessHeap(), 0, to_del );
386 CurProfile->changed =TRUE;
389 section = &(*section)->next;
394 /***********************************************************************
395 * PROFILE_Find
397 * Find a key in a profile tree, optionally creating it.
399 static PROFILEKEY *PROFILE_Find( PROFILESECTION **section, LPCWSTR section_name,
400 LPCWSTR key_name, BOOL create, BOOL create_always )
402 LPCWSTR p;
403 int seclen, keylen;
405 while (PROFILE_isspace(*section_name)) section_name++;
406 p = section_name + strlenW(section_name) - 1;
407 while ((p > section_name) && PROFILE_isspace(*p)) p--;
408 seclen = p - section_name + 1;
410 while (PROFILE_isspace(*key_name)) key_name++;
411 p = key_name + strlenW(key_name) - 1;
412 while ((p > key_name) && PROFILE_isspace(*p)) p--;
413 keylen = p - key_name + 1;
415 while (*section)
417 if ( ((*section)->name[0])
418 && (!(strncmpiW( (*section)->name, section_name, seclen )))
419 && (((*section)->name)[seclen] == '\0') )
421 PROFILEKEY **key = &(*section)->key;
423 while (*key)
425 /* If create_always is FALSE then we check if the keyname
426 * already exists. Otherwise we add it regardless of its
427 * existence, to allow keys to be added more than once in
428 * some cases.
430 if(!create_always)
432 if ( (!(strncmpiW( (*key)->name, key_name, keylen )))
433 && (((*key)->name)[keylen] == '\0') )
434 return *key;
436 key = &(*key)->next;
438 if (!create) return NULL;
439 if (!(*key = HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY) + strlenW(key_name) * sizeof(WCHAR) )))
440 return NULL;
441 strcpyW( (*key)->name, key_name );
442 (*key)->value = NULL;
443 (*key)->next = NULL;
444 return *key;
446 section = &(*section)->next;
448 if (!create) return NULL;
449 *section = HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION) + strlenW(section_name) * sizeof(WCHAR) );
450 if(*section == NULL) return NULL;
451 strcpyW( (*section)->name, section_name );
452 (*section)->next = NULL;
453 if (!((*section)->key = HeapAlloc( GetProcessHeap(), 0,
454 sizeof(PROFILEKEY) + strlenW(key_name) * sizeof(WCHAR) )))
456 HeapFree(GetProcessHeap(), 0, *section);
457 return NULL;
459 strcpyW( (*section)->key->name, key_name );
460 (*section)->key->value = NULL;
461 (*section)->key->next = NULL;
462 return (*section)->key;
466 /***********************************************************************
467 * PROFILE_FlushFile
469 * Flush the current profile to disk if changed.
471 static BOOL PROFILE_FlushFile(void)
473 char *p, buffer[MAX_PATHNAME_LEN];
474 const char *unix_name;
475 FILE *file = NULL;
476 struct stat buf;
478 if(!CurProfile)
480 WARN("No current profile!\n");
481 return FALSE;
484 if (!CurProfile->changed || !CurProfile->dos_name) return TRUE;
485 if (!(unix_name = CurProfile->unix_name) || !(file = fopen(unix_name, "w")))
487 int drive = toupperW(CurProfile->dos_name[0]) - 'A';
488 WCHAR *name, *name_lwr;
489 /* Try to create it in $HOME/.wine */
490 /* FIXME: this will need a more general solution */
491 strcpy( buffer, wine_get_config_dir() );
492 p = buffer + strlen(buffer);
493 *p++ = '/';
494 *p = 0; /* make strlen() below happy */
495 name = strrchrW( CurProfile->dos_name, '\\' ) + 1;
497 /* create a lower cased version of the name */
498 name_lwr = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1) * sizeof(WCHAR));
499 strcpyW(name_lwr, name);
500 strlwrW(name_lwr);
501 WideCharToMultiByte(DRIVE_GetCodepage(drive), 0, name_lwr, -1,
502 p, sizeof(buffer) - strlen(buffer), NULL, NULL);
503 HeapFree(GetProcessHeap(), 0, name_lwr);
505 file = fopen( buffer, "w" );
506 unix_name = buffer;
509 if (!file)
511 WARN("could not save profile file %s\n", debugstr_w(CurProfile->dos_name));
512 return FALSE;
515 TRACE("Saving %s into '%s'\n", debugstr_w(CurProfile->dos_name), unix_name );
516 PROFILE_Save( file, CurProfile->section );
517 fclose( file );
518 CurProfile->changed = FALSE;
519 if(!stat(unix_name,&buf))
520 CurProfile->mtime=buf.st_mtime;
521 return TRUE;
525 /***********************************************************************
526 * PROFILE_ReleaseFile
528 * Flush the current profile to disk and remove it from the cache.
530 static void PROFILE_ReleaseFile(void)
532 PROFILE_FlushFile();
533 PROFILE_Free( CurProfile->section );
534 if (CurProfile->dos_name) HeapFree( GetProcessHeap(), 0, CurProfile->dos_name );
535 if (CurProfile->unix_name) HeapFree( GetProcessHeap(), 0, CurProfile->unix_name );
536 if (CurProfile->filename) HeapFree( GetProcessHeap(), 0, CurProfile->filename );
537 CurProfile->changed = FALSE;
538 CurProfile->section = NULL;
539 CurProfile->dos_name = NULL;
540 CurProfile->unix_name = NULL;
541 CurProfile->filename = NULL;
542 CurProfile->mtime = 0;
546 /***********************************************************************
547 * PROFILE_Open
549 * Open a profile file, checking the cached file first.
551 static BOOL PROFILE_Open( LPCWSTR filename )
553 DOS_FULL_NAME full_name;
554 char buffer[MAX_PATHNAME_LEN];
555 WCHAR *newdos_name;
556 WCHAR *name, *name_lwr;
557 char *p;
558 FILE *file = NULL;
559 int i,j;
560 struct stat buf;
561 PROFILE *tempProfile;
563 /* First time around */
565 if(!CurProfile)
566 for(i=0;i<N_CACHED_PROFILES;i++)
568 MRUProfile[i]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE) );
569 if(MRUProfile[i] == NULL) break;
570 MRUProfile[i]->changed=FALSE;
571 MRUProfile[i]->section=NULL;
572 MRUProfile[i]->dos_name=NULL;
573 MRUProfile[i]->unix_name=NULL;
574 MRUProfile[i]->filename=NULL;
575 MRUProfile[i]->mtime=0;
578 /* Check for a match */
580 if (strchrW( filename, '/' ) || strchrW( filename, '\\' ) ||
581 strchrW( filename, ':' ))
583 if (!DOSFS_GetFullName( filename, FALSE, &full_name )) return FALSE;
585 else
587 static const WCHAR bkslashW[] = {'\\',0};
588 WCHAR windirW[MAX_PATH];
590 GetWindowsDirectoryW( windirW, MAX_PATH );
591 strcatW( windirW, bkslashW );
592 strcatW( windirW, filename );
593 if (!DOSFS_GetFullName( windirW, FALSE, &full_name )) return FALSE;
596 for(i=0;i<N_CACHED_PROFILES;i++)
598 if ((MRUProfile[i]->filename && !strcmpW( filename, MRUProfile[i]->filename )) ||
599 (MRUProfile[i]->dos_name && !strcmpW( full_name.short_name, MRUProfile[i]->dos_name )))
601 if(i)
603 PROFILE_FlushFile();
604 tempProfile=MRUProfile[i];
605 for(j=i;j>0;j--)
606 MRUProfile[j]=MRUProfile[j-1];
607 CurProfile=tempProfile;
609 if(!stat(CurProfile->unix_name,&buf) && CurProfile->mtime==buf.st_mtime)
610 TRACE("(%s): already opened (mru=%d)\n",
611 debugstr_w(filename), i );
612 else
613 TRACE("(%s): already opened, needs refreshing (mru=%d)\n",
614 debugstr_w(filename), i );
615 return TRUE;
619 /* Flush the old current profile */
620 PROFILE_FlushFile();
622 /* Make the oldest profile the current one only in order to get rid of it */
623 if(i==N_CACHED_PROFILES)
625 tempProfile=MRUProfile[N_CACHED_PROFILES-1];
626 for(i=N_CACHED_PROFILES-1;i>0;i--)
627 MRUProfile[i]=MRUProfile[i-1];
628 CurProfile=tempProfile;
630 if(CurProfile->filename) PROFILE_ReleaseFile();
632 /* OK, now that CurProfile is definitely free we assign it our new file */
633 newdos_name = HeapAlloc( GetProcessHeap(), 0, (strlenW(full_name.short_name)+1) * sizeof(WCHAR) );
634 strcpyW( newdos_name, full_name.short_name );
635 CurProfile->dos_name = newdos_name;
636 CurProfile->filename = HeapAlloc( GetProcessHeap(), 0, (strlenW(filename)+1) * sizeof(WCHAR) );
637 strcpyW( CurProfile->filename, filename );
639 /* Try to open the profile file, first in $HOME/.wine */
641 /* FIXME: this will need a more general solution */
642 strcpy( buffer, wine_get_config_dir() );
643 p = buffer + strlen(buffer);
644 *p++ = '/';
645 *p = 0; /* make strlen() below happy */
646 name = strrchrW( newdos_name, '\\' ) + 1;
648 /* create a lower cased version of the name */
649 name_lwr = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1) * sizeof(WCHAR));
650 strcpyW(name_lwr, name);
651 strlwrW(name_lwr);
652 WideCharToMultiByte(DRIVE_GetCodepage(full_name.drive), 0, name_lwr, -1,
653 p, sizeof(buffer) - strlen(buffer), NULL, NULL);
654 HeapFree(GetProcessHeap(), 0, name_lwr);
656 if ((file = fopen( buffer, "r" )))
658 TRACE("(%s): found it in %s\n", debugstr_w(filename), buffer );
659 CurProfile->unix_name = HeapAlloc( GetProcessHeap(), 0, strlen(buffer)+1 );
660 strcpy( CurProfile->unix_name, buffer );
662 else
664 CurProfile->unix_name = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
665 strcpy( CurProfile->unix_name, full_name.long_name );
666 if ((file = fopen( full_name.long_name, "r" )))
667 TRACE("(%s): found it in %s\n",
668 debugstr_w(filename), full_name.long_name );
671 if (file)
673 CurProfile->section = PROFILE_Load( file );
674 fclose( file );
675 if(!stat(CurProfile->unix_name,&buf))
676 CurProfile->mtime=buf.st_mtime;
678 else
680 /* Does not exist yet, we will create it in PROFILE_FlushFile */
681 WARN("profile file %s not found\n", debugstr_w(newdos_name) );
683 return TRUE;
687 /***********************************************************************
688 * PROFILE_GetSection
690 * Returns all keys of a section.
691 * If return_values is TRUE, also include the corresponding values.
693 static INT PROFILE_GetSection( PROFILESECTION *section, LPCWSTR section_name,
694 LPWSTR buffer, UINT len, BOOL handle_env,
695 BOOL return_values )
697 PROFILEKEY *key;
699 if(!buffer) return 0;
701 TRACE("%s,%p,%u\n", debugstr_w(section_name), buffer, len);
703 while (section)
705 if (section->name[0] && !strcmpiW( section->name, section_name ))
707 UINT oldlen = len;
708 for (key = section->key; key; key = key->next)
710 if (len <= 2) break;
711 if (!*key->name) continue; /* Skip empty lines */
712 if (IS_ENTRY_COMMENT(key->name)) continue; /* Skip comments */
713 PROFILE_CopyEntry( buffer, key->name, len - 1, handle_env, 0 );
714 len -= strlenW(buffer) + 1;
715 buffer += strlenW(buffer) + 1;
716 if (len < 2)
717 break;
718 if (return_values && key->value) {
719 buffer[-1] = '=';
720 PROFILE_CopyEntry ( buffer,
721 key->value, len - 1, handle_env, 0 );
722 len -= strlenW(buffer) + 1;
723 buffer += strlenW(buffer) + 1;
726 *buffer = '\0';
727 if (len <= 1)
728 /*If either lpszSection or lpszKey is NULL and the supplied
729 destination buffer is too small to hold all the strings,
730 the last string is truncated and followed by two null characters.
731 In this case, the return value is equal to cchReturnBuffer
732 minus two. */
734 buffer[-1] = '\0';
735 return oldlen - 2;
737 return oldlen - len;
739 section = section->next;
741 buffer[0] = buffer[1] = '\0';
742 return 0;
745 /* See GetPrivateProfileSectionNamesA for documentation */
746 static INT PROFILE_GetSectionNames( LPWSTR buffer, UINT len )
748 LPWSTR buf;
749 UINT f,l;
750 PROFILESECTION *section;
752 if (!buffer || !len)
753 return 0;
754 if (len==1) {
755 *buffer='\0';
756 return 0;
759 f=len-1;
760 buf=buffer;
761 section = CurProfile->section;
762 while ((section!=NULL)) {
763 if (section->name[0]) {
764 l = strlenW(section->name)+1;
765 if (l > f) {
766 if (f>0) {
767 strncpyW(buf, section->name, f-1);
768 buf += f-1;
769 *buf++='\0';
771 *buf='\0';
772 return len-2;
774 strcpyW(buf, section->name);
775 buf += l;
776 f -= l;
778 section = section->next;
780 *buf='\0';
781 return buf-buffer;
785 /***********************************************************************
786 * PROFILE_GetString
788 * Get a profile string.
790 * Tests with GetPrivateProfileString16, W95a,
791 * with filled buffer ("****...") and section "set1" and key_name "1" valid:
792 * section key_name def_val res buffer
793 * "set1" "1" "x" 43 [data]
794 * "set1" "1 " "x" 43 [data] (!)
795 * "set1" " 1 "' "x" 43 [data] (!)
796 * "set1" "" "x" 1 "x"
797 * "set1" "" "x " 1 "x" (!)
798 * "set1" "" " x " 3 " x" (!)
799 * "set1" NULL "x" 6 "1\02\03\0\0"
800 * "set1" "" "x" 1 "x"
801 * NULL "1" "x" 0 "" (!)
802 * "" "1" "x" 1 "x"
803 * NULL NULL "" 0 ""
807 static INT PROFILE_GetString( LPCWSTR section, LPCWSTR key_name,
808 LPCWSTR def_val, LPWSTR buffer, UINT len )
810 PROFILEKEY *key = NULL;
811 static const WCHAR empty_strW[] = { 0 };
813 if(!buffer) return 0;
815 if (!def_val) def_val = empty_strW;
816 if (key_name)
818 if (!key_name[0])
820 /* Win95 returns 0 on keyname "". Tested with Likse32 bon 000227 */
821 return 0;
823 key = PROFILE_Find( &CurProfile->section, section, key_name, FALSE, FALSE);
824 PROFILE_CopyEntry( buffer, (key && key->value) ? key->value : def_val,
825 len, FALSE, TRUE );
826 TRACE("(%s,%s,%s): returning %s\n",
827 debugstr_w(section), debugstr_w(key_name),
828 debugstr_w(def_val), debugstr_w(buffer) );
829 return strlenW( buffer );
831 /* no "else" here ! */
832 if (section && section[0])
834 INT ret = PROFILE_GetSection(CurProfile->section, section, buffer, len, FALSE, FALSE);
835 if (!buffer[0]) /* no luck -> def_val */
837 PROFILE_CopyEntry(buffer, def_val, len, FALSE, TRUE);
838 ret = strlenW(buffer);
840 return ret;
842 buffer[0] = '\0';
843 return 0;
847 /***********************************************************************
848 * PROFILE_SetString
850 * Set a profile string.
852 static BOOL PROFILE_SetString( LPCWSTR section_name, LPCWSTR key_name,
853 LPCWSTR value, BOOL create_always )
855 if (!key_name) /* Delete a whole section */
857 TRACE("(%s)\n", debugstr_w(section_name));
858 CurProfile->changed |= PROFILE_DeleteSection( &CurProfile->section,
859 section_name );
860 return TRUE; /* Even if PROFILE_DeleteSection() has failed,
861 this is not an error on application's level.*/
863 else if (!value) /* Delete a key */
865 TRACE("(%s,%s)\n", debugstr_w(section_name), debugstr_w(key_name) );
866 CurProfile->changed |= PROFILE_DeleteKey( &CurProfile->section,
867 section_name, key_name );
868 return TRUE; /* same error handling as above */
870 else /* Set the key value */
872 PROFILEKEY *key = PROFILE_Find(&CurProfile->section, section_name,
873 key_name, TRUE, create_always );
874 TRACE("(%s,%s,%s):\n",
875 debugstr_w(section_name), debugstr_w(key_name), debugstr_w(value) );
876 if (!key) return FALSE;
877 if (key->value)
879 /* strip the leading spaces. We can safely strip \n\r and
880 * friends too, they should not happen here anyway. */
881 while (PROFILE_isspace(*value)) value++;
883 if (!strcmpW( key->value, value ))
885 TRACE(" no change needed\n" );
886 return TRUE; /* No change needed */
888 TRACE(" replacing %s\n", debugstr_w(key->value) );
889 HeapFree( GetProcessHeap(), 0, key->value );
891 else TRACE(" creating key\n" );
892 key->value = HeapAlloc( GetProcessHeap(), 0, (strlenW(value)+1) * sizeof(WCHAR) );
893 strcpyW( key->value, value );
894 CurProfile->changed = TRUE;
896 return TRUE;
900 /***********************************************************************
901 * get_profile_key
903 static HKEY get_profile_key(void)
905 static HKEY profile_key;
907 if (!profile_key)
909 OBJECT_ATTRIBUTES attr;
910 UNICODE_STRING nameW;
911 HKEY hkey;
913 attr.Length = sizeof(attr);
914 attr.RootDirectory = 0;
915 attr.ObjectName = &nameW;
916 attr.Attributes = 0;
917 attr.SecurityDescriptor = NULL;
918 attr.SecurityQualityOfService = NULL;
920 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\Software\\Wine\\Wine\\Config" ) ||
921 NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ))
923 ERR("Cannot create config registry key\n" );
924 ExitProcess( 1 );
926 RtlFreeUnicodeString( &nameW );
928 if (InterlockedCompareExchangePointer( (void **)&profile_key, hkey, 0 ))
929 NtClose( hkey ); /* somebody beat us to it */
931 return profile_key;
935 /***********************************************************************
936 * PROFILE_GetWineIniString
938 * Get a config string from the wine.ini file.
940 int PROFILE_GetWineIniString( LPCWSTR section, LPCWSTR key_name,
941 LPCWSTR def, LPWSTR buffer, int len )
943 HKEY hkey;
944 NTSTATUS err;
945 OBJECT_ATTRIBUTES attr;
946 UNICODE_STRING nameW;
948 attr.Length = sizeof(attr);
949 attr.RootDirectory = get_profile_key();
950 attr.ObjectName = &nameW;
951 attr.Attributes = 0;
952 attr.SecurityDescriptor = NULL;
953 attr.SecurityQualityOfService = NULL;
954 RtlInitUnicodeString( &nameW, section );
955 if (!(err = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )))
957 char tmp[PROFILE_MAX_LINE_LEN*sizeof(WCHAR) + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
958 DWORD count;
960 RtlInitUnicodeString( &nameW, key_name );
961 if (!(err = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
962 tmp, sizeof(tmp), &count )))
964 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
965 PROFILE_CopyEntry( buffer, str, len, TRUE, TRUE );
967 NtClose( hkey );
970 if (err) PROFILE_CopyEntry( buffer, def, len, TRUE, TRUE );
971 TRACE( "(%s,%s,%s): returning %s\n", debugstr_w(section),
972 debugstr_w(key_name), debugstr_w(def), debugstr_w(buffer) );
973 return strlenW(buffer);
977 /******************************************************************************
979 * PROFILE_GetWineIniBool
981 * Reads a boolean value from the wine.ini file. This function attempts to
982 * be user-friendly by accepting 'n', 'N' (no), 'f', 'F' (false), or '0'
983 * (zero) for false, 'y', 'Y' (yes), 't', 'T' (true), or '1' (one) for
984 * true. Anything else results in the return of the default value.
986 * This function uses 1 to indicate true, and 0 for false. You can check
987 * for existence by setting def to something other than 0 or 1 and
988 * examining the return value.
990 int PROFILE_GetWineIniBool( LPCWSTR section, LPCWSTR key_name, int def )
992 static const WCHAR def_valueW[] = {'~',0};
993 WCHAR key_value[2];
994 int retval;
996 PROFILE_GetWineIniString(section, key_name, def_valueW, key_value, 2);
998 switch(key_value[0]) {
999 case 'n':
1000 case 'N':
1001 case 'f':
1002 case 'F':
1003 case '0':
1004 retval = 0;
1005 break;
1007 case 'y':
1008 case 'Y':
1009 case 't':
1010 case 'T':
1011 case '1':
1012 retval = 1;
1013 break;
1015 default:
1016 retval = def;
1019 TRACE("(%s, %s, %s), [%c], ret %s\n", debugstr_w(section), debugstr_w(key_name),
1020 def ? "TRUE" : "FALSE", key_value[0],
1021 retval ? "TRUE" : "FALSE");
1023 return retval;
1027 /***********************************************************************
1028 * PROFILE_UsageWineIni
1030 * Explain the wine.ini file to those who don't read documentation.
1031 * Keep below one screenful in length so that error messages above are
1032 * noticed.
1034 void PROFILE_UsageWineIni(void)
1036 MESSAGE("Perhaps you have not properly edited or created "
1037 "your Wine configuration file,\n");
1038 MESSAGE("which is (supposed to be) '%s/config'.\n", wine_get_config_dir());
1042 /********************* API functions **********************************/
1044 /***********************************************************************
1045 * GetProfileInt (KERNEL.57)
1047 UINT16 WINAPI GetProfileInt16( LPCSTR section, LPCSTR entry, INT16 def_val )
1049 return GetPrivateProfileInt16( section, entry, def_val, "win.ini" );
1053 /***********************************************************************
1054 * GetProfileIntA (KERNEL32.@)
1056 UINT WINAPI GetProfileIntA( LPCSTR section, LPCSTR entry, INT def_val )
1058 return GetPrivateProfileIntA( section, entry, def_val, "win.ini" );
1061 /***********************************************************************
1062 * GetProfileIntW (KERNEL32.@)
1064 UINT WINAPI GetProfileIntW( LPCWSTR section, LPCWSTR entry, INT def_val )
1066 return GetPrivateProfileIntW( section, entry, def_val, wininiW );
1070 * if allow_section_name_copy is TRUE, allow the copying :
1071 * - of Section names if 'section' is NULL
1072 * - of Keys in a Section if 'entry' is NULL
1073 * (see MSDN doc for GetPrivateProfileString)
1075 static int PROFILE_GetPrivateProfileString( LPCWSTR section, LPCWSTR entry,
1076 LPCWSTR def_val, LPWSTR buffer,
1077 UINT len, LPCWSTR filename,
1078 BOOL allow_section_name_copy )
1080 int ret;
1081 LPWSTR pDefVal = NULL;
1083 if (!filename)
1084 filename = wininiW;
1086 TRACE("%s,%s,%s,%p,%u,%s\n", debugstr_w(section), debugstr_w(entry),
1087 debugstr_w(def_val), buffer, len, debugstr_w(filename));
1089 /* strip any trailing ' ' of def_val. */
1090 if (def_val)
1092 LPCWSTR p = &def_val[strlenW(def_val)]; /* even "" works ! */
1094 while (p > def_val)
1096 p--;
1097 if ((*p) != ' ')
1098 break;
1100 if (*p == ' ') /* ouch, contained trailing ' ' */
1102 int len = (int)(p - def_val);
1103 pDefVal = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1104 strncpyW(pDefVal, def_val, len);
1105 pDefVal[len] = '\0';
1108 if (!pDefVal)
1109 pDefVal = (LPWSTR)def_val;
1111 RtlEnterCriticalSection( &PROFILE_CritSect );
1113 if (PROFILE_Open( filename )) {
1114 if ((allow_section_name_copy) && (section == NULL))
1115 ret = PROFILE_GetSectionNames(buffer, len);
1116 else
1117 /* PROFILE_GetString already handles the 'entry == NULL' case */
1118 ret = PROFILE_GetString( section, entry, pDefVal, buffer, len );
1119 } else {
1120 lstrcpynW( buffer, pDefVal, len );
1121 ret = strlenW( buffer );
1124 RtlLeaveCriticalSection( &PROFILE_CritSect );
1126 if (pDefVal != def_val) /* allocated */
1127 HeapFree(GetProcessHeap(), 0, pDefVal);
1129 TRACE("returning %s, %d\n", debugstr_w(buffer), ret);
1131 return ret;
1134 /***********************************************************************
1135 * GetPrivateProfileString (KERNEL.128)
1137 INT16 WINAPI GetPrivateProfileString16( LPCSTR section, LPCSTR entry,
1138 LPCSTR def_val, LPSTR buffer,
1139 UINT16 len, LPCSTR filename )
1141 UNICODE_STRING sectionW, entryW, def_valW, filenameW;
1142 LPWSTR bufferW;
1143 INT16 retW, ret = 0;
1145 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
1146 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1147 else sectionW.Buffer = NULL;
1148 if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1149 else entryW.Buffer = NULL;
1150 if (def_val) RtlCreateUnicodeStringFromAsciiz(&def_valW, def_val);
1151 else def_valW.Buffer = NULL;
1152 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1153 else filenameW.Buffer = NULL;
1155 retW = PROFILE_GetPrivateProfileString( sectionW.Buffer, entryW.Buffer,
1156 def_valW.Buffer, bufferW, len,
1157 filenameW.Buffer, FALSE );
1158 if (len)
1160 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
1161 if (!ret)
1163 ret = len - 1;
1164 buffer[ret] = 0;
1166 else
1167 ret--; /* strip terminating 0 */
1170 RtlFreeUnicodeString(&sectionW);
1171 RtlFreeUnicodeString(&entryW);
1172 RtlFreeUnicodeString(&def_valW);
1173 RtlFreeUnicodeString(&filenameW);
1174 if (bufferW) HeapFree(GetProcessHeap(), 0, bufferW);
1175 return ret;
1178 /***********************************************************************
1179 * GetPrivateProfileStringA (KERNEL32.@)
1181 INT WINAPI GetPrivateProfileStringA( LPCSTR section, LPCSTR entry,
1182 LPCSTR def_val, LPSTR buffer,
1183 UINT len, LPCSTR filename )
1185 UNICODE_STRING sectionW, entryW, def_valW, filenameW;
1186 LPWSTR bufferW;
1187 INT retW, ret = 0;
1189 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
1190 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1191 else sectionW.Buffer = NULL;
1192 if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1193 else entryW.Buffer = NULL;
1194 if (def_val) RtlCreateUnicodeStringFromAsciiz(&def_valW, def_val);
1195 else def_valW.Buffer = NULL;
1196 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1197 else filenameW.Buffer = NULL;
1199 retW = GetPrivateProfileStringW( sectionW.Buffer, entryW.Buffer,
1200 def_valW.Buffer, bufferW, len,
1201 filenameW.Buffer);
1202 if (len)
1204 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
1205 if (!ret)
1207 ret = len - 1;
1208 buffer[ret] = 0;
1210 else
1211 ret--; /* strip terminating 0 */
1214 RtlFreeUnicodeString(&sectionW);
1215 RtlFreeUnicodeString(&entryW);
1216 RtlFreeUnicodeString(&def_valW);
1217 RtlFreeUnicodeString(&filenameW);
1218 if (bufferW) HeapFree(GetProcessHeap(), 0, bufferW);
1219 return ret;
1222 /***********************************************************************
1223 * GetPrivateProfileStringW (KERNEL32.@)
1225 INT WINAPI GetPrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
1226 LPCWSTR def_val, LPWSTR buffer,
1227 UINT len, LPCWSTR filename )
1229 return PROFILE_GetPrivateProfileString( section, entry, def_val,
1230 buffer, len, filename, TRUE );
1233 /***********************************************************************
1234 * GetProfileString (KERNEL.58)
1236 INT16 WINAPI GetProfileString16( LPCSTR section, LPCSTR entry, LPCSTR def_val,
1237 LPSTR buffer, UINT16 len )
1239 return GetPrivateProfileString16( section, entry, def_val,
1240 buffer, len, "win.ini" );
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 * WriteProfileString (KERNEL.59)
1266 BOOL16 WINAPI WriteProfileString16( LPCSTR section, LPCSTR entry,
1267 LPCSTR string )
1269 return WritePrivateProfileString16( section, entry, string, "win.ini" );
1272 /***********************************************************************
1273 * WriteProfileStringA (KERNEL32.@)
1275 BOOL WINAPI WriteProfileStringA( LPCSTR section, LPCSTR entry,
1276 LPCSTR string )
1278 return WritePrivateProfileStringA( section, entry, string, "win.ini" );
1281 /***********************************************************************
1282 * WriteProfileStringW (KERNEL32.@)
1284 BOOL WINAPI WriteProfileStringW( LPCWSTR section, LPCWSTR entry,
1285 LPCWSTR string )
1287 return WritePrivateProfileStringW( section, entry, string, wininiW );
1291 /***********************************************************************
1292 * GetPrivateProfileInt (KERNEL.127)
1294 UINT16 WINAPI GetPrivateProfileInt16( LPCSTR section, LPCSTR entry,
1295 INT16 def_val, LPCSTR filename )
1297 /* we used to have some elaborate return value limitation (<= -32768 etc.)
1298 * here, but Win98SE doesn't care about this at all, so I deleted it.
1299 * AFAIR versions prior to Win9x had these limits, though. */
1300 return (INT16)GetPrivateProfileIntA(section,entry,def_val,filename);
1303 /***********************************************************************
1304 * GetPrivateProfileIntW (KERNEL32.@)
1306 UINT WINAPI GetPrivateProfileIntW( LPCWSTR section, LPCWSTR entry,
1307 INT def_val, LPCWSTR filename )
1309 WCHAR buffer[30];
1310 UNICODE_STRING bufferW;
1311 INT len;
1312 ULONG result;
1314 if (!(len = GetPrivateProfileStringW( section, entry, emptystringW,
1315 buffer, sizeof(buffer)/sizeof(WCHAR),
1316 filename )))
1317 return def_val;
1319 if (len+1 == sizeof(buffer)/sizeof(WCHAR)) FIXME("result may be wrong!\n");
1321 /* FIXME: if entry can be found but it's empty, then Win16 is
1322 * supposed to return 0 instead of def_val ! Difficult/problematic
1323 * to implement (every other failure also returns zero buffer),
1324 * thus wait until testing framework avail for making sure nothing
1325 * else gets broken that way. */
1326 if (!buffer[0]) return (UINT)def_val;
1328 RtlInitUnicodeString( &bufferW, buffer );
1329 RtlUnicodeStringToInteger( &bufferW, 10, &result);
1330 return result;
1333 /***********************************************************************
1334 * GetPrivateProfileIntA (KERNEL32.@)
1336 * FIXME: rewrite using unicode
1338 UINT WINAPI GetPrivateProfileIntA( LPCSTR section, LPCSTR entry,
1339 INT def_val, LPCSTR filename )
1341 UNICODE_STRING entryW, filenameW, sectionW;
1342 UINT res;
1343 if(entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1344 else entryW.Buffer = NULL;
1345 if(filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1346 else filenameW.Buffer = NULL;
1347 if(section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1348 else sectionW.Buffer = NULL;
1349 res = GetPrivateProfileIntW(sectionW.Buffer, entryW.Buffer, def_val,
1350 filenameW.Buffer);
1351 RtlFreeUnicodeString(&sectionW);
1352 RtlFreeUnicodeString(&filenameW);
1353 RtlFreeUnicodeString(&entryW);
1354 return res;
1357 /***********************************************************************
1358 * GetPrivateProfileSection (KERNEL.418)
1360 INT16 WINAPI GetPrivateProfileSection16( LPCSTR section, LPSTR buffer,
1361 UINT16 len, LPCSTR filename )
1363 return GetPrivateProfileSectionA( section, buffer, len, filename );
1366 /***********************************************************************
1367 * GetPrivateProfileSectionW (KERNEL32.@)
1369 INT WINAPI GetPrivateProfileSectionW( LPCWSTR section, LPWSTR buffer,
1370 DWORD len, LPCWSTR filename )
1372 int ret = 0;
1374 RtlEnterCriticalSection( &PROFILE_CritSect );
1376 if (PROFILE_Open( filename ))
1377 ret = PROFILE_GetSection(CurProfile->section, section, buffer, len,
1378 FALSE, TRUE);
1380 RtlLeaveCriticalSection( &PROFILE_CritSect );
1382 return ret;
1385 /***********************************************************************
1386 * GetPrivateProfileSectionA (KERNEL32.@)
1388 INT WINAPI GetPrivateProfileSectionA( LPCSTR section, LPSTR buffer,
1389 DWORD len, LPCSTR filename )
1391 UNICODE_STRING sectionW, filenameW;
1392 LPWSTR bufferW;
1393 INT retW, ret = 0;
1395 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
1396 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1397 else sectionW.Buffer = NULL;
1398 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1399 else filenameW.Buffer = NULL;
1401 retW = GetPrivateProfileSectionW(sectionW.Buffer, bufferW, len, filenameW.Buffer);
1402 if (len > 2)
1404 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 2, buffer, len, NULL, NULL);
1405 if (ret > 2)
1406 ret -= 2;
1407 else
1409 ret = 0;
1410 buffer[len-2] = 0;
1411 buffer[len-1] = 0;
1414 else
1416 buffer[0] = 0;
1417 buffer[1] = 0;
1420 RtlFreeUnicodeString(&sectionW);
1421 RtlFreeUnicodeString(&filenameW);
1422 if (bufferW) HeapFree(GetProcessHeap(), 0, bufferW);
1423 return ret;
1426 /***********************************************************************
1427 * GetProfileSection (KERNEL.419)
1429 INT16 WINAPI GetProfileSection16( LPCSTR section, LPSTR buffer, UINT16 len )
1431 return GetPrivateProfileSection16( section, buffer, len, "win.ini" );
1434 /***********************************************************************
1435 * GetProfileSectionA (KERNEL32.@)
1437 INT WINAPI GetProfileSectionA( LPCSTR section, LPSTR buffer, DWORD len )
1439 return GetPrivateProfileSectionA( section, buffer, len, "win.ini" );
1442 /***********************************************************************
1443 * GetProfileSectionW (KERNEL32.@)
1445 INT WINAPI GetProfileSectionW( LPCWSTR section, LPWSTR buffer, DWORD len )
1447 return GetPrivateProfileSectionW( section, buffer, len, wininiW );
1451 /***********************************************************************
1452 * WritePrivateProfileString (KERNEL.129)
1454 BOOL16 WINAPI WritePrivateProfileString16( LPCSTR section, LPCSTR entry,
1455 LPCSTR string, LPCSTR filename )
1457 return WritePrivateProfileStringA(section,entry,string,filename);
1460 /***********************************************************************
1461 * WritePrivateProfileStringW (KERNEL32.@)
1463 BOOL WINAPI WritePrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
1464 LPCWSTR string, LPCWSTR filename )
1466 BOOL ret = FALSE;
1468 RtlEnterCriticalSection( &PROFILE_CritSect );
1470 if (PROFILE_Open( filename ))
1472 if (!section && !entry && !string) /* documented "file flush" case */
1474 PROFILE_FlushFile();
1475 PROFILE_ReleaseFile(); /* always return FALSE in this case */
1477 else {
1478 if (!section) {
1479 FIXME("(NULL?,%s,%s,%s)?\n",
1480 debugstr_w(entry), debugstr_w(string), debugstr_w(filename));
1481 } else {
1482 ret = PROFILE_SetString( section, entry, string, FALSE);
1483 PROFILE_FlushFile();
1488 RtlLeaveCriticalSection( &PROFILE_CritSect );
1489 return ret;
1492 /***********************************************************************
1493 * WritePrivateProfileStringA (KERNEL32.@)
1495 BOOL WINAPI WritePrivateProfileStringA( LPCSTR section, LPCSTR entry,
1496 LPCSTR string, LPCSTR filename )
1498 UNICODE_STRING sectionW, entryW, stringW, filenameW;
1499 BOOL ret;
1501 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1502 else sectionW.Buffer = NULL;
1503 if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
1504 else entryW.Buffer = NULL;
1505 if (string) RtlCreateUnicodeStringFromAsciiz(&stringW, string);
1506 else stringW.Buffer = NULL;
1507 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1508 else filenameW.Buffer = NULL;
1510 ret = WritePrivateProfileStringW(sectionW.Buffer, entryW.Buffer,
1511 stringW.Buffer, filenameW.Buffer);
1512 RtlFreeUnicodeString(&sectionW);
1513 RtlFreeUnicodeString(&entryW);
1514 RtlFreeUnicodeString(&stringW);
1515 RtlFreeUnicodeString(&filenameW);
1516 return ret;
1519 /***********************************************************************
1520 * WritePrivateProfileSection (KERNEL.416)
1522 BOOL16 WINAPI WritePrivateProfileSection16( LPCSTR section,
1523 LPCSTR string, LPCSTR filename )
1525 return WritePrivateProfileSectionA( section, string, filename );
1528 /***********************************************************************
1529 * WritePrivateProfileSectionW (KERNEL32.@)
1531 BOOL WINAPI WritePrivateProfileSectionW( LPCWSTR section,
1532 LPCWSTR string, LPCWSTR filename )
1534 BOOL ret = FALSE;
1535 LPWSTR p;
1537 RtlEnterCriticalSection( &PROFILE_CritSect );
1539 if (PROFILE_Open( filename )) {
1540 if (!section && !string)
1541 PROFILE_ReleaseFile(); /* always return FALSE in this case */
1542 else if (!string) {/* delete the named section*/
1543 ret = PROFILE_SetString(section,NULL,NULL, FALSE);
1544 PROFILE_FlushFile();
1545 } else {
1546 PROFILE_DeleteAllKeys(section);
1547 ret = TRUE;
1548 while(*string) {
1549 LPWSTR buf = HeapAlloc( GetProcessHeap(), 0, (strlenW(string)+1) * sizeof(WCHAR) );
1550 strcpyW( buf, string );
1551 if((p = strchrW( buf, '='))) {
1552 *p='\0';
1553 ret = PROFILE_SetString( section, buf, p+1, TRUE);
1555 HeapFree( GetProcessHeap(), 0, buf );
1556 string += strlenW(string)+1;
1558 PROFILE_FlushFile();
1562 RtlLeaveCriticalSection( &PROFILE_CritSect );
1563 return ret;
1566 /***********************************************************************
1567 * WritePrivateProfileSectionA (KERNEL32.@)
1569 BOOL WINAPI WritePrivateProfileSectionA( LPCSTR section,
1570 LPCSTR string, LPCSTR filename)
1573 UNICODE_STRING sectionW, filenameW;
1574 LPWSTR stringW;
1575 BOOL ret;
1577 if (string)
1579 INT lenA, lenW;
1580 LPCSTR p = string;
1582 while(*p) p += strlen(p) + 1;
1583 lenA = p - string + 1;
1584 lenW = MultiByteToWideChar(CP_ACP, 0, string, lenA, NULL, 0);
1585 if ((stringW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR))))
1586 MultiByteToWideChar(CP_ACP, 0, string, lenA, stringW, lenW);
1588 else stringW = NULL;
1589 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1590 else sectionW.Buffer = NULL;
1591 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1592 else filenameW.Buffer = NULL;
1594 ret = WritePrivateProfileSectionW(sectionW.Buffer, stringW, filenameW.Buffer);
1596 HeapFree(GetProcessHeap(), 0, stringW);
1597 RtlFreeUnicodeString(&sectionW);
1598 RtlFreeUnicodeString(&filenameW);
1599 return ret;
1602 /***********************************************************************
1603 * WriteProfileSection (KERNEL.417)
1605 BOOL16 WINAPI WriteProfileSection16( LPCSTR section, LPCSTR keys_n_values)
1607 return WritePrivateProfileSection16( section, keys_n_values, "win.ini");
1610 /***********************************************************************
1611 * WriteProfileSectionA (KERNEL32.@)
1613 BOOL WINAPI WriteProfileSectionA( LPCSTR section, LPCSTR keys_n_values)
1616 return WritePrivateProfileSectionA( section, keys_n_values, "win.ini");
1619 /***********************************************************************
1620 * WriteProfileSectionW (KERNEL32.@)
1622 BOOL WINAPI WriteProfileSectionW( LPCWSTR section, LPCWSTR keys_n_values)
1624 return WritePrivateProfileSectionW(section, keys_n_values, wininiW);
1627 /***********************************************************************
1628 * GetPrivateProfileSectionNames (KERNEL.143)
1630 WORD WINAPI GetPrivateProfileSectionNames16( LPSTR buffer, WORD size,
1631 LPCSTR filename )
1633 return GetPrivateProfileSectionNamesA(buffer,size,filename);
1637 /***********************************************************************
1638 * GetProfileSectionNames (KERNEL.142)
1640 WORD WINAPI GetProfileSectionNames16(LPSTR buffer, WORD size)
1643 return GetPrivateProfileSectionNamesA(buffer,size,"win.ini");
1647 /***********************************************************************
1648 * GetPrivateProfileSectionNamesW (KERNEL32.@)
1650 * Returns the section names contained in the specified file.
1651 * FIXME: Where do we find this file when the path is relative?
1652 * The section names are returned as a list of strings with an extra
1653 * '\0' to mark the end of the list. Except for that the behavior
1654 * depends on the Windows version.
1656 * Win95:
1657 * - if the buffer is 0 or 1 character long then it is as if it was of
1658 * infinite length.
1659 * - otherwise, if the buffer is to small only the section names that fit
1660 * are returned.
1661 * - note that this means if the buffer was to small to return even just
1662 * the first section name then a single '\0' will be returned.
1663 * - the return value is the number of characters written in the buffer,
1664 * except if the buffer was too smal in which case len-2 is returned
1666 * Win2000:
1667 * - if the buffer is 0, 1 or 2 characters long then it is filled with
1668 * '\0' and the return value is 0
1669 * - otherwise if the buffer is too small then the first section name that
1670 * does not fit is truncated so that the string list can be terminated
1671 * correctly (double '\0')
1672 * - the return value is the number of characters written in the buffer
1673 * except for the trailing '\0'. If the buffer is too small, then the
1674 * return value is len-2
1675 * - Win2000 has a bug that triggers when the section names and the
1676 * trailing '\0' fit exactly in the buffer. In that case the trailing
1677 * '\0' is missing.
1679 * Wine implements the observed Win2000 behavior (except for the bug).
1681 * Note that when the buffer is big enough then the return value may be any
1682 * value between 1 and len-1 (or len in Win95), including len-2.
1684 DWORD WINAPI GetPrivateProfileSectionNamesW( LPWSTR buffer, DWORD size,
1685 LPCWSTR filename)
1687 DWORD ret = 0;
1689 RtlEnterCriticalSection( &PROFILE_CritSect );
1691 if (PROFILE_Open( filename ))
1692 ret = PROFILE_GetSectionNames(buffer, size);
1694 RtlLeaveCriticalSection( &PROFILE_CritSect );
1696 return ret;
1700 /***********************************************************************
1701 * GetPrivateProfileSectionNamesA (KERNEL32.@)
1703 DWORD WINAPI GetPrivateProfileSectionNamesA( LPSTR buffer, DWORD size,
1704 LPCSTR filename)
1706 UNICODE_STRING filenameW;
1707 LPWSTR bufferW;
1708 INT retW, ret = 0;
1710 bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR)) : NULL;
1711 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1712 else filenameW.Buffer = NULL;
1714 retW = GetPrivateProfileSectionNamesW(bufferW, size, filenameW.Buffer);
1715 if (retW && size)
1717 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW, buffer, size, NULL, NULL);
1718 if (!ret)
1720 ret = size;
1721 buffer[size-1] = 0;
1725 RtlFreeUnicodeString(&filenameW);
1726 if (bufferW) HeapFree(GetProcessHeap(), 0, bufferW);
1727 return ret;
1730 /***********************************************************************
1731 * GetPrivateProfileStruct (KERNEL.407)
1733 BOOL16 WINAPI GetPrivateProfileStruct16(LPCSTR section, LPCSTR key,
1734 LPVOID buf, UINT16 len, LPCSTR filename)
1736 return GetPrivateProfileStructA( section, key, buf, len, filename );
1739 /***********************************************************************
1740 * GetPrivateProfileStructW (KERNEL32.@)
1742 * Should match Win95's behaviour pretty much
1744 BOOL WINAPI GetPrivateProfileStructW (LPCWSTR section, LPCWSTR key,
1745 LPVOID buf, UINT len, LPCWSTR filename)
1747 BOOL ret = FALSE;
1749 RtlEnterCriticalSection( &PROFILE_CritSect );
1751 if (PROFILE_Open( filename )) {
1752 PROFILEKEY *k = PROFILE_Find ( &CurProfile->section, section, key, FALSE, FALSE);
1753 if (k) {
1754 TRACE("value (at %p): %s\n", k->value, debugstr_w(k->value));
1755 if (((strlenW(k->value) - 2) / 2) == len)
1757 LPWSTR end, p;
1758 BOOL valid = TRUE;
1759 WCHAR c;
1760 DWORD chksum = 0;
1762 end = k->value + strlenW(k->value); /* -> '\0' */
1763 /* check for invalid chars in ASCII coded hex string */
1764 for (p=k->value; p < end; p++)
1766 if (!isxdigitW(*p))
1768 WARN("invalid char '%x' in file %s->[%s]->%s !\n",
1769 *p, debugstr_w(filename), debugstr_w(section), debugstr_w(key));
1770 valid = FALSE;
1771 break;
1774 if (valid)
1776 BOOL highnibble = TRUE;
1777 BYTE b = 0, val;
1778 LPBYTE binbuf = (LPBYTE)buf;
1780 end -= 2; /* don't include checksum in output data */
1781 /* translate ASCII hex format into binary data */
1782 for (p=k->value; p < end; p++)
1784 c = toupperW(*p);
1785 val = (c > '9') ?
1786 (c - 'A' + 10) : (c - '0');
1788 if (highnibble)
1789 b = val << 4;
1790 else
1792 b += val;
1793 *binbuf++ = b; /* feed binary data into output */
1794 chksum += b; /* calculate checksum */
1796 highnibble ^= 1; /* toggle */
1798 /* retrieve stored checksum value */
1799 c = toupperW(*p++);
1800 b = ( (c > '9') ? (c - 'A' + 10) : (c - '0') ) << 4;
1801 c = toupperW(*p);
1802 b += (c > '9') ? (c - 'A' + 10) : (c - '0');
1803 if (b == (chksum & 0xff)) /* checksums match ? */
1804 ret = TRUE;
1809 RtlLeaveCriticalSection( &PROFILE_CritSect );
1811 return ret;
1814 /***********************************************************************
1815 * GetPrivateProfileStructA (KERNEL32.@)
1817 BOOL WINAPI GetPrivateProfileStructA (LPCSTR section, LPCSTR key,
1818 LPVOID buffer, UINT len, LPCSTR filename)
1820 UNICODE_STRING sectionW, keyW, filenameW;
1821 INT ret;
1823 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, 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 ret = GetPrivateProfileStructW(sectionW.Buffer, keyW.Buffer, buffer, len,
1831 filenameW.Buffer);
1832 /* Do not translate binary data. */
1834 RtlFreeUnicodeString(&sectionW);
1835 RtlFreeUnicodeString(&keyW);
1836 RtlFreeUnicodeString(&filenameW);
1837 return ret;
1842 /***********************************************************************
1843 * WritePrivateProfileStruct (KERNEL.406)
1845 BOOL16 WINAPI WritePrivateProfileStruct16 (LPCSTR section, LPCSTR key,
1846 LPVOID buf, UINT16 bufsize, LPCSTR filename)
1848 return WritePrivateProfileStructA( section, key, buf, bufsize, filename );
1851 /***********************************************************************
1852 * WritePrivateProfileStructW (KERNEL32.@)
1854 BOOL WINAPI WritePrivateProfileStructW (LPCWSTR section, LPCWSTR key,
1855 LPVOID buf, UINT bufsize, LPCWSTR filename)
1857 BOOL ret = FALSE;
1858 LPBYTE binbuf;
1859 LPWSTR outstring, p;
1860 DWORD sum = 0;
1862 if (!section && !key && !buf) /* flush the cache */
1863 return WritePrivateProfileStringW( NULL, NULL, NULL, filename );
1865 /* allocate string buffer for hex chars + checksum hex char + '\0' */
1866 outstring = HeapAlloc( GetProcessHeap(), 0, (bufsize*2 + 2 + 1) * sizeof(WCHAR) );
1867 p = outstring;
1868 for (binbuf = (LPBYTE)buf; binbuf < (LPBYTE)buf+bufsize; binbuf++) {
1869 *p++ = hex[*binbuf >> 4];
1870 *p++ = hex[*binbuf & 0xf];
1871 sum += *binbuf;
1873 /* checksum is sum & 0xff */
1874 *p++ = hex[(sum & 0xf0) >> 4];
1875 *p++ = hex[sum & 0xf];
1876 *p++ = '\0';
1878 RtlEnterCriticalSection( &PROFILE_CritSect );
1880 if (PROFILE_Open( filename )) {
1881 ret = PROFILE_SetString( section, key, outstring, FALSE);
1882 PROFILE_FlushFile();
1885 RtlLeaveCriticalSection( &PROFILE_CritSect );
1887 HeapFree( GetProcessHeap(), 0, outstring );
1889 return ret;
1892 /***********************************************************************
1893 * WritePrivateProfileStructA (KERNEL32.@)
1895 BOOL WINAPI WritePrivateProfileStructA (LPCSTR section, LPCSTR key,
1896 LPVOID buf, UINT bufsize, LPCSTR filename)
1898 UNICODE_STRING sectionW, keyW, filenameW;
1899 INT ret;
1901 if (section) RtlCreateUnicodeStringFromAsciiz(&sectionW, section);
1902 else sectionW.Buffer = NULL;
1903 if (key) RtlCreateUnicodeStringFromAsciiz(&keyW, key);
1904 else keyW.Buffer = NULL;
1905 if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
1906 else filenameW.Buffer = NULL;
1908 /* Do not translate binary data. */
1909 ret = WritePrivateProfileStructW(sectionW.Buffer, keyW.Buffer, buf, bufsize,
1910 filenameW.Buffer);
1912 RtlFreeUnicodeString(&sectionW);
1913 RtlFreeUnicodeString(&keyW);
1914 RtlFreeUnicodeString(&filenameW);
1915 return ret;
1919 /***********************************************************************
1920 * WriteOutProfiles (KERNEL.315)
1922 void WINAPI WriteOutProfiles16(void)
1924 RtlEnterCriticalSection( &PROFILE_CritSect );
1925 PROFILE_FlushFile();
1926 RtlLeaveCriticalSection( &PROFILE_CritSect );
1929 /***********************************************************************
1930 * CloseProfileUserMapping (KERNEL32.@)
1932 BOOL WINAPI CloseProfileUserMapping(void) {
1933 FIXME("(), stub!\n");
1934 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1935 return FALSE;