jamulus/src/settings.cpp

459 lines
13 KiB
C++
Raw Normal View History

2006-01-28 12:29:22 +01:00
/******************************************************************************\
* Copyright (c) 2004-2006
*
* Author(s):
* Volker Fischer, Robert Kesterson
2006-01-28 12:29:22 +01:00
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#include "settings.h"
/* Implementation *************************************************************/
void CSettings::Load ( std::string sFileName )
2006-01-28 12:29:22 +01:00
{
/* load settings from init-file */
ReadIniFile ( sFileName );
2006-01-28 12:29:22 +01:00
}
void CSettings::Save ( std::string sFileName )
2006-01-28 12:29:22 +01:00
{
/* write settings in init-file */
WriteIniFile ( sFileName );
2006-01-28 12:29:22 +01:00
}
/* Read and write init-file ***************************************************/
void CSettings::ReadIniFile ( std::string sFileName )
2006-01-28 12:29:22 +01:00
{
int iValue;
bool bValue;
INIFile ini;
// load data from init-file
if ( !sFileName.empty() )
{
ini = LoadIni ( sFileName.c_str() );
}
else
{
// use default file name
ini = LoadIni ( LLCON_INIT_FILE_NAME );
}
// IP address
2008-01-22 22:15:04 +01:00
pClient->strIPAddress = GetIniSetting ( ini, "Client", "ipaddress" ).c_str();
2006-12-10 12:06:14 +01:00
// name
2008-01-22 22:15:04 +01:00
pClient->strName = GetIniSetting ( ini, "Client", "name" ).c_str();
2006-12-10 12:06:14 +01:00
// audio fader
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet ( ini, "Client", "audfad", 0, AUD_FADER_IN_MAX, iValue ) == TRUE )
{
pClient->SetAudioInFader ( iValue );
}
// reverberation level
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet(ini, "Client", "revlev", 0, AUD_REVERB_MAX, iValue ) == TRUE )
{
pClient->SetReverbLevel ( iValue );
}
// reverberation channel assignment
2007-09-08 12:45:14 +02:00
if ( GetFlagIniSet ( ini, "Client", "reverblchan", bValue ) == TRUE )
{
pClient->SetReverbOnLeftChan ( bValue );
}
// sound card in number of buffers
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet ( ini, "Client", "audinbuf", 0, AUD_SLIDER_LENGTH, iValue ) == TRUE )
{
pClient->GetSndInterface()->SetInNumBuf( iValue );
}
// sound card out number of buffers
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet ( ini, "Client", "audoutbuf", 0, AUD_SLIDER_LENGTH, iValue ) == TRUE )
{
pClient->GetSndInterface()->SetOutNumBuf ( iValue );
}
// network jitter buffer size
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet ( ini, "Client", "jitbuf", 0, MAX_NET_BUF_SIZE_NUM_BL, iValue ) == TRUE )
{
pClient->SetSockBufSize ( iValue );
}
// network buffer size factor in
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet ( ini, "Client", "netwbusifactin", 1, MAX_NET_BLOCK_SIZE_FACTOR, iValue ) == TRUE )
{
pClient->SetNetwBufSizeFactIn ( iValue );
}
// network buffer size factor out
2007-09-08 12:45:14 +02:00
if ( GetNumericIniSet ( ini, "Client", "netwbusifactout", 1, MAX_NET_BLOCK_SIZE_FACTOR, iValue ) == TRUE )
{
pClient->SetNetwBufSizeFactOut ( iValue );
}
2006-01-28 12:29:22 +01:00
}
void CSettings::WriteIniFile ( std::string sFileName )
2006-01-28 12:29:22 +01:00
{
INIFile ini;
2006-01-28 12:29:22 +01:00
// IP address
2008-01-22 22:15:04 +01:00
PutIniSetting ( ini, "Client", "ipaddress", pClient->strIPAddress.toStdString().c_str() );
2006-01-28 12:29:22 +01:00
2006-12-10 12:06:14 +01:00
// name
2008-01-22 22:15:04 +01:00
PutIniSetting ( ini, "Client", "name", pClient->strName.toStdString().c_str() );
2006-12-10 12:06:14 +01:00
// audio fader
SetNumericIniSet ( ini, "Client", "audfad", pClient->GetAudioInFader() );
2006-01-28 12:29:22 +01:00
// reverberation level
SetNumericIniSet ( ini, "Client", "revlev", pClient->GetReverbLevel() );
2006-01-28 12:29:22 +01:00
// reverberation channel assignment
SetFlagIniSet ( ini, "Client", "reverblchan", pClient->IsReverbOnLeftChan() );
2006-01-28 12:29:22 +01:00
// sound card in number of buffers
SetNumericIniSet ( ini, "Client", "audinbuf", pClient->GetSndInterface()->GetInNumBuf() );
2006-01-28 12:29:22 +01:00
// sound card out number of buffers
SetNumericIniSet ( ini, "Client", "audoutbuf", pClient->GetSndInterface()->GetOutNumBuf() );
2006-01-28 12:29:22 +01:00
// network jitter buffer size
SetNumericIniSet ( ini, "Client", "jitbuf", pClient->GetSockBufSize() );
// network buffer size factor in
SetNumericIniSet ( ini, "Client", "netwbusifactin", pClient->GetNetwBufSizeFactIn() );
// network buffer size factor out
SetNumericIniSet ( ini, "Client", "netwbusifactout", pClient->GetNetwBufSizeFactOut() );
2006-01-28 12:29:22 +01:00
// save settings in init-file
if ( !sFileName.empty() )
{
SaveIni ( ini, sFileName.c_str() );
}
else
{
// use default file name
SaveIni ( ini, LLCON_INIT_FILE_NAME );
}
2006-01-28 12:29:22 +01:00
}
bool CSettings::GetNumericIniSet ( INIFile& theINI, string strSection,
2007-09-08 12:45:14 +02:00
string strKey, int iRangeStart,
int iRangeStop, int& iValue )
2006-01-28 12:29:22 +01:00
{
/* Init return value */
bool bReturn = FALSE;
2006-01-28 12:29:22 +01:00
const string strGetIni =
GetIniSetting ( theINI, strSection.c_str(), strKey.c_str() );
2006-01-28 12:29:22 +01:00
/* Check if it is a valid parameter */
if ( !strGetIni.empty () )
{
iValue = atoi( strGetIni.c_str () );
2006-01-28 12:29:22 +01:00
/* Check range */
if ( ( iValue >= iRangeStart ) && ( iValue <= iRangeStop ) )
{
bReturn = TRUE;
}
}
2006-01-28 12:29:22 +01:00
return bReturn;
2006-01-28 12:29:22 +01:00
}
void CSettings::SetNumericIniSet ( INIFile& theINI, string strSection,
string strKey, int iValue )
2006-01-28 12:29:22 +01:00
{
char cString[256];
2006-01-28 12:29:22 +01:00
sprintf ( cString, "%d", iValue );
PutIniSetting ( theINI, strSection.c_str(), strKey.c_str(), cString );
2006-01-28 12:29:22 +01:00
}
bool CSettings::GetFlagIniSet ( INIFile& theINI, string strSection,
string strKey, bool& bValue )
2006-01-28 12:29:22 +01:00
{
/* Init return value */
bool bReturn = FALSE;
const string strGetIni =
GetIniSetting ( theINI, strSection.c_str(), strKey.c_str() );
if ( !strGetIni.empty() )
{
if ( atoi ( strGetIni.c_str() ) )
{
bValue = TRUE;
}
else
{
bValue = FALSE;
}
bReturn = TRUE;
}
return bReturn;
2006-01-28 12:29:22 +01:00
}
void CSettings::SetFlagIniSet ( INIFile& theINI, string strSection, string strKey,
bool bValue )
2006-01-28 12:29:22 +01:00
{
if ( bValue == TRUE )
{
PutIniSetting ( theINI, strSection.c_str(), strKey.c_str(), "1" );
}
else
{
PutIniSetting ( theINI, strSection.c_str(), strKey.c_str(), "0" );
}
2006-01-28 12:29:22 +01:00
}
/* INI File routines using the STL ********************************************/
/* The following code was taken from "INI File Tools (STLINI)" written by
Robert Kesterson in 1999. The original files are stlini.cpp and stlini.h.
The homepage is http://robertk.com/source
Copyright August 18, 1999 by Robert Kesterson */
#ifdef _MSC_VER
/* These pragmas are to quiet VC++ about the expanded template identifiers
exceeding 255 chars. You won't be able to see those variables in a debug
session, but the code will run normally */
2007-09-08 12:45:14 +02:00
#pragma warning ( push )
#pragma warning ( disable : 4786 4503 )
2006-01-28 12:29:22 +01:00
#endif
2008-01-22 22:15:04 +01:00
std::string CSettings::GetIniSetting ( CSettings::INIFile& theINI, const char* section,
const char* key, const char* defaultval )
2006-01-28 12:29:22 +01:00
{
2008-01-22 22:15:04 +01:00
std::string result ( defaultval );
INIFile::iterator iSection = theINI.find ( std::string ( section ) );
2006-01-28 12:29:22 +01:00
2007-09-08 12:45:14 +02:00
if ( iSection != theINI.end() )
{
2008-01-22 22:15:04 +01:00
INISection::iterator apair = iSection->second.find ( std::string ( key ) );
2006-01-28 12:29:22 +01:00
2007-09-08 12:45:14 +02:00
if ( apair != iSection->second.end() )
{
result = apair->second;
2007-09-08 12:45:14 +02:00
}
}
2006-01-28 12:29:22 +01:00
return result;
2006-01-28 12:29:22 +01:00
}
2007-09-08 12:45:14 +02:00
void CSettings::PutIniSetting ( CSettings::INIFile &theINI, const char *section,
const char *key, const char *value )
2006-01-28 12:29:22 +01:00
{
INIFile::iterator iniSection;
INISection::iterator apair;
2008-01-22 22:15:04 +01:00
if ( ( iniSection = theINI.find ( std::string ( section ) ) ) == theINI.end() )
{
2008-01-22 22:15:04 +01:00
// no such section? Then add one
INISection newsection;
if (key)
{
2007-09-08 12:45:14 +02:00
newsection.insert (
2008-01-22 22:15:04 +01:00
std::pair<std::string, std::string> ( std::string ( key ), std::string ( value ) ) );
}
2007-09-08 12:45:14 +02:00
theINI.insert (
2008-01-22 22:15:04 +01:00
std::pair<std::string, INISection> ( std::string ( section ), newsection ) );
}
2007-09-08 12:45:14 +02:00
else
{
if ( key )
{
/* Found section, make sure key isn't in there already,
if it is, just drop and re-add */
apair = iniSection->second.find ( string ( key ) );
if ( apair != iniSection->second.end() )
{
iniSection->second.erase(apair);
}
iniSection->second.insert (
2008-01-22 22:15:04 +01:00
std::pair<std::string, std::string> ( std::string ( key ), std::string ( value ) ) );
2007-09-08 12:45:14 +02:00
}
}
2006-01-28 12:29:22 +01:00
}
2007-09-08 12:45:14 +02:00
CSettings::INIFile CSettings::LoadIni ( const char* filename )
2006-01-28 12:29:22 +01:00
{
INIFile theINI;
char *value, *temp;
2008-01-22 22:15:04 +01:00
std::string section;
char buffer[MAX_INI_LINE];
2007-09-08 12:45:14 +02:00
std::fstream file ( filename, std::ios::in );
2007-09-08 12:45:14 +02:00
while ( file.good() )
{
2007-09-08 12:45:14 +02:00
memset ( buffer, 0, sizeof ( buffer ) );
file.getline ( buffer, sizeof ( buffer ) );
2007-09-08 12:45:14 +02:00
if ( ( temp = strchr ( buffer, '\n' ) ) )
{
2008-01-22 22:15:04 +01:00
*temp = '\0'; // cut off at newline
2007-09-08 12:45:14 +02:00
}
2007-09-08 12:45:14 +02:00
if ( ( temp = strchr ( buffer, '\r' ) ) )
{
2008-01-22 22:15:04 +01:00
*temp = '\0'; // cut off at linefeeds
2007-09-08 12:45:14 +02:00
}
2007-09-08 12:45:14 +02:00
if ( ( buffer[0] == '[' ) && ( temp = strrchr ( buffer, ']' ) ) )
2008-01-22 22:15:04 +01:00
{ // if line is like --> [section name]
*temp = '\0'; // chop off the trailing ']'
section = &buffer[1];
2008-01-22 22:15:04 +01:00
PutIniSetting ( theINI, &buffer[1] ); // start new section
}
2007-09-08 12:45:14 +02:00
else
{
if ( buffer[0] && ( value = strchr ( buffer, '=' ) ) )
{
2008-01-22 22:15:04 +01:00
// assign whatever follows = sign to value, chop at "="
2007-09-08 12:45:14 +02:00
*value++ = '\0';
2008-01-22 22:15:04 +01:00
// and add both sides to INISection
2007-09-08 12:45:14 +02:00
PutIniSetting ( theINI, section.c_str(), buffer, value );
}
else
{
if ( buffer[0] )
{
2008-01-22 22:15:04 +01:00
// must be a comment or something
2007-09-08 12:45:14 +02:00
PutIniSetting ( theINI, section.c_str(), buffer, "" );
}
}
}
}
2007-09-08 12:45:14 +02:00
return theINI;
2006-01-28 12:29:22 +01:00
}
2007-09-08 12:45:14 +02:00
void CSettings::SaveIni ( CSettings::INIFile &theINI, const char* filename )
2006-01-28 12:29:22 +01:00
{
2008-01-22 22:15:04 +01:00
bool bFirstSection = TRUE; // init flag
2007-09-08 12:45:14 +02:00
std::fstream file ( filename, std::ios::out );
if ( !file.good() )
{
return;
2007-09-08 12:45:14 +02:00
}
2008-01-22 22:15:04 +01:00
// just iterate the hashes and values and dump them to a file
INIFile::iterator section = theINI.begin();
2007-09-08 12:45:14 +02:00
while ( section != theINI.end() )
{
2007-09-08 12:45:14 +02:00
if ( section->first > "" )
{
2007-09-08 12:45:14 +02:00
if ( bFirstSection == TRUE )
{
2008-01-22 22:15:04 +01:00
// do not put a newline at the beginning of the first section
file << "[" << section->first << "]" << std::endl;
2008-01-22 22:15:04 +01:00
// reset flag
bFirstSection = FALSE;
}
else
2007-09-08 12:45:14 +02:00
{
file << std::endl << "[" << section->first << "]" << std::endl;
2007-09-08 12:45:14 +02:00
}
}
INISection::iterator pair = section->second.begin();
2007-09-08 12:45:14 +02:00
while ( pair != section->second.end() )
{
2007-09-08 12:45:14 +02:00
if ( pair->second > "" )
{
file << pair->first << "=" << pair->second << std::endl;
2007-09-08 12:45:14 +02:00
}
else
2007-09-08 12:45:14 +02:00
{
file << pair->first << "=" << std::endl;
2007-09-08 12:45:14 +02:00
}
pair++;
}
section++;
}
file.close();
2006-01-28 12:29:22 +01:00
}
/* Return true or false depending on whether the first string is less than the
second */
2008-01-22 22:15:04 +01:00
bool CSettings::StlIniCompareStringNoCase::operator() ( const std::string& x,
const std::string& y ) const
2006-01-28 12:29:22 +01:00
{
#ifdef WIN32
2007-09-08 12:45:14 +02:00
return ( stricmp ( x.c_str(), y.c_str() ) < 0 ) ? true : false;
2006-01-28 12:29:22 +01:00
#else
#ifdef strcasecmp
2007-09-08 12:45:14 +02:00
return ( strcasecmp ( x.c_str(), y.c_str() ) < 0 ) ? true : false;
2006-01-28 12:29:22 +01:00
#else
unsigned nCount = 0;
int nResult = 0;
const char *p1 = x.c_str();
const char *p2 = y.c_str();
2007-09-08 12:45:14 +02:00
while ( *p1 && *p2 )
{
2007-09-08 12:45:14 +02:00
nResult = toupper ( *p1 ) - toupper ( *p2 );
if ( nResult != 0 )
{
break;
2007-09-08 12:45:14 +02:00
}
p1++;
p2++;
nCount++;
}
2007-09-08 12:45:14 +02:00
if ( nResult == 0 )
{
2007-09-08 12:45:14 +02:00
if ( *p1 && !*p2 )
{
nResult = -1;
2007-09-08 12:45:14 +02:00
}
if ( !*p1 && *p2 )
{
nResult = 1;
2007-09-08 12:45:14 +02:00
}
}
2007-09-08 12:45:14 +02:00
if ( nResult < 0 )
{
return true;
2007-09-08 12:45:14 +02:00
}
return false;
2006-01-28 12:29:22 +01:00
#endif /* strcasecmp */
#endif
}
#ifdef _MSC_VER
2007-09-08 12:45:14 +02:00
#pragma warning ( pop )
2006-01-28 12:29:22 +01:00
#endif