示例#1
0
		/// <summary>Creates a new DelimitedClassBuilder.</summary>
		/// <param name="options">The specifications for the Csv file.</param>
		public CsvClassBuilder(CsvOptions options): base(options.RecordClassName, options.Delimiter.ToString())
		{
			IgnoreFirstLines = 1;

			if (options.SampleFileName != string.Empty)
			{
				string firstLine = CommonEngine.RawReadFirstLines(options.SampleFileName, 1);

				if (options.HeaderLines > 0)
				{
					foreach (string header in firstLine.Split(options.HeaderDelimiter == char.MinValue ? options.Delimiter : options.HeaderDelimiter))
					{
						AddField(StringToIdentifier(header));
					}
				}
				else
				{
					int fieldsNbr = firstLine.Split(options.Delimiter).Length;
					for(int i = 0; i < fieldsNbr; i++)
						AddField(options.FieldsPrefix + i.ToString());
				}

			}
			else if (options.NumberOfFields > 0)
			{
				AddFields(options.NumberOfFields, options.FieldsPrefix);
			}
			else
				throw new BadUsageException("You must provide a SampleFileName or a NumberOfFields to parse a genric CSV file.");

		}
示例#2
0
 /// <summary>
 /// Reads a CSV File and return their contents as DataTable
 /// </summary>
 /// <param name="classname">The name of the record class</param>
 /// <param name="delimiter">The delimiter for each field</param>
 /// <param name="filename">The file to read.</param>
 /// <param name="hasHeader">Indicates if the file contains a header with the field names.</param>
 /// <returns>The contents of the file as a DataTable</returns>
 public static DataTable CsvToDataTable(string filename, string classname, char delimiter, bool hasHeader)
 {
     var options = new CsvOptions(classname, delimiter, filename);
     if (hasHeader == false)
         options.HeaderLines = 0;
     return CsvToDataTable(filename, options);
 }
示例#3
0
        /// <summary>
        /// Creates a new instance of this class.
        /// </summary>
        /// <param name="stream">The stream to be read.</param>
        /// <param name="options">The CSV options.</param>
        public CsvReader(System.IO.Stream stream, CsvOptions options)
            : base(stream, options.Encoding)
        {
            // Validate constructor parameters.
            if (options == null) { throw new System.ArgumentNullException("options"); }

            // Initialize field members.
            this._header = null;
            this._options = options;
        }
示例#4
0
        /// <summary>
        /// Creates a new instance of this class.
        /// </summary>
        /// <param name="path">The file path to write to.</param>
        /// <param name="append">
        /// True to append data to the file; False to overwrite the file. If the specified
        /// file does not exist, this parameter has no effect, and the constructor creates
        /// a new file.
        /// </param>
        /// <param name="options">The CSV options.</param>
        public CsvWriter(string path, bool append, CsvOptions options)
            : base(path, append, options.Encoding)
        {
            // Validate constructor parameters.
            if (options == null) { throw new System.ArgumentNullException("options"); }

            // Initialize field members.
            this._header = null;
            this._options = options;
        }
示例#5
0
        /// <summary> Constructs the CSV reader for the provided text reader </summary>
        /// <param name="reader">The text reader to read from</param>
        /// <param name="options">Options for parsing the text</param>
        /// <param name="fieldDelim">The character used to delineate fields</param>
        /// <param name="formatter">The format provided used for interpreting numbers and dates</param>
        /// <param name="depth">Provides for nested CSV parsers</param>
        protected CsvReader(TextReader reader, CsvOptions options, char fieldDelim, IFormatProvider formatter, int depth)
        {
            _fieldNames = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
            _delim = fieldDelim;
            _reader = reader;
            _options = options;
            _formatting = formatter;
            _depth = depth;

            _recordCount = 0;
            _closed = false;
            _currentFields = new string[0];

            ReadHeader();
        }
示例#6
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, StrUtil.Utf8, true);
			string strData = sr.ReadToEnd();
			sr.Close();

			CsvOptions opt = new CsvOptions();
			opt.BackslashIsEscape = false;

			CsvStreamReaderEx csr = new CsvStreamReaderEx(strData, opt);

			while(true)
			{
				string[] vLine = csr.ReadLine();
				if(vLine == null) break;

				AddEntry(vLine, pwStorage);
			}
		}
        public void ReadFileHeader1()
        {
            string file = TestCommon.GetPath("Good", "RealCsvComma1.txt");
            string classname = "CustomerComma";
            char delimiter = ',';

            CsvOptions options = new CsvOptions(classname, delimiter, file);
            options.HeaderLines = 0;

            CsvEngine engine = new CsvEngine(options);

            Assert.AreEqual(classname, engine.RecordType.Name);

            DataTable dt = engine.ReadFileAsDT(file);

            Assert.AreEqual(21, dt.Rows.Count);
            Assert.AreEqual(21, engine.TotalRecords);
            Assert.AreEqual(0, engine.ErrorManager.ErrorCount);

            Assert.AreEqual("Field_0", dt.Columns[0].ColumnName);
        }
示例#8
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, StrUtil.Utf8, true);
			string str = sr.ReadToEnd();
			sr.Close();

			// All fields are enclosed in '"', however '"' in data is
			// not encoded (broken format)

			str = str.Trim();
			str = StrUtil.NormalizeNewLines(str, false); // To Unix
			char chFieldSep = StrUtil.GetUnusedChar(str);
			str = str.Replace("\",\"", new string(chFieldSep, 1));
			char chRecSep = StrUtil.GetUnusedChar(str);
			str = str.Replace("\"\n\"", new string(chRecSep, 1));
			if(str.StartsWith("\"") && str.EndsWith("\"") && (str.Length >= 2))
				str = str.Substring(1, str.Length - 2);
			else { Debug.Assert(false); }
			if(!NativeLib.IsUnix()) str = StrUtil.NormalizeNewLines(str, true);

			CsvOptions opt = new CsvOptions();
			opt.BackslashIsEscape = false;
			opt.FieldSeparator = chFieldSep;
			opt.RecordSeparator = chRecSep;
			opt.TextQualifier = char.MinValue;

			CsvStreamReaderEx csr = new CsvStreamReaderEx(str, opt);

			while(true)
			{
				string[] vLine = csr.ReadLine();
				if(vLine == null) break;

				AddEntry(vLine, pwStorage);
			}
		}
示例#9
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, Encoding.Unicode, true);
			string strData = sr.ReadToEnd();
			sr.Close();

			CsvOptions opt = new CsvOptions();
			opt.BackslashIsEscape = false;

			CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

			while(true)
			{
				string[] v = csv.ReadLine();
				if(v == null) break;
				if(v.Length < 5) continue;

				if(v[0].Equals("url", StrUtil.CaseIgnoreCmp) &&
					v[1].Equals("username", StrUtil.CaseIgnoreCmp) &&
					v[2].Equals("password", StrUtil.CaseIgnoreCmp))
					continue; // Header

				PwGroup pg = pwStorage.RootGroup;
				string strGroup = v[4];
				if(!string.IsNullOrEmpty(strGroup))
					pg = pg.FindCreateGroup(strGroup, true);

				PwEntry pe = new PwEntry(true, true);
				pg.AddEntry(pe, true);

				ImportUtil.AppendToField(pe, PwDefs.UrlField, v[0], pwStorage);
				ImportUtil.AppendToField(pe, PwDefs.UserNameField, v[1], pwStorage);
				ImportUtil.AppendToField(pe, PwDefs.PasswordField, v[2], pwStorage);
				ImportUtil.AppendToField(pe, PwDefs.TitleField, v[3], pwStorage);
			}
		}
