using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace Rokojori { public enum FilePathType { Absolute, Relative } public class FilePath { public FilePathType type; public FilePath parent; public string path; public string _fileName; public string _fileExtension; string _fullPath = null; /**Only fileName without fileExtension */ public string fileName { get { if ( _fileName == null) { _fileName = Path.GetFileNameWithoutExtension( path ); } return _fileName; } } /**Combines fileName + fileExtension */ public string fullFileName => fileName + fileExtension; /***/ public string fileExtension { get { if ( _fileExtension == null) { _fileExtension = Path.GetExtension( path ); } return _fileExtension; } } public string fullPath { get { if ( _fullPath == null ) { if ( this.parent != null ) { _fullPath = this.parent.fullPath + "/" + path; } else { _fullPath = path; } } return _fullPath; } } public static FilePath Create( string path, FilePathType type = FilePathType.Relative, FilePath parent = null ) { var rp = new FilePath(); rp.type = type; rp.parent = parent; rp.path = path; return rp; } public static FilePath Absolute( string path ) { return FilePath.Create( path, FilePathType.Absolute ); } public FilePath MakeRelative( string path ) { return FilePath.Create( path, FilePathType.Relative, this ); } public FilePath MakeAbsolutePathRelative( string absolutePath ) { absolutePath = FilePath.Normalize( absolutePath ); var ownAbsolutePath = FilePath.Normalize( fullPath ); if ( ! absolutePath.StartsWith( ownAbsolutePath ) ) { return null; } var relativePath = absolutePath.Substring( ownAbsolutePath.Length ); return MakeRelative( relativePath ); } public FilePath CreateRelativeFromAbsolutePathWithParent( string absolutePath, FilePath absoluteParent ) { var rp = new FilePath(); rp.type = FilePathType.Relative; rp.parent = absoluteParent; rp.path = Path.GetFileName( absolutePath ); rp._fullPath = absolutePath; return rp; } public static string Normalize( string path, bool removeFirst = true, bool removeLast = true ) { path = Regex.Replace( path, @"\\", "/" ); path = Regex.Replace( path, @"/+", "/" ); if ( removeFirst ) { path = Regex.Replace( path, @"^/", "" ); } if ( removeLast ) { path = Regex.Replace( path, @"/$", "" ); } return path; } public static string Join( params string[] paths ) { var sb = new StringBuilder(); for ( int i = 0; i < paths.Length; i++ ) { var path = paths[ i ]; path = Normalize( path ); if ( i != 0 ) { sb.Append( "/" ); } sb.Append( path ); } return sb.ToString(); } } }