const bool Exists(const CString& strFilename){ DWORD dwAttr = GetFileAttributesW(strFilename.GetPointer()); if(dwAttr != INVALID_FILE_ATTRIBUTES && !(dwAttr & FILE_ATTRIBUTE_DIRECTORY)){ return true; } return false; }
bool IDockScreenDisplay::EvalBool (const CString &sCode, bool *retbResult, CString *retsError) // EvalBool // // Evaluates the given string { CCodeChainCtx Ctx; Ctx.SetScreen(m_pDockScreen); Ctx.SaveAndDefineSourceVar(m_pLocation); Ctx.SaveAndDefineDataVar(m_pData); char *pPos = sCode.GetPointer(); ICCItem *pExp = Ctx.Link(sCode, 1, NULL); ICCItem *pResult = Ctx.Run(pExp); // LATER:Event Ctx.Discard(pExp); if (pResult->IsError()) { *retsError = pResult->GetStringValue(); Ctx.Discard(pResult); return false; } *retbResult = !pResult->IsNil(); Ctx.Discard(pResult); return true; }
CWindow::CWindow(CRefPtr<CWindowManager> pMng, const CString& strClass, const CString& strTitle, const Window::Style uStyle, const Math::CSize& Size, const Math::CPoint& Position) : Manage::IManagedObject<CWindowManager, CWindow>(pMng), m_hWindow(0), m_uStyle(uStyle) { Log::Write(L"Initializing window " + strTitle + L" of class " + strClass + L".", Log::LogLevel::Debug); DWORD dwStyle = 0, dwExStyle = 0; if(!GetMSWindowStyle(uStyle, dwStyle, dwExStyle)){ throw Exception::CInvalidArgumentException(L"uStyle", String::ToString(uStyle), L"Unrecognized window style.", CR_INFO()); } Log::Write(L"Selected window style: " + String::ToString(uStyle) + L".", Log::LogLevel::Debug); HINSTANCE hInstance = GetModuleHandleW(0); RECT winRect = {0, 0, Size.Width, Size.Height}; if(!AdjustWindowRectEx(&winRect, dwStyle, FALSE, dwExStyle)){ CR_THROW(L"Failed to adjust client area rect."); } this->m_hWindow = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, strClass.GetPointer(), strTitle.GetPointer(), WS_OVERLAPPEDWINDOW, Position.X, Position.Y, winRect.right - winRect.left, winRect.bottom - winRect.top, 0, 0, hInstance, this); if(this->m_hWindow == 0 || this->m_hWindow == INVALID_HANDLE_VALUE){ throw Exception::CWindowException(GetLastError(), L"Fatal error durring window creation.", CR_INFO()); } this->OnClose.SetCombiner(ORCombiner); this->OnCreate.SetCombiner(ORCombiner); this->OnEvent.SetCombiner(ORCombiner); this->OnFocusChange.SetCombiner(ORCombiner); this->OnFocusGain.SetCombiner(ORCombiner); this->OnFocusLost.SetCombiner(ORCombiner); this->OnPositionChange.SetCombiner(ORCombiner); this->OnSizeChange.SetCombiner(ORCombiner); this->OnVisibilityChange.SetCombiner(ORCombiner); this->OnChar.SetCombiner(ORCombiner); this->OnKeyDown.SetCombiner(ORCombiner); this->OnKeyUp.SetCombiner(ORCombiner); this->OnMouseMove.SetCombiner(ORCombiner); this->OnMouseButtonUp.SetCombiner(ORCombiner); this->OnMouseButtonDown.SetCombiner(ORCombiner); Log::Write(L"Window initialized.", Log::LogLevel::Debug); }
ALERROR CHighScoreList::Save (const CString &sFilename) // Save // // Save the high score list { ALERROR error; if (m_bModified) { CFileWriteStream DataFile(sFilename, FALSE); if (error = DataFile.Create()) return error; // Write the XML header CString sData = strPatternSubst(CONSTLIT("<?xml version=\"1.0\"?>\r\n\r\n<TranscendenceHighScores lastPlayerName=\"%s\" lastPlayerGenome=\"%d\">\r\n\r\n"), m_sMostRecentPlayerName, m_iMostRecentPlayerGenome); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; // Loop over scores for (int i = 0; i < m_iCount; i++) { if (error = m_List[i].WriteToXML(DataFile)) return error; } // Done sData = CONSTLIT("\r\n</TranscendenceHighScores>\r\n"); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; if (error = DataFile.Close()) return error; } return NOERROR; }
ParserCtx::ParserCtx (ParserCtx *pParentCtx, const CString &sString) : m_pController(pParentCtx->m_pController) { pPos = sString.GetPointer(); pEndPos = pPos + sString.GetLength(); pElement = NULL; iToken = tkEOF; iLine = 1; m_bParseRootElement = false; m_bParseRootTag = false; m_pParentCtx = pParentCtx; }
ALERROR CMarkovWordGenerator::WriteAsXML (IWriteStream *pOutput) // WriteAsXML // // Writes out the Markov chain data to an XML element { ALERROR error; int i; // Open tag CString sData; sData = CONSTLIT("\t<WordGenerator>\r\n"); if (error = pOutput->Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; // Fragments for (i = 0; i < m_Table.GetCount(); i++) { sData = CONSTLIT("\t\t<Syl>"); if (error = pOutput->Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; sData = strPatternSubst(CONSTLIT("%s;%d;%d;"), strToXMLText(CString(m_Table[i]->sFrag)), m_Table[i]->dwCount, m_Table[i]->dwFlags); if (error = pOutput->Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; SChainChar *pChain = GetChain(m_Table[i]); while ((*(DWORD *)pChain) != 0) { char chChar[2]; chChar[0] = pChain->chChar; chChar[1] = '\0'; CString sChar = strToXMLText(CString(chChar, 1, true)); sData = strPatternSubst(CONSTLIT("%s;%d;"), sChar, pChain->dwCount); if (error = pOutput->Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; pChain++; } sData = CONSTLIT("</Syl>\r\n"); if (error = pOutput->Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; } // Done // Close tag sData = CONSTLIT("\t</WordGenerator>\r\n"); if (error = pOutput->Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; return NOERROR; }
bool CCodeChainCtx::RunEvalString (const CString &sString, bool bPlain, CString *retsResult) // RunString // // If sString starts with '=' or if bPlain is TRUE, then we evaluate sString as an // expression. If success (no error) we return TRUE. Otherwise, we return FALSE and // the error is in retsResult. { char *pPos = sString.GetPointer(); if (bPlain || *pPos == '=') { ICCItem *pExp = Link(sString, (bPlain ? 0 : 1), NULL); ICCItem *pResult = Run(pExp); // LATER:Event Discard(pExp); if (pResult->IsError()) { *retsResult = pResult->GetStringValue(); Discard(pResult); return false; } // Note: We use GetStringValue instead of Unlink because we don't // want to preserve CC semantics (e.g., we don't need strings to // be quoted). *retsResult = pResult->GetStringValue(); Discard(pResult); return true; } else { *retsResult = strCEscapeCodes(sString); return true; } }
ALERROR CDataFile::WriteEntry (int iEntry, const CString &sData) // WriteEntry // // Does some stuff { ALERROR error; ASSERT(IsOpen()); ASSERT(!m_fReadOnly); // Make sure we're in bounds if (iEntry < 0 || iEntry >= m_iEntryTableCount) return ERR_FAIL; if (m_pEntryTable[iEntry].dwBlock == FREE_ENTRY) return ERR_FAIL; // Resize the entry if ((error = ResizeEntry(iEntry, sData.GetLength(), NULL))) return error; // Write out the data. if ((error = WriteBlockChain(m_pEntryTable[iEntry].dwBlock, sData.GetPointer(), sData.GetLength()))) return error; // Flush if ((error = Flush())) return error; return NOERROR; }
ALERROR CDataFile::AddEntry (const CString &sData, int *retiEntry) // AddEntry // // Does some stuff { ALERROR error; int i, iEntry; DWORD dwStartingBlock; DWORD dwBlockCount; ASSERT(IsOpen()); ASSERT(!m_fReadOnly); // Look for a free entry for (i = 0; i < m_iEntryTableCount; i++) if (m_pEntryTable[i].dwBlock == FREE_ENTRY) break; // If we could not find a free entry, grow the entry table if (i == m_iEntryTableCount) { if ((error = GrowEntryTable(&iEntry))) goto Fail; } else iEntry = i; // Figure out how many blocks we need dwBlockCount = (sData.GetLength() / m_iBlockSize) + 1; // Allocate a block chain large enough to contain the entry if ((error = AllocBlockChain(dwBlockCount, &dwStartingBlock))) goto Fail; // Write the block chain if ((error = WriteBlockChain(dwStartingBlock, sData.GetPointer(), sData.GetLength()))) { FreeBlockChain(dwStartingBlock, dwBlockCount); goto Fail; } // Set the entry m_pEntryTable[iEntry].dwBlock = dwStartingBlock; m_pEntryTable[iEntry].dwBlockCount = dwBlockCount; m_pEntryTable[iEntry].dwSize = (DWORD)sData.GetLength(); m_pEntryTable[iEntry].dwFlags = 0; m_fEntryTableModified = TRUE; // Flush if ((error = Flush())) goto Fail; // Done *retiEntry = iEntry; return NOERROR; Fail: return error; }
void CUIHelper::GenerateDockScreenRTF (const CString &sText, CString *retsRTF) const // GenerateDockScreenRTF // // Converts from plain text to RTF. { CMemoryWriteStream Output; if (Output.Create() != NOERROR) return; // LATER: Get this from VI. We can't right now because it only stores // 16-bit colors. DWORD wQuoteColor = RGB(178, 217, 255); // H:210 S:30 B:100 CString sCloseQuote = CONSTLIT("”}"); // Start with the RTF open Output.Write("{/rtf ", 6); // Parse bool bInQuotes = false; char *pPos = sText.GetASCIIZPointer(); while (*pPos != '\0') { if (*pPos == '\"') { if (bInQuotes) { Output.Write(sCloseQuote.GetPointer(), sCloseQuote.GetLength()); bInQuotes = false; } else { CString sQuoteStart = strPatternSubst(CONSTLIT("{/c:0x%08x; “"), wQuoteColor); Output.Write(sQuoteStart.GetASCIIZPointer(), sQuoteStart.GetLength()); bInQuotes = true; } } else if (*pPos == '\\') Output.Write("/\\", 2); else if (*pPos == '/') Output.Write("//", 2); else if (*pPos == '{') Output.Write("/{", 2); else if (*pPos == '}') Output.Write("/}", 2); else if (*pPos == '\n') { if (bInQuotes) { // Look ahead and see if the next paragraph starts with a quote. If it does, then // we close here. char *pScan = pPos + 1; while (*pScan != '\0' && (*pScan == '\n' || *pScan == ' ')) pScan++; if (*pScan == '\"') { Output.Write("}", 1); bInQuotes = false; } } Output.Write("/n", 2); } else Output.Write(pPos, 1); pPos++; } // Make sure we always match if (bInQuotes) Output.Write("}", 1); // Close Output.Write("}", 1); // Done *retsRTF = CString(Output.GetPointer(), Output.GetLength()); }
bool CMission::ParseCriteria (const CString &sCriteria, SCriteria *retCriteria) // ParseCriteria // // Parses criteria. Returns TRUE if successful. { // Initialize *retCriteria = SCriteria(); // Parse char *pPos = sCriteria.GetPointer(); while (*pPos != '\0') { switch (*pPos) { case '*': retCriteria->bIncludeOpen = true; retCriteria->bIncludeUnavailable = true; retCriteria->bIncludeActive = true; retCriteria->bIncludeRecorded = true; break; case 'a': retCriteria->bIncludeActive = true; break; case 'o': retCriteria->bIncludeOpen = true; break; case 'r': retCriteria->bIncludeRecorded = true; break; case 'u': retCriteria->bIncludeUnavailable = true; break; case 'S': retCriteria->bOnlySourceOwner = true; break; case '+': case '-': { bool bRequired = (*pPos == '+'); bool bBinaryParam; CString sParam = ParseCriteriaParam(&pPos, false, &bBinaryParam); if (bRequired) { if (bBinaryParam) retCriteria->SpecialRequired.Insert(sParam); else retCriteria->AttribsRequired.Insert(sParam); } else { if (bBinaryParam) retCriteria->SpecialNotAllowed.Insert(sParam); else retCriteria->AttribsNotAllowed.Insert(sParam); } break; } } pPos++; } // Make sure we include some missions if (!retCriteria->bIncludeUnavailable && !retCriteria->bIncludeActive && !retCriteria->bIncludeRecorded && !retCriteria->bIncludeOpen) { retCriteria->bIncludeUnavailable = true; retCriteria->bIncludeActive = true; retCriteria->bIncludeRecorded = true; retCriteria->bIncludeOpen = true; } return true; }
ALERROR CGameSettings::Save (const CString &sFilespec) // Save // // Save game settings to a file (if necessary) { ALERROR error; if (!m_bModified) return NOERROR; // Create the file CFileWriteStream DataFile(sFilespec, FALSE); if (error = DataFile.Create()) return error; // Write the XML header CString sData = CONSTLIT("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n\r\n<TranscendenceSettings>\r\n\r\n"); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; // Loop over options for (int i = 0; i < OPTIONS_COUNT; i++) { // Don't bother saving if our current value is the same // as the default value if (strEquals(m_Options[i].sSettingsValue, CString(g_OptionData[i].pszDefaultValue, -1, true))) continue; // Compose option element and write sData = strPatternSubst(CONSTLIT("\t<Option name=\"%s\"\tvalue=\"%s\"/>\r\n"), CString(g_OptionData[i].pszName, -1, true), m_Options[i].sSettingsValue); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; } // Write the key map if (error = DataFile.Write("\r\n", 2, NULL)) return error; if (error = m_KeyMap.WriteAsXML(&DataFile)) return error; // Write additional settings if (m_pExtra) { if (error = DataFile.Write("\r\n", 2, NULL)) return error; if (error = m_pExtra->OnSaveSettings(&DataFile)) return error; } // Done sData = CONSTLIT("\r\n</TranscendenceSettings>\r\n"); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; if (error = DataFile.Close()) return error; return NOERROR; }
void CWindow::SetTitle(const CString& strTitle){ if(!SetWindowTextW(this->m_hWindow, strTitle.GetPointer())){ throw Exception::CWindowException(GetLastError(), L"Failed to set new window title, Title: " + strTitle, CR_INFO()); } }
const bool Delete(const CString& strFilename){ if(::DeleteFileW(strFilename.GetPointer())){ return true; } return false; }
ALERROR CGameSettings::Save (const CString &sFilespec) // Save // // Save game settings to a file (if necessary) { int i; ALERROR error; if (!m_bModified) return NOERROR; // Settings file CString sSettingsFilespec = pathAddComponent(m_sAppData, sFilespec); // Create the file CFileWriteStream DataFile(sSettingsFilespec, FALSE); if (error = DataFile.Create()) return error; // Write the XML header CString sData = CONSTLIT("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n\r\n<TranscendenceSettings>\r\n\r\n"); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; // Write extension folders if (m_ExtensionFolders.GetCount() > 0) { for (i = 0; i < m_ExtensionFolders.GetCount(); i++) { sData = strPatternSubst(CONSTLIT("\t<ExtensionFolder path=\"%s\"/>\r\n"), m_ExtensionFolders[i]); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength())) return error; } if (error = DataFile.Write("\r\n", 2, NULL)) return error; } // Loop over options for (i = 0; i < OPTIONS_COUNT; i++) { // Compose option element and write sData = strPatternSubst(CONSTLIT("\t<Option name=\"%s\"\tvalue=\"%s\"/>\r\n"), CString(g_OptionData[i].pszName, -1, true), strToXMLText(m_Options[i].sSettingsValue)); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; } // Write the key map if (error = DataFile.Write("\r\n", 2, NULL)) return error; if (error = m_KeyMap.WriteAsXML(&DataFile)) return error; // Write the extensions list if (error = DataFile.Write("\r\n", 2)) return error; if (error = m_Extensions.WriteAsXML(&DataFile)) return error; // Write additional settings if (m_pExtra) { if (error = DataFile.Write("\r\n", 2, NULL)) return error; if (error = m_pExtra->OnSaveSettings(&DataFile)) return error; } // Done sData = CONSTLIT("\r\n</TranscendenceSettings>\r\n"); if (error = DataFile.Write(sData.GetPointer(), sData.GetLength(), NULL)) return error; if (error = DataFile.Close()) return error; return NOERROR; }
void CCommandLineDisplay::AutoCompleteSearch (void) // AutocompleteSearch // // Searches the global symbol table for matches to the current command. { const CString sCurCmd = GetCurrentCmd(); CString sCommon; CString sHelp; ClearHint(); if (sCurCmd.IsBlank()) return; // Get the list of global symbols ICCItem *pGlobals = g_pUniverse->GetCC().GetGlobals(); int iMatches = 0; for (int i = 0; i < pGlobals->GetCount(); i++) { CString sGlobal = pGlobals->GetKey(i); // Partial match if (strStartsWith(sGlobal, sCurCmd)) { if (iMatches == 0) sCommon = sGlobal; // If we have multiple matching commands then find the longest common stem else { int iLen = min(sCommon.GetLength(), sGlobal.GetLength()); char *pPos1 = sCommon.GetPointer(); char *pPos2 = sGlobal.GetPointer(); int i; for (i = 0; i < iLen; i++) { if (CharLower((LPTSTR)(BYTE)(*pPos1)) != CharLower((LPTSTR)(BYTE)(*pPos2))) break; pPos1++; pPos2++; } sCommon.Truncate(i); m_sHint.Append(CONSTLIT(" ")); } // Append the command to the auto complete hint m_sHint.Append(sGlobal); iMatches++; } if (strEquals(sGlobal, sCurCmd)) { // Exact match - get help text ICCItem *pItem = pGlobals->GetElement(i); if (pItem->IsPrimitive()) sHelp = pItem->GetHelp(); } } // If the common stem is longer than the current command, then auto complete if (sCommon.GetLength() > sCurCmd.GetLength()) Input(strSubString(sCommon, sCurCmd.GetLength(), -1)); // If we only have one match then no need to show hint as we have either // auto completed or will show help text insead if (iMatches == 1) m_sHint = NULL_STR; if (!sHelp.IsBlank()) { if (!m_sHint.IsBlank()) m_sHint.Append(CONSTLIT("\n")); m_sHint.Append(sHelp); } }