示例#10
0
        public void WorkWithGanttChartColumn()
        {
            // ExStart:WorkWithGanttChartColumn
            // ExFor: GanttChartColumn
            // ExFor: GanttChartColumn.#ctor(Int32,Field)
            // ExFor: GanttChartColumn.#ctor(String,Int32,Field)
            // ExFor: GanttChartColumn.#ctor(String,Int32,TaskToColumnTextConverter)
            // ExFor: GanttChartColumn.#ctor(String,Int32,TaskToColumnTextConverter,Field)
            // ExFor: GanttChartColumn.Field
            // ExFor: GanttChartColumn.GetColumnText(Task)
            // ExFor: TaskToColumnTextConverter
            // ExSummary: Shows how to add Gantt chart view columns to be exported.
            var project = new Project(DataDir + "Project2.mpp");
            var task    = project.RootTask.Children.GetById(1);

            var columns = new List <ViewColumn>
            {
                new GanttChartColumn(20, Field.TaskUniqueID),
                new GanttChartColumn("Name", 150, Field.TaskName),
                new GanttChartColumn("Start", 100, Field.TaskStart),
                new GanttChartColumn("End", 100, Field.TaskFinish),
                new GanttChartColumn("R-Initials", 100, Field.TaskResourceInitials),
                new GanttChartColumn("R-Names", 100, Field.TaskResourceNames),
                new GanttChartColumn("Work", 50, Field.TaskWork),
                new GanttChartColumn(
                    "Cost",
                    80,
                    delegate(Task t)
                {
                    return(t.Get(Tsk.Cost).ToString(CultureInfo.InvariantCulture));
                }),
                new GanttChartColumn(
                    "Actual Cost",
                    80,
                    delegate(Task t)
                {
                    return(t.Get(Tsk.ActualCost).ToString(CultureInfo.InvariantCulture));
                },
                    Field.TaskActualCost)
            };

            // iterate over columns
            foreach (var column in columns)
            {
                var col = (GanttChartColumn)column;
                Console.WriteLine("Column Name: " + col.Name);
                Console.WriteLine("Column Field: " + col.Field);
                Console.WriteLine("Column Text: " + col.GetColumnText(task));
                Console.WriteLine();
            }

            var options = new CsvOptions
            {
                View = new ProjectView(columns),
                PresentationFormat = PresentationFormat.GanttChart
            };

            project.Save(OutDir + "WorkWithGanttChartColumn_out.csv", options);

            // ExEnd:WorkWithGanttChartColumn
        }
示例#11
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
                                    IStatusLogger slLogger)
        {
            StreamReader sr      = new StreamReader(sInput, StrUtil.Utf8, true);
            string       strData = sr.ReadToEnd();

            sr.Close();

            CsvOptions opt = new CsvOptions();

            opt.BackslashIsEscape = false;
            opt.TextQualifier     = char.MinValue;
            opt.TrimFields        = true;

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

            string strMapIgnore  = Guid.NewGuid().ToString();
            string strMapGroup   = Guid.NewGuid().ToString();
            string strMapTags    = Guid.NewGuid().ToString();
            string strMapLastMod = Guid.NewGuid().ToString();
            string strMapEMail   = Guid.NewGuid().ToString();

            Dictionary <string, string> dMaps = new Dictionary <string, string>(
                StrUtil.CaseIgnoreComparer);

            dMaps["title"]                  = PwDefs.TitleField;
            dMaps["type"]                   = strMapIgnore;
            dMaps["username_field"]         = strMapIgnore;
            dMaps["username"]               = PwDefs.UserNameField;
            dMaps["password_field"]         = strMapIgnore;
            dMaps["password"]               = PwDefs.PasswordField;
            dMaps["url"]                    = PwDefs.UrlField;
            dMaps["category"]               = strMapGroup;
            dMaps["note"]                   = PwDefs.NotesField;
            dMaps["autofill"]               = strMapIgnore;
            dMaps["autofillenabled"]        = strMapIgnore;
            dMaps["last_password_change"]   = strMapIgnore;
            dMaps["lastmodified"]           = strMapLastMod;
            dMaps["iban"]                   = PwDefs.UserNameField;
            dMaps["bic"]                    = "BIC";
            dMaps["banking_pin"]            = PwDefs.PasswordField;
            dMaps["card_number"]            = PwDefs.UserNameField;
            dMaps["card_holder"]            = "Card Holder";
            dMaps["card_pin"]               = PwDefs.PasswordField;
            dMaps["card_verification_code"] = "Verification Code";
            dMaps["valid_from"]             = "Valid From";
            dMaps["valid_thru"]             = "Valid To";
            dMaps["name"]                   = PwDefs.UserNameField;
            dMaps["firstname"]              = PwDefs.UserNameField;
            dMaps["street"]                 = PwDefs.NotesField;
            dMaps["houseno"]                = PwDefs.NotesField;
            dMaps["zip"]                    = PwDefs.NotesField;
            dMaps["city"]                   = PwDefs.NotesField;
            dMaps["mobile_phone"]           = PwDefs.NotesField;
            dMaps["phone"]                  = PwDefs.NotesField;
            dMaps["email"]                  = strMapEMail;
            dMaps["birthday"]               = "Birthday";
            dMaps["tags"]                   = strMapTags;
            dMaps["keyword"]                = strMapTags;

            string[] vNames = csv.ReadLine();
            if ((vNames == null) || (vNames.Length == 0))
            {
                Debug.Assert(false); return;
            }

            for (int i = 0; i < vNames.Length; ++i)
            {
                string str = vNames[i];

                if (string.IsNullOrEmpty(str))
                {
                    Debug.Assert(false); str = strMapIgnore;
                }
                else
                {
                    string strMapped = null;
                    dMaps.TryGetValue(str, out strMapped);

                    if (string.IsNullOrEmpty(strMapped))
                    {
                        Debug.Assert(false);
                        strMapped = ImportUtil.MapNameToStandardField(str, true);
                        if (string.IsNullOrEmpty(strMapped))
                        {
                            strMapped = PwDefs.NotesField;
                        }
                    }

                    str = strMapped;
                }

                vNames[i] = str;
            }

            Dictionary <string, PwGroup> dGroups = new Dictionary <string, PwGroup>();

            while (true)
            {
                string[] v = csv.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length == 0)
                {
                    continue;
                }

                PwEntry pe = new PwEntry(true, true);
                PwGroup pg = pwStorage.RootGroup;

                for (int i = 0; i < v.Length; ++i)
                {
                    string strValue = v[i];
                    if (string.IsNullOrEmpty(strValue))
                    {
                        continue;
                    }

                    strValue = strValue.Replace(@"<COMMA>", ",");
                    strValue = strValue.Replace(@"<-N/L-/>", "\n");

                    strValue = StrUtil.NormalizeNewLines(strValue, true);

                    string strName = ((i < vNames.Length) ? vNames[i] : PwDefs.NotesField);

                    if (strName == strMapIgnore)
                    {
                    }
                    else if (strName == strMapGroup)
                    {
                        dGroups.TryGetValue(strValue, out pg);
                        if (pg == null)
                        {
                            pg      = new PwGroup(true, true);
                            pg.Name = strValue;

                            pwStorage.RootGroup.AddGroup(pg, true);
                            dGroups[strValue] = pg;
                        }
                    }
                    else if (strName == strMapTags)
                    {
                        StrUtil.AddTags(pe.Tags, StrUtil.StringToTags(strValue));
                    }
                    else if (strName == strMapLastMod)
                    {
                        double dUnix;
                        if (double.TryParse(strValue, out dUnix))
                        {
                            pe.LastModificationTime = TimeUtil.ConvertUnixTime(dUnix);
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else if (strName == strMapEMail)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.UrlField,
                                                 "mailto:" + strValue, pwStorage);
                    }
                    else
                    {
                        ImportUtil.AppendToField(pe, strName, strValue, pwStorage);
                    }
                }

                pg.AddEntry(pe, true);
            }
        }
