Tanoda
pb_FileUtility.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Text.RegularExpressions;
6
7namespace GILES
8{
12 public static class pb_FileUtility
13 {
17 public static string ReadFile(string path)
18 {
19 if( !File.Exists(path))
20 {
21 Debug.LogError("File path does not exist!\n" + path);
22 return "";
23 }
24
25 string contents = File.ReadAllText(path);
26
27 return contents;
28 }
29
33 public static bool SaveFile(string path, string contents)
34 {
35 try
36 {
37 File.WriteAllText(path, contents);
38 }
39 catch(System.Exception e)
40 {
41 Debug.LogError("Failed writing to path: " + path + "\n" + e.ToString());
42 return false;
43 }
44
45 return true;
46 }
47
48 public static bool IsValidPath(string path, string extension)
49 {
50 return !string.IsNullOrEmpty(path) &&
51 Directory.Exists(path.Remove(path.LastIndexOf("/"))) &&
52 path.EndsWith(extension);
53 }
54
55
56 public static bool IsValidPath(string path)
57 {
58 return !string.IsNullOrEmpty(path) &&
59 Directory.Exists(path.Remove(path.LastIndexOf("/")));
60 }
61
66 public static string GetFullPath(string path)
67 {
68 string full = Path.GetFullPath(path);
69 return full;
70 }
71
75 public static PathType GetPathType(string path)
76 {
77 return File.Exists(path) ? PathType.File : (Directory.Exists(path) ? PathType.Directory : PathType.Null);
78 }
79
83 public static string SanitizePath(string path, string extension = null)
84 {
85 string rep = GetFullPath(path);
86 // @todo On Windows this defaults to '\', but doesn't escape correctly.
87 // Path.DirectorySeparatorChar.ToString());
88 rep = Regex.Replace(rep, "(\\\\|\\\\\\\\){1,2}|(/)", "/");
89 // white space gets the escaped symbol
90 //rep = Regex.Replace(rep, "\\s", "%20"); //COMMENTED OUT TO PREVENT THE PREVENTION OF SAVING IN SOMEWHERE WITH SPACE IN IT'S PATH
91
92 if(extension != null && !rep.EndsWith(extension))
93 {
94 if(!extension.StartsWith("."))
95 extension = "." + extension;
96
97 rep += extension;
98 }
99
100 return rep;
101 }
102 }
103}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
PathType
Definition: pb_Enum.cs:76