Beispiel #1
0
        /// <summary>
        /// Writes an entry and its annotations to a TextWriter.
        /// </summary>
        /// <param name="type">Type of entry (usually 'b' or 'f')</param>
        /// <param name="entry">Entry instance to be written</param>
        void WriteEntry(LineWriter writer, string type, Entry entry)
        {
            entry.FullyLoad();

            // Reset the tag so that writing the cache can work.
            entry.Tag = new EntryTag(entry.Tag.DictionarySection, writer.Position);

            writer.Write(type);
            writer.WriteLine(Escape(entry.Phrase, false));

            foreach (Translation tr in entry.Translations)
            {
                writer.Write("t ");
                writer.WriteLine(Escape(tr.Value, false));

                //if (tr.PartOfSpeech != null) {
                //    writer.Write("ps ");
                //    writer.WriteLine(Escape(tr.PartOfSpeech, true));
                //}
            }
        }
Beispiel #2
0
        /// <summary>
        /// Writes an entry and its annotations to a TextWriter.
        /// </summary>
        /// <param name="type">Type of entry (usually 'b' or 'f')</param>
        /// <param name="entry">Entry instance to be written</param>
        void WriteEntry(LineWriter writer, string type, Entry entry)
        {
            entry.FullyLoad();

            // Reset the tag so that writing the cache can work.
            entry.Tag = new EntryTag(entry.Tag.DictionarySection, writer.Position);

            writer.Write(type);
            writer.WriteLine(Escape(entry.Phrase, false));

            foreach (Translation tr in entry.Translations) {
                writer.Write("t ");
                writer.WriteLine(Escape(tr.Value, false));

                //if (tr.PartOfSpeech != null) {
                //    writer.Write("ps ");
                //    writer.WriteLine(Escape(tr.PartOfSpeech, true));
                //}
            }
        }
Beispiel #3
0
        /// <summary>
        /// Writes all entries to a file. There's no way to tell if the dictionary entries have been modified,
        /// as of yet, so currently we must write the file whether it's necessary or not.
        /// </summary>
        /// <param name="path">The file name (relative or absolute) to write to.</param>
        protected void Write(string path)
        {
            // Update the revision ID. Instead of incrementing a number, pick a random GUID and use that.
            // If incrementing were used, then if a dictionary were copied to a different computer, modified,
            // and copied back to the original computer, where the dictionary had also been modified, the
            // cache revision ID on the original computer would still match that of the dictionary file, despite
            // being invalid.
            revisionID = Guid.NewGuid().ToString();

            // If we're not writing cautiously, we can't read the entries on-demand while the temporary output file
            // is written.
            if (path == Path)
                CloseFile();

            PreloadPartialEntries();

            Metrics.Measure(string.Format("Writing dictionary {0} to file", this.Name), delegate {
                // Now dispose of the reader so that we can unlock the file.
                // The file needs to be truncated anyway, so it's not possible to share the file stream
                // with the Line Reader.
                if (reader != null) {
                    reader.Dispose();
                    reader = null;
                }

                using (var stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None)) {
                    using (var writer = new LineWriter(stream)) {
                        writer.WriteLine(magicNumber);

                        if (Name != null)
                            writer.WriteLine("name " + Uri.EscapeDataString(Name));
                        if (Author != null)
                            writer.WriteLine("author " + Uri.EscapeDataString(Author));
                        if (Url != null)
                            writer.WriteLine("url " + Uri.EscapeDataString(Url));
                        writer.WriteLine("revision-id " + Uri.EscapeDataString(revisionID));

                        // TODO: WritePairedProperty
                        writer.WriteLine(string.Format("headwords-hint {0} {1}", forwards.HeadWords, backwards.HeadWords));
                        if (FirstLanguage != null || SecondLanguage != null)
                            writer.WriteLine(string.Format("languages {0} {1}", Uri.EscapeDataString(FirstLanguage), Uri.EscapeDataString(SecondLanguage)));
                        if (FirstLanguageCode != null || SecondLanguageCode != null)
                            writer.WriteLine(string.Format("language-codes {0} {1}", Uri.EscapeDataString(FirstLanguageCode), Uri.EscapeDataString(SecondLanguageCode)));
                        writer.WriteLine("sorted");

                        foreach (Entry entry in ForwardsSection)
                            WriteEntry(writer, "f ", entry);

                        foreach (Entry entry in ReverseSection)
                            WriteEntry(writer, "b ", entry);
                    }
                }
            });
        }
Beispiel #4
0
        /// <summary>
        /// Writes all entries to a file. There's no way to tell if the dictionary entries have been modified,
        /// as of yet, so currently we must write the file whether it's necessary or not.
        /// </summary>
        /// <param name="path">The file name (relative or absolute) to write to.</param>
        protected void Write(string path)
        {
            // Update the revision ID. Instead of incrementing a number, pick a random GUID and use that.
            // If incrementing were used, then if a dictionary were copied to a different computer, modified,
            // and copied back to the original computer, where the dictionary had also been modified, the
            // cache revision ID on the original computer would still match that of the dictionary file, despite
            // being invalid.
            revisionID = Guid.NewGuid().ToString();

            // If we're not writing cautiously, we can't read the entries on-demand while the temporary output file
            // is written.
            if (path == Path)
            {
                CloseFile();
            }

            PreloadPartialEntries();

            Metrics.Measure(string.Format("Writing dictionary {0} to file", this.Name), delegate {
                // Now dispose of the reader so that we can unlock the file.
                // The file needs to be truncated anyway, so it's not possible to share the file stream
                // with the Line Reader.
                if (reader != null)
                {
                    reader.Dispose();
                    reader = null;
                }

                using (var stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None)) {
                    using (var writer = new LineWriter(stream)) {
                        writer.WriteLine(magicNumber);

                        if (Name != null)
                        {
                            writer.WriteLine("name " + Uri.EscapeDataString(Name));
                        }
                        if (Author != null)
                        {
                            writer.WriteLine("author " + Uri.EscapeDataString(Author));
                        }
                        if (Url != null)
                        {
                            writer.WriteLine("url " + Uri.EscapeDataString(Url));
                        }
                        writer.WriteLine("revision-id " + Uri.EscapeDataString(revisionID));

                        // TODO: WritePairedProperty
                        writer.WriteLine(string.Format("headwords-hint {0} {1}", forwards.HeadWords, backwards.HeadWords));
                        if (FirstLanguage != null || SecondLanguage != null)
                        {
                            writer.WriteLine(string.Format("languages {0} {1}", Uri.EscapeDataString(FirstLanguage), Uri.EscapeDataString(SecondLanguage)));
                        }
                        if (FirstLanguageCode != null || SecondLanguageCode != null)
                        {
                            writer.WriteLine(string.Format("language-codes {0} {1}", Uri.EscapeDataString(FirstLanguageCode), Uri.EscapeDataString(SecondLanguageCode)));
                        }
                        writer.WriteLine("sorted");

                        foreach (Entry entry in ForwardsSection)
                        {
                            WriteEntry(writer, "f ", entry);
                        }

                        foreach (Entry entry in ReverseSection)
                        {
                            WriteEntry(writer, "b ", entry);
                        }
                    }
                }
            });
        }