void WriteNumber (CMemoryWriteStream &Stream, int iNumber, int iLeadingZeros) { CString sNumber = strFromInt(iNumber, false); if (sNumber.GetLength() < iLeadingZeros) Stream.Write("0000000000", iLeadingZeros - sNumber.GetLength()); Stream.Write(sNumber.GetASCIIZPointer(), sNumber.GetLength()); }
ALERROR WriteHeader (int iGameFile, CSymbolTable &Resources, CDataFile &Out) { ALERROR error; CMemoryWriteStream Stream; DWORD dwSave; if (error = Stream.Create()) { printf("Out of memory\n"); return error; } // Signature dwSave = TDB_SIGNATURE; Stream.Write((char *)&dwSave, sizeof(dwSave)); // Version dwSave = TDB_VERSION; Stream.Write((char *)&dwSave, sizeof(dwSave)); // Game file entry dwSave = iGameFile; Stream.Write((char *)&dwSave, sizeof(dwSave)); // Game title CString sGameTitle = CONSTLIT("Transcendence: The March of the Heretic"); sGameTitle.WriteToStream(&Stream); // Resource map CString sSave; if (error = CObject::Flatten(&Resources, &sSave)) { printf("Unable to flatten resources map\n"); return error; } sSave.WriteToStream(&Stream); // Write out the header Stream.Close(); CString sData(Stream.GetPointer(), Stream.GetLength(), TRUE); int iEntry; if (error = Out.AddEntry(sData, &iEntry)) { printf("Unable to write out header\n"); return error; } Out.SetDefaultEntry(iEntry); return NOERROR; }
void WriteTimeValue (CMemoryWriteStream &Output, DWORD dwTime) { if (dwTime == INVALID_TIME) Output.Write(NIL_VALUE.GetASCIIZPointer(), NIL_VALUE.GetLength()); else { CString sInt = strFromInt(dwTime); Output.Write(sInt.GetASCIIZPointer(), sInt.GetLength()); } }
CJSONValue::CJSONValue (const CString &sValue, bool bToUTF8) // CJSONValue constructor { m_iType = typeString; if (bToUTF8) { // See if there are any characters that we need to encode. char *pPos = sValue.GetASCIIZPointer(); char *pPosEnd = pPos + sValue.GetLength(); while (pPos < pPosEnd && (BYTE)*pPos < 0x80) pPos++; // If we don't have to encode anything, just copy if (pPos == pPosEnd) m_pValue = CString::INTGetStorage(sValue); else { // Otherwise, we encode CMemoryWriteStream Output; if (Output.Create() != NOERROR) m_pValue = CString::INTGetStorage(CONSTLIT("Out of memory")); else { pPos = sValue.GetASCIIZPointer(); pPosEnd = pPos + sValue.GetLength(); while (pPos < pPosEnd) { if ((BYTE)*pPos < 0x80) Output.Write(pPos, 1); else { CString sUTF8 = strEncodeW1252ToUTF8Char(*pPos); Output.Write(sUTF8.GetASCIIZPointer(), sUTF8.GetLength()); } pPos++; } m_pValue = CString::INTGetStorage(CString(Output.GetPointer(), Output.GetLength())); } } } else m_pValue = CString::INTGetStorage(sValue); }
void CopyGameStatsToClipboard (HWND hWnd, const CGameStats &GameStats) // CopyGameStatsToClipboard // // Copy the game stats to the clipboard { CMemoryWriteStream Output; if (Output.Create() != NOERROR) return; GameStats.WriteAsText(&Output); // Terminate Output.Write("\0", 1); // Copy uiCopyTextToClipboard(hWnd, CString(Output.GetPointer(), Output.GetLength(), TRUE)); Output.Close(); }
CString CCSymbolTable::Print (CCodeChain *pCC, DWORD dwFlags) // Print // // Render as text { int i; CMemoryWriteStream Stream; if (Stream.Create() != NOERROR) return CONSTLIT("ERROR-OUT-OF-MEMORY"); // Open paren Stream.Write("{ ", 2); // Write items for (i = 0; i < m_Symbols.GetCount(); i++) { CString sKey = CCString::Print(m_Symbols.GetKey(i)); Stream.Write(sKey.GetASCIIZPointer(), sKey.GetLength()); Stream.Write(":", 1); ICCItem *pValue = dynamic_cast<ICCItem *>(m_Symbols.GetValue(i)); CString sValue = pValue->Print(pCC); Stream.Write(sValue.GetASCIIZPointer(), sValue.GetLength()); Stream.Write(" ", 1); } // Close paren Stream.Write("}", 1); return CString(Stream.GetPointer(), Stream.GetLength()); }
void WordGenerator (CXMLElement *pCmdLine) { int i; // Load input file CString sFilespec = pCmdLine->GetAttribute(CONSTLIT("input")); if (sFilespec.IsBlank()) { printf("ERROR: input filename expected.\n"); return; } CFileReadBlock InputFile(sFilespec); if (InputFile.Open() != NOERROR) { printf("ERROR: Unable to open file: %s\n", sFilespec.GetASCIIZPointer()); return; } // "Novel" means that we only generate words that are not // in the input file. bool bNovelWordsOnly = pCmdLine->GetAttributeBool(NOVEL_ATTRIB); // Build up a word generator CMarkovWordGenerator Generator; TMap<CString, DWORD> InputWords; // Read each line of the file char *pPos = InputFile.GetPointer(0); char *pEndPos = pPos + InputFile.GetLength(); while (pPos < pEndPos) { // Skip whitespace while (pPos < pEndPos && (strIsWhitespace(pPos) || *pPos < ' ')) pPos++; // Parse the line char *pStart = pPos; while (pPos < pEndPos && *pPos != '\r' && *pPos != '\n' && *pPos >= ' ') pPos++; CString sWord(pStart, pPos - pStart); // Add the word to the generator if (!sWord.IsBlank()) { Generator.AddSample(strTrimWhitespace(sWord)); // If we are looking for novel words we need to keep a map // of all words in the input file. if (bNovelWordsOnly) InputWords.Insert(sWord); } } // If we have a count, then output a list of random words int iCount; if (pCmdLine->FindAttributeInteger(COUNT_ATTRIB, &iCount)) { if (iCount > 0) { TArray<CString> Result; Generator.GenerateUnique(iCount, &Result); for (i = 0; i < Result.GetCount(); i++) if (InputWords.Find(Result[i])) { Result.Delete(i); i--; } Result.Sort(); for (i = 0; i < Result.GetCount(); i++) printf("%s\n", Result[i].GetASCIIZPointer()); } } // Otherwise, output the generator as XML else { CMemoryWriteStream Output; if (Output.Create() != NOERROR) { printf("ERROR: Out of memory.\n"); return; } if (Generator.WriteAsXML(&Output) != NOERROR) { printf("ERROR: Unable to output generator as XML.\n"); return; } Output.Write("\0", 1); printf(Output.GetPointer()); } }
CString CJSONValue::AsCP1252 (void) const // AsCP1252 // // Converts to (Windows Western) CP1252 string { if (m_iType != typeString) return NULL_STR; // Do a first pass and see if we have any high-bit characters CString sValue = AsString(); char *pPos = sValue.GetASCIIZPointer(); char *pPosEnd = pPos + sValue.GetLength(); while (pPos < pPosEnd && (BYTE)*pPos < 0x80) pPos++; if (pPos == pPosEnd) return sValue; // Convert CMemoryWriteStream Output; if (Output.Create() != NOERROR) return CONSTLIT("Out of memory"); else { pPos = sValue.GetASCIIZPointer(); pPosEnd = pPos + sValue.GetLength(); while (pPos < pPosEnd) { if ((BYTE)*pPos == 0xc2) { pPos++; Output.Write(pPos, 1); } else if ((BYTE)*pPos == 0xc3) { pPos++; BYTE byCode = ((BYTE)*pPos) | 0x40; Output.Write((char *)&byCode, 1); } else if ((BYTE)*pPos == 0xc5) { pPos++; BYTE byCode; switch ((BYTE)*pPos) { case 0xa0: byCode = 0x8a; break; case 0x92: byCode = 0x8c; break; case 0x93: byCode = 0x9c; break; case 0xa1: byCode = 0x9a; break; case 0xb8: byCode = 0x9f; break; case 0xbd: byCode = 0x8e; break; case 0xbe: byCode = 0x9e; break; default: byCode = 0x20; } Output.Write((char *)&byCode, 1); } else if ((BYTE)*pPos == 0xc6) { pPos++; BYTE byCode; switch ((BYTE)*pPos) { case 0x92: byCode = 0x83; break; default: byCode = 0x20; } Output.Write((char *)&byCode, 1); } else if ((BYTE)*pPos == 0xcb) { pPos++; BYTE byCode; switch ((BYTE)*pPos) { case 0x86: byCode = 0x88; break; case 0x9c: byCode = 0x98; break; default: byCode = 0x20; } Output.Write((char *)&byCode, 1); } else if ((BYTE)*pPos == 0xe2) { pPos++; BYTE byCode; if ((BYTE)*pPos == 0x80) { pPos++; switch ((BYTE)*pPos) { case 0x93: byCode = 0x96; break; case 0x94: byCode = 0x97; break; case 0x98: byCode = 0x91; break; case 0x99: byCode = 0x92; break; case 0x9a: byCode = 0x82; break; case 0x9c: byCode = 0x93; break; case 0x9d: byCode = 0x94; break; case 0x9e: byCode = 0x84; break; case 0xa0: byCode = 0x86; break; case 0xa1: byCode = 0x87; break; case 0xa2: byCode = 0x95; break; case 0xa6: byCode = 0x85; break; case 0xb0: byCode = 0x89; break; case 0xb9: byCode = 0x8b; break; case 0xba: byCode = 0x9b; break; default: byCode = 0x20; } } else if ((BYTE)*pPos == 0x82) { pPos++; switch ((BYTE)*pPos) { case 0xac: byCode = 0x80; break; default: byCode = 0x20; } } else if ((BYTE)*pPos == 0x84) { pPos++; switch ((BYTE)*pPos) { case 0xa2: byCode = 0x99; break; default: byCode = 0x20; } } else { pPos++; byCode = 0x20; } Output.Write((char *)&byCode, 1); } else Output.Write(pPos, 1); pPos++; } return CString(Output.GetPointer(), Output.GetLength()); } }
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()); }
CString CPlayerGameStats::GetStat (const CString &sStat) const // GetStat // // Returns the given stat { if (strEquals(sStat, BEST_ENEMY_SHIPS_DESTROYED_STATS)) { DWORD dwUNID; int iCount = GetBestEnemyShipsDestroyed(&dwUNID); if (iCount == 0) return NULL_STR; return strPatternSubst(CONSTLIT("'(%d %d)"), dwUNID, iCount); } else if (strEquals(sStat, ENEMY_SHIPS_DESTROYED_STAT)) { CMapIterator i; int iCount = 0; m_ShipStats.Reset(i); while (m_ShipStats.HasMore(i)) { SShipClassStats *pStats; DWORD dwUNID = m_ShipStats.GetNext(i, &pStats); iCount += pStats->iEnemyDestroyed; } if (iCount + m_iExtraEnemyShipsDestroyed == 0) return NULL_STR; return ::strFromInt(iCount + m_iExtraEnemyShipsDestroyed); } else if (strEquals(sStat, ENEMY_STATIONS_DESTROYED_STAT)) { CSovereign *pPlayerSovereign = g_pUniverse->FindSovereign(g_PlayerSovereignUNID); if (pPlayerSovereign == NULL) return NULL_STR; CMapIterator i; int iCount = 0; m_StationStats.Reset(i); while (m_StationStats.HasMore(i)) { SStationTypeStats *pStats; DWORD dwUNID = m_StationStats.GetNext(i, &pStats); CStationType *pType = g_pUniverse->FindStationType(dwUNID); if (pType == NULL) continue; if (pType->GetSovereign()->IsEnemy(pPlayerSovereign)) iCount += pStats->iDestroyed; } if (iCount == 0) return NULL_STR; return ::strFromInt(iCount); } else if (strEquals(sStat, FRIENDLY_SHIPS_DESTROYED_STAT)) { CMapIterator i; int iCount = 0; m_ShipStats.Reset(i); while (m_ShipStats.HasMore(i)) { SShipClassStats *pStats; DWORD dwUNID = m_ShipStats.GetNext(i, &pStats); iCount += pStats->iFriendDestroyed; } if (iCount == 0) return NULL_STR; return ::strFromInt(iCount); } else if (strEquals(sStat, FRIENDLY_STATIONS_DESTROYED_STAT)) { CSovereign *pPlayerSovereign = g_pUniverse->FindSovereign(g_PlayerSovereignUNID); if (pPlayerSovereign == NULL) return NULL_STR; CMapIterator i; int iCount = 0; m_StationStats.Reset(i); while (m_StationStats.HasMore(i)) { SStationTypeStats *pStats; DWORD dwUNID = m_StationStats.GetNext(i, &pStats); CStationType *pType = g_pUniverse->FindStationType(dwUNID); if (pType == NULL) continue; if (!pType->GetSovereign()->IsEnemy(pPlayerSovereign)) iCount += pStats->iDestroyed; } if (iCount == 0) return NULL_STR; return ::strFromInt(iCount); } else if (strEquals(sStat, RESURRECT_COUNT_STAT)) return ::strFromInt(m_iResurrectCount); else if (strEquals(sStat, SCORE_STAT)) return ::strFromInt(m_iScore); else if (strEquals(sStat, SYSTEM_DATA_STAT)) { CMemoryWriteStream Output; if (Output.Create() != NOERROR) return NIL_VALUE; Output.Write("'(", 2); CMapIterator i; m_SystemStats.Reset(i); while (m_SystemStats.HasMore(i)) { SSystemStats *pStats; const CString &sNodeID = m_SystemStats.GetNext(i, &pStats); Output.Write("(", 1); Output.Write(sNodeID.GetASCIIZPointer(), sNodeID.GetLength()); Output.Write(" ", 1); WriteTimeValue(Output, pStats->dwFirstEntered); Output.Write(" ", 1); WriteTimeValue(Output, pStats->dwLastEntered); Output.Write(" ", 1); WriteTimeValue(Output, pStats->dwLastLeft); Output.Write(" ", 1); WriteTimeValue(Output, pStats->dwTotalTime); Output.Write(") ", 2); } Output.Write(")", 1); return CString(Output.GetPointer(), Output.GetLength()); } else if (strEquals(sStat, SYSTEMS_VISITED_STAT)) { CMapIterator i; int iCount = 0; m_SystemStats.Reset(i); while (m_SystemStats.HasMore(i)) { SSystemStats *pStats; const CString &sNodeID = m_SystemStats.GetNext(i, &pStats); if (pStats->dwLastEntered != INVALID_TIME) iCount++; } return ::strFromInt(iCount + m_iExtraSystemsVisited); } else return NULL_STR; }
CString CPlayerGameStats::GenerateKeyEventStat (TArray<SKeyEventStatsResult> &List) const // GenerateKeyEventStat // // Generates a stat from the list of events { int i; CMemoryWriteStream Output; if (Output.Create() != NOERROR) return NIL_VALUE; Output.Write("'(", 2); for (i = 0; i < List.GetCount(); i++) { SKeyEventStatsResult *pResult = &List[i]; if (pResult->bMarked) { Output.Write("(", 1); // Type switch (pResult->pStats->iType) { case eventEnemyDestroyedByPlayer: Output.Write(STR_ENEMY_DESTROYED.GetASCIIZPointer(), STR_ENEMY_DESTROYED.GetLength()); break; case eventFriendDestroyedByPlayer: Output.Write(STR_FRIEND_DESTROYED.GetASCIIZPointer(), STR_FRIEND_DESTROYED.GetLength()); break; case eventMajorDestroyed: Output.Write(STR_DESTROYED.GetASCIIZPointer(), STR_DESTROYED.GetLength()); break; case eventSavedByPlayer: Output.Write(STR_SAVED.GetASCIIZPointer(), STR_SAVED.GetLength()); break; default: Output.Write("?", 1); break; } // sNodeID time, UNID, cause, name, flags CString sValue = strPatternSubst(" %s 0x%x 0x%x 0x%x %s 0x%x) ", CCString::Print(pResult->sNodeID), pResult->pStats->dwTime, pResult->pStats->dwObjUNID, pResult->pStats->dwCauseUNID, CCString::Print(pResult->pStats->sObjName), pResult->pStats->dwObjNameFlags); Output.Write(sValue.GetASCIIZPointer(), sValue.GetLength()); } } Output.Write(")", 1); return CString(Output.GetPointer(), Output.GetLength()); }