示例#12
0
        public static IList <LocalizationItem> ImportFromCsv(string fileName, out string errors)
        {
            CsvOptions options = new CsvOptions();

            options.Separator = Properties.Settings.Default.ImportCsvDelimiter[0];
            if (Properties.Settings.Default.ImportContainsHeader == true)
            {
                options.HeaderMode = HeaderMode.HeaderPresent;
            }
            else
            {
                options.HeaderMode = HeaderMode.HeaderAbsent;
            }
            errors = null;
            try
            {
                string rawText = "";
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    TextReader tr = new StreamReader(fs);
                    rawText = tr.ReadToEnd();
                    tr.Close();
                }
                IEnumerable <ICsvLine>  lines = CsvReader.ReadFromText(rawText, options);
                List <LocalizationItem> items = new List <LocalizationItem>();
                LocalizationItem        item;
                int    i = 1;
                string temp;
                bool   state, boolValue;
                foreach (var line in lines)
                {
                    if (line.ColumnCount < 7)
                    {
                        errors = "Row " + i.ToString() + " has too few values. 7 columns are expected.";
                        return(null);
                    }
                    item = new LocalizationItem();
                    for (int j = 0; j < line.ColumnCount; j++)
                    {
                        if (j == 0)
                        {
                            item.StreamName = line[j];
                        }
                        else if (j == 1)
                        {
                            item.resourceKey = line[j];
                        }
                        else if (j == 2)
                        {
                            item.ResourceCategory = line[j];
                        }
                        else if (j == 3)
                        {
                            temp  = line[j];
                            state = FemtoXLSX.WorksheetReader.GetBooleanValue(temp, out boolValue);
                            if (state == false)
                            {
                                errors = "The boolean value of column " + (j + 1).ToString() + " in row " + i.ToString() + "could not be resolved";
                                return(null);
                            }
                            else
                            {
                                item.IsReadable = boolValue;
                            }
                        }
                        else if (j == 4)
                        {
                            temp  = line[j];
                            state = FemtoXLSX.WorksheetReader.GetBooleanValue(temp, out boolValue);
                            if (state == false)
                            {
                                errors = "The boolean value of column " + (j + 1).ToString() + " in row " + i.ToString() + "could not be resolved";
                                return(null);
                            }
                            else
                            {
                                item.IsModifieable = boolValue;
                            }
                        }
                        else if (j == 5)
                        {
                            item.comment = line[j];
                        }
                        else if (j == 6)
                        {
                            item.content = line[j];
                        }
                    }
                    items.Add(item);
                    i++;
                }
                return(items);
            }
            catch (Exception e)
            {
                errors = e.Message;
                return(null);
            }
        }
示例#13
0
        /// <summary>
        /// Simply dumps the DataTable contents to a delimited file. Only
        /// allows to set the delimiter.
        /// If options.IncludeHeaderNames = true, add to the very first row of the CSV, the name of the columns
        /// taken from DataTable
        /// </summary>
        /// <param name="dt">The source Data Table</param>
        /// <param name="filename">The destination file.</param>
        /// <param name="options">The options used to write the file</param>
        public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options)
        {
            using (var fs = new StreamWriter(filename, false, options.Encoding, DefaultWriteBufferSize)) {
                // output header
                if( options.IncludeHeaderNames ){
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (i > 0)
                        {
                            fs.Write(options.Delimiter);
                        }
                        fs.Write(options.ValueToString(dt.Columns[i].ColumnName));
                    }
                    fs.Write(StringHelper.NewLine);
                }

                //output rows
                foreach (DataRow dr in dt.Rows) {
                    object[] fields = dr.ItemArray;

                    for (int i = 0; i < fields.Length; i++) {
                        if (i > 0)
                            fs.Write(options.Delimiter);

                        fs.Write(options.ValueToString(fields[i]));
                    }
                    fs.Write(StringHelper.NewLine);
                }
                fs.Close();
            }
        }
示例#14
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
            IStatusLogger slLogger)
        {
            StreamReader sr = new StreamReader(sInput, Encoding.Default);
            string strData = sr.ReadToEnd();
            sr.Close();
            sInput.Close();

            Dictionary<string, PwGroup> dGroups = new Dictionary<string, PwGroup>();
            dGroups[string.Empty] = pwStorage.RootGroup;

            CsvOptions opt = new CsvOptions();
            opt.BackslashIsEscape = false;

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

            while(true)
            {
                string[] v = csv.ReadLine();
                if(v == null) break;
                if(v.Length == 0) continue;
                if(v[0].StartsWith("TurboPasswords CSV Export File")) continue;
                if(v.Length < 24) { Debug.Assert(false); continue; }
                if((v[0] == "Category") && (v[1] == "Type")) continue;

                PwEntry pe = new PwEntry(true, true);

                PwGroup pg;
                string strGroup = v[0];
                if(!dGroups.TryGetValue(strGroup, out pg))
                {
                    pg = new PwGroup(true, true, strGroup, PwIcon.Folder);
                    dGroups[string.Empty].AddGroup(pg, true);
                    dGroups[strGroup] = pg;
                }
                pg.AddEntry(pe, true);

                string strType = v[1];

                for(int f = 0; f < 6; ++f)
                {
                    string strKey = v[2 + (2 * f)];
                    string strValue = v[2 + (2 * f) + 1];
                    if(strKey.Length == 0) strKey = PwDefs.NotesField;
                    if(strValue.Length == 0) continue;

                    if(strKey == "Description")
                        strKey = PwDefs.TitleField;
                    else if(((strType == "Contact") || (strType == "Personal Info")) &&
                        (strKey == "Name"))
                        strKey = PwDefs.TitleField;
                    else if(((strType == "Membership") || (strType == "Insurance")) &&
                        (strKey == "Company"))
                        strKey = PwDefs.TitleField;
                    else if(strKey == "SSN")
                        strKey = PwDefs.UserNameField;
                    else
                    {
                        string strMapped = ImportUtil.MapNameToStandardField(strKey, false);
                        if(!string.IsNullOrEmpty(strMapped)) strKey = strMapped;
                    }

                    ImportUtil.AppendToField(pe, strKey, strValue, pwStorage,
                        ((strKey == PwDefs.NotesField) ? "\r\n" : ", "), false);
                }

                ImportUtil.AppendToField(pe, PwDefs.NotesField, v[20], pwStorage,
                    "\r\n\r\n", false);
                if(v[21].Length > 0)
                    ImportUtil.AppendToField(pe, "Login URL", v[21], pwStorage, null, true);
            }
        }
 /// <summary>
 /// Create a CsvEngine using the specified sample file with
 /// their headers.
 /// </summary>
 /// <param name="options">The options used to create the record mapping class.</param>
 public CsvEngine(CsvOptions options)
     : base(GetMappingClass(options))
 {
 }
示例#16
0
        private bool GuessFieldTypes()
        {
            CsvOptions opt = GetCsvOptions();

            if (opt == null)
            {
                Debug.Assert(false); return(false);
            }

            string            strData = GetDecodedText();
            CsvStreamReaderEx csv     = new CsvStreamReaderEx(strData, opt);

            string[] v;
            while (true)
            {
                v = csv.ReadLine();
                if (v == null)
                {
                    return(false);
                }
                if (v.Length == 0)
                {
                    continue;
                }
                if ((v.Length == 1) && (v[0].Length == 0))
                {
                    continue;
                }
                break;
            }
            if (v.Length <= 3)
            {
                return(false);
            }

            CsvFieldInfo[] vFields     = new CsvFieldInfo[v.Length];
            int            nDetermined = 0;

            for (int i = 0; i < v.Length; ++i)
            {
                CsvFieldInfo fi = GuessFieldType(v[i]);
                if (fi != null)
                {
                    ++nDetermined;
                }
                else
                {
                    fi = new CsvFieldInfo(CsvFieldType.Ignore, null, null);
                }

                vFields[i] = fi;
            }

            // Accept the guesses only if at least half of them are
            // probably correct
            if (nDetermined < (v.Length + 1) / 2)
            {
                return(false);
            }

            m_lvFields.Items.Clear();
            foreach (CsvFieldInfo fi in vFields)
            {
                AddCsvField(fi.Type, fi.Name, fi.Format);
            }

            return(true);
        }
示例#17
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, Encoding.Default, true);
			string strData = sr.ReadToEnd();
			sr.Close();

			CsvOptions o = new CsvOptions();
			o.BackslashIsEscape = false;

			CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, o);

			SortedDictionary<string, PwGroup> dictGroups =
				new SortedDictionary<string, PwGroup>();
			while(true)
			{
				string[] vLine = csv.ReadLine();
				if(vLine == null) break;
				if(vLine.Length == 0) continue;

				if(vLine.Length == 1)
				{
					Debug.Assert(vLine[0] == StrHeader);
					continue;
				}

				// Support old version 3.4
				if(vLine.Length == 9)
				{
					string[] v = new string[13];
					for(int i = 0; i < 7; ++i) v[i] = vLine[i];
					for(int i = 7; i < 11; ++i) v[i] = string.Empty;
					v[11] = vLine[7];
					v[12] = vLine[8];

					vLine = v;
				}

				if(vLine.Length == 13)
					ProcessCsvLine(vLine, pwStorage, dictGroups);
				else { Debug.Assert(false); }
			}
		}
示例#18
0
        public static void HandleCsvLineRead(object[] fields, CsvOptions options, ILogger logger)
        {
            string outline = fields.Join(options.FieldSeparator.ToString());

            logger.LogDebug($"Read: {outline}");
        }
示例#19
0
 /// <summary>
 /// Initialisiert ein neues <see cref="CsvWriter"/>-Objekt, mit dem eine CSV-Datei ohne Kopfzeile geschrieben wird.
 /// </summary>
 /// <param name="fileName">Der Dateipfad der zu schreibenden CSV-Datei. Wenn die Datei existiert, wird sie überschrieben.</param>
 /// <param name="columnsCount">Anzahl der Spalten in der CSV-Datei.</param>
 /// <param name="options">Optionen für die zu schreibende CSV-Datei.</param>
 /// <param name="textEncoding">Die zu verwendende Textkodierung oder <c>null</c> für UTF-8 mit BOM.</param>
 /// <param name="fieldSeparator">Das in der CSV-Datei zu verwendende Feldtrennzeichen.</param>
 ///
 /// <exception cref="ArgumentNullException"><paramref name="fileName"/> ist <c>null</c>.</exception>
 /// <exception cref="ArgumentException"><paramref name="fileName"/> ist kein gültiger Dateipfad.</exception>
 /// <exception cref="IOException">E/A-Fehler.</exception>
 public CsvWriter(
     string fileName, int columnsCount, CsvOptions options = CsvOptions.Default, Encoding?textEncoding = null, char fieldSeparator = ',')
     : this(columnsCount, fieldSeparator, options) => _writer = InitStreamWriter(fileName, textEncoding);
 public CsvGeneratorImplTests()
 {
     stream         = new MemoryStream();
     writer         = new StreamWriter(stream);
     defaultOptions = GetDefaultOptions();
 }
示例#21
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="inputFile">The text file to read from</param>
 /// <param name="options">Options for parsing the text</param>
 public CsvReader(string inputFile, CsvOptions options)
     : this(inputFile, options, ',', CultureInfo.CurrentCulture)
 { }
示例#22
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="reader">The text reader to read from</param>
 /// <param name="options">Options for parsing the text</param>
 /// <param name="fieldDelim">The character used to delineate fields</param>
 /// <param name="formatter">The format provided used for interpreting numbers and dates</param>
 public CsvReader(TextReader reader, CsvOptions options, char fieldDelim, IFormatProvider formatter)
     : this(reader, options, fieldDelim, formatter, 0)
 { }
示例#23
0
        private static PwGroup FindCreateGroup(string strSpec, PwGroup pgStorage,
                                               Dictionary <string, PwGroup> dRootGroups, string strSep, CsvOptions opt,
                                               bool bMergeGroups)
        {
            List <string> l = SplitGroupPath(strSpec, strSep, opt);

            if (bMergeGroups)
            {
                char   chSep   = StrUtil.GetUnusedChar(strSpec);
                string strPath = AssembleGroupPath(l, 0, chSep);

                return(pgStorage.FindCreateSubTree(strPath, new char[1] {
                    chSep
                }));
            }

            string strRootSub = l[0];

            if (!dRootGroups.ContainsKey(strRootSub))
            {
                PwGroup pgNew = new PwGroup(true, true);
                pgNew.Name = strRootSub;
                pgStorage.AddGroup(pgNew, true);
                dRootGroups[strRootSub] = pgNew;
            }
            PwGroup pg = dRootGroups[strRootSub];

            if (l.Count > 1)
            {
                char   chSep      = StrUtil.GetUnusedChar(strSpec);
                string strSubPath = AssembleGroupPath(l, 1, chSep);

                pg = pg.FindCreateSubTree(strSubPath, new char[1] {
                    chSep
                });
            }

            return(pg);
        }
示例#24
0
文件: CsvReader.cs 项目: d-barker/csv
 public ReadLine(string[] headers, Dictionary <string, int> headerLookup, int index, string raw, CsvOptions options)
 {
     this.headerLookup = headerLookup;
     this.options      = options;
     Headers           = headers;
     Raw   = raw;
     Index = index;
 }
        public void IsSetTest1()
        {
            CsvOptions options = CsvOptions.ThrowOnEmptyLines | CsvOptions.TrimColumns;

            Assert.IsTrue(options.IsSet(CsvOptions.ThrowOnEmptyLines));
        }
        /// <summary>
        /// Simply dumps the DataTable contents to a delimited file. Only
        /// allows to set the delimiter.
        /// </summary>
        /// <param name="dt">The source Data Table</param>
        /// <param name="filename">The destination file.</param>
        /// <param name="options">The options used to write the file</param>
        public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options)
        {
            using (var fs = new StreamWriter(filename, false, options.Encoding, EngineBase.DefaultWriteBufferSize))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    object[] fields = dr.ItemArray;

                    for(int i = 0; i < fields.Length; i++)
                    {
                        if (i > 0)
                            fs.Write(options.Delimiter);

                        fs.Write(options.ValueToString(fields[i]));
                    }
                    fs.Write(StringHelper.NewLine);
                }
                fs.Close();
            }
        }
        public void IsSetTest2()
        {
            CsvOptions options = CsvOptions.ThrowOnEmptyLines | CsvOptions.TrimColumns;

            Assert.IsFalse(options.IsSet(CsvOptions.None));
        }
示例#28
0
 /// <summary>Reads a Csv File and return their contents as DataTable</summary>
 /// <param name="filename">The file to read.</param>
 /// <param name="options">The options used to create the record mapping class.</param>
 /// <returns>The contents of the file as a DataTable</returns>
 public static DataTable CsvToDataTable(string filename, CsvOptions options)
 {
     return(CsvEngine.CsvToDataTable(filename, options));
 }
