bool EncodingTable::load(const std::wstring& fileName, TextFile::Encoding encoding) { unsigned char hexBuffer[MAXHEXLENGTH]; TextFile input; if (input.open(fileName,TextFile::Read,encoding) == false) return false; hexData.clear(); valueData.clear(); entries.clear(); setTerminationEntry((unsigned char*)"\0",1); while (!input.atEnd()) { std::wstring line = input.readLine(); if (line.empty() || line[0] == '*') continue; if (line[0] == '/') { std::wstring hex = line.substr(1); if (hex.empty() || hex.length() > 2*MAXHEXLENGTH) { // error continue; } int length = parseHexString(hex,hexBuffer); if (length == -1) { // error continue; } setTerminationEntry(hexBuffer,length); } else { size_t pos = line.find(L'='); std::wstring hex = line.substr(0,pos); std::wstring value = line.substr(pos+1); if (hex.empty() || value.empty() || hex.length() > 2*MAXHEXLENGTH) { // error continue; } int length = parseHexString(hex,hexBuffer); if (length == -1) { // error continue; } addEntry(hexBuffer,length,value); } } return true; }
void LoadAssemblyFile(const std::wstring& fileName, TextFile::Encoding encoding) { tTextData Text; int num = 0; AddFileName((char*)convertWStringToUtf8(fileName).c_str()); Global.IncludeNestingLevel++; if (Global.IncludeNestingLevel == ASSEMBLER_INCLUDE_NESTING_LEVEL) { Logger::printError(Logger::Error,L"Maximum include nesting level reached"); return; } TextFile input; if (input.open(fileName,TextFile::Read,encoding) == false) { Logger::printError(Logger::Error,L"Could not open file"); return; } while (!input.atEnd()) { Global.FileInfo.LineNumber++; Global.FileInfo.TotalLineCount++; if (GetLine(input,Text.buffer) == false) continue; if (Text.buffer.size() == 0) continue; Text.buffer = Global.symbolTable.insertEquations(Text.buffer,Global.FileInfo.FileNum,Global.Section); if (CheckEquLabel(Text.buffer) == false) { Text.buffer = checkLabel(Text.buffer,false); splitLine(Text.buffer,Text.name,Text.params); if (Text.name.empty()) continue; if (ParseMacro(input,Text.name,Text.params) == true) continue; if (Arch->AssembleDirective(Text.name,Text.params) == false) { Arch->AssembleOpcode(Text.name,Text.params); } } if (Logger::hasFatalError()) return; } Logger::printQueue(); Global.IncludeNestingLevel--; input.close(); }
void SynonymMap::load(const string& sFileName) { close(); TextFile tf; tf.open(sFileName.c_str(), "r"); char szLine[512]; if(tf.isFileOpen()) { while(!tf.isEof() && tf.readLine(szLine, 512, true) > 0) { StringTokenizer st(szLine, ":", StringTokenizer::TOKEN_TRIM | StringTokenizer::TOKEN_IGNORE_EMPTY ); if(st.getNumTokens() != 2) continue; addSynonyms(st[0].c_str(), st[1].c_str()); } } }
CAssemblerCommand* parseDirectiveInclude(Parser& parser, int flags) { const Token& start = parser.peekToken(); std::vector<Expression> parameters; if (parser.parseExpressionList(parameters,1,2) == false) return nullptr; std::wstring fileName; if (parameters[0].evaluateString(fileName,true) == false) return nullptr; fileName = getFullPathName(fileName); TextFile::Encoding encoding = TextFile::GUESS; if (parameters.size() == 2) { std::wstring encodingName; if (parameters[1].evaluateString(encodingName,true) == false && parameters[1].evaluateIdentifier(encodingName) == false) return nullptr; encoding = getEncodingFromString(encodingName); } // don't include the file if it's inside a false block if (parser.isInsideTrueBlock() == false) return new DummyCommand(); if (fileExists(fileName) == false) { parser.printError(start,L"Included file \"%s\" does not exist",fileName); return nullptr; } TextFile f; if (f.open(fileName,TextFile::Read,encoding) == false) { parser.printError(start,L"Could not open included file \"%s\"",fileName); return nullptr; } return parser.parseFile(f); }
int main() { TextFile file; String s; if (file.open("birthday.in")) { while (true) { String f = file.read_line(); if (f.is_empty()) { break; } s += f; } } for (int i = 0; i < s.len(); i += 1) { } return 0; }
void SynonymMap::save(const string& sFileName) { TextFile tf; tf.open(sFileName.c_str(), "w"); for(SynMap::const_iterator it = m_synMap.begin(); it != m_synMap.end();it++) { tf.write(it->first.c_str()); Group* pGroup = it->second; tf.write(":"); for(Group::const_iterator git = pGroup->begin(); git != pGroup->end(); git++) { tf.write((*git).c_str()); if( (git + 1) != pGroup->end()) { tf.write(","); } } tf.write("\n"); } tf.close(); }
bool runArmips(ArmipsArguments& arguments) { // initialize and reset global data Global.Radix = 10; Global.Revalidate = true; Global.Section = 0; Global.nocash = false; Global.IncludeNestingLevel = 0; Global.MacroNestingLevel = 0; Global.FileInfo.FileCount = 0; Global.FileInfo.TotalLineCount = 0; Global.DebugMessages = 0; Global.relativeInclude = false; Global.validationPasses = 0; Global.multiThreading = true; Arch = &InvalidArchitecture; Tokenizer::clearEquValues(); Logger::clear(); Global.symData.clear(); Global.Table.clear(); Global.symbolTable.clear(); Global.tempData.clear(); Global.FileInfo.FileList.clear(); Global.FileInfo.FileCount = 0; Global.FileInfo.TotalLineCount = 0; Global.FileInfo.LineNumber = 0; Global.FileInfo.FileNum = 0; Arm.clear(); // process arguments Parser parser; Logger::setSilent(arguments.silent); Logger::setErrorOnWarning(arguments.errorOnWarning); if (!arguments.symFileName.empty()) Global.symData.setNocashSymFileName(arguments.symFileName,arguments.symFileVersion); if (!arguments.tempFileName.empty()) Global.tempData.setFileName(arguments.tempFileName); Token token; for (size_t i = 0; i < arguments.equList.size(); i++) { parser.addEquation(token,arguments.equList[i].name, arguments.equList[i].value); } Global.symbolTable.addLabels(arguments.labels); if (Logger::hasError()) return false; // run assembler TextFile input; switch (arguments.mode) { case ArmipsMode::FILE: Global.memoryMode = false; if (input.open(arguments.inputFileName,TextFile::Read) == false) { Logger::printError(Logger::Error,L"Could not open file"); return false; } break; case ArmipsMode::MEMORY: Global.memoryMode = true; Global.memoryFile = arguments.memoryFile; input.openMemory(arguments.content); break; } CAssemblerCommand* content = parser.parseFile(input); bool result = !Logger::hasError(); if (result == true && content != nullptr) result = encodeAssembly(content); else if (g_fileManager->hasOpenFile()) { if (!Global.memoryMode) Logger::printError(Logger::Warning,L"File not closed"); g_fileManager->closeFile(); } // return errors if (arguments.errorsResult != NULL) { StringList errors = Logger::getErrors(); for (size_t i = 0; i < errors.size(); i++) arguments.errorsResult->push_back(errors[i]); } return result; }
/** * Test utf8 bom */ void test_utf8_bom(const char* fileName) { TextFile tf; tf.open(fileName); assert(tf.getEncoding() == UTF8); }
void ExtensionsWizard::OnWizardFinished(wxWizardEvent &e) { String extensionName = conversions::to_utf16(m_pageGeneral->getExtensionName()); String extensionDescription = conversions::to_utf16(m_pageGeneral->getExtensionDescription()); String extensionAuthor = conversions::to_utf16(m_pageGeneral->getExtensionAuthor()); shared_ptr<ExtensionsWizardPageTemplate::TemplateDetails> extensionTemplate = m_pageTemplate->getTemplate(); const ExtensionsWizardPageTemplate::TemplateDetails::Files &extensionFiles = extensionTemplate->getFiles(); ExtensionID extensionID = ExtensionID::generate(); String rootPath = ExtensionsSystem::instance()->getDataPath(); String extensionPath = utils::makeFolderPath(rootPath, extensionID.toUTF16()); shared_ptr<XMLDocument> document(OS_NEW XMLDocument()); shared_ptr<XMLNode> nodeRoot = document->create(OS_EXTENSION_XML_NODE_ROOT); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_ID, extensionID.getString()); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_NAME, extensionName); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_DESCRIPTION, extensionDescription); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_AUTHOR, extensionAuthor); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_VERSION_CODE, _S("1")); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_VERSION_NAME, _S("0.1")); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_ICON, _S("icon.png")); nodeRoot->setAttributeString(OS_EXTENSION_XML_NODE_ROOT_LOGO, _S("logo.png")); shared_ptr<XMLNode> nodeScripts; ExtensionsWizardPageTemplate::TemplateDetails::Files::const_iterator i; for(i = extensionFiles.begin(); i != extensionFiles.end(); ++i) { if((*i).script) { if(nodeScripts == nullptr) nodeScripts = nodeRoot->addChild(OS_EXTENSION_XML_NODE_SCRIPTS); shared_ptr<XMLNode> nodeScript = nodeScripts->addChild(OS_EXTENSION_XML_NODE_SCRIPT); nodeScript->setAttributeString(OS_EXTENSION_XML_NODE_SCRIPT_PATH, (*i).target); // TODO: temp nodeScript->setAttributeString(OS_EXTENSION_XML_NODE_SCRIPT_LANGUAGE, "python"); } } String documentString; if(document->writeString(documentString) == false) { OS_ASSERTFALSE(); Application::instance()->reportError("Internal error generating extension's xml, please report it", true); return; } if(XMLManager::instance()->validateString(documentString, XMLSchema::fromFile(utils::makeFilePath(utils::makeFolderPath(Options::instance()->getSharePath(), OS_SCHEMAS_PATH), OS_EXTENSIONS_EXTENSION_SCHEMA))) == false) { OS_ASSERTFALSE(); Application::instance()->reportError("Internal error validating extension's xml, please report it", true); return; } if(FileSystem::instance()->ensureDirectory(extensionPath) == false) { Application::instance()->reportError("Cannot create directory '" + extensionPath + "'", true); return; } for(i = extensionFiles.begin(); i != extensionFiles.end(); ++i) { String targetFile = utils::makeFilePath(extensionPath, (*i).target); String sourceFile = utils::makeFilePath(extensionTemplate->getFolder(), (*i).source); String targetPath = FileSystem::instance()->getFilePath(targetFile); if(FileSystem::instance()->ensureDirectory(targetPath) == false) { Application::instance()->reportError("Cannot create directory '" + targetPath + "'", true); return; } if(FileSystem::instance()->copyFile(sourceFile, targetFile) == false) { Application::instance()->reportError("Cannot copy file '" + sourceFile + "' to '" + targetFile + "'", true); return; } if((*i).macro) { // Open file, do replaces and save. String data; TextFile::readFile(targetFile, data); data = data.replace_all("{extension_id}", extensionID.getString()); data = data.replace_all("{extension_name}", extensionName); data = data.replace_all("{extension_description}", extensionDescription); data = data.replace_all("{extension_author}", extensionAuthor); UniqueID uniqueID = UniqueID::generate(); data = data.replace_all("{unique_id}", uniqueID.getString()); TextFile file; file.open(targetFile, TextFile::ofWrite); file.writeLine(data); file.close(); } } String documentFile = utils::makeFilePath(extensionPath, OS_MANIFESTXML); if(document->writeFile(documentFile) == false) { Application::instance()->reportError("Error writing file '" + documentFile + "'", true); return; } OS_ASSERT(XMLManager::instance()->validateFile(documentFile, XMLSchema::fromFile(utils::makeFilePath(utils::makeFolderPath(Options::instance()->getSharePath(), OS_SCHEMAS_PATH), OS_EXTENSIONS_EXTENSION_SCHEMA)))); if(m_pageFinish->getOpenFolder()) Application::instance()->shell(extensionPath); Application::instance()->reloadExtensions(); }
void PrintCallStackOfThisThread(const wchar_t* name, const wchar_t* expression, const wchar_t* function, const wchar_t* file, int line) { if (name == NULL) name = L"Unknown"; if (expression == NULL) expression = L"No Expression"; if (function == NULL) function = L"Unknown Function"; if (file == NULL) file = L"Unknown File"; wchar_t folder[MAX_PATH] = { 0, }; GetProcessFolder(::GetCurrentProcess(), folder, _countof(folder)); wchar_t callStackFile[MAX_PATH] = { 0, }; SYSTEMTIME systemTime; ::memset(&systemTime, 0, sizeof(systemTime)); ::GetLocalTime(&systemTime); ::swprintf_s(callStackFile, _countof(callStackFile), L"%s/%s_%04d-%02d-%02d.rpt", folder, name, systemTime.wYear, systemTime.wMonth, systemTime.wDay); TextFile textFile; int count = 0; while (textFile.open(callStackFile, GENERIC_WRITE, FILE_SHARE_READ) == false && textFile.create(callStackFile, GENERIC_WRITE, FILE_SHARE_READ) == false) { ++count; ::swprintf_s(callStackFile, _countof(callStackFile), L"%s/%s_%04d-%02d-%02d(%d).rpt", folder, name, systemTime.wYear, systemTime.wMonth, systemTime.wDay, count); } textFile.seek(0, FILE_END); DumpPrint(&textFile, L"[%04d-%02d-%02d %02d:%02d:%02d] %s at %s\r\n%s(%d)\r\n", systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond, expression, function, file, line); // current thread call stack DWORD processId = ::GetCurrentProcessId(); HANDLE process = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId); if (process != NULL) { HANDLE thread = ::GetCurrentThread(); DWORD threadId = ::GetCurrentThreadId(); DumpPrint(&textFile, L"%s(Thread(%u) Call Stack)\r\n", function, threadId); EXCEPTION_RECORD exceptionRecord = {}; CONTEXT context = {}; EXCEPTION_POINTERS exceptionPointers = { &exceptionRecord, &context }; context.ContextFlags = CONTEXT_FULL; if (::GetThreadContext(thread, &context) != FALSE) { PrintThreadCallStack(&textFile, &exceptionPointers, process, thread); DumpPrint(&textFile, L"\r\n"); } ::CloseHandle(process); process = NULL; } DumpPrint(&textFile, L"\r\n"); textFile.flush(); }
void SymbolData::writeNocashSym() { if (nocashSymFileName.empty()) return; std::vector<NocashSymEntry> entries; for (size_t k = 0; k < modules.size(); k++) { SymDataModule& module = modules[k]; for (size_t i = 0; i < module.symbols.size(); i++) { SymDataSymbol& sym = module.symbols[i]; NocashSymEntry entry; entry.address = sym.address; entry.text = sym.name; entries.push_back(entry); } for (size_t i = 0; i < module.data.size(); i++) { SymDataData& data = module.data[i]; NocashSymEntry entry; entry.address = data.address; switch (data.type) { case Data8: entry.text = formatString(".byt:%04X",data.size); break; case Data16: entry.text = formatString(".wrd:%04X",data.size); break; case Data32: entry.text = formatString(".dbl:%04X",data.size); break; case DataAscii: entry.text = formatString(".asc:%04X",data.size); break; } entries.push_back(entry); } } std::sort(entries.begin(),entries.end()); TextFile file; if (file.open(nocashSymFileName,TextFile::Write,TextFile::ASCII) == false) { Logger::printError(Logger::Error,L"Could not open sym file %s.",file.getFileName().c_str()); return; } file.writeLine(L"00000000 0"); for (size_t i = 0; i < entries.size(); i++) { file.writeFormat(L"%08X %S\n",entries[i].address,entries[i].text.c_str()); } file.write("\x1A"); file.close(); }