using System; using System.Linq; using System.Text; using DTS.Common.Utilities.Logging; namespace DTS.Common.Utilities { /// /// helper class for reading and processing GM Milford INI files, hopefully in the future this can be expanded into a general Export.ini file /// public class ExportINIFile { public string Location { get; } public string RemoteSaveArchiveData { get; } public string LocalSaveArchiveData { get; } public string ISFPathLocation { get; } public string JCLDir { get; } public string RemoteSaveROIData { get; } public string LocalSaveROIData { get; } public string RemoteSaveISF { get; } public string LocalSaveISF { get; } public string RemoteBinaryAllData { get; } public string LocalBinaryAllData { get; } public string RemoteBinaryROIData { get; } public string LocalBinaryROIData { get; } public string LocalXMLSetup { get; } public string RemoteXMLSetup { get; } public string LocalExportData { get; } public string RemoteExportData { get; } public string LocalLogsData { get; } public string RemoteLogsData { get; } public string LocalReportsData { get; } public string RemoteReportsData { get; } public enum Rows { [FileLineAttr("---- Default Directories to Save GM Data (remote is default if available) ----")] DefaultDirectoriesComment, [FileLineAttr("remote_save_archive_data=")] RemoteSaveArchiveData, [FileLineAttr("local_save_archive_data=")] LocalSaveArchiveData, [FileLineAttr("---- Default Directory to Read GM ISF ----")] DefaultDirectoryToReadGMISFComment, [FileLineAttr("ISF_path_location=")] ISFPathLocation, [FileLineAttr("---- ROI & JCL file location ----")] ROIAndJCLFileLocationComment, [FileLineAttr("JCL_Dir=")] JCLDir, [FileLineAttr("remote_save_roi_data=")] RemoteSaveROIData, [FileLineAttr("local_save_roi_data=")] LocalSaveROIData, [FileLineAttr("remote_save_isf=")] RemoteSaveISF, [FileLineAttr("local_save_isf=")] LocalSaveISF, [FileLineAttr("---- End INI File ----")] EOFComment, [FileLineAttr("---- Default Directories to Save Data (remote is default if available) ----")] GenericSaveDataComment, [FileLineAttr("---- Binary ALL Upload Folders ----")] GenericAllUploadFolders, [FileLineAttr("remote_binary_all_data=")] RemoteBinaryAllData, [FileLineAttr("local_binary_all_data=")] LocalBinaryAllData, [FileLineAttr("---- Binary ROI Upload Folders ----")] GenericROIUploadFolders, [FileLineAttr("remote_binary_roi_data=")] RemoteBinaryROIData, [FileLineAttr("local_binary_roi_data=")] LocalBinaryROIData, [FileLineAttr("---- XML Setup Upload Folders ----")] XMLSetupUploadFolders, [FileLineAttr("local_xml_setup=")] LocalXMLSetup, [FileLineAttr("remote_xml_setup=")] RemoteXMLSetup, [FileLineAttr("---- Export Upload Folders ----")] ExportUploadFolders, [FileLineAttr("local_export_data=")] LocalExportData, [FileLineAttr("remote_export_data=")] RemoteExportData, [FileLineAttr("---- Logs Upload Folders ----")] LogsUploadFolders, [FileLineAttr("local_logs_data=")] LocalLogsData, [FileLineAttr("remote_logs_data=")] RemoteLogsData, [FileLineAttr("---- Reports Upload Folders ----")] ReportsUploadFolder, [FileLineAttr("local_reports_data=")] LocalReportsData, [FileLineAttr("remote_reports_data=")] RemoteReportsData } public enum Errors { FILE_NOT_FOUND, FILE_NOT_READ, FILE_INCOMPLETE, FILE_INVALID_DATA } public class FileLineAttr : Attribute { public string LineString { get; } internal FileLineAttr(string attr) { LineString = attr; } public static string GetFileLine(object o) { if (o != null) { var mi = o.GetType().GetMember(o.ToString()); if (mi != null && mi.Length > 0) { var attr = GetCustomAttribute(mi[0], typeof(FileLineAttr)) as FileLineAttr; if (null != attr) { return attr.LineString; } } } return null; } } public class ExportINIFileException : Exception { public Errors Error { get; } public ExportINIFileException(Errors error, string message) : base(string.Format("{0} - {1}", error.ToString(), message)) { Error = error; } } public ExportINIFile(string path) { if (!System.IO.File.Exists(path)) { throw new ExportINIFileException(Errors.FILE_NOT_FOUND, path); } string[] lines; try { lines = System.IO.File.ReadAllLines(path); } catch (System.Exception ex) { throw new ExportINIFileException(Errors.FILE_NOT_READ, ex.Message); } if (null == lines || lines.Length < 2) { throw new ExportINIFileException(Errors.FILE_INCOMPLETE, path); } Location = lines[0]; var rows = Enum.GetValues(typeof(Rows)).Cast().ToArray(); var bValid = false; for (var i = 1; i < lines.Length; i++) { var line = lines[i].Trim(); foreach (var row in rows) { var key = FileLineAttr.GetFileLine(row); if (line.StartsWith(key)) { switch (row) { case Rows.DefaultDirectoriesComment: break; //just a comment line, nothing to store case Rows.RemoteSaveArchiveData: RemoteSaveArchiveData = line.Substring(key.Length); break; case Rows.LocalSaveArchiveData: LocalSaveArchiveData = line.Substring(key.Length); break; case Rows.DefaultDirectoryToReadGMISFComment: break; //just a comment line, nothing to store case Rows.ISFPathLocation: ISFPathLocation = line.Substring(key.Length); break; case Rows.ROIAndJCLFileLocationComment: break; //just a comment line, nothing to store case Rows.JCLDir: JCLDir = line.Substring(key.Length); break; case Rows.RemoteSaveROIData: RemoteSaveROIData = line.Substring(key.Length); break; case Rows.LocalSaveROIData: LocalSaveROIData = line.Substring(key.Length); break; case Rows.RemoteSaveISF: RemoteSaveISF = line.Substring(key.Length); break; case Rows.LocalSaveISF: LocalSaveISF = line.Substring(key.Length); break; case Rows.GenericSaveDataComment: break; //empty line case Rows.GenericAllUploadFolders: break; //empty line case Rows.GenericROIUploadFolders: break; //empty line case Rows.XMLSetupUploadFolders: break; //empty line case Rows.ExportUploadFolders: break; //empty line case Rows.LogsUploadFolders: break; //empty line case Rows.ReportsUploadFolder: break; //empty line case Rows.RemoteBinaryAllData: RemoteBinaryAllData = line.Substring(key.Length); break; case Rows.LocalBinaryAllData: LocalBinaryAllData = line.Substring(key.Length); break; case Rows.RemoteBinaryROIData: RemoteBinaryROIData = line.Substring(key.Length); break; case Rows.LocalBinaryROIData: LocalBinaryROIData = line.Substring(key.Length); break; case Rows.LocalXMLSetup: LocalXMLSetup = line.Substring(key.Length); break; case Rows.RemoteXMLSetup: RemoteXMLSetup = line.Substring(key.Length); break; case Rows.LocalExportData: LocalExportData = line.Substring(key.Length); break; case Rows.RemoteExportData: RemoteExportData = line.Substring(key.Length); break; case Rows.LocalLogsData: LocalLogsData = line.Substring(key.Length); break; case Rows.RemoteLogsData: RemoteLogsData = line.Substring(key.Length); break; case Rows.LocalReportsData: LocalReportsData = line.Substring(key.Length); break; case Rows.RemoteReportsData: RemoteReportsData = line.Substring(key.Length); break; case Rows.EOFComment: bValid = true; //for now the only thing we require is the end of file comment line ... break; default: //silently eat and log for now APILogger.Log("Export INI File parsing error, unknown field: " + row.ToString()); break; } break; } } } if (!bValid) { throw new ExportINIFileException(Errors.FILE_INCOMPLETE, "EOF line not found"); } } } }