示例#29
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
                                    IStatusLogger slLogger)
        {
            StreamReader sr      = new StreamReader(sInput, StrUtil.Utf8, true);
            string       strData = sr.ReadToEnd();

            sr.Close();

            CsvOptions opt = new CsvOptions();

            opt.BackslashIsEscape = false;
            opt.TrimFields        = true;

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

            string[] vNames = csv.ReadLine();
            if ((vNames == null) || (vNames.Length == 0))
            {
                Debug.Assert(false); return;
            }

            for (int i = 0; i < vNames.Length; ++i)
            {
                vNames[i] = (vNames[i] ?? string.Empty).ToLowerInvariant();
            }

            while (true)
            {
                string[] v = csv.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length == 0)
                {
                    continue;
                }

                PwEntry pe = new PwEntry(true, true);
                pwStorage.RootGroup.AddEntry(pe, true);

                Debug.Assert(v.Length == vNames.Length);
                int m = Math.Min(v.Length, vNames.Length);

                for (int i = 0; i < m; ++i)
                {
                    string strValue = v[i];
                    if (string.IsNullOrEmpty(strValue))
                    {
                        continue;
                    }

                    string   strName = vNames[i];
                    string   strTo   = null;
                    DateTime?odt;

                    switch (strName)
                    {
                    case "autologin":
                    case "protectedwithpassword":
                    case "subdomainonly":
                    case "type":
                    case "tk_export_version":
                        break;                                 // Ignore

                    case "kind":
                        if (strValue.Equals("note", StrUtil.CaseIgnoreCmp))
                        {
                            pe.IconId = PwIcon.Note;
                        }
                        else if (strValue.Equals("identity", StrUtil.CaseIgnoreCmp) ||
                                 strValue.Equals("drivers", StrUtil.CaseIgnoreCmp) ||
                                 strValue.Equals("passport", StrUtil.CaseIgnoreCmp) ||
                                 strValue.Equals("ssn", StrUtil.CaseIgnoreCmp))
                        {
                            pe.IconId = PwIcon.Identity;
                        }
                        else if (strValue.Equals("cc", StrUtil.CaseIgnoreCmp))
                        {
                            pe.IconId = PwIcon.Money;
                        }
                        else if (strValue.Equals("membership", StrUtil.CaseIgnoreCmp))
                        {
                            pe.IconId = PwIcon.UserKey;
                        }
                        break;

                    case "name":
                    case "title":
                        strTo = PwDefs.TitleField;
                        break;

                    case "cardholder":
                    case "email":
                    case "login":
                        strTo = PwDefs.UserNameField;
                        break;

                    case "password":
                        strTo = PwDefs.PasswordField;
                        break;

                    case "url":
                    case "website":
                        strTo = PwDefs.UrlField;
                        break;

                    case "document_content":
                    case "memo":
                    case "note":
                        strTo = PwDefs.NotesField;
                        break;

                    case "city":
                    case "company":
                    case "country":
                    case "number":
                    case "state":
                    case "street":
                    case "telephone":
                        strTo = (new string(char.ToUpperInvariant(strName[0]), 1)) +
                                strName.Substring(1);
                        break;

                    case "deliveryplace":
                        strTo = "Delivery Place";
                        break;

                    case "firstname":
                        strTo = "First Name";
                        break;

                    case "lastname":
                        strTo = "Last Name";
                        break;

                    case "memberid":
                        strTo = "Member ID";
                        break;

                    case "phonenumber":
                        strTo = "Phone Number";
                        break;

                    case "streetnumber":
                        strTo = "Street Number";
                        break;

                    case "zipcode":
                        strTo = "ZIP Code";
                        break;

                    case "dateofbirth":
                        strTo    = "Date of Birth";
                        strValue = DateToString(strValue);
                        break;

                    case "expirationdate":
                    case "expirydate":
                        odt = ParseTime(strValue);
                        if (odt.HasValue)
                        {
                            pe.Expires    = true;
                            pe.ExpiryTime = odt.Value;
                        }
                        break;

                    case "favorite":
                        if (StrUtil.StringToBoolEx(strValue).GetValueOrDefault(false))
                        {
                            pe.AddTag("Favorite");
                        }
                        break;

                    case "gender":
                        if ((strValue == "0") || (strValue == "1"))
                        {
                            strTo    = "Gender";
                            strValue = ((strValue == "0") ? "Male" : "Female");
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case "hexcolor":
                        if ((strValue.Length == 6) && StrUtil.IsHexString(strValue, true))
                        {
                            Color c = Color.FromArgb(unchecked ((int)(0xFF000000U |
                                                                      Convert.ToUInt32(strValue, 16))));

                            Color cG = UIUtil.ColorToGrayscale(c);
                            if (cG.B < 128)
                            {
                                c = UIUtil.LightenColor(c, 0.5);
                            }

                            pe.BackgroundColor = c;
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                        break;

                    case "issuedate":
                    case "issueddate":
                        strTo    = "Issue Date";
                        strValue = DateToString(strValue);
                        break;

                    case "membersince":
                        strTo    = "Member Since";
                        strValue = DateToString(strValue);
                        break;

                    default:
                        Debug.Assert(false);                                 // Unknown field
                        break;
                    }

                    if (!string.IsNullOrEmpty(strTo))
                    {
                        ImportUtil.AppendToField(pe, strTo, strValue, pwStorage);
                    }
                }
            }
        }
示例#30
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="inputFile">The text file to read from</param>
 /// <param name="options">Options for parsing the text</param>
 /// <param name="fieldDelim">The character used to delineate fields</param>
 /// <param name="formatter">The format provided used for interpreting numbers and dates</param>
 public CsvReader(string inputFile, CsvOptions options, char fieldDelim, IFormatProvider formatter)
     : this(new StreamReader(File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read)), options, fieldDelim, formatter)
 { }
示例#31
0
 public CastrCSVBase(string csv, CsvOptions csvOptions)
 {
     _csv        = csv;
     _csvOptions = csvOptions;
 }
示例#32
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
                                    IStatusLogger slLogger)
        {
            StreamReader sr      = new StreamReader(sInput, Encoding.Default);
            string       strData = sr.ReadToEnd();

            sr.Close();

            CsvOptions opt = new CsvOptions();

            opt.BackslashIsEscape = false;
            opt.TextQualifier     = char.MaxValue;         // No text qualifier

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);
            PwGroup           pg  = pwStorage.RootGroup;

            char[] vGroupSplit = new char[] { '\\' };

            while (true)
            {
                string[] v = csv.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length < 1)
                {
                    continue;
                }

                for (int i = 0; i < v.Length; ++i)
                {
                    v[i] = ParseString(v[i]);
                }

                if (v[0].StartsWith("\\"))                    // Group
                {
                    string strGroup = v[0].Trim(vGroupSplit); // Also from end
                    if (strGroup.Length > 0)
                    {
                        pg = pwStorage.RootGroup.FindCreateSubTree(strGroup,
                                                                   vGroupSplit);

                        if (v.Length >= 6)
                        {
                            pg.Notes = v[5].Trim();
                        }
                        if ((v.Length >= 5) && (v[4].Trim().Length > 0))
                        {
                            if (pg.Notes.Length > 0)
                            {
                                pg.Notes += Environment.NewLine + Environment.NewLine;
                            }

                            pg.Notes += v[4].Trim();
                        }
                    }
                }
                else                 // Entry
                {
                    PwEntry pe = new PwEntry(true, true);
                    pg.AddEntry(pe, true);

                    List <string> l = new List <string>(v);
                    while (l.Count < 8)
                    {
                        Debug.Assert(false);
                        l.Add(string.Empty);
                    }

                    ImportUtil.AppendToField(pe, PwDefs.TitleField, l[0], pwStorage);
                    ImportUtil.AppendToField(pe, PwDefs.UserNameField, l[1], pwStorage);
                    ImportUtil.AppendToField(pe, PwDefs.PasswordField, l[2], pwStorage);
                    ImportUtil.AppendToField(pe, PwDefs.UrlField, l[3], pwStorage);
                    ImportUtil.AppendToField(pe, PwDefs.NotesField, l[4], pwStorage);

                    if (l[5].Length > 0)
                    {
                        ImportUtil.AppendToField(pe, "Custom 1", l[5], pwStorage);
                    }
                    if (l[6].Length > 0)
                    {
                        ImportUtil.AppendToField(pe, "Custom 2", l[6], pwStorage);
                    }
                    if (l[7].Length > 0)
                    {
                        ImportUtil.AppendToField(pe, "Custom 3", l[7], pwStorage);
                    }
                }
            }
        }
