BOOL SetVariable( CStr &varName, CValue value ) { VarError.Empty(); int pos; CValue *val; if ( varName.GetLength() > 0 && varName.GetAt(0) == L'[' ) { CStr expression = varName.Mid( 1 ); CInterpreter parser; CValue result = parser.EvaluateExpression( expression, FALSE ); int subPos = parser.GetErrorPosition(); if ( parser.GetError() != 0 ) { VarError = CStr( L"Error in variable reference: " ) + parser.GetErrorMessage(); return FALSE; } if ( expression[subPos] != L']' ) { VarError = L"Variable reference not closed (missing ']')"; return FALSE; } val = GetVariable( (CStr)result, TRUE, &pos ); } else{ val = GetVariable( varName, TRUE, &pos ); } val->CopyFrom( value ); return (pos != -1); }
BOOL CIniFile::WriteFile( LPCTSTR filename, CStr &content, BOOL append, UINT cp ) { HANDLE file; char *buffer = NULL; char *tmpStr = NULL; ULONG iLen, feLen; BOOL rc = TRUE, comPort = FALSE; if ( filename[wcslen(filename)-1] == ':' ) { file = CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); if ( file != INVALID_HANDLE_VALUE ) SetComPort( filename, file ); comPort = TRUE; } else { CStr fileWithPath; fileWithPath = filename; if ( CurrentInterpreter != NULL && ( fileWithPath.GetLength() < 2 || ( fileWithPath.GetAt(0) != '\\' && fileWithPath.GetAt(1) != ':' ) ) ) { int len = CurrentInterpreter->ScriptFile.ReverseFind('\\'); if ( len == -1 ) fileWithPath = L"\\" + fileWithPath; else fileWithPath = CurrentInterpreter->ScriptFile.Left( len+1 ) + fileWithPath; } if ( append ) { file = CreateFile( fileWithPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( SetFilePointer( file, 0, NULL, FILE_END ) == 0 ) append = FALSE; } else file = CreateFile( fileWithPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); } if ( file == INVALID_HANDLE_VALUE ) rc = FALSE; if ( rc == TRUE ) { iLen = content.GetLength(); if ( cp != CP_UNICODE ) { tmpStr = new char[iLen*2+1]; // special characters might use 2 bytes! if ( tmpStr == NULL ) rc = FALSE; } else tmpStr = NULL; } if ( rc == TRUE ) { ULONG written; int stat; if ( cp != CP_UNICODE && cp != CP_UNICODE_PREFIX ) { if ( cp == CP_UTF8_PREFIX ) { if ( ! comPort && ! append ) ::WriteFile( file, (void*)"\xEF\xBB\xBF", 3, &written, NULL ); // omit the \0! cp = CP_UTF8; } feLen = WideCharToMultiByte( cp, 0, (LPCTSTR)content, -1, tmpStr, iLen*2+1, NULL, NULL ); if ( !comPort ) stat = ::WriteFile( file, (void*)tmpStr, feLen-1, &written, NULL ); // omit the \0! else { stat = 1, written = 1; for ( ULONG i=0; i<feLen-1 && stat != 0 && written == 1; i++ ) { stat = ::WriteFile( file, (void*)&(tmpStr[i]), 1, &written, NULL ); // omit the \0! } } } else { if ( cp == CP_UNICODE_PREFIX ) { if ( ! comPort && ! append ) ::WriteFile( file, (void*)"\xFF\xFE", 2, &written, NULL ); // omit the \0! } stat = ::WriteFile( file, (void*)(LPCTSTR)content, (content.GetLength())*2, &written, NULL ); // omit the \0! } if ( stat == 0 ) rc = FALSE; } if ( file != NULL ) CloseHandle( file ); if ( buffer != NULL ) content.ReleaseBuffer(); if ( tmpStr != NULL ) delete[] tmpStr; return rc; }
void CIniFile::Parse( LPCTSTR cont ) { CStr content = cont; CStr sectionName, key, value; CMapStrToString *section = NULL; BOOL hasEmptySection = FALSE; Sections.RemoveAll(); while ( content.GetLength() > 0 ) { CStr line; int pos = content.Find( _T("\n") ); if ( pos != -1 ) { line = content.Left( pos ); content = content.Mid( pos+1 ); } else { line = content; content = _T(""); } line.TrimLeft(); line.TrimRight(); if ( line.GetLength() > 0 && line.GetAt(0) != '#' && line.GetAt(0) != ';' ) { if ( line.GetAt(0) == '[' && line.Right(1) == L"]" ) { sectionName = line.Mid( 1, line.GetLength()-2 ); sectionName.MakeLower(); section = new CMapStrToString(); Sections.SetAt( sectionName, section ); } else { int eqPos = line.Find( '=' ); if ( eqPos != -1 ) { key = line.Left( eqPos ); key.TrimLeft(); key.TrimRight(); value = line.Mid( eqPos+1 ); value.TrimLeft(); value.TrimRight(); if ( value.Left(1) == L"\"" && value.Right(1) == L"\"" ) { value = value.Mid( 1, value.GetLength()-2 ); } key.MakeLower(); if ( section == NULL && hasEmptySection == FALSE ) { section = new CMapStrToString(); Sections.SetAt( L"", section ); hasEmptySection = TRUE; } section->SetAt( key, value ); } } } } }
int CIniFile::ReadFile( LPCTSTR filename, CStr &content, int size, int cp ) { HANDLE file = NULL; char *buffer = NULL; char *tmpStr = NULL; ULONG fileSize, readSize; ULONG bufSize; BOOL rc = 0, com; int timeout; if ( wcsnicmp( filename, L"COM", 3 ) == 0 && filename[wcslen(filename)-1] == ':' ) { file = CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); if ( file != INVALID_HANDLE_VALUE ) { timeout = SetComPort( filename, file ); } com = TRUE; } else { CStr fileWithPath; fileWithPath = filename; if ( CurrentInterpreter != NULL && ( fileWithPath.GetLength() < 2 || ( fileWithPath.GetAt(0) != '\\' && fileWithPath.GetAt(1) != ':' ) ) ) { int len = CurrentInterpreter->ScriptFile.ReverseFind('\\'); if ( len == -1 ) fileWithPath = L"\\" + fileWithPath; else fileWithPath = CurrentInterpreter->ScriptFile.Left( len+1 ) + fileWithPath; } file = CreateFile( fileWithPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); com = FALSE; } if ( file == INVALID_HANDLE_VALUE ) { int error = GetLastError(); rc = -2; } if ( rc == 0 ) { if ( size <= 0 ) { fileSize = GetFileSize( file, NULL ); } else { fileSize = size; } if ( fileSize > 1024 * 1024 || fileSize == -1 ) { // Too big rc = -3; } } if ( rc == 0 ) { bufSize = (fileSize+1)*sizeof(TCHAR); buffer = (char*)content.GetBufferSetLength( bufSize ); if ( buffer == NULL ) rc = -4; } if ( rc == 0 ) { tmpStr = new char[fileSize+2]; // if it's unicode, two binary zeroes are required at the end if ( tmpStr == NULL ) rc = -4; } if ( rc == 0 ) { if ( ! com ) { int stat = ::ReadFile( file, (void*)tmpStr, fileSize, &readSize, NULL ); if ( stat == 0 ) { int error = GetLastError(); rc = -5; } } else { comPort = &file; comTargetSize = fileSize+1; comTargetBuffer = tmpStr; DWORD dwThreadID; HANDLE readThread = CreateThread( NULL, 0, PortReadThread, 0, 0, &dwThreadID ); if ( readThread != NULL ) { WaitForSingleObject( readThread, timeout ); TerminateThread( readThread, 0 ); CloseHandle( readThread ); SetCommMask( file, 0 ); readSize = comReadSize; } else { rc = -6; readSize = 0; } } } if ( rc == 0 ) { if ( cp == CP_ACP ) { if ( readSize >= 3 && memcmp( tmpStr, (void*)"\xEF\xBB\xBF", 3 ) == 0 ) { cp = CP_UTF8_PREFIX; } if ( readSize >= 2 && memcmp( tmpStr, (void*)"\xFF\xFE", 2 ) == 0 ) { cp = CP_UNICODE_PREFIX; } } if ( cp != CP_UNICODE && cp != CP_UNICODE_PREFIX && cp != -1) { int offset = 0; // Strip first 3 bytes for prefixed UFT8 if ( cp == CP_UTF8_PREFIX ) { if ( memcmp( tmpStr, (void*)"\xEF\xBB\xBF", 3 ) == 0 ) { offset = 3; } cp = CP_UTF8; } // set binary zero at end tmpStr[readSize] = '\0'; MultiByteToWideChar( cp, 0, tmpStr+offset, readSize-offset+1, (LPTSTR)buffer, bufSize ); } else if (cp != -1) { int offset = 0; // Strip first 2 bytes for prefixed UFT8 if ( cp == CP_UNICODE_PREFIX ) { if ( memcmp( tmpStr, (void*)"\xFF\xFE", 2 ) == 0 ) { offset = 2; } cp = CP_UNICODE; } // for unicode, two binary zeroes are required at the end tmpStr[readSize] = '\0'; tmpStr[readSize+1] = '\0'; wcscpy( (TCHAR*)buffer, (TCHAR*)(tmpStr+offset) ); } } if ( file != NULL ) CloseHandle( file ); if ( buffer != NULL ) content.ReleaseBuffer(); if ( tmpStr != NULL ) delete[] tmpStr; return rc; }