MCPcopy Create free account
hub / github.com/codereader/DarkRadiant / removeDeclarationFromFile

Method removeDeclarationFromFile

radiantcore/decl/DeclarationManager.cpp:478–529  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

476}
477
478void DeclarationManager::removeDeclarationFromFile(const IDeclaration::Ptr& decl)
479{
480 const auto& syntax = decl->getBlockSyntax();
481
482 // Nothing to do if the decl hasn't been saved
483 if (syntax.fileInfo.name.empty()) return;
484
485 if (!syntax.fileInfo.getIsPhysicalFile())
486 {
487 throw std::logic_error("Only declarations stored in physical files can be removed.");
488 }
489
490 auto fullPath = GlobalFileSystem().findFile(syntax.fileInfo.fullPath());
491
492 if (fullPath.empty() || !fs::exists(fullPath))
493 {
494 return;
495 }
496
497 fullPath += syntax.fileInfo.fullPath();
498
499 // Load the syntax tree from the existing file
500 std::ifstream existingFile(fullPath);
501
502 if (!existingFile.is_open())
503 {
504 throw std::runtime_error(fmt::format(_("Cannot open file for reading: {0}"), fullPath));
505 }
506
507 // Parse the existing file into a syntax tree for manipulation
508 parser::DefBlockSyntaxParser<std::istream> parser(existingFile);
509
510 auto syntaxTree = parser.parse();
511 existingFile.close();
512
513 // Try to remove the decl from the syntax tree (using the original name)
514 // Returns false if the decl could not be located (decl has not been saved to this file then)
515 if (removeDeclarationFromSyntaxTree(syntaxTree, decl->getOriginalDeclName()))
516 {
517 // Open a temporary file
518 stream::TemporaryOutputStream tempStream(fullPath);
519 auto& stream = tempStream.getStream();
520
521 // Move the old file to .bak before overwriting it
522 os::moveToBackupFile(fullPath);
523
524 // Export the modified syntax tree
525 stream << syntaxTree->getString();
526
527 tempStream.closeAndReplaceTargetFile();
528 }
529}
530
531bool DeclarationManager::renameDeclaration(Type type, const std::string& oldName_Incoming, const std::string& newName)
532{

Callers

nothing calls this directly

Calls 12

moveToBackupFileFunction · 0.85
getIsPhysicalFileMethod · 0.80
fullPathMethod · 0.80
formatFunction · 0.50
_Function · 0.50
emptyMethod · 0.45
findFileMethod · 0.45
parseMethod · 0.45
closeMethod · 0.45
getStringMethod · 0.45

Tested by

no test coverage detected