示例#33
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="reader">The text reader to read from</param>
 /// <param name="options">Options for parsing the text</param>
 public CsvReader(TextReader reader, CsvOptions options)
     : this(reader, options, ',', CultureInfo.CurrentCulture)
 {
 }
示例#34
0
        static void Main(string[] args)
        {
            Console.WriteLine("Version Updater 1.0");
            if (args.Any())
            {
                foreach (string arg in args)
                {
                    Console.WriteLine(arg);
                }
            }

            Parser.Default.ParseArguments <CommandLineOptions>(args).WithParsed(options =>
            {
                if (!string.IsNullOrWhiteSpace(options.CsvPath))
                {
                    StringBuilder builder = new StringBuilder();
                    using (FileStream stream = new FileStream(options.CsvPath, FileMode.Open))
                    {
                        Console.WriteLine("File opened...");
                        CsvOptions csvOptions = new CsvOptions();
                        csvOptions.HeaderMode = HeaderMode.HeaderAbsent;

                        var csvFile = CsvReader.ReadFromStream(stream, csvOptions);

                        var csvLines = csvFile.ToList();
                        Console.WriteLine($"Lines in file: {csvLines.Count()}");

                        Task.Run(async() =>
                        {
                            foreach (var line in csvLines)
                            {
                                Console.WriteLine("Processing CSV entry...");

                                var packageId = line[1];
                                packageId     = Regex.Replace(packageId, @"(\[(.*?)\])*", "");

                                Console.WriteLine($"Package ID: {packageId}");
                                HttpClient client = new HttpClient();
                                var response      =
                                    await client.GetAsync(
                                        $"https://api.nuget.org/v3/registration3/{packageId.ToLower()}/index.json");
                                var contents = await response.Content.ReadAsStringAsync();

                                try
                                {
                                    JObject jsonContent     = JObject.Parse(contents);
                                    var deserializedContent = JsonConvert.DeserializeObject <NuGetPackage>(jsonContent.ToString());

                                    var version = (from c in deserializedContent.items.Last().items where c.catalogEntry.listed == true select c).Last().catalogEntry.version;

                                    Console.WriteLine(packageId);

                                    if (packageId.Equals("Microsoft.ServiceFabric.Data", StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        Console.WriteLine("Here it is!");
                                    }
                                    Console.WriteLine("Latest version: " + version);

                                    builder.AppendLine($"{line[0]},{line[1]},{version}");
                                }
                                catch
                                {
                                    Console.WriteLine("Could not get information for package: " + packageId);
                                }
                            }
                        }).Wait();
                    }

                    File.WriteAllText(options.CsvPath, builder.ToString());
                }
            });
        }
示例#35
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="reader">The text reader to read from</param>
 /// <param name="options">Options for parsing the text</param>
 /// <param name="fieldDelim">The character used to delineate fields</param>
 /// <param name="formatter">The format provided used for interpreting numbers and dates</param>
 public CsvReader(TextReader reader, CsvOptions options, char fieldDelim, IFormatProvider formatter)
     : this(reader, options, fieldDelim, formatter, 0)
 {
 }
示例#36
0
文件: CastrCSV.cs 项目: nasa03/Castr
 public CastrCSV(string csv, CsvOptions csvOptions)
     : base(csv, csvOptions)
 {
 }
示例#37
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="inputFile">The text file to read from</param>
 /// <param name="options">Options for parsing the text</param>
 public CsvReader(string inputFile, CsvOptions options, char FieldDelim)
     : this(inputFile, options, FieldDelim, CultureInfo.CurrentCulture)
 {
 }
示例#38
0
        private void PerformImport(PwGroup pgStorage, bool bCreatePreview)
        {
            List <CsvFieldInfo> lFields = GetCsvFieldInfos();

            if (bCreatePreview)
            {
                int dx = m_lvImportPreview.ClientRectangle.Width;                 // Before clearing

                m_lvImportPreview.Items.Clear();
                m_lvImportPreview.Columns.Clear();

                foreach (CsvFieldInfo cfi in lFields)
                {
                    string strCol = CsvFieldToString(cfi.Type);
                    if (cfi.Type == CsvFieldType.CustomString)
                    {
                        strCol = (cfi.Name ?? string.Empty);
                    }
                    m_lvImportPreview.Columns.Add(strCol, dx / lFields.Count);
                }
            }

            CsvOptions opt = GetCsvOptions();

            if (opt == null)
            {
                Debug.Assert(bCreatePreview); return;
            }

            string            strData = GetDecodedText();
            CsvStreamReaderEx csr     = new CsvStreamReaderEx(strData, opt);

            Dictionary <string, PwGroup> dGroups = new Dictionary <string, PwGroup>();

            dGroups[string.Empty] = pgStorage;

            if (bCreatePreview)
            {
                m_lvImportPreview.BeginUpdate();
            }

            DateTime dtNow           = DateTime.UtcNow;
            DateTime dtNoExpire      = KdbTime.NeverExpireTime.ToDateTime();
            bool     bIgnoreFirstRow = m_cbIgnoreFirst.Checked;
            bool     bIsFirstRow     = true;
            bool     bMergeGroups    = m_cbMergeGroups.Checked;

            while (true)
            {
                string[] v = csr.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length == 0)
                {
                    continue;
                }
                if ((v.Length == 1) && (v[0].Length == 0))
                {
                    continue;
                }

                if (bIsFirstRow && bIgnoreFirstRow)
                {
                    bIsFirstRow = false;
                    continue;
                }
                bIsFirstRow = false;

                PwGroup pg = pgStorage;
                PwEntry pe = new PwEntry(true, true);

                ListViewItem lvi = null;
                for (int i = 0; i < Math.Min(v.Length, lFields.Count); ++i)
                {
                    string       strField = v[i];
                    CsvFieldInfo cfi      = lFields[i];

                    if (cfi.Type == CsvFieldType.Ignore)
                    {
                    }
                    else if (cfi.Type == CsvFieldType.Group)
                    {
                        pg = FindCreateGroup(strField, pgStorage, dGroups,
                                             cfi.Format, opt, bMergeGroups);
                    }
                    else if (cfi.Type == CsvFieldType.Title)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.TitleField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.UserName)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.UserNameField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.Password)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.PasswordField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.Url)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.UrlField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.Notes)
                    {
                        ImportUtil.AppendToField(pe, PwDefs.NotesField,
                                                 strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.CustomString)
                    {
                        ImportUtil.AppendToField(pe, (string.IsNullOrEmpty(cfi.Name) ?
                                                      PwDefs.NotesField : cfi.Name), strField, m_pwDatabase);
                    }
                    else if (cfi.Type == CsvFieldType.CreationTime)
                    {
                        pe.CreationTime = ParseDateTime(ref strField, cfi, dtNow);
                    }
                    // else if(cfi.Type == CsvFieldType.LastAccessTime)
                    //	pe.LastAccessTime = ParseDateTime(ref strField, cfi, dtNow);
                    else if (cfi.Type == CsvFieldType.LastModTime)
                    {
                        pe.LastModificationTime = ParseDateTime(ref strField, cfi, dtNow);
                    }
                    else if (cfi.Type == CsvFieldType.ExpiryTime)
                    {
                        bool bParseSuccess;
                        pe.ExpiryTime = ParseDateTime(ref strField, cfi, dtNow,
                                                      out bParseSuccess);
                        pe.Expires = (bParseSuccess && (pe.ExpiryTime != dtNoExpire));
                    }
                    else if (cfi.Type == CsvFieldType.Tags)
                    {
                        List <string> lTags = StrUtil.StringToTags(strField);
                        foreach (string strTag in lTags)
                        {
                            pe.AddTag(strTag);
                        }
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    if (bCreatePreview)
                    {
                        strField = StrUtil.MultiToSingleLine(strField);

                        if (lvi != null)
                        {
                            lvi.SubItems.Add(strField);
                        }
                        else
                        {
                            lvi = m_lvImportPreview.Items.Add(strField);
                        }
                    }
                }

                if (bCreatePreview)
                {
                    // Create remaining subitems
                    for (int r = v.Length; r < lFields.Count; ++r)
                    {
                        if (lvi != null)
                        {
                            lvi.SubItems.Add(string.Empty);
                        }
                        else
                        {
                            lvi = m_lvImportPreview.Items.Add(string.Empty);
                        }
                    }
                }

                pg.AddEntry(pe, true);
            }

            if (bCreatePreview)
            {
                m_lvImportPreview.EndUpdate();
                ProcessResize();
            }
        }
