Thread-safe implementation of profile functions (Windows and Wine).
This commit is contained in:
parent
35ffc5d8c0
commit
ed15575954
162
files/profile.c
162
files/profile.c
|
@ -53,7 +53,7 @@ static PROFILE *MRUProfile[N_CACHED_PROFILES]={NULL};
|
|||
#define CurProfile (MRUProfile[0])
|
||||
|
||||
/* wine.ini profile content */
|
||||
static PROFILESECTION *WineProfile;
|
||||
static PROFILESECTION *PROFILE_WineProfile;
|
||||
|
||||
#define PROFILE_MAX_LINE_LEN 1024
|
||||
|
||||
|
@ -70,6 +70,8 @@ static char PROFILE_WineIniUsed[MAX_PATHNAME_LEN] = "";
|
|||
|
||||
static LPCWSTR wininiW = NULL;
|
||||
|
||||
static CRITICAL_SECTION PROFILE_CritSect;
|
||||
|
||||
/***********************************************************************
|
||||
* PROFILE_CopyEntry
|
||||
*
|
||||
|
@ -675,16 +677,26 @@ static BOOL PROFILE_SetString( LPCSTR section_name, LPCSTR key_name,
|
|||
int PROFILE_GetWineIniString( const char *section, const char *key_name,
|
||||
const char *def, char *buffer, int len )
|
||||
{
|
||||
int ret;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if (key_name)
|
||||
{
|
||||
PROFILEKEY *key = PROFILE_Find(&WineProfile, section, key_name, FALSE);
|
||||
PROFILEKEY *key = PROFILE_Find(&PROFILE_WineProfile, section, key_name, FALSE);
|
||||
PROFILE_CopyEntry( buffer, (key && key->value) ? key->value : def,
|
||||
len, TRUE );
|
||||
TRACE(profile, "('%s','%s','%s'): returning '%s'\n",
|
||||
section, key_name, def, buffer );
|
||||
return strlen( buffer );
|
||||
ret = strlen( buffer );
|
||||
}
|
||||
return PROFILE_GetSection( WineProfile, section, buffer, len, TRUE, FALSE );
|
||||
else
|
||||
{
|
||||
ret = PROFILE_GetSection( PROFILE_WineProfile, section, buffer, len, TRUE, FALSE );
|
||||
}
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
@ -698,13 +710,23 @@ int PROFILE_GetWineIniInt( const char *section, const char *key_name, int def )
|
|||
char buffer[20];
|
||||
char *p;
|
||||
long result;
|
||||
PROFILEKEY *key;
|
||||
int ret;
|
||||
|
||||
PROFILEKEY *key = PROFILE_Find( &WineProfile, section, key_name, FALSE );
|
||||
if (!key || !key->value) return def;
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
key = PROFILE_Find( &PROFILE_WineProfile, section, key_name, FALSE );
|
||||
if (!key || !key->value) {
|
||||
ret = def;
|
||||
} else {
|
||||
PROFILE_CopyEntry( buffer, key->value, sizeof(buffer), TRUE );
|
||||
result = strtol( buffer, &p, 0 );
|
||||
if (p == buffer) return 0; /* No digits at all */
|
||||
return (int)result;
|
||||
ret = (p == buffer) ? 0 /* No digits at all */ : (int)result;
|
||||
}
|
||||
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
@ -741,8 +763,10 @@ int PROFILE_EnumerateWineIniSection(
|
|||
PROFILEKEY *scankey;
|
||||
int calls = 0;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
/* Search for the correct section */
|
||||
for(scansect = WineProfile; scansect; scansect = scansect->next) {
|
||||
for(scansect = PROFILE_WineProfile; scansect; scansect = scansect->next) {
|
||||
if(scansect->name && !strcasecmp(scansect->name, section)) {
|
||||
|
||||
/* Enumerate each key with the callback */
|
||||
|
@ -759,6 +783,7 @@ int PROFILE_EnumerateWineIniSection(
|
|||
break;
|
||||
}
|
||||
}
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return calls;
|
||||
}
|
||||
|
@ -831,10 +856,13 @@ int PROFILE_LoadWineIni(void)
|
|||
const char *p;
|
||||
FILE *f;
|
||||
|
||||
InitializeCriticalSection( &PROFILE_CritSect );
|
||||
MakeCriticalSectionGlobal( &PROFILE_CritSect );
|
||||
|
||||
if ( (Options.configFileName!=NULL) && (f = fopen(Options.configFileName, "r")) )
|
||||
{
|
||||
/* Open -config specified file */
|
||||
WineProfile = PROFILE_Load ( f);
|
||||
PROFILE_WineProfile = PROFILE_Load ( f);
|
||||
fclose ( f );
|
||||
strncpy(PROFILE_WineIniUsed,Options.configFileName,MAX_PATHNAME_LEN-1);
|
||||
return 1;
|
||||
|
@ -842,7 +870,7 @@ int PROFILE_LoadWineIni(void)
|
|||
|
||||
if ( (p = getenv( "WINE_INI" )) && (f = fopen( p, "r" )) )
|
||||
{
|
||||
WineProfile = PROFILE_Load( f );
|
||||
PROFILE_WineProfile = PROFILE_Load( f );
|
||||
fclose( f );
|
||||
strncpy(PROFILE_WineIniUsed,p,MAX_PATHNAME_LEN-1);
|
||||
return 1;
|
||||
|
@ -853,7 +881,7 @@ int PROFILE_LoadWineIni(void)
|
|||
strcat( buffer, PROFILE_WineIniName );
|
||||
if ((f = fopen( buffer, "r" )) != NULL)
|
||||
{
|
||||
WineProfile = PROFILE_Load( f );
|
||||
PROFILE_WineProfile = PROFILE_Load( f );
|
||||
fclose( f );
|
||||
strncpy(PROFILE_WineIniUsed,buffer,MAX_PATHNAME_LEN-1);
|
||||
return 1;
|
||||
|
@ -865,7 +893,7 @@ int PROFILE_LoadWineIni(void)
|
|||
|
||||
if ((f = fopen( WINE_INI_GLOBAL, "r" )) != NULL)
|
||||
{
|
||||
WineProfile = PROFILE_Load( f );
|
||||
PROFILE_WineProfile = PROFILE_Load( f );
|
||||
fclose( f );
|
||||
strncpy(PROFILE_WineIniUsed,WINE_INI_GLOBAL,MAX_PATHNAME_LEN-1);
|
||||
return 1;
|
||||
|
@ -895,7 +923,6 @@ void PROFILE_UsageWineIni(void)
|
|||
/* RTFM, so to say */
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* PROFILE_GetStringItem
|
||||
*
|
||||
|
@ -921,7 +948,6 @@ char* PROFILE_GetStringItem( char* start )
|
|||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/********************* API functions **********************************/
|
||||
|
||||
/***********************************************************************
|
||||
|
@ -999,10 +1025,6 @@ INT WINAPI GetProfileSectionW( LPCWSTR section, LPWSTR buffer, DWORD len )
|
|||
return GetPrivateProfileSectionW( section, buffer, len, wininiW );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* WriteProfileString16 (KERNEL.59)
|
||||
*/
|
||||
|
@ -1097,12 +1119,23 @@ INT WINAPI GetPrivateProfileStringA( LPCSTR section, LPCSTR entry,
|
|||
LPCSTR def_val, LPSTR buffer,
|
||||
UINT len, LPCSTR filename )
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!filename)
|
||||
filename = "win.ini";
|
||||
if (PROFILE_Open( filename ))
|
||||
return PROFILE_GetString( section, entry, def_val, buffer, len );
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if (PROFILE_Open( filename )) {
|
||||
ret = PROFILE_GetString( section, entry, def_val, buffer, len );
|
||||
} else {
|
||||
lstrcpynA( buffer, def_val, len );
|
||||
return strlen( buffer );
|
||||
ret = strlen( buffer );
|
||||
}
|
||||
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
|
@ -1134,10 +1167,16 @@ INT WINAPI GetPrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
|
|||
INT WINAPI GetPrivateProfileSectionA( LPCSTR section, LPSTR buffer,
|
||||
DWORD len, LPCSTR filename )
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if (PROFILE_Open( filename ))
|
||||
return PROFILE_GetSection(CurProfile->section, section, buffer, len,
|
||||
ret = PROFILE_GetSection(CurProfile->section, section, buffer, len,
|
||||
FALSE, TRUE);
|
||||
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1175,9 +1214,21 @@ BOOL16 WINAPI WritePrivateProfileString16( LPCSTR section, LPCSTR entry,
|
|||
BOOL WINAPI WritePrivateProfileStringA( LPCSTR section, LPCSTR entry,
|
||||
LPCSTR string, LPCSTR filename )
|
||||
{
|
||||
if (!PROFILE_Open( filename )) return FALSE;
|
||||
if (!section) return PROFILE_FlushFile();
|
||||
return PROFILE_SetString( section, entry, string );
|
||||
BOOL ret;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if (!PROFILE_Open( filename )) {
|
||||
ret = FALSE;
|
||||
} else if (!section) {
|
||||
ret = PROFILE_FlushFile();
|
||||
} else {
|
||||
ret = PROFILE_SetString( section, entry, string );
|
||||
}
|
||||
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
|
@ -1266,27 +1317,35 @@ WORD WINAPI GetPrivateProfileSectionNames16( LPSTR buffer, WORD size,
|
|||
char *buf;
|
||||
int l,cursize;
|
||||
PROFILESECTION *section;
|
||||
BOOL ret = FALSE;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if (PROFILE_Open( filename )) {
|
||||
buf=buffer;
|
||||
cursize=0;
|
||||
section=CurProfile->section;
|
||||
buf = buffer;
|
||||
cursize = 0;
|
||||
section = CurProfile->section;
|
||||
for ( ; section; section = section->next)
|
||||
if (section->name) {
|
||||
l=strlen (section->name);
|
||||
cursize+=l+1;
|
||||
if (cursize > size+1)
|
||||
l = strlen (section->name);
|
||||
cursize += l+1;
|
||||
if (cursize > size+1) {
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
return size-2;
|
||||
}
|
||||
strcpy (buf,section->name);
|
||||
buf+=l;
|
||||
*buf=0;
|
||||
buf += l;
|
||||
*buf = 0;
|
||||
buf++;
|
||||
}
|
||||
buf++;
|
||||
*buf=0;
|
||||
return (buf-buffer);
|
||||
ret = buf-buffer;
|
||||
}
|
||||
return FALSE;
|
||||
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1336,14 +1395,19 @@ DWORD WINAPI GetPrivateProfileSectionNamesW( LPWSTR buffer, DWORD size,
|
|||
BOOL WINAPI GetPrivateProfileStructA (LPCSTR section, LPCSTR key,
|
||||
LPVOID buf, UINT len, LPCSTR filename)
|
||||
{
|
||||
PROFILEKEY *k;
|
||||
BOOL ret = FALSE;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if (PROFILE_Open( filename )) {
|
||||
k=PROFILE_Find ( &CurProfile->section, section, key, FALSE);
|
||||
if (!k) return FALSE;
|
||||
PROFILEKEY *k = PROFILE_Find ( &CurProfile->section, section, key, FALSE);
|
||||
if (k) {
|
||||
lstrcpynA( buf, k->value, strlen(k->value));
|
||||
return TRUE;
|
||||
ret = TRUE;
|
||||
}
|
||||
}
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
@ -1378,13 +1442,23 @@ BOOL WINAPI GetPrivateProfileStructW (LPCWSTR section, LPCWSTR key,
|
|||
BOOL WINAPI WritePrivateProfileStructA (LPCSTR section, LPCSTR key,
|
||||
LPVOID buf, UINT bufsize, LPCSTR filename)
|
||||
{
|
||||
BOOL ret;
|
||||
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
if ((!section) && (!key) && (!buf)) { /* flush the cache */
|
||||
PROFILE_FlushFile();
|
||||
return FALSE;
|
||||
ret = FALSE;
|
||||
} else {
|
||||
if (!PROFILE_Open( filename ))
|
||||
ret = FALSE;
|
||||
else
|
||||
ret = PROFILE_SetString( section, key, buf);
|
||||
}
|
||||
|
||||
if (!PROFILE_Open( filename )) return FALSE;
|
||||
return PROFILE_SetString( section, key, buf);
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
|
@ -1411,5 +1485,7 @@ BOOL WINAPI WritePrivateProfileStructW (LPCWSTR section, LPCWSTR key,
|
|||
*/
|
||||
void WINAPI WriteOutProfiles16(void)
|
||||
{
|
||||
EnterCriticalSection( &PROFILE_CritSect );
|
||||
PROFILE_FlushFile();
|
||||
LeaveCriticalSection( &PROFILE_CritSect );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue