Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Almanac: Recompose a Path by Flags

The filePathPart() function below lets the developer output a path in pieces depending on the flags passed it. This can be used for optional path decomposition and recomposition based on a single variable.

enum PathPart{
    Ext = 1,
    Name = 2,
    Path = 4,
    Root = 8,
    Suffix = 16
}

char[] filePathPart(FilePath path,PathPart parts){
    char[] result;
    
    if(parts | PathPart.Root) result ~= path.root;
    if(parts | PathPart.Path) result ~= path.path;
    if(parts | PathPart.Name) result ~= path.name;
    if(parts | PathPart.Suffix) result ~= path.suffix;
    if(parts | PathPart.Ext) result ~= path.ext;
    
    return result;  
}

void example(FilePath path){
    auto myPath = filePathPart(path,PathPart.Path | PathPart.Name | PathPart.Suffix);
}