Exemple #1
0
        /// <summary>
        /// Import new entries from file as a single bulk change.
        /// </summary>
        public void BulkAdd(string dictPath, string workingFolder)
        {
            CedictParser parser = new CedictParser();

            Startup.InitDB(config, null, false);
            SqlDict dict    = new SqlDict(null, mut);
            int     lineNum = 0;

            DateTime dt    = DateTime.Now;
            string   fnLog = "importlog-" + dt.Year + "-" + dt.Month.ToString("00") + "-" + dt.Day.ToString("00") + "!" + dt.Hour.ToString("00") + "-" + dt.Minute.ToString("00") + "-" + dt.Second.ToString("00") + ".txt";

            fnLog = Path.Combine(workingFolder, fnLog);
            using (FileStream fs = new FileStream(dictPath, FileMode.Open, FileAccess.Read))
                using (StreamReader sr = new StreamReader(fs))
                    using (FileStream fsLog = new FileStream(fnLog, FileMode.Create, FileAccess.ReadWrite))
                        using (StreamWriter swLog = new StreamWriter(fsLog))
                        {
                            // First two lines are commented and have metainfo
                            // First line: user name
                            // Second line: bulk change's comment
                            string user = sr.ReadLine().Substring(1).Trim();
                            string note = sr.ReadLine().Substring(1).Trim();
                            lineNum = 2;
                            using (SqlDict.ImportBuilder builder = dict.GetBulkBuilder(user, note))
                            {
                                string line;
                                while ((line = sr.ReadLine()) != null)
                                {
                                    ++lineNum;
                                    if (line == "" || line.StartsWith("#"))
                                    {
                                        continue;
                                    }
                                    CedictEntry entry = parser.ParseEntry(line, lineNum, swLog);
                                    if (entry == null)
                                    {
                                        swLog.WriteLine(line);
                                        continue;
                                    }
                                    entry.Status = EntryStatus.Approved;
                                    bool ok = builder.AddNewEntry(entry);
                                    if (!ok)
                                    {
                                        swLog.WriteLine("Line " + lineNum + ": Entry rejected by importer.");
                                        swLog.WriteLine(line);
                                        continue;
                                    }
                                }
                                builder.CommitRest();
                            }
                        }
        }
Exemple #2
0
        /// <summary>
        /// Import dictionary data including version history.
        /// Only used to initialize from scratch.
        /// </summary>
        public void ImportDict(string dictPath, string workingFolder)
        {
            Startup.InitDB(config, null, false);
            SqlDict             dict = new SqlDict(null, mut);
            List <EntryVersion> vers = new List <EntryVersion>();

            using (FileStream fs = new FileStream(dictPath, FileMode.Open, FileAccess.Read))
                using (StreamReader sr = new StreamReader(fs))
                {
                    EntryBlockParser ebp = new EntryBlockParser(sr);
                    // Two passes. In the first we collect user names and bulk changes.
                    var users = new HashSet <string>();
                    var bulks = new Dictionary <int, SqlDict.ImportBuilder.BulkChangeInfo>();
                    while (true)
                    {
                        vers.Clear();
                        int id = ebp.ReadBlock(vers);
                        if (id == -1)
                        {
                            break;
                        }
                        for (int i = 0; i != vers.Count; ++i)
                        {
                            var ver = vers[i];
                            users.Add(ver.User);
                            // First change referencing this bulk
                            if (ver.BulkRef != -1 && !bulks.ContainsKey(ver.BulkRef))
                            {
                                SqlDict.ImportBuilder.BulkChangeInfo bci = new SqlDict.ImportBuilder.BulkChangeInfo
                                {
                                    Timestamp      = ver.Timestamp,
                                    UserName       = ver.User,
                                    Comment        = ver.Comment,
                                    NewEntries     = i == 0 ? 1 : 0,
                                    ChangedEntries = i != 0 ? 1 : 0,
                                };
                                bulks[ver.BulkRef] = bci;
                            }
                            // Bulk, and seen before
                            else if (ver.BulkRef != -1)
                            {
                                if (i == 0)
                                {
                                    ++bulks[ver.BulkRef].NewEntries;
                                }
                                else
                                {
                                    ++bulks[ver.BulkRef].ChangedEntries;
                                }
                            }
                        }
                    }
                    // Enrich known built-in user names with "about"
                    HashSet <string> richUsers = new HashSet <string>();
                    foreach (string x in users)
                    {
                        if (x == "HanDeDict")
                        {
                            richUsers.Add(x + "\t" + "Platzhalter für das ursprüngliche HanDeDict-Team");
                        }
                        if (x == "zydeo-robot")
                        {
                            richUsers.Add(x + "\t" + "Platzhalter für automatische Datenverarbeitung");
                        }
                        else
                        {
                            richUsers.Add(x + "\t");
                        }
                    }
                    // Second pass. Actual import.
                    fs.Position = 0;
                    using (SqlDict.ImportBuilder builder = dict.GetBulkBuilder(richUsers, bulks))
                    {
                        while (true)
                        {
                            vers.Clear();
                            int id = ebp.ReadBlock(vers);
                            if (id == -1)
                            {
                                break;
                            }
                            builder.AddEntry(id, vers);
                        }
                        builder.CommitRest();
                    }
                }
        }