Skip to content

Commit

Permalink
libdeng2|FS: Improved interface for file creation
Browse files Browse the repository at this point in the history
Instead of a simple bool flag, use an enum for the behavior when
creating new files.
  • Loading branch information
skyjake committed Apr 3, 2013
1 parent 9e1b621 commit b2b2bbc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions doomsday/libdeng2/include/de/filesys/folder.h
Expand Up @@ -83,6 +83,12 @@ class DENG2_PUBLIC Folder : public File
PopulateOnlyThisFolder = 0x1 ///< Do not descend into subfolders while populating.
};

/// Behavior for creating new files.
enum FileCreationBehavior {
KeepExisting, ///< Existing file will be kept unchanged (safe).
ReplaceExisting ///< Existing file will be replaced.
};

public:
Folder(String const &name = "");

Expand Down Expand Up @@ -120,12 +126,12 @@ class DENG2_PUBLIC Folder : public File
* decide what kind of file is actually created. The new file is added to
* the file system's index.
*
* @param name Name or path of the new file, relative to this folder.
* @param replaceExisting Replacing existing file with the same name.
* @param name Name or path of the new file, relative to this folder.
* @param behavior How to treat existing files.
*
* @return The created file (write mode enabled).
*/
File &newFile(String const &name, bool replaceExisting = false);
File &newFile(String const &name, FileCreationBehavior behavior = KeepExisting);

/**
* Creates a new file in the folder, replacing an existing file with the
Expand Down
8 changes: 4 additions & 4 deletions doomsday/libdeng2/src/filesys/folder.cpp
Expand Up @@ -153,18 +153,18 @@ Folder::Contents const &Folder::contents() const
return _contents;
}

File &Folder::newFile(String const &newPath, bool replaceExisting)
File &Folder::newFile(String const &newPath, FileCreationBehavior behavior)
{
String path = newPath.fileNamePath();
if(!path.empty())
{
// Locate the folder where the file will be created in.
return locate<Folder>(path).newFile(newPath.fileName(), replaceExisting);
return locate<Folder>(path).newFile(newPath.fileName(), behavior);
}

verifyWriteAccess();

if(replaceExisting && has(newPath))
if(behavior == ReplaceExisting && has(newPath))
{
try
{
Expand Down Expand Up @@ -199,7 +199,7 @@ File &Folder::newFile(String const &newPath, bool replaceExisting)

File &Folder::replaceFile(String const &newPath)
{
return newFile(newPath, true);
return newFile(newPath, ReplaceExisting);
}

void Folder::removeFile(String const &removePath)
Expand Down

0 comments on commit b2b2bbc

Please sign in to comment.