示例#39
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="inputFile">The text file to read from</param>
 /// <param name="options">Options for parsing the text</param>
 /// <param name="fieldDelim">The character used to delineate fields</param>
 /// <param name="formatter">The format provided used for interpreting numbers and dates</param>
 public CsvReader(string inputFile, CsvOptions options, char fieldDelim, IFormatProvider formatter)
     : this(new StreamReader(File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.Default), options, fieldDelim, formatter)
 {
 }
        public static IEnumerable <SampleFeed> GetSampleFeeds()
        {
            using (var csvStream = _currentAssembly.GetManifestResourceStream(_manifestResourceStreamPrefix + "files-metadata.csv"))
            {
                var csvOptions = new CsvOptions
                {
                    HeaderMode = HeaderMode.HeaderPresent,
                };

                foreach (var line in CsvReader.ReadFromStream(csvStream, csvOptions))
                {
                    var feed = new SampleFeed
                    {
                        FileName = line["FileName"],
                        FeedUrl  = line["FeedUrl"],
                        Title    = line["Title"],
                        WebUrl   = line["WebUrl"],
                        Source   = line["Source"],
                    };

                    if (feed.FileName.EndsWith(".xml"))
                    {
                        feed.LazyXDocument = new Lazy <XDocument>(() =>
                        {
                            var streamName = $"{_manifestResourceStreamPrefix}Files.{feed.FileName}";
                            using (var feedStream = _currentAssembly.GetManifestResourceStream(streamName))
                            {
                                if (feedStream == null)
                                {
                                    throw new ArgumentNullException($"Couldn't find manifest resource stream '{streamName}'.");
                                }

                                try
                                {
                                    return(RelaxedXDocumentLoader.LoadFromStream(feedStream));
                                }

                                catch (XmlException ex)
                                {
                                    Debugger.Break();
                                    throw;
                                }
                            }
                        });
                    }
                    else if (feed.FileName.EndsWith(".json"))
                    {
                        feed.LazyJsonDocument = new Lazy <JObject>(() =>
                        {
                            var streamName = $"{_manifestResourceStreamPrefix}Files.{feed.FileName}";
                            using (var feedStream = _currentAssembly.GetManifestResourceStream(streamName))
                            {
                                if (feedStream == null)
                                {
                                    throw new ArgumentNullException($"Couldn't find manifest resource stream '{streamName}'.");
                                }

                                try
                                {
                                    using (var streamReader = new StreamReader(feedStream))
                                        using (var jsonReader = new JsonTextReader(streamReader))
                                        {
                                            var jsonDocument = JObject.Load(jsonReader,
                                                                            new JsonLoadSettings
                                            {
                                                CommentHandling = CommentHandling.Ignore,
                                                DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Replace,
                                                LineInfoHandling = LineInfoHandling.Load,
                                            });
                                            return(jsonDocument);
                                        }
                                }
                                catch (JsonException ex)
                                {
                                    Debugger.Break();
                                    throw;
                                }
                            }
                        });
                    }
                    else
                    {
                        throw new Exception($"Cannot parse file '{feed.FileName}'.");
                    }

                    yield return(feed);
                }
            }
        }
示例#41
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
                                    IStatusLogger slLogger)
        {
            StreamReader sr      = new StreamReader(sInput, Encoding.Default, true);
            string       strData = sr.ReadToEnd();

            sr.Close();

            CsvOptions o = new CsvOptions();

            o.BackslashIsEscape = false;

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, o);

            SortedDictionary <string, PwGroup> dictGroups =
                new SortedDictionary <string, PwGroup>();

            while (true)
            {
                string[] vLine = csv.ReadLine();
                if (vLine == null)
                {
                    break;
                }
                if (vLine.Length == 0)
                {
                    continue;
                }

                if (vLine.Length == 1)
                {
                    Debug.Assert(vLine[0] == StrHeader);
                    continue;
                }

                // Support old version 3.4
                if (vLine.Length == 9)
                {
                    string[] v = new string[13];
                    for (int i = 0; i < 7; ++i)
                    {
                        v[i] = vLine[i];
                    }
                    for (int i = 7; i < 11; ++i)
                    {
                        v[i] = string.Empty;
                    }
                    v[11] = vLine[7];
                    v[12] = vLine[8];

                    vLine = v;
                }

                if (vLine.Length == 13)
                {
                    ProcessCsvLine(vLine, pwStorage, dictGroups);
                }
                else
                {
                    Debug.Assert(false);
                }
            }
        }
 protected void AssertThatOptionsAreCsv(CsvOptions actual)
 {
     Assert.That(actual.QuoteChar, Is.EqualTo('"'));
     Assert.That(actual.Delimiter, Is.EqualTo(','));
 }
示例#43
0
 /// <summary>
 /// Creates a new instance of this class.
 /// </summary>
 /// <param name="path">The file path to write to.</param>
 /// <param name="options">The CSV options.</param>
 public CsvWriter(string path, CsvOptions options)
     : this(path, false, options)
 {
 }
 /// <summary>
 /// Reads a CSV File and return their contents as
 /// DataTable
 /// </summary>
 /// <param name="filename">The file to read.</param>
 /// <param name="options">The options used to create the record mapping class.</param>
 /// <returns>The contents of the file as a DataTable</returns>
 public static DataTable CsvToDataTable(string filename, CsvOptions options)
 {
     CsvEngine engine = new CsvEngine(options);
     return engine.ReadFileAsDT(filename);
 }
示例#45
0
 /// <summary>Simply dumps the DataTable contents to a delimited file. Only allows to set the delimiter.</summary>
 /// <param name="dt">The source Data Table</param>
 /// <param name="filename">The destination file.</param>
 /// <param name="options">The options used to write the file</param>
 public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options)
 {
     CsvEngine.DataTableToCsv(dt, filename, options);
 }
 private static Type GetMappingClass(CsvOptions options)
 {
     CsvClassBuilder cb = new CsvClassBuilder(options);
     return cb.CreateRecordClass();
 }
