| | 1 | | using System.Text; |
| | 2 | | using System.Text.RegularExpressions; |
| | 3 | |
|
| | 4 | | namespace SharpHoundCommonLib |
| | 5 | | { |
| | 6 | | public class CSVComputerStatus |
| | 7 | | { |
| | 8 | | public const string StatusSuccess = "Success"; |
| 23 | 9 | | public string ComputerName { get; set; } |
| 23 | 10 | | public string Task { get; set; } |
| 27 | 11 | | public string Status { get; set; } |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Converts to CSV format for output |
| | 15 | | /// </summary> |
| | 16 | | /// <returns></returns> |
| | 17 | | public string ToCsv() |
| 0 | 18 | | { |
| 0 | 19 | | return $"{StringToCsvCell(ComputerName)}, {StringToCsvCell(Task)}, {StringToCsvCell(Status)}"; |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Helper function to escape text before encoding to CSV |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="str"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | private static string StringToCsvCell(string str) |
| 0 | 28 | | { |
| 0 | 29 | | if (str == null) |
| 0 | 30 | | return null; |
| 0 | 31 | | str = Regex.Replace(str, @"\t|\n|\r", ""); |
| 0 | 32 | | var mustQuote = str.Contains(",") || str.Contains("\"") || str.Contains("\r") || str.Contains("\n"); |
| 0 | 33 | | if (!mustQuote) return str; |
| 0 | 34 | | var sb = new StringBuilder(); |
| 0 | 35 | | sb.Append("\""); |
| 0 | 36 | | foreach (var nextChar in str) |
| 0 | 37 | | { |
| 0 | 38 | | sb.Append(nextChar); |
| 0 | 39 | | if (nextChar == '"') |
| 0 | 40 | | sb.Append("\""); |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | sb.Append("\""); |
| 0 | 44 | | return sb.ToString(); |
| 0 | 45 | | } |
| | 46 | | } |
| | 47 | | } |