Skip to content

Commit

Permalink
Bugfix for CRLF to LF #91
Browse files Browse the repository at this point in the history
Bugfix, Reformat, Add Column and Sort always change CRLF to LF
  • Loading branch information
BdR76 committed Jun 24, 2024
1 parent 6ab8b69 commit 5e492b7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions CSVLintNppPlugin/CsvLint/CsvDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ public int SkipCommentLinesAtStart(StreamReader strdata)
/// when editing, sorting or copying a data file, also copy the first comment lines
/// </summary>
/// <param name="data"> csv data </param>
public void CopyCommentLinesAtStart(StreamReader strdata, StringBuilder sb, string prefix)
public void CopyCommentLinesAtStart(StreamReader strdata, StringBuilder sb, string prefix, string CRLF)
{
// how many lines of comment to skip
int skip = (this.SkipLines >= 0 ? this.SkipLines : 0);
Expand All @@ -992,7 +992,7 @@ public void CopyCommentLinesAtStart(StreamReader strdata, StringBuilder sb, stri
// copy and add prefix and line feed
sb.Append(prefix);
sb.Append(line);
sb.Append("\n");
sb.Append(CRLF);

skip--;
}
Expand Down
38 changes: 27 additions & 11 deletions CSVLintNppPlugin/CsvLint/CsvEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public static string RemoveQuotesToString(string strinput)
return res;
}

private static string getEditorEOLchars(EndOfLine EOL)
{
switch (EOL)
{
case EndOfLine.CR: return "\r";
case EndOfLine.LF: return "\n";
default: return "\r\n"; // default is EndOfLine.CRLF
}
}

/// <summary>
/// reformat file for date, decimal and separator
/// </summary>
Expand Down Expand Up @@ -129,6 +139,7 @@ public static void ReformatDataFile(CsvDefinition csvdef, string reformatSeparat

// handle to editor
ScintillaGateway scintillaGateway = PluginBase.CurrentScintillaGateway;
var CRLF = getEditorEOLchars(scintillaGateway.GetEOLMode());

// use stringreader to go line by line
var strdata = ScintillaStreams.StreamAllText();
Expand All @@ -155,7 +166,7 @@ public static void ReformatDataFile(CsvDefinition csvdef, string reformatSeparat
}

// copy any comment lines
csvdef.CopyCommentLinesAtStart(strdata, datanew, "");
csvdef.CopyCommentLinesAtStart(strdata, datanew, "", CRLF);

// read all lines
while (!strdata.EndOfStream)
Expand Down Expand Up @@ -266,7 +277,7 @@ public static void ReformatDataFile(CsvDefinition csvdef, string reformatSeparat
};

// add line break
datanew.Append("\n");
datanew.Append(CRLF);
};
}

Expand Down Expand Up @@ -450,6 +461,7 @@ public static void ConvertToSQL(CsvDefinition csvdef)
if (enumcols1 != "") sb.Append(string.Format("-- Enumeration columns (optional)\r\n/*\r\n{0}{1}*/\r\n", enumcols1, enumcols2));

// add comment table
comment.Insert(0, "Table imported using");
var tabcomment = string.Join("\r\n", comment).Replace("'", "''");
sb.Append("-- Table comment\r\n");
switch (Main.Settings.DataConvertSQL)
Expand All @@ -475,7 +487,7 @@ public static void ConvertToSQL(CsvDefinition csvdef)
int lastend = -1; // last line end character

// copy any comment lines
csvdef.CopyCommentLinesAtStart(strdata, sb, "-- ");
csvdef.CopyCommentLinesAtStart(strdata, sb, "-- ", "\r\n");

