This repository has been archived by the owner on Feb 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilesUtils.cs
100 lines (85 loc) · 2.55 KB
/
FilesUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.IO;
using MimeDetective;
using System.Linq;
using Toxy;
namespace ATPR.Utils
{
/// <summary>
/// Files utils.
/// Methods for common File tasks.
/// </summary>
public static class FilesUtils
{
public static string FileToText(string filePath)
{
FileType fType;
using (Stream s = new FileStream(filePath, FileMode.Open))
{
fType = s.GetFileType();
}
if (fType == null)
return new R2PipeStrategy().ExtractText(filePath, "");
var availableStrategies = from type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
where typeof(IFileToTextStrategy).IsAssignableFrom(type) && !type.IsInterface
select type;
foreach (var sType in availableStrategies)
{
var strategy = Activator.CreateInstance(sType) as IFileToTextStrategy;
if (strategy.IsSupportedExtension(fType.Extension))
return strategy.ExtractText(filePath, fType.Extension);
}
return null; // Unsupported file
}
/// <summary>
/// Returns an array of files to be parsed
/// </summary>
/// <returns>A file paths array</returns>
/// <param name="inputPath">The input path.</param>
public static string[] GetFiles(string inputPath)
{
if (Directory.Exists(inputPath))
return Directory.GetFiles(inputPath);
if (File.Exists(inputPath))
return new string[] { inputPath };
throw new DirectoryNotFoundException(inputPath);
}
/// <summary>
/// Check if language model files exists.
/// </summary>
/// <returns><c>true</c>, if models was existsed, <c>false</c> otherwise.</returns>
/// <param name="path">Path.</param>
public static bool ExistsModels(string path)
{
return File.Exists(path);
}
/// <summary>
/// Checks if the file exists on the PATH
/// </summary>
/// <returns><c>true</c>, if on path exists, <c>false</c> otherwise.</returns>
/// <param name="fileName">File name.</param>
public static bool ExistsOnPath(string fileName)
{
return GetFullPath(fileName) != null;
}
/// <summary>
/// Returns the full path of a fileName in PATH
/// </summary>
/// <returns>The full path.</returns>
/// <param name="fileName">File name.</param>
public static string GetFullPath(string fileName)
{
if (File.Exists(fileName))
return Path.GetFullPath(fileName);
var values = Environment.GetEnvironmentVariable("PATH");
char sep = values.Contains(':') ? ':' : ';';
foreach (var path in values.Split(sep))
{
var fullPath = Path.Combine(path, fileName);
if (File.Exists(fullPath))
return fullPath;
}
return null;
}
}
}