public static bool DumpListingAsCsv(string path, ListingInfo listingInfo, string title, out string error, int start = 0, int count = Int32.MaxValue) { error = null; StreamWriter sw = null; try { int writeCnt = Math.Min(listingInfo.Items.Length, count); int startIndex = start >= writeCnt ? 0 : start; sw = new StreamWriter(path); var items = listingInfo.Items; sw.WriteLine(); int itemCount = items[0].Count; for (int i = startIndex, icnt = writeCnt; i < icnt; ++i) { sw.Write(items[i].GetItem(0)); for (int j = 1, jcnt = itemCount; j < jcnt; ++j) { sw.Write(Constants.Semicolon); sw.Write(items[i].GetItem(j).Trim()); } sw.WriteLine(); } return(true); } catch (Exception ex) { error = Utils.GetExceptionErrorString(ex); return(false); } finally { sw?.Close(); } }
public static bool DumpListing(string path, ListingInfo listingInfo, string title, out string error, int start = 0, int count = Int32.MaxValue) { error = null; StreamWriter sw = null; try { int writeCnt = Math.Min(listingInfo.Items.Length, count); int startIndex = start >= writeCnt ? 0 : start; sw = new StreamWriter(path); var cols = listingInfo.ColInfos; sw.WriteLine("### MDRDESK REPORT: "); sw.WriteLine("### TITLE: " + title); sw.WriteLine("### COUNT: " + Utils.LargeNumberString(writeCnt)); sw.WriteLine("### SEPARATOR: " + Constants.HeavyGreekCrossPadded); //sw.WriteLine("### COLUMNS: Access Count|int \u271A Last Tick|int \u271A Last Access|string \u271A String|string"); sw.Write("### COLUMNS: "); sw.Write(cols[0].Name + "|" + cols[0].ColumnType); for (int i = 1, icnt = cols.Length; i < icnt; ++i) { sw.Write(Constants.HeavyGreekCrossPadded); sw.Write(cols[i].Name + "|" + cols[i].ColumnType); } if (!string.IsNullOrWhiteSpace(listingInfo.Notes)) { var noteLines = listingInfo.Notes.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0, icnt = noteLines.Length; i < icnt; ++i) { if (noteLines[i].StartsWith(ReportFile.DescrPrefix)) { sw.WriteLine(noteLines[i]); } else { sw.WriteLine(ReportFile.DescrPrefix + noteLines[i]); } } } var items = listingInfo.Items; sw.WriteLine(); int itemCount = items[0].Count; for (int i = startIndex, icnt = writeCnt; i < icnt; ++i) { sw.Write(items[i].GetItem(0)); for (int j = 1, jcnt = itemCount; j < jcnt; ++j) { sw.Write(Constants.HeavyGreekCrossPadded); sw.Write(items[i].GetItem(j)); } sw.WriteLine(); } return(true); } catch (Exception ex) { error = Utils.GetExceptionErrorString(ex); return(false); } finally { sw?.Close(); } }
public static ListingInfo ReadReportFile(string path, out string title, out string error) { error = null; title = String.Empty; StreamReader sr = null; StringBuilder sbDescr = StringBuilderCache.Acquire(); try { int itemCnt = 0; string colSpecs = string.Empty; string[] separators = new string[] { Constants.HeavyAsteriskPadded }; string report = null; sr = new StreamReader(path); string ln = sr.ReadLine(); if (!ln.StartsWith(ReportFile.ReportPrefix)) { error = "This is not a mdrdesk report file"; return(null); } report = ln.Substring(ReportPrefix.Length); if (!string.IsNullOrWhiteSpace(report)) { sbDescr.Append("Report: ").Append(report).AppendLine(); } while (ln.StartsWith(InfoPrefix)) { if (ln.StartsWith(CountPrefix)) { var pos = Utils.SkipWhites(ln, CountPrefix.Length); itemCnt = Int32.Parse(ln.Substring(pos, ln.Length - pos), NumberStyles.AllowThousands); ln = sr.ReadLine(); } else if (ln.StartsWith(ColumnPrefix)) { var pos = Utils.SkipWhites(ln, ColumnPrefix.Length); colSpecs = ln.Substring(pos); ln = sr.ReadLine(); } else if (ln.StartsWith(TitlePrefix)) { title = ln.Substring(TitlePrefix.Length); ln = sr.ReadLine(); } else if (ln.StartsWith(SeparatorPrefix)) { var sep = ln.Substring(SeparatorPrefix.Length); separators = new[] { sep }; ln = sr.ReadLine(); } else { ln = sr.ReadLine(); } } while (ln.StartsWith(DescrPrefix)) { sbDescr.Append(ln.Substring(DescrPrefix.Length)).AppendLine(); ln = sr.ReadLine(); } ColumnType[] colTypes; ValueTuple <string, ColumnType, int>[] columns = GetColumnInfo(colSpecs, separators, out colTypes); var itemLst = new List <string>(itemCnt * columns.Length); while (ln != null) { if (ln.Length == 0 || ln[0] != '#') { ParseReportLine(ln, separators, colTypes, itemLst); } else { if (ln.StartsWith(DescrPrefix)) { sbDescr.Append(ln.Substring(DescrPrefix.Length)).AppendLine(); } else if (ln.StartsWith(CountPrefix)) { var pos = Utils.SkipWhites(ln, CountPrefix.Length); itemCnt = Int32.Parse(ln.Substring(pos, ln.Length - pos), NumberStyles.AllowThousands); } } ln = sr.ReadLine(); } int colCnt = columns.Length; Debug.Assert((itemLst.Count % colCnt) == 0); var dataAry = itemLst.ToArray(); var itemAry = new listing <string> [dataAry.Length / colCnt]; var dataNdx = 0; for (int i = 0, icnt = dataAry.Length; i < icnt; i += colCnt) { itemAry[dataNdx++] = new listing <string>(dataAry, i, colCnt); } var colInfos = new ColumnInfo[columns.Length]; for (int i = 0, icnt = colInfos.Length; i < icnt; ++i) { colInfos[i] = new ColumnInfo(columns[i].Item1, columns[i].Item2, columns[i].Item3, i + 1, true); } //if (IsColumnTypeString(colInfos[0].ColumnType)) //{ // Array.Sort(itemAry, new ListingStrCmpAsc(0)); //} //else //{ // Array.Sort(itemAry, new ListingNumCmpAsc(0)); //} var result = new ListingInfo(null, itemAry, colInfos, sbDescr.ToString(), title); return(result); } catch (Exception ex) { error = Utils.GetExceptionErrorString(ex); return(null); } finally { StringBuilderCache.Release(sbDescr); sr?.Close(); } }