示例#47
0
 public static Frame Csv(this FluentReader reader, Stream inputStream, CsvOptions options = null)
 {
     return(CsvFormatReader.ReadToFrame(inputStream, options));
 }
示例#48
0
		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			StreamReader sr = new StreamReader(sInput, Encoding.Default);
			string strData = sr.ReadToEnd();
			sr.Close();

			CsvOptions opt = new CsvOptions();
			opt.BackslashIsEscape = false;
			opt.TextQualifier = char.MaxValue; // No text qualifier

			CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);
			PwGroup pg = pwStorage.RootGroup;
			char[] vGroupSplit = new char[] { '\\' };

			while(true)
			{
				string[] v = csv.ReadLine();
				if(v == null) break;
				if(v.Length < 1) continue;

				for(int i = 0; i < v.Length; ++i)
					v[i] = ParseString(v[i]);

				if(v[0].StartsWith("\\")) // Group
				{
					string strGroup = v[0].Trim(vGroupSplit); // Also from end
					if(strGroup.Length > 0)
					{
						pg = pwStorage.RootGroup.FindCreateSubTree(strGroup,
							vGroupSplit);

						if(v.Length >= 6) pg.Notes = v[5].Trim();
						if((v.Length >= 5) && (v[4].Trim().Length > 0))
						{
							if(pg.Notes.Length > 0)
								pg.Notes += Environment.NewLine + Environment.NewLine;

							pg.Notes += v[4].Trim();
						}
					}
				}
				else // Entry
				{
					PwEntry pe = new PwEntry(true, true);
					pg.AddEntry(pe, true);

					List<string> l = new List<string>(v);
					while(l.Count < 8)
					{
						Debug.Assert(false);
						l.Add(string.Empty);
					}

					ImportUtil.AppendToField(pe, PwDefs.TitleField, l[0], pwStorage);
					ImportUtil.AppendToField(pe, PwDefs.UserNameField, l[1], pwStorage);
					ImportUtil.AppendToField(pe, PwDefs.PasswordField, l[2], pwStorage);
					ImportUtil.AppendToField(pe, PwDefs.UrlField, l[3], pwStorage);
					ImportUtil.AppendToField(pe, PwDefs.NotesField, l[4], pwStorage);

					if(l[5].Length > 0)
						ImportUtil.AppendToField(pe, "Custom 1", l[5], pwStorage);
					if(l[6].Length > 0)
						ImportUtil.AppendToField(pe, "Custom 2", l[6], pwStorage);
					if(l[7].Length > 0)
						ImportUtil.AppendToField(pe, "Custom 3", l[7], pwStorage);
				}
			}
		}
示例#49
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
                                    IStatusLogger slLogger)
        {
            StreamReader sr      = new StreamReader(sInput, Encoding.Default);
            string       strData = sr.ReadToEnd();

            sr.Close();
            sInput.Close();

            Dictionary <string, PwGroup> dGroups = new Dictionary <string, PwGroup>();

            dGroups[string.Empty] = pwStorage.RootGroup;

            CsvOptions opt = new CsvOptions();

            opt.BackslashIsEscape = false;

            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

            while (true)
            {
                string[] v = csv.ReadLine();
                if (v == null)
                {
                    break;
                }
                if (v.Length == 0)
                {
                    continue;
                }
                if (v[0].StartsWith("TurboPasswords CSV Export File"))
                {
                    continue;
                }
                if (v.Length < 24)
                {
                    Debug.Assert(false); continue;
                }
                if ((v[0] == "Category") && (v[1] == "Type"))
                {
                    continue;
                }

                PwEntry pe = new PwEntry(true, true);

                PwGroup pg;
                string  strGroup = v[0];
                if (!dGroups.TryGetValue(strGroup, out pg))
                {
                    pg = new PwGroup(true, true, strGroup, PwIcon.Folder);
                    dGroups[string.Empty].AddGroup(pg, true);
                    dGroups[strGroup] = pg;
                }
                pg.AddEntry(pe, true);

                string strType = v[1];

                for (int f = 0; f < 6; ++f)
                {
                    string strKey   = v[2 + (2 * f)];
                    string strValue = v[2 + (2 * f) + 1];
                    if (strKey.Length == 0)
                    {
                        strKey = PwDefs.NotesField;
                    }
                    if (strValue.Length == 0)
                    {
                        continue;
                    }

                    if (strKey == "Description")
                    {
                        strKey = PwDefs.TitleField;
                    }
                    else if (((strType == "Contact") || (strType == "Personal Info")) &&
                             (strKey == "Name"))
                    {
                        strKey = PwDefs.TitleField;
                    }
                    else if (((strType == "Membership") || (strType == "Insurance")) &&
                             (strKey == "Company"))
                    {
                        strKey = PwDefs.TitleField;
                    }
                    else if (strKey == "SSN")
                    {
                        strKey = PwDefs.UserNameField;
                    }
                    else
                    {
                        string strMapped = ImportUtil.MapNameToStandardField(strKey, false);
                        if (!string.IsNullOrEmpty(strMapped))
                        {
                            strKey = strMapped;
                        }
                    }

                    ImportUtil.AppendToField(pe, strKey, strValue, pwStorage,
                                             ((strKey == PwDefs.NotesField) ? "\r\n" : ", "), false);
                }

                ImportUtil.AppendToField(pe, PwDefs.NotesField, v[20], pwStorage,
                                         "\r\n\r\n", false);
                if (v[21].Length > 0)
                {
                    ImportUtil.AppendToField(pe, "Login URL", v[21], pwStorage, null, true);
                }
            }
        }
        private void RunTest(string file, char delimiter, char delimiterHdr, string classname)
        {
            CsvOptions options = new CsvOptions(classname, delimiter, file);
            options.HeaderDelimiter = delimiterHdr;
            CsvEngine engine = new CsvEngine(options);

            Assert.AreEqual(classname, engine.RecordType.Name);

            DataTable dt = engine.ReadFileAsDT(file);

            Assert.AreEqual(20, dt.Rows.Count);
            Assert.AreEqual(20, engine.TotalRecords);
            Assert.AreEqual(0, engine.ErrorManager.ErrorCount);

            Assert.AreEqual("CustomerID", dt.Columns[0].ColumnName);
        }
示例#51
0
 /// <summary>
 /// Creates a new instance of this class.
 /// </summary>
 /// <param name="path">The file path to write to.</param>
 /// <param name="options">The CSV options.</param>
 public CsvWriter(string path, CsvOptions options) :
     this(path, false, options)
 {
 }
示例#52
0
        /// <summary>
        /// Reads an IEnumerable of CSV lines, each containing an IEnumerable of CSV cell values
        /// </summary>
        /// <param name="fi">The file info to read</param>
        /// <param name="options"></param>
        /// <returns>An IEnumerable of CSV lines, each containing an IEnumerable of CSV cell values</returns>
        public static IEnumerable <IEnumerable <string> > ReadCsvItems(this FileInfo fi, CsvOptions options = null)
        {
            options = options ?? new CsvOptions();

            if (fi is null)
            {
                throw new ArgumentNullException(nameof(fi));
            }

            return(ReadFile(fi).SplitCsvLines(options.LineDelimeter).Select(l => l.Trim('\r').SplitQuotedString(options)));
        }
示例#53
0
 /// <summary> Constructs the CSV reader for the provided text reader </summary>
 /// <param name="reader">The text reader to read from</param>
 /// <param name="options">Options for parsing the text</param>
 public CsvReader(TextReader reader, CsvOptions options)
     : this(reader, options, ',', CultureInfo.CurrentCulture)
 { }