/******************************************************************************** Copyright (C) 2001-2012 Hugh Bailey 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 "XT.h" //WARNING: LEAVE THIS FILE NOW OR YOU WILL BE FOREVER SCARRED BY THE HORRENDOUS CODE I WROTE 4000 YEARS AGO // // ...yes, I really need to rewrite this file, I know. /*========================================================= Config ===========================================================*/ BOOL ConfigFile::Create(CTSTR lpConfigFile) { strFileName = lpConfigFile; if(LoadFile(XFILE_CREATEALWAYS)) LoadData(); else return 0; return 1; } BOOL ConfigFile::Open(CTSTR lpConfigFile, BOOL bOpenAlways) { strFileName = lpConfigFile; if(LoadFile(bOpenAlways ? XFILE_OPENALWAYS : XFILE_OPENEXISTING)) LoadData(); else return 0; return 1; } BOOL ConfigFile::LoadFile(DWORD dwOpenMode) { XFile file; if(!file.Open(strFileName, XFILE_READ, dwOpenMode)) { //Log(TEXT("Couldn't load config file: \"%s\""), (TSTR)strFileName); return 0; } OSEnterMutex(hHorribleThreadSafetyMutex); if(bOpen) Close(); dwLength = (DWORD)file.GetFileSize(); if (dwLength >= 3) // remove BOM if present { char buff[3]; file.Read(&buff, 3); if (memcmp(buff, "\xEF\xBB\xBF", 3)) file.SetPos(0, XFILE_BEGIN); else dwLength -= 3; } LPSTR lpTempFileData = (LPSTR)Allocate(dwLength+5); file.Read(&lpTempFileData[2], dwLength); lpTempFileData[0] = lpTempFileData[dwLength+2] = 13; lpTempFileData[1] = lpTempFileData[dwLength+3] = 10; lpTempFileData[dwLength+4] = 0; file.Close(); lpFileData = utf8_createTstr(lpTempFileData); dwLength = slen(lpFileData); Free(lpTempFileData); bOpen = 1; OSLeaveMutex(hHorribleThreadSafetyMutex); return 1; } void ConfigFile::LoadData() { OSEnterMutex(hHorribleThreadSafetyMutex); TSTR lpCurLine = lpFileData, lpNextLine; ConfigSection *lpCurSection=NULL; DWORD i; lpNextLine = schr(lpCurLine, '\r'); while(*(lpCurLine = (lpNextLine+2))) { lpNextLine = schr(lpCurLine, '\r'); if (!lpNextLine) CrashError(TEXT("Your %s file is corrupt, please delete it and re-launch OBS."), strFileName.Array()); *lpNextLine = 0; if((*lpCurLine == '[') && (*(lpNextLine-1) == ']')) { lpCurSection = Sections.CreateNew(); lpCurSection->name = sfix(sdup(lpCurLine+1)); lpCurSection->name[lpNextLine-lpCurLine-2] = 0; } else if(lpCurSection && *lpCurLine && (*(LPWORD)lpCurLine != '//')) { TSTR lpValuePtr = schr(lpCurLine, '='); if (!lpValuePtr) CrashError(TEXT("Your %s file is corrupt, please delete it and re-launch OBS."), strFileName.Array()); if(lpValuePtr[1] != 0) { ConfigKey *key=NULL; *lpValuePtr = 0; for(i=0; iKeys.Num(); i++) { if(scmpi(lpCurLine, lpCurSection->Keys[i].name) == 0) { key = &lpCurSection->Keys[i]; break; } } if(!key) { key = lpCurSection->Keys.CreateNew(); key->name = sfix(sdup(lpCurLine)); } *lpValuePtr = '='; lpCurLine = lpValuePtr+1; TSTR value = sfix(sdup(lpCurLine)); key->ValueList << value; } } *lpNextLine = '\r'; } OSLeaveMutex(hHorribleThreadSafetyMutex); } void ConfigFile::Close() { OSEnterMutex(hHorribleThreadSafetyMutex); DWORD i,j,k; for(i=0; i &IntList) { OSEnterMutex(hHorribleThreadSafetyMutex); assert(lpSection); assert(lpKey); DWORD i,j,k; BOOL bFoundKey = 0; for(i=0; i &FloatList) { OSEnterMutex(hHorribleThreadSafetyMutex); assert(lpSection); assert(lpKey); DWORD i,j,k; BOOL bFoundKey = 0; for(i=0; i &ColorList) { OSEnterMutex(hHorribleThreadSafetyMutex); assert(lpSection); assert(lpKey); DWORD i,j,k; BOOL bFoundKey = 0; for(i=0; i &IntList) { while(HasKey(lpSection, lpKey)) Remove(lpSection, lpKey); for(unsigned int i=0; i &FloatList) { while(HasKey(lpSection, lpKey)) Remove(lpSection, lpKey); for(unsigned int i=0; i &ColorList) { while(HasKey(lpSection, lpKey)) Remove(lpSection, lpKey); for(unsigned int i=0; i