while (!strdata.EndOfStream)
{
Expand Down Expand Up @@ -663,7 +675,7 @@ public static void ConvertToXML(CsvDefinition csvdef)
// copy any comment lines
if (csvdef.SkipLines > 0) {
sb.Append("\t<!--\n");
csvdef.CopyCommentLinesAtStart(strdata, sb, "\t");
csvdef.CopyCommentLinesAtStart(strdata, sb, "\t", "\r\n");
sb.Append("\t-->\n");
}

Expand Down Expand Up @@ -1013,6 +1025,10 @@ public static void SortData(CsvDefinition csvdef, int SortIdx, bool AscDesc, boo
return;
}

// handle to editor
ScintillaGateway scintillaGateway = PluginBase.CurrentScintillaGateway;
var CRLF = getEditorEOLchars(scintillaGateway.GetEOLMode());

// examine data and keep list of all data lines
// Note: can be a dictionary, not a list, because the sortable values are guaranteed to be unique
Dictionary<string, string> sortlines = new Dictionary<string, string>();
Expand All @@ -1031,7 +1047,7 @@ public static void SortData(CsvDefinition csvdef, int SortIdx, bool AscDesc, boo
StringBuilder sbsort = new StringBuilder();

// copy any comment lines
csvdef.CopyCommentLinesAtStart(strdata, sbsort, "");
csvdef.CopyCommentLinesAtStart(strdata, sbsort, "", CRLF);

// if first line is header column names
if (csvdef.ColNameHeader) {
Expand All @@ -1040,7 +1056,7 @@ public static void SortData(CsvDefinition csvdef, int SortIdx, bool AscDesc, boo

sbsort.Append(csvdef.ConstructLine(values, iscomm));

sbsort.Append("\n");
sbsort.Append(CRLF);
}

string sortval = "";
Expand Down Expand Up @@ -1084,11 +1100,10 @@ public static void SortData(CsvDefinition csvdef, int SortIdx, bool AscDesc, boo
// add all lines, sort by count
foreach (KeyValuePair<string, string> rec in sortlines)
{
sbsort.Append(string.Format("{0}\n", rec.Value));
sbsort.Append(string.Format("{0}{1}", rec.Value, CRLF));
}

// update text in editor
ScintillaGateway scintillaGateway = PluginBase.CurrentScintillaGateway;
scintillaGateway.SetText(sbsort.ToString());
}

Expand All @@ -1101,6 +1116,7 @@ public static void ColumnSplit(CsvDefinition csvdef, int ColumnIndex, int SplitC
{
// handle to editor
ScintillaGateway scintillaGateway = PluginBase.CurrentScintillaGateway;
var CRLF = getEditorEOLchars(scintillaGateway.GetEOLMode());

// use stringreader to go line by line
var strdata = ScintillaStreams.StreamAllText();
Expand Down Expand Up @@ -1161,7 +1177,7 @@ public static void ColumnSplit(CsvDefinition csvdef, int ColumnIndex, int SplitC
}

// copy any comment lines
csvdef.CopyCommentLinesAtStart(strdata, datanew, "");
csvdef.CopyCommentLinesAtStart(strdata, datanew, "", CRLF);

// list for building new columns
List<string> newcols = new List<string>();
Expand All @@ -1180,7 +1196,7 @@ public static void ColumnSplit(CsvDefinition csvdef, int ColumnIndex, int SplitC

datanew.Append(csvnew.ConstructLine(newcols, false));

datanew.Append("\n");
datanew.Append(CRLF);
}

// read all lines
Expand Down Expand Up @@ -1324,7 +1340,7 @@ public static void ColumnSplit(CsvDefinition csvdef, int ColumnIndex, int SplitC
datanew.Append(csvnew.ConstructLine(newcols, iscomm));

// add line break
datanew.Append("\n");
datanew.Append(CRLF);
}
};

Expand Down
Binary file modified CSVLintNppPlugin/bin/Release-x64/CSVLint.dll
Binary file not shown.
Binary file modified CSVLintNppPlugin/bin/Release/CSVLint.dll
Binary file not shown.

0 comments on commit 5e492b7

Please sign in to comment.