Example #1
0
        private bool LoadCache(string filePath)
        {
            // Disable caching here
            if (File.Exists(filePath) == false)
            {
                return(false);
            }
            try
            {
                using (var file = File.OpenRead(filePath))
                {
                    data = Serializer.Deserialize <PrenomsData>(file);
                }

                return(true);
            }
            catch (Exception) { }
            return(false);
        }
Example #2
0
        private void LoadData(string prenomsFile)
        {
            // Load file
            if (File.Exists(prenomsFile) == false)
            {
                throw new ArgumentException("Configuration file not found! " + prenomsFile);
            }

            data = new PrenomsData();
            data.MinYearGlobal = 2999;
            data.MaxYearGlobal = -1;

            // Extract data line by line
            int    n = 0;
            string line;

            using (System.IO.StreamReader file = new System.IO.StreamReader(prenomsFile))
            {
                while ((line = file.ReadLine()) != null)
                {
                    // Ignore first 4 lines
                    if (n >= 4)
                    {
                        // Parse
                        var p = Parse(line);

                        if (p != null)
                        {
                            var collection = p.Sex == Prenom.BOY ? data.Boys : data.Girls;

                            if (collection.ContainsKey(p.Value) == false)
                            {
                                for (int i = 1900; i < DateTime.Now.Year; i++)
                                {
                                    if (p.Counts.ContainsKey(i) == false)
                                    {
                                        p.Counts.Add(i, 0);
                                    }
                                }

                                collection.Add(p.Value, p);
                            }
                            else
                            {
                                var pExist = collection[p.Value];

                                // Merge stats
                                pExist.TotalCount += p.TotalCount;

                                foreach (var y in p.Counts.Keys)
                                {
                                    if (pExist.Counts.ContainsKey(y) == false)
                                    {
                                        pExist.Counts[y] = 0;
                                    }
                                    pExist.Counts[y] += p.Counts[y];
                                }

                                collection[p.Value] = pExist;
                            }

                            var first = p.Counts.Where(a => a.Value > 0).FirstOrDefault();
                            var last  = p.Counts.Where(a => a.Value > 0).LastOrDefault();

                            p.MinYear = first.Key;
                            p.MaxYear = last.Key;

                            if (p.MinYear > 0)
                            {
                                data.MinYearGlobal = Math.Min(MinYearGlobal, p.MinYear);
                            }
                            if (p.MaxYear > 0)
                            {
                                data.MaxYearGlobal = Math.Max(MaxYearGlobal, p.MaxYear);
                            }
                        }
                    }

                    n++;
                }
            }

            // Sort by key
            foreach (var g in data.Boys.Values)
            {
                g.Counts = g.Counts.OrderBy(key => key.Key).ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
            }
            foreach (var f in data.Girls.Values)
            {
                f.Counts = f.Counts.OrderBy(key => key.Key).ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
            }
        }