protected string popField(ref string sIn) { if (sIn == null) { return(""); } if (sIn.Length == 0) { return(""); } int startIndex = 0; int qi2 = -1; int qi = sIn.IndexOf('"'); if (qi >= 0) { qi2 = sIn.IndexOf('"', qi + 1); } int nTab = 0; while (true) { switch (_delimiter) { case enumSourceTextDelimiter.TAB: nTab = sIn.IndexOf('\t', startIndex); break; case enumSourceTextDelimiter.Comma: nTab = sIn.IndexOf(',', startIndex); break; default: throw new Exception("Unsupported delimiter"); } if (qi < 0 || qi2 < 0 || nTab <= 0) { break; } if (nTab > qi && nTab < qi2) { startIndex = qi2 + 1; } else { break; } } if (nTab == 0) { sIn = sIn.Substring(1); return(""); } string sRet; if (nTab > 0) { sRet = sIn.Substring(0, nTab); if (nTab == sIn.Length - 1) { sIn = ""; } else { sIn = sIn.Substring(nTab + 1); } } else { sRet = sIn; sIn = ""; } if (!string.IsNullOrEmpty(sRet) && sRet.Length > 1) { if (sRet.StartsWith("\"", StringComparison.Ordinal) && sRet.EndsWith("\"", StringComparison.Ordinal)) { sRet = sRet.Substring(1, sRet.Length - 2); } } return(DataTransferConvert.Decode(_delimiter, sRet)); }
public string ReceiveData(DataTable tblSrc, bool bSilent) { string error = null; System.IO.StreamWriter sw = null; if (tblSrc == null) { return("Source data is null"); } if (tblSrc.Columns.Count == 0) { return("Source data is empty"); } try { string sf = _links.GetValue(FILENAME) as string; bool bWriteHeader = HasHeader; if (bWriteHeader) { if (System.IO.File.Exists(sf)) { System.IO.FileInfo fi = new System.IO.FileInfo(sf); if (fi.Length > 0) { bWriteHeader = false; } } } System.Text.StringBuilder sb; // sw = new System.IO.StreamWriter(sf, _bAppend, EncodeUtility.GetEncoding(EncodingType), BufferSize); if (bWriteHeader) { sb = new System.Text.StringBuilder(); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[0].ColumnName)); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[i].ColumnName)); } sw.WriteLine(sb.ToString()); } for (int r = 0; r < tblSrc.Rows.Count; r++) { sb = new System.Text.StringBuilder(); sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(tblSrc.Rows[r][0]))); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(tblSrc.Rows[r][i]))); } sw.WriteLine(sb.ToString()); } } catch (Exception er) { error = ExceptionLimnorDatabase.FormExceptionText(er, "Error saving data to file {0}", Filename); TraceLogClass.TraceLog.ShowMessageBox = !bSilent; TraceLogClass.TraceLog.Log(er); } finally { if (sw != null) { sw.Close(); } } return(error); }