Пример #1
0
        public static Dictionary <TD, T_line> ReadRawData_and_fillDictionary <TD, T_line>(DictionaryReadWriteOptions_base options) where T_line : class
        {
            FileInfo                file       = new FileInfo(options.File);
            StreamReader            stream     = file.OpenText();
            Dictionary <TD, T_line> dictionary = ReadRawData_and_fillDictionary <TD, T_line>(stream, options, options.File);

            return(dictionary);
        }
Пример #2
0
        public static Dictionary <TD, T_line> ReadRawData_and_fillDictionary <TD, T_line>(StreamReader stream, DictionaryReadWriteOptions_base options, string file_name) where T_line : class
        {
            if ((options.Report == ReadWrite_report_enum.Report_main) || (options.Report == ReadWrite_report_enum.Report_everything))
            {
                Report_class.WriteLine("{0}:\nRead file for dictionary: {1}", typeof(T_line).Name, file_name);
            }
            Stopwatch timer = new Stopwatch();

            timer.Start();
            //Read headline, if it exists, determine indexes of columns to be safed in list
            //Begin
            FileInfo file = new FileInfo(options.File);

            PropertyInfo[] propInfo          = typeof(T_line).GetProperties();
            PropertyInfo   dict_key_propInfo = typeof(TD).GetProperties()[0];

            string[] columnNames = { Global_class.Empty_entry };
            int      dictionaryKey_index;

            int[] columnIndexes;
            int[] invalidLine_defining_columnIndexes  = new int[0];
            int[] invalidLine_defining_popertyIndexes = new int[0];
            int[] propertyIndexes;

            if (options.File_has_headline)
            {
                string headline = stream.ReadLine();
                columnNames         = Get_and_modify_columnNames(headline, options);
                columnIndexes       = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.Key_columnNames);
                dictionaryKey_index = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.DictionaryKey_columnName)[0];
                if (options.Invalid_line_defining_columnNames.Length > 0)
                {
                    invalidLine_defining_columnIndexes  = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.Invalid_line_defining_columnNames);
                    invalidLine_defining_popertyIndexes = Get_propertyIndexes_of_corresponding_given_columnNames <T_line>(propInfo, options.Key_propertyNames, options.Key_columnNames, options.Invalid_line_defining_columnNames);
                }
            }
            else
            {
                columnIndexes       = options.Key_columnIndexes;
                dictionaryKey_index = options.DictionaryKey_columnIndex;
            }
            propertyIndexes = Get_propertyIndexes <T_line>(propInfo, options.Key_propertyNames);
            if (columnIndexes.Length != propertyIndexes.Length)
            {
                Report_class.Write_error_line("{0}: Length columnIndexes (Key_columnNames/columnIndexes) != propertyIndexes (Key_propertyNames)", typeof(T_line).Name);
            }
            //End

            //Skip lines
            for (int indexSkip = 0; indexSkip < options.Skip_lines; indexSkip++)
            {
                stream.ReadLine();
            }

            //Determine indexes of columns which contain a safecondition, if safeconditions exist
            //Begin
            bool safeConditions_exist = options.SafeCondition_entries != null;

            int[]    safeConditions_columnIndexes = new int[0];
            string[] safeConditions_entries       = options.SafeCondition_entries;
            int      safeConditions_length        = -1;

            if (safeConditions_exist == true)
            {
                safeConditions_length = options.SafeCondition_entries.Length;
                if (options.File_has_headline)
                {
                    safeConditions_columnIndexes = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.SafeCondition_columnNames);
                }
                else
                {
                    safeConditions_columnIndexes = options.SafeCondition_columnIndexes;
                }
                if (safeConditions_columnIndexes.Length != safeConditions_entries.Length)
                {
                    Report_class.WriteLine("{0}: length safeConditions_columnIndexes (_columnNames/columnIndexes) != length safeConditions_columnEntries", typeof(T_line).Name);
                }
            }
            //End

            //Generate and fill list
            //Begin
            Dictionary <TD, T_line> dictionary = new Dictionary <TD, T_line>();

            var TD_Type     = typeof(TD);
            var T_line_Type = typeof(T_line);

            int    invalidLine_defining_columnIndexes_length = invalidLine_defining_columnIndexes.Length;
            string inputLine;
            int    readLines  = 0;
            int    safedLines = 0;
            int    colIndex;
            int    propIndex;
            bool   safeLine;
            bool   report_check_lineDelimiters = false;
            bool   valid;
            string invalidLineDefiningColumnEntry;
            int    line_count = 0;

            while ((inputLine = stream.ReadLine()) != null)
            {
                if ((inputLine.Length > 0) && (!inputLine.Substring(0, 5).Equals("-----")))
                {
                    line_count++;
                    string[] columnEntries = inputLine.Split(options.LineDelimiters);
                    if (columnEntries.Length == 1)
                    {
                        report_check_lineDelimiters = true;
                    }
                    safeLine = true;
                    if (safeConditions_exist)
                    {
                        for (int indexSC = 0; indexSC < safeConditions_length; indexSC++)
                        {
                            columnEntries[safeConditions_columnIndexes[indexSC]] = char.ToUpper(columnEntries[safeConditions_columnIndexes[indexSC]][0]) + columnEntries[safeConditions_columnIndexes[indexSC]].ToLower().Substring(1);  ///transient solution!!!!!!
                            if (safeConditions_entries[indexSC] != columnEntries[safeConditions_columnIndexes[indexSC]])
                            {
                                safeLine = false;
                            }
                        }
                    }
                    valid = true;
                    for (int indexIndex = 0; indexIndex < invalidLine_defining_columnIndexes_length; indexIndex++)
                    {
                        invalidLineDefiningColumnEntry = columnEntries[invalidLine_defining_columnIndexes[indexIndex]];
                        try
                        {
                            var obj = Convert.ChangeType(invalidLineDefiningColumnEntry, propInfo[invalidLine_defining_popertyIndexes[indexIndex]].PropertyType);
                            valid = true;
                        }
                        catch (InvalidCastException)
                        {
                            valid = false;
                        }
                        catch (FormatException)
                        {
                            valid = false;
                        }
                        catch (OverflowException)
                        {
                            valid = false;
                        }
                        catch (ArgumentNullException)
                        {
                            valid = false;
                        }
                    }
                    if ((safeLine) && (valid))
                    {
                        //TD new_dictionary_key = new TD();//)Activator.CreateInstance(TD_Type);
                        TD new_dictionary_key = (TD)Convert.ChangeType(columnEntries[dictionaryKey_index], TD_Type);

                        T_line newLine = (T_line)Activator.CreateInstance(T_line_Type);
                        for (int i = 0; i < columnIndexes.Length; i++)
                        {
                            colIndex  = columnIndexes[i];
                            propIndex = propertyIndexes[i];
                            if (columnEntries[colIndex] == "#DIV/0!")
                            {
                                columnEntries[colIndex] = "NaN";
                            }
                            if (propInfo[propIndex].PropertyType.IsEnum)
                            {
                                columnEntries[colIndex] = char.ToUpper(columnEntries[colIndex][0]) + columnEntries[colIndex].ToLower().Substring(1);
                                propInfo[propIndex].SetValue(newLine, Enum.Parse(propInfo[propIndex].PropertyType, columnEntries[colIndex]), null);
                            }
                            else if (string.IsNullOrEmpty(columnEntries[colIndex]))
                            {
                                if (propInfo[propIndex].PropertyType == typeof(int))
                                {
                                    propInfo[propIndex].SetValue(newLine, options.Empty_integer_value, null);
                                }
                                else if (propInfo[propIndex].PropertyType == typeof(string))
                                {
                                    propInfo[propIndex].SetValue(newLine, options.Empty_string_value, null);
                                }
                                else
                                {
                                    Report_class.Write_error_line("{0}: ReadRawData_and_FillList: {1} unhandled null entry", typeof(ReadWriteClass).Name, options.File);
                                }
                            }
                            else
                            {
                                if ((columnEntries[colIndex] != "") && ((columnEntries[colIndex] != "NA") || (propInfo[propIndex].PropertyType == typeof(string))))
                                {
                                    propInfo[propIndex].SetValue(newLine, Convert.ChangeType(columnEntries[colIndex], propInfo[propIndex].PropertyType), null);
                                }
                            }
                        }
                        dictionary.Add(new_dictionary_key, newLine);
                        safedLines = safedLines + 1;
                    }
                    readLines = readLines + 1;
                    if ((options.Report == ReadWrite_report_enum.Report_everything) && (readLines % 2000000 == 0))
                    {
                        Report_class.WriteLine("{0}: Read lines: {1} Mio, \tSafed lines: {2} Mio", typeof(T_line).Name, (double)readLines / 1000000, (double)safedLines / 1000000);
                    }
                }
            }
            if (report_check_lineDelimiters)
            {
                Report_class.Write_error_line("{0}: only one column entry: Check lineDelimiters", typeof(ReadWriteClass).Name);
            }
            timer.Stop();
            if (options.Report == ReadWrite_report_enum.Report_everything)
            {
                Report_class.WriteLine("{0}: Read lines: {1} Mio, Safed lines: {2} Mio", typeof(T_line).Name, (double)readLines / 1000000, (double)safedLines / 1000000);
                Report_class.WriteLine("{0}: Time: {1}", typeof(T_line).Name, timer.Elapsed);
            }
            if (options.Report >= ReadWrite_report_enum.Report_main)
            {
                Report_class.WriteLine();
            }
            stream.Close();
            if (dictionary.Count == 0)
            {
                Report_class.Write_error_line("{0}: ReadFile {1} data.count == 0, no lines filled!", typeof(ReadWriteClass).Name, options.File);
            }
            return(dictionary);
        }