2020-07-26 23:46:59 -07:00
using System ;
using System.IO ;
2020-12-07 15:08:57 -08:00
namespace SabreTools.IO
2020-07-26 23:39:33 -07:00
{
/// <summary>
/// A path that optionally contains a parent root
/// </summary>
public class ParentablePath
{
2020-07-26 23:46:59 -07:00
/// <summary>
/// Current full path represented
/// </summary>
public string CurrentPath { get ; private set ; }
/// <summary>
/// Possible parent path represented (may be null or empty)
/// </summary>
public string ParentPath { get ; private set ; }
2020-07-26 23:39:33 -07:00
public ParentablePath ( string currentPath , string parentPath = null )
{
CurrentPath = currentPath ;
ParentPath = parentPath ;
}
2020-07-26 23:46:59 -07:00
/// <summary>
/// Get the proper filename (with subpath) from the file and parent combination
/// </summary>
/// <param name="sanitize">True if path separators should be converted to '-', false otherwise</param>
/// <returns>Subpath for the file</returns>
public string GetNormalizedFileName ( bool sanitize )
{
2020-12-18 22:37:33 -08:00
// If the current path is empty, we can't do anything
if ( string . IsNullOrWhiteSpace ( CurrentPath ) )
return null ;
2021-01-29 10:40:54 -08:00
// Assume the current path is the filename
string filename = Path . GetFileName ( CurrentPath ) ;
// If we have a true ParentPath, remove it from CurrentPath and return the remainder
if ( ! string . IsNullOrWhiteSpace ( ParentPath ) & & ! string . Equals ( CurrentPath , ParentPath , StringComparison . Ordinal ) )
filename = CurrentPath . Remove ( 0 , ParentPath . Length + 1 ) ;
// If we're sanitizing the path after, do so
if ( sanitize )
filename = filename . Replace ( Path . DirectorySeparatorChar , '-' ) . Replace ( Path . AltDirectorySeparatorChar , '-' ) ;
return filename ;
2020-07-26 23:46:59 -07:00
}
/// <summary>
/// Get the proper output path for a given input file and output directory
/// </summary>
/// <param name="outDir">Output directory to use</param>
/// <param name="inplace">True if the output file should go to the same input folder, false otherwise</param>
/// <returns>Complete output path</returns>
public string GetOutputPath ( string outDir , bool inplace )
{
2020-12-18 22:37:33 -08:00
// If the current path is empty, we can't do anything
if ( string . IsNullOrWhiteSpace ( CurrentPath ) )
return null ;
// If the output dir is empty (and we're not inplace), we can't do anything
if ( string . IsNullOrWhiteSpace ( outDir ) & & ! inplace )
return null ;
2020-07-26 23:46:59 -07:00
// Check if we have a split path or not
bool splitpath = ! string . IsNullOrWhiteSpace ( ParentPath ) ;
2021-01-29 10:40:54 -08:00
// If we have an inplace output, use the directory name from the input path
if ( inplace )
return Path . GetDirectoryName ( CurrentPath ) ;
// If the current and parent paths are the same, just use the output directory
if ( ! splitpath | | CurrentPath . Length = = ParentPath . Length )
return outDir ;
// By default, the working parent directory is the parent path
string workingParent = ParentPath ;
// TODO: Should this be the default? Always create a subfolder if a folder is found?
// If we are processing a path that is coming from a directory and we are outputting to the current directory, we want to get the subfolder to write to
if ( outDir = = Environment . CurrentDirectory )
workingParent = Path . GetDirectoryName ( ParentPath ) ;
// Determine the correct subfolder based on the working parent directory
int extraLength = workingParent . EndsWith ( ':' )
| | workingParent . EndsWith ( Path . DirectorySeparatorChar )
| | workingParent . EndsWith ( Path . AltDirectorySeparatorChar ) ? 0 : 1 ;
return Path . GetDirectoryName ( Path . Combine ( outDir , CurrentPath . Remove ( 0 , workingParent . Length + extraLength ) ) ) ;
2020-07-26 23:46:59 -07:00
}
2020-07-26 23:39:33 -07:00
}
}