105 lines
No EOL
3.9 KiB
C#
105 lines
No EOL
3.9 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace ForbiddenStrings
|
|
{
|
|
class UtilsForbiddenStringsManager
|
|
{
|
|
// Normalize filenames
|
|
public static string GetUsableFileName(string fileName)
|
|
{
|
|
fileName = fileName.Replace(@"\", "/");
|
|
fileName = fileName.ToLower();
|
|
|
|
return fileName;
|
|
}
|
|
|
|
// Return the extension of the file (blablabla.exe will return exe)
|
|
public static string GetExtensionFile(string fileName)
|
|
{
|
|
return fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - fileName.LastIndexOf(".") - 1);
|
|
}
|
|
|
|
// Return true if the extension is managed by the ForbiddenStrings
|
|
//TODO : Do not hardcode the values
|
|
public static bool isExtensionManaged(string fileName)
|
|
{
|
|
string extension = UtilsForbiddenStringsManager.GetExtensionFile(fileName);
|
|
|
|
return extension == "cpp" || extension == "h" || extension == "inl" || extension == "cxx" || extension == "cs";
|
|
}
|
|
|
|
public static List<string> GetListNamesFilesFromDirectoryAndItsSubDirectories(string directory)
|
|
{
|
|
List<string> namesFiles = new List<string>();
|
|
namesFiles.AddRange(UtilsForbiddenStringsManager.GetListNamesFilesFromDirectory(directory));
|
|
namesFiles.AddRange(UtilsForbiddenStringsManager.GetListNamesFilesFromSubDirectories(directory));
|
|
return namesFiles;
|
|
}
|
|
|
|
// Get a list of the names of the files of a directory
|
|
// (and only the directory, not the subdirectories)
|
|
// according to the extensions which are managed.
|
|
private static List<string> GetListNamesFilesFromDirectory(string directory)
|
|
{
|
|
List<string> fileNames = new List<string>();
|
|
|
|
foreach (string file in System.IO.Directory.GetFiles(directory))
|
|
{
|
|
if (UtilsForbiddenStringsManager.isExtensionManaged(file))
|
|
fileNames.Add(file);
|
|
}
|
|
|
|
return fileNames;
|
|
}
|
|
|
|
// Get a list of the names of the files of the subdirectories
|
|
// (and only the subdirectory, not the directory itself)
|
|
// according to the extensions which are managed.
|
|
private static List<string> GetListNamesFilesFromSubDirectories(string directory)
|
|
{
|
|
List<string> fileNames = new List<string>();
|
|
|
|
foreach (string currentDirectory in Directory.GetDirectories(directory))
|
|
{
|
|
fileNames.AddRange(GetListNamesFilesFromDirectory(currentDirectory));
|
|
fileNames.AddRange(GetListNamesFilesFromSubDirectories(currentDirectory));
|
|
}
|
|
|
|
return fileNames;
|
|
}
|
|
|
|
public static string GetFormattedFileName(string fileName, string rootName)
|
|
{
|
|
fileName = fileName.Replace(UtilsForbiddenStringsManager.FormatRootName(rootName), "");
|
|
return GetUsableFileName(fileName);
|
|
}
|
|
|
|
// If the name of the root finish by / or \, we remove it to be "on par" with the formart of m_Path in forbidden_strings
|
|
private static string FormatRootName(string rootName)
|
|
{
|
|
if (rootName.EndsWith("/") || rootName.EndsWith("\\"))
|
|
rootName = rootName.Substring(0, rootName.Length - 2);
|
|
|
|
return rootName;
|
|
}
|
|
|
|
}
|
|
|
|
class UtilsForbiddenStrings
|
|
{
|
|
public static string GetFileContent(string fileName)
|
|
{
|
|
// Initialize everything needed to compare the ForbiddenString and the file's content
|
|
//StreamReader srCurrentFile = System.IO.File.OpenText(GetUsableFileName(fileName, m_ClientRoot, rootValue));
|
|
StreamReader srCurrentFile = System.IO.File.OpenText(fileName);
|
|
|
|
string fileText = srCurrentFile.ReadToEnd();
|
|
|
|
srCurrentFile.Close();
|
|
|
|
return fileText;
|
|
}
|
|
}
|
|
}
|
|
; |