Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
UFile.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.ComponentModel;
5 
6 namespace SiliconStudio.Core.IO
7 {
8  /// <summary>
9  /// Defines a normalized file path. See <see cref="UPath"/> for details. This class cannot be inherited.
10  /// </summary>
11  [DataContract("UFile")]
12  [TypeConverter(typeof(UFileTypeConverter))]
13  public sealed class UFile : UPath
14  {
15  /// <summary>
16  /// Initializes a new instance of the <see cref="UFile"/> class.
17  /// </summary>
18  /// <param name="filePath">The file path.</param>
19  public UFile(string filePath)
20  : base(filePath, false)
21  {
22  }
23 
24  /// <summary>
25  /// Initializes a new instance of the <see cref="UPath" /> class.
26  /// </summary>
27  /// <param name="name">The name.</param>
28  /// <param name="extension">The extension.</param>
29  public UFile(string name, string extension)
30  : this(null, null, name, extension)
31  {
32  }
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="UPath" /> class.
36  /// </summary>
37  /// <param name="directory">The directory.</param>
38  /// <param name="name">The name.</param>
39  /// <param name="extension">The extension.</param>
40  public UFile(string directory, string name, string extension)
41  : this(null, directory, name, extension)
42  {
43  }
44 
45  /// <summary>
46  /// Initializes a new instance of the <see cref="UPath" /> class.
47  /// </summary>
48  /// <param name="drive">The drive.</param>
49  /// <param name="directory">The directory.</param>
50  /// <param name="name">The name.</param>
51  /// <param name="extension">The extension.</param>
52  public UFile(string drive, string directory, string name, string extension)
53  : base(drive, directory, name, extension)
54  {
55  }
56 
57  /// <summary>
58  /// Gets the file path (<see cref="UPath.GetDirectory()"/> + '/' + <see cref="UFile.GetFileName()"/>) without the extension or drive. Can be an null if no filepath.
59  /// </summary>
60  /// <returns>The path.</returns>
61  public string GetDirectoryAndFileName()
62  {
63  var span = DirectorySpan;
64  if (NameSpan.IsValid)
65  {
66  span.Length = NameSpan.Next - span.Start;
67  }
68  return span.IsValid ? FullPath.Substring(span) : null;
69  }
70 
71  /// <summary>
72  /// Gets the name of the file without its extension. Can be null.
73  /// </summary>
74  /// <returns>The name.</returns>
75  public string GetFileName()
76  {
77  return NameSpan.IsValid ? FullPath.Substring(NameSpan) : null;
78  }
79 
80  /// <summary>
81  /// Gets the extension of the file. Can be null.
82  /// </summary>
83  /// <returns>The extension.</returns>
84  public string GetFileExtension()
85  {
86  return ExtensionSpan.IsValid ? FullPath.Substring(ExtensionSpan) : null;
87  }
88 
89  /// <summary>
90  /// Gets the name of the file with its extension.
91  /// </summary>
92  /// <value>The name of file.</value>
93  public string GetFileNameWithExtension()
94  {
95  var span = NameSpan;
96  if (ExtensionSpan.IsValid)
97  {
98  span.Length = ExtensionSpan.Next - span.Start;
99  }
100  return span.IsValid ? FullPath.Substring(span) : null;
101  }
102 
103  /// <summary>
104  /// Makes this instance relative to the specified anchor directory.
105  /// </summary>
106  /// <param name="anchorDirectory">The anchor directory.</param>
107  /// <returns>A relative path of this instance to the anchor directory.</returns>
108  public new UFile MakeRelative(UDirectory anchorDirectory)
109  {
110  return (UFile)base.MakeRelative(anchorDirectory);
111  }
112 
113  /// <summary>
114  /// Determines whether the specified path is a valid <see cref="UFile"/>
115  /// </summary>
116  /// <param name="path">The path.</param>
117  /// <returns><c>true</c> if the specified path is a valid <see cref="UFile"/>; otherwise, <c>false</c>.</returns>
118  public new static bool IsValid(string path)
119  {
120  if (path == null) throw new ArgumentNullException("path");
121  if (!UPath.IsValid(path))
122  {
123  return false;
124  }
125  if (path.Length > 0 && path.EndsWith(DirectorySeparatorChar, DirectorySeparatorCharAlt))
126  {
127  return false;
128  }
129  return true;
130  }
131 
132  /// <summary>
133  /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="UPath"/>.
134  /// </summary>
135  /// <param name="fullPath">The full path.</param>
136  /// <returns>The result of the conversion.</returns>
137  public static implicit operator UFile(string fullPath)
138  {
139  return fullPath != null ? new UFile(fullPath) : null;
140  }
141  }
142 }
UFile(string filePath)
Initializes a new instance of the UFile class.
Definition: UFile.cs:19
UFile(string name, string extension)
Initializes a new instance of the UPath class.
Definition: UFile.cs:29
static new bool IsValid(string path)
Determines whether the specified path is a valid UFile
Definition: UFile.cs:118
string GetFileExtension()
Gets the extension of the file. Can be null.
Definition: UFile.cs:84
UFile(string drive, string directory, string name, string extension)
Initializes a new instance of the UPath class.
Definition: UFile.cs:52
static bool IsValid(string path)
Determines whether the specified path is a valid UPath
Definition: UPath.cs:497
Defines a normalized directory path. See UPath for details. This class cannot be inherited.
Definition: UDirectory.cs:13
string GetFileName()
Gets the name of the file without its extension. Can be null.
Definition: UFile.cs:75
Base class that describes a uniform path and provides method to manipulate them. Concrete class are U...
Definition: UPath.cs:21
string GetFileNameWithExtension()
Gets the name of the file with its extension.
Definition: UFile.cs:93
delegate object TypeConverter(object arg)
document false
UFile(string directory, string name, string extension)
Initializes a new instance of the UPath class.
Definition: UFile.cs:40
string GetDirectoryAndFileName()
Gets the file path (UPath.GetDirectory() + '/' + UFile.GetFileName()) without the extension or drive...
Definition: UFile.cs:61
new UFile MakeRelative(UDirectory anchorDirectory)
Makes this instance relative to the specified anchor directory.
Definition: UFile.cs:108
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13