private 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)
            {
                if (_delimiter == enumSourceTextDelimiter.TAB)
                {
                    nTab = sIn.IndexOf("\t", startIndex, StringComparison.Ordinal);
                }
                else
                {
                    nTab = sIn.IndexOf(",", startIndex, StringComparison.Ordinal);
                }
                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 bool ExportToCsvFile(string filename, DataTable tblSrc, bool append, EnumCharEncode encode)
 {
     _error = null;
     System.IO.StreamWriter sw = null;
     if (tblSrc == null)
     {
         _error = "Source data is null";
     }
     else if (tblSrc.Columns.Count == 0)
     {
         _error = "Source data is empty";
     }
     else
     {
         try
         {
             bool bWriteHeader = HasHeader;
             if (bWriteHeader && append)
             {
                 if (System.IO.File.Exists(filename))
                 {
                     System.IO.FileInfo fi = new System.IO.FileInfo(filename);
                     if (fi.Length > 0)
                     {
                         bWriteHeader = false;
                     }
                 }
             }
             System.Text.StringBuilder sb;
             //
             sw = new System.IO.StreamWriter(filename, append, EncodeUtility.GetEncoding(encode), 2048);
             if (bWriteHeader && !append)
             {
                 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();
                 object v = tblSrc.Rows[r][0];
                 if (v == DBNull.Value || v == null)
                 {
                     v = VPLUtil.GetDefaultValue(tblSrc.Columns[0].DataType);
                 }
                 sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(v)));
                 for (int i = 1; i < tblSrc.Columns.Count; i++)
                 {
                     sb.Append(DataTransferConvert.Delimiter(_delimiter));
                     v = tblSrc.Rows[r][i];
                     if (v == DBNull.Value || v == null)
                     {
                         v = VPLUtil.GetDefaultValue(tblSrc.Columns[i].DataType);
                     }
                     sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(v)));
                 }
                 sw.WriteLine(sb.ToString());
             }
         }
         catch (Exception er)
         {
             _error = ExceptionLimnorDatabase.FormExceptionText(er, "Error saving data to file {0}", filename);
         }
         finally
         {
             if (sw != null)
             {
                 sw.Close();
             }
         }
     }
     return(string.IsNullOrEmpty(_error));
 }