CString sysGetUserName (void) // sysGetUserName // // Returns the name of the currently logged-on user { CString sName; DWORD dwLen = 256; char *pPos = sName.GetWritePointer(dwLen); ::GetUserName(pPos, &dwLen); sName.Truncate(lstrlen(pPos)); return sName; }
void GrailMain (void) { ALERROR error; CTextOut Output; CLabyrinth Lab(&Output); // Boot printf("Grail Language v1.0\n\n"); if (error = Lab.Boot()) { printf("%s\n", Lab.GetLastErrorMsg().GetASCIIZPointer()); return; } printf("Grail ready\n\n"); // Command loop while (true) { // Get user input CString sInput; printf(": "); gets(sInput.GetWritePointer(1024)); sInput.Truncate(lstrlen(sInput.GetASCIIZPointer())); // If this is the quit command, we're done if (strEquals(sInput, CONSTLIT("quit"))) break; // Process the command } // Done Lab.Shutdown(); printf("End session."); }
CString CTextFileLog::GetSessionLog (void) // GetSessionLog // // Returns all output since the start of the session. { ASSERT(m_hFile); // Figure out the current position of the file pointer DWORD dwCurPos = ::SetFilePointer(m_hFile, 0, NULL, FILE_CURRENT); if (dwCurPos == INVALID_SET_FILE_POINTER) return CONSTLIT("ERROR: Unable to seek in kernel debug log."); // Set the file pointer to the start of the session if (::SetFilePointer(m_hFile, m_dwSessionStart, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return CONSTLIT("ERROR: Unable to seek in kernel debug log."); // Make sure the file log is not too big DWORD dwLogSize = dwCurPos - m_dwSessionStart; if (dwCurPos < m_dwSessionStart || dwLogSize > 1000000) return CONSTLIT("ERROR: Kernel debug log is too big."); // Allocate an appropriate string CString sLog; char *pLog = sLog.GetWritePointer(dwLogSize); // Read from the file DWORD dwRead; if (!::ReadFile(m_hFile, pLog, dwLogSize, &dwRead, NULL)) return CONSTLIT("ERROR: Unable to read kernel debug log."); // Restore position ::SetFilePointer(m_hFile, 0, NULL, FILE_END); return sLog; }
int AlchemyMain (CXMLElement *pCmdLine) // AlchemyMain // // Main entry-point after kernel initialization { ALERROR error; bool bLogo = !pCmdLine->GetAttributeBool(NO_LOGO_SWITCH); if (bLogo) { printf("CCShell v1.0\n"); printf("Copyright (c) 2010 by George Moromisato. All Rights Reserved.\n\n"); } // Initialize CodeChain environment CCodeChain CC; if (error = CC.Boot()) { PrintError(ERR_UNABLE_TO_INIT_CODECHAIN); return 1; } // Load some functions if (error = RegisterShellPrimitives(CC)) { PrintError(ERR_UNABLE_TO_REGISTER_PRIMITIVES); return 1; } // Prepare context SExecuteCtx Ctx; Ctx.bQuit = false; // Interpreter loop while (!Ctx.bQuit) { // Prompt printf(": "); // Read a line CString sInput; char *pDest = sInput.GetWritePointer(65536); if (fgets(pDest, 65536, stdin) == NULL) { PrintError(ERR_INVALID_INPUT); continue; } sInput.Truncate(strlen(sInput.GetASCIIZPointer())); // Parse the line ICCItem *pCode = CC.Link(sInput, 0, NULL); if (pCode->IsError()) { printf("codechain: %s\n", pCode->Print(&CC).GetASCIIZPointer()); pCode->Discard(&CC); continue; } // Execute ICCItem *pResult = CC.TopLevel(pCode, &Ctx); // Output if (pResult->IsError()) printf("codechain: %s\n", pResult->Print(&CC).GetASCIIZPointer()); else printf("%s\n", pResult->Print(&CC).GetASCIIZPointer()); // Done pCode->Discard(&CC); pResult->Discard(&CC); } return 0; }
bool arcList (const CString &sArchive, TArray<CString> *retFiles, CString *retsError) // arcList // // Returns a list of files in the given archive { unzFile theZipFile = unzOpen(sArchive.GetASCIIZPointer()); if (theZipFile == NULL) { if (retsError) *retsError = strPatternSubst(CONSTLIT("Unable to open file: %s."), sArchive); return false; } // Start at the first file if (unzGoToFirstFile(theZipFile) != UNZ_OK) { unzClose(theZipFile); if (retsError) *retsError = strPatternSubst(CONSTLIT("Unable to list files in archive: %s."), sArchive); return false; } // Loop until we reach the last file while (true) { const int MAX_LEN = 2048; CString sFilename; if (unzGetCurrentFileInfo(theZipFile, NULL, sFilename.GetWritePointer(MAX_LEN), MAX_LEN, NULL, 0, NULL, 0) != UNZ_OK) { unzClose(theZipFile); if (retsError) *retsError = strPatternSubst(CONSTLIT("Error listing files in archive: %s."), sArchive); return false; } sFilename.Truncate(strlen(sFilename.GetASCIIZPointer())); retFiles->Insert(sFilename); // Next int err = unzGoToNextFile(theZipFile); if (err == UNZ_END_OF_LIST_OF_FILE) break; else if (err != UNZ_OK) { unzClose(theZipFile); if (retsError) *retsError = strPatternSubst(CONSTLIT("Error listing files in archive: %s."), sArchive); return false; } // Continue } // Done unzClose(theZipFile); return true; }
CString ComposePlayerNameString (const CString &sString, const CString &sPlayerName, int iGenome, ICCItem *pArgs) // ComposePlayerNameString // // Replaces the following variables: // // %name% player name // %he% he or she // %his% his or her (matching case) // %hers% his or hers (matching case) // %him% him or her (matching case) // %sir% sir or ma'am (matching case) // %man% man or woman (matching case) // %brother% brother or sister (matching case) // %% % { // Prepare output CString sOutput; int iOutLeft = sString.GetLength() * 2; char *pOut = sOutput.GetWritePointer(iOutLeft); // Compose. Loop once for each segment that we need to add bool bDone = false; bool bVar = false; char *pPos = sString.GetASCIIZPointer(); while (!bDone) { CString sVar; char *pSeg; char *pSegEnd; if (bVar) { ASSERT(*pPos == '%'); // Skip % pPos++; char *pStart = pPos; while (*pPos != '%' && *pPos != '\0') pPos++; sVar = CString(pStart, pPos - pStart); // Skip the next % if (*pPos == '%') { pPos++; bVar = false; } else bDone = true; bool bCapitalize = (*sVar.GetASCIIZPointer() >= 'A' && *sVar.GetASCIIZPointer() <= 'Z'); // Setup the segment depending on the variable if (sVar.IsBlank()) sVar = CONSTLIT("%"); else if (strEquals(sVar, CONSTLIT("name"))) sVar = sPlayerName; else if (strEquals(sVar, CONSTLIT("he"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("he"); else sVar = CONSTLIT("she"); } else if (strEquals(sVar, CONSTLIT("sir"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("sir"); else sVar = CONSTLIT("ma'am"); } else if (strEquals(sVar, CONSTLIT("man"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("man"); else sVar = CONSTLIT("woman"); } else if (strEquals(sVar, CONSTLIT("his"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("his"); else sVar = CONSTLIT("her"); } else if (strEquals(sVar, CONSTLIT("him"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("him"); else sVar = CONSTLIT("her"); } else if (strEquals(sVar, CONSTLIT("hers"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("his"); else sVar = CONSTLIT("hers"); } else if (strEquals(sVar, CONSTLIT("brother"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("brother"); else sVar = CONSTLIT("sister"); } else if (strEquals(sVar, CONSTLIT("son"))) { if (iGenome == genomeHumanMale) sVar = CONSTLIT("son"); else sVar = CONSTLIT("daughter"); } else if (pArgs) { int iArg = strToInt(sVar, 0); if (iArg < 0) { iArg = -iArg; bCapitalize = true; } if (iArg > 0) { ICCItem *pArg = pArgs->GetElement(iArg + 1); if (pArg) sVar = pArg->GetStringValue(); } } // If we could not find a valid var, then we assume a // single % sign. else { sVar = CONSTLIT("%"); pPos = pStart; bDone = (*pPos == '\0'); bVar = false; bCapitalize = false; } // Capitalize, if necessary if (bCapitalize) sVar = strCapitalize(sVar); // Setup segment pSeg = sVar.GetASCIIZPointer(); pSegEnd = pSeg + sVar.GetLength(); } else { // Skip to the next variable or the end of the string pSeg = pPos; while (*pPos != '%' && *pPos != '\0') pPos++; if (*pPos == '\0') bDone = true; else bVar = true; pSegEnd = pPos; } // Add the next segment int iLen = pSegEnd - pSeg; if (iLen > 0) { if (iLen > iOutLeft) { int iAlloc = sOutput.GetLength(); int iCurLen = iAlloc - iOutLeft; int iNewAlloc = max(iAlloc * 2, iAlloc + iLen); pOut = sOutput.GetWritePointer(iNewAlloc); pOut += iCurLen; iOutLeft = iNewAlloc - iCurLen; } while (pSeg < pSegEnd) *pOut++ = *pSeg++; iOutLeft -= iLen; } } // Done int iAlloc = sOutput.GetLength(); int iCurLen = iAlloc - iOutLeft; sOutput.Truncate(iCurLen); return sOutput; }