示例#1
0
        static void Main(string[] args)
        {
            var dictIn = new Dictionary<int, string>();
            dictIn.Add(1, "One");
            dictIn.Add(2, "Two");
            dictIn.Add(3, "Three");
            dictIn.Add(4, "Four");

            Console.WriteLine("Writing data...");
            using (EasyWriter writer = new EasyWriter("example.dat"))
            {
                writer.Write(dictIn);
            }

            Console.WriteLine("Reading data...");
            using (EasyReader reader = new EasyReader("example.dat"))
            {
                var dictOut = reader.ReadDictionary<int, string>();
                foreach(var pair in dictOut)
                {
                    Console.WriteLine(pair);
                }
            }

            Console.ReadKey();
        }
示例#2
0
 public override void Serialize(EasyWriter output)
 {
     output.Write(FILTER_REGEX);
     output.Write(Outcome);
     output.Write((int)Regex.Options);
     output.Write(Regex.ToString());
 }
示例#3
0
        protected override IEnumerator <RST> Serialize(EasyWriter output)
        {
            output.Write(_query.Name);
            output.Write(_query.Subtype);
            output.Write(_query.Exclusive);
            output.Write(_query.FilterCount);
            foreach (var filter in _query.GetAllFilters())
            {
                filter.Serialize(output);
            }

            // Carrier
            if (_query.Carrier != null)
            {
                output.Write(true);
                _query.Carrier.Serialize(output);
            }
            else
            {
                output.Write(false);
            }

            // Complement
            yield return(_query.Complement);
        }
示例#4
0
文件: RST.cs 项目: thygrrr/rant3
        public static void SerializeRST(RST rst, EasyWriter output)
        {
            var stack = new Stack <IEnumerator <RST> >(10);

            stack.Push(rst.SerializeObject(output));
top:
            while (stack.Count > 0)
            {
                var serializer = stack.Peek();

                while (serializer.MoveNext())
                {
                    if (serializer.Current == null)
                    {
                        output.Write(NullRST);
                    }
                    else
                    {
                        stack.Push(serializer.Current.SerializeObject(output));
                        goto top;
                    }
                }

                stack.Pop();
            }
        }
示例#5
0
 /// <summary>
 /// Writes this document using the specified EasyWriter.
 /// </summary>
 /// <param name="writer">The writer that will be used to write this document.</param>
 internal void Write(EasyWriter writer)
 {
     _stringTableIndex = 0;
     _stringTable.Clear();
     WriteItem(writer, Top, null, true);
     writer.Close();
 }
示例#6
0
文件: RST.cs 项目: thygrrr/rant3
 private IEnumerator <RST> SerializeObject(EasyWriter output)
 {
     output.Write(_rstIDMap[GetType()]);
     output.Write(Location.Line);
     output.Write(Location.Column);
     output.Write(Location.Index);
     return(Serialize(output));
 }
示例#7
0
 protected override IEnumerator <RST> Serialize(EasyWriter output)
 {
     output.Write(_codeHigh);
     output.Write(_codeLow);
     output.Write(_times);
     output.Write(_unicode);
     yield break;
 }
示例#8
0
 protected override IEnumerator <RST> Serialize(EasyWriter output)
 {
     output.Write(Actions.Count);
     foreach (var action in Actions)
     {
         yield return(action);
     }
 }
示例#9
0
        protected override IEnumerator <RST> Serialize(EasyWriter output)
        {
            output.Write((_regex.Options & RegexOptions.IgnoreCase) > 0); // Ignore case?
            output.Write(_regex.ToString());
            yield return(_rstSource);

            yield return(_rstMatchEval);
        }
示例#10
0
 protected override IEnumerator <RST> Serialize(EasyWriter output)
 {
     output.Write(_argc);
     output.Write(_funcInfo.Name);
     foreach (var arg in _args)
     {
         yield return(arg);
     }
 }
示例#11
0
        /// <summary>
        /// Saves the package using the old Rant package format to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        public void SaveOld(string path)
        {
            if (String.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                // Magic
                writer.WriteBytes(Encoding.ASCII.GetBytes(OLD_MAGIC));

                // Counts
                writer.Write(_patterns?.Count ?? 0);
                writer.Write(_tables?.Count ?? 0);

                // Patterns
                if (_patterns != null)
                {
                    foreach (var pattern in _patterns)
                    {
                        writer.Write(pattern.Name);
                        writer.Write(pattern.Code);
                    }
                }

                // Tables
                if (_tables != null)
                {
                    foreach (var table in _tables)
                    {
                        writer
                        .Write(table.Name)
                        .Write(table.Subtypes)
                        .Write(table.EntryCount)
                        .Write(table.HiddenClasses.ToArray());

                        foreach (var entry in table.GetEntries())
                        {
                            writer
                            .Write(entry.Weight)
                            .Write(false)     // Used to be the NSFW field, will use for something else in the future!
                            .Write(entry.Terms.Length);

                            for (int i = 0; i < entry.Terms.Length; i++)
                            {
                                writer
                                .Write(entry.Terms[i].Value)
                                .Write(entry.Terms[i].Pronunciation);
                            }

                            writer.Write(entry.GetClasses().ToArray());
                        }
                    }
                }
            }
        }
示例#12
0
        /// <summary>
        /// Saves the package to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        public void Save(string path)
        {
            if (String.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                // Magic
                writer.WriteBytes(Encoding.ASCII.GetBytes(Magic));

                // Counts
                writer.Write(_patterns?.Count ?? 0);
                writer.Write(_tables?.Count ?? 0);

                // Patterns
                if (_patterns != null)
                {
                    foreach (var pattern in _patterns)
                    {
                        writer.Write(pattern.Name);
                        writer.Write(pattern.Code);
                    }
                }

                // Tables
                if (_tables != null)
                {
                    foreach (var table in _tables)
                    {
                        writer
                        .Write(table.Name)
                        .Write(table.Subtypes)
                        .Write(table.EntryCount)
                        .Write(table.HiddenClasses.ToArray());

                        foreach (var entry in table.GetEntries())
                        {
                            writer
                                .Write(entry.Weight)
                                .Write(false) // Used to be the NSFW field, will use for something else in the future!
                                .Write(entry.Terms.Length);

                            for (int i = 0; i < entry.Terms.Length; i++)
                            {
                                writer
                                    .Write(entry.Terms[i].Value)
                                    .Write(entry.Terms[i].Pronunciation);
                            }

                            writer.Write(entry.GetClasses().ToArray());
                        }
                    }
                }
            }
        }
示例#13
0
        /// <summary>
        /// Writes this BSON document to the specified path.
        /// </summary>
        /// <param name="path">The path to write to.</param>
        public void Write(string path, bool includeStringTable = false)
        {
            var stream = File.Open(path, FileMode.Create);
            var writer = new EasyWriter(stream);

            Write(writer);
            writer.Close();
            stream.Close();
        }
示例#14
0
        internal static void SerializeResource(RantResource resource, EasyWriter writer)
        {
            if (!_resourceIdRegistry.TryGetValue(resource.GetType(), out string rstr))
            {
                throw new ArgumentException($"Resource type '{resource.GetType()}' is not registered.");
            }

            writer.WriteBytes(Encoding.ASCII.GetBytes(rstr));
            resource.SerializeData(writer);
        }
示例#15
0
        /// <summary>
        /// Saves the package to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        /// <param name="compress">Specifies whether to compress the package contents.</param>
        public void Save(
            string path,
            bool compress = true)
        {
            if (string.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += EXTENSION;
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                writer.Write(Encoding.ASCII.GetBytes(MAGIC));
                writer.Write(PACKAGE_FORMAT_VERSION);
                writer.Write(compress);
                writer.Write(_title);
                writer.Write(_id);
                writer.Write(Description);
                writer.Write(Tags);
                writer.Write(Authors);
                writer.Write(Version.Major);
                writer.Write(Version.Minor);
                writer.Write(Version.Revision);
                writer.Write(_dependencies.Count);
                foreach (var dep in _dependencies)
                {
                    writer.Write(dep.ID);
                    writer.Write(dep.Version.Major);
                    writer.Write(dep.Version.Minor);
                    writer.Write(dep.Version.Revision);
                    writer.Write(dep.AllowNewer);
                }
                writer.Write(_resources.Count);

                if (compress)
                {
                    using (var compressStream = new DeflateStream(writer.BaseStream, CompressionMode.Compress, true))
                    {
                        foreach (var res in _resources)
                        {
                            RantResource.SerializeResource(res, new EasyWriter(compressStream, leaveOpen: true));
                        }
                        compressStream.Flush();
                    }
                }
                else
                {
                    foreach (var res in _resources)
                    {
                        RantResource.SerializeResource(res, writer);
                    }
                }
                writer.BaseStream.Flush();
            }
        }
示例#16
0
 internal void WriteStringTable(EasyWriter writer, bool include)
 {
     writer.Write(include);
     if (include)
     {
         writer.Write((byte)_stringTableMode);
         writer.Write((byte)STRING_TABLE_VERSION);
         var stringTableBytes = GenerateStringTable();
         writer.Write(stringTableBytes.Length);
         writer.Write(stringTableBytes);
     }
 }
示例#17
0
 internal void Serialize(EasyWriter output)
 {
     output.Write(_components.Count);
     foreach (var kv in _components)
     {
         output.Write((byte)kv.Key);
         output.Write(kv.Value.Count);
         foreach (string compName in kv.Value)
         {
             output.Write(compName);
         }
     }
 }
示例#18
0
 /// <summary>
 /// Saves the compiled program to the specified stream.
 /// </summary>
 /// <param name="stream">The stream to save the program to.</param>
 public void SaveToStream(Stream stream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     using (var output = new EasyWriter(stream, Endian.Little, true))
     {
         output.WriteBytes(Encoding.ASCII.GetBytes(Magic));
         RST.SerializeRST(SyntaxTree, output);
     }
     stream.Flush();
 }
示例#19
0
 public override void Serialize(EasyWriter output)
 {
     output.Write(FILTER_CLASS);
     output.Write(_items.Count);
     foreach (var filter in _items)
     {
         output.Write(filter.Length);
         foreach (var rule in filter)
         {
             output.Write(rule.ShouldMatch);
             output.Write(rule.Class);
         }
     }
 }
示例#20
0
 public byte[] GenerateStringTable()
 {
     var stream = new MemoryStream();
     var writer = new EasyWriter(stream);
     writer.Write(_stringTable.Count);
     foreach(string key in _stringTable.Keys)
     {
         writer.Write(_stringTable[key]);
         writer.Write(key, Encoding.UTF8);
     }
     writer.Close();
     stream.Close();
     return stream.ToArray();
 }
示例#21
0
        public byte[] GenerateStringTable()
        {
            var stream = new MemoryStream();
            var writer = new EasyWriter(stream);

            writer.Write(_stringTable.Count);
            foreach (string key in _stringTable.Keys)
            {
                writer.Write(_stringTable[key]);
                writer.Write(key, Encoding.UTF8);
            }
            writer.Close();
            stream.Close();
            return(stream.ToArray());
        }
示例#22
0
        /// <summary>
        /// Writes this document as BSON to the specified stream.
        /// </summary>
        /// <param name="stream">The stream that will be written to.</param>
        public void Write(Stream stream, bool includeStringTable = false)
        {
            var memoryStream = new MemoryStream();

            using (var writer = new EasyWriter(memoryStream))
            {
                Write(writer);
            }
            memoryStream.Close();
            var bytes       = memoryStream.ToArray();
            var tableWriter = new EasyWriter(stream);

            WriteStringTable(tableWriter, includeStringTable);
            stream.Write(bytes, 0, bytes.Length);
        }
示例#23
0
        protected override IEnumerator <RST> Serialize(EasyWriter output)
        {
            var iterMain = base.Serialize(output);

            while (iterMain.MoveNext())
            {
                yield return(iterMain.Current);
            }
            output.Write(Parameters.Count);
            foreach (var subParam in Parameters)
            {
                output.Write(subParam.Name);
                output.Write((byte)subParam.Type);
            }
        }
示例#24
0
        protected override IEnumerator <RST> Serialize(EasyWriter output)
        {
            var iterMain = base.Serialize(output);

            while (iterMain.MoveNext())
            {
                yield return(iterMain.Current);
            }
            output.Write(_inModule);
            output.Write(_moduleFunctionName);
            output.Write(Arguments.Count);
            foreach (var arg in Arguments)
            {
                yield return(arg);
            }
        }
示例#25
0
        internal override void SerializeData(EasyWriter writer)
        {
            writer.Write(Name);
            writer.Write(Language);
            writer.Write(TermsPerEntry);
            for (int i = 0; i < TermsPerEntry; i++)
            {
                writer.Write(GetSubtypesForIndex(i).ToArray());
            }
            writer.Write(_hidden.ToArray());

            writer.Write(_entriesList.Count);
            for (int i = 0; i < _entriesList.Count; i++)
            {
                var entry = _entriesList[i];
                for (int j = 0; j < TermsPerEntry; j++)
                {
                    var term = entry[j];
                    writer.Write(term.Value);
                    writer.Write(term.Pronunciation);
                    writer.Write(term.ValueSplitIndex);
                    writer.Write(term.PronunciationSplitIndex);
                }
                writer.Write(entry.Weight);
                writer.Write(entry.GetRequiredClasses().ToArray());
                writer.Write(entry.GetOptionalClasses().ToArray());
                var metaKeys = entry.GetMetadataKeys().ToArray();
                writer.Write(metaKeys.Length);
                for (int j = 0; j < metaKeys.Length; j++)
                {
                    var metaObj   = entry.GetMetadata(metaKeys[j]);
                    var metaArray = metaObj as IEnumerable;
                    writer.Write(metaArray != null);
                    writer.Write(metaKeys[j]);
                    if (metaArray != null)
                    {
                        writer.Write(metaArray.OfType <object>().Select(m => m.ToString()).ToArray());
                    }
                    else
                    {
                        writer.Write(metaObj.ToString());
                    }
                }
            }
        }
示例#26
0
        protected override IEnumerator <RST> Serialize(EasyWriter output)
        {
            output.Write(_count);
            output.Write(_weighted);
            if (_weighted)
            {
                // Write constant weights
                if (_weights != null)
                {
                    output.Write(true);
                    output.Write(_constantWeightSum);
                    for (int i = 0; i < _count; i++)
                    {
                        output.Write(_weights[i]);
                    }
                }
                else
                {
                    output.Write(false);
                }

                // Write dynamic weights
                if (_dynamicWeights != null)
                {
                    output.Write(_dynamicWeights.Count);
                    foreach (var dw in _dynamicWeights)
                    {
                        output.Write(dw.Item1);
                        yield return(dw.Item2);
                    }
                }
                else
                {
                    output.Write(0);
                }
            }

            // Block elements
            foreach (var e in _elements)
            {
                yield return(e);
            }
        }
示例#27
0
        public override void Serialize(EasyWriter output)
        {
            output.Write(FILTER_SYLRANGE);
            if (_min == null)
            {
                output.Write(false);
            }
            else
            {
                output.Write(true);
                output.Write(_min.Value);
            }

            if (_max == null)
            {
                output.Write(false);
            }
            else
            {
                output.Write(true);
                output.Write(_max.Value);
            }
        }
示例#28
0
        /// <summary>
        /// Saves the package to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        public void Save(
            string path,
            bool compress = true,
            BsonStringTableMode stringTableMode = BsonStringTableMode.None)
        {
            if (String.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                var doc  = new BsonDocument(stringTableMode);
                var info = doc.Top["info"] = new BsonItem();
                info["title"]        = new BsonItem(_title);
                info["id"]           = new BsonItem(_id);
                info["description"]  = new BsonItem(Description);
                info["tags"]         = new BsonItem(Tags);
                info["version"]      = new BsonItem(Version.ToString());
                info["authors"]      = new BsonItem(Authors);
                info["dependencies"] = new BsonItem(_dependencies.Select(dep =>
                {
                    var depObj            = new BsonItem();
                    depObj["id"]          = dep.ID;
                    depObj["version"]     = dep.Version.ToString();
                    depObj["allow-newer"] = dep.AllowNewer;
                    return(depObj);
                }).ToArray());

                var patterns = doc.Top["patterns"] = new BsonItem();
                if (_patterns != null)
                {
                    foreach (var pattern in _patterns)
                    {
                        patterns[pattern.Name] = new BsonItem(pattern.Code);
                    }
                }

                var tables = doc.Top["tables"] = new BsonItem();
                if (_tables != null)
                {
                    foreach (var table in _tables)
                    {
                        var t = tables[table.Name] = new BsonItem();
                        t["name"]     = new BsonItem(table.Name);
                        t["subs"]     = new BsonItem(table.Subtypes);
                        t["language"] = new BsonItem(table.Language);
                        t["hidden"]   = new BsonItem(table.HiddenClasses.ToArray());
                        t["hints"]    = new BsonItem(0);
                        var entries = new List <BsonItem>();
                        foreach (var entry in table.GetEntries())
                        {
                            var e = new BsonItem();
                            if (entry.Weight != 1)
                            {
                                e["weight"] = new BsonItem(entry.Weight);
                            }
                            var requiredClasses = entry.GetRequiredClasses().ToArray();
                            if (requiredClasses.Length > 0)
                            {
                                e["classes"] = new BsonItem(requiredClasses);
                            }
                            var optionalClasses = entry.GetOptionalClasses().ToArray();
                            if (optionalClasses.Length > 0)
                            {
                                e["optional_classes"] = new BsonItem();
                            }
                            var terms = new List <BsonItem>();
                            foreach (var term in entry.Terms)
                            {
                                var et = new BsonItem();
                                et["value"] = new BsonItem(term.Value);
                                if (term.Pronunciation != "")
                                {
                                    et["pron"] = new BsonItem(term.Pronunciation);
                                }
                                terms.Add(et);
                            }
                            e["terms"] = new BsonItem(terms.ToArray());
                            entries.Add(e);
                        }
                        t["entries"] = new BsonItem(entries.ToArray());
                    }
                }

                var data = doc.ToByteArray(stringTableMode != BsonStringTableMode.None);
                if (compress)
                {
                    data = EasyCompressor.Compress(data);
                }
                writer.Write(Encoding.ASCII.GetBytes("RANT"));
                writer.Write((uint)2);
                writer.Write(compress);
                writer.Write(data.Length);
                writer.Write(data);
            }
        }
示例#29
0
        static void EasyWriterTest(string FileName)
        {
            var styles = new OpenXmlExStyles(
                new List <BaseOpenXmlExStyle>()
            {
                new BaseOpenXmlExStyle()
                {
                    FontColor = System.Drawing.Color.Crimson, IsBoldFont = true
                },
                new BaseOpenXmlExStyle()
                {
                    FontSize = 20, FontName = "Calibri", BorderColor = System.Drawing.Color.Red
                }
            });

            using var writer = new EasyWriter(FileName, styles);



            #region 1 лист
            var sheet_name_1 = "Test_sheet_name";
            writer.AddNewSheet(sheet_name_1);

            #region Надстройка страницы - кнопки группировки сверху

            writer.SetGrouping(false, false);

            #endregion

            #region Установка ширины колонок

            //Установка размеров колонок
            var width_setting = new List <WidthOpenXmlEx>
            {
                new (1, 2, 7),
                new (3, 3, 11),
                new (4, 12, 9.5),
                new (13, 13, 17),
                new (14, 14, 40),
                new (15, 16, 15),
                new (18, 20, 15)
            };
            writer.SetWidth(width_setting);

            #endregion

            var(key, value) = writer.FindStyleOrDefault(
                new BaseOpenXmlExStyle()
            {
                FontColor        = System.Drawing.Color.Crimson,
                FontSize         = 20,
                IsBoldFont       = true,
                LeftBorderStyle  = BorderStyleValues.Dashed,
                RightBorderStyle = BorderStyleValues.Dashed
            });

            #region SheetData

            writer.AddRow(3, 0, true, true);

            writer.AddCell("Test", 1, 3, 0);
            writer.AddCell("Test", 7, 3, 0);
            writer.AddRow(4, 0, true, true);
            writer.AddCell("Test", 4, 4, 1);
            writer.AddCell("Test", 5, 4, 2);
            writer.AddCell("Test", 6, 4, 3);

            writer.AddCell("Test", 7, 4, 3);

            #endregion

            #region Secondary setting

            writer.MergeCells(6, 3, 10, 5);
            writer.SetFilter(1, 5, 3, 5);


            #endregion


            #endregion

            #region 2 лист

            var sheet_name_2 = "Sheet 2.0";
            writer.AddNewSheet(sheet_name_2);

            #region Надстройка страницы - кнопки группировки сверху

            writer.SetGrouping(false, false);

            #endregion

            #region Установка ширины колонок

            writer.SetWidth(width_setting);

            #endregion

            #region SheetData

            writer.AddRow(3, 0, true, true);

            writer.AddCell("Test 2", 1, 3, 0);
            writer.AddCell("Test 2", 7, 3, 0);
            writer.AddRow(4, 0, true, true);
            writer.AddCell("Test 2", 4, 4, 1);
            writer.AddCell("Test 2", 5, 4, 2);
            writer.AddCell("Test 2", 6, 4, 3);

            writer.AddCell("Test 2", 7, 4, 3);
            writer.MergeCells(6, 3, 10, 5);

            #endregion

            #region Secondary setting

            writer.SetFilter(1, 5, 3, 5);


            #endregion


            #endregion
        }
示例#30
0
 protected override IEnumerator <RST> Serialize(EasyWriter output)
 {
     output.Write(Name);
     yield return(Body);
 }
示例#31
0
        /// <summary>
        /// Saves the package to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        public void Save(
            string path, 
            bool compress = true, 
            BsonStringTableMode stringTableMode = BsonStringTableMode.None)
        {
            if (String.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                var doc = new BsonDocument(stringTableMode);
                var info = doc.Top["info"] = new BsonItem();
                info["title"] = new BsonItem(_title);
                info["id"] = new BsonItem(_id);
                info["description"] = new BsonItem(Description);
                info["tags"] = new BsonItem(Tags);
                info["version"] = new BsonItem(Version.ToString());
                info["authors"] = new BsonItem(Authors);
				info["dependencies"] = new BsonItem(_dependencies.Select(dep =>
				{
					var depObj = new BsonItem();
					depObj["id"] = dep.ID;
					depObj["version"] = dep.Version.ToString();
					depObj["allow-newer"] = dep.AllowNewer;
					return depObj;
				}).ToArray());
                
                var patterns = doc.Top["patterns"] = new BsonItem();
                if(_patterns != null)
                    foreach(var pattern in _patterns)
                        patterns[pattern.Name] = new BsonItem(pattern.Code);

                var tables = doc.Top["tables"] = new BsonItem();
	            if (_tables != null)
	            {
					foreach (var table in _tables)
					{
						var t = tables[table.Name] = new BsonItem();
						t["name"] = new BsonItem(table.Name);
						t["subs"] = new BsonItem(table.Subtypes);
						t["language"] = new BsonItem(table.Language);
						t["hidden"] = new BsonItem(table.HiddenClasses.ToArray());
						t["hints"] = new BsonItem(0);
						var entries = new List<BsonItem>();
						foreach (var entry in table.GetEntries())
						{
							var e = new BsonItem();
							if (entry.Weight != 1)
								e["weight"] = new BsonItem(entry.Weight);
							var requiredClasses = entry.GetRequiredClasses().ToArray();
							if (requiredClasses.Length > 0)
								e["classes"] = new BsonItem(requiredClasses);
							var optionalClasses = entry.GetOptionalClasses().ToArray();
							if (optionalClasses.Length > 0)
								e["optional_classes"] = new BsonItem();
							var terms = new List<BsonItem>();
							foreach (var term in entry.Terms)
							{
								var et = new BsonItem();
								et["value"] = new BsonItem(term.Value);
								if (term.Pronunciation != "")
									et["pron"] = new BsonItem(term.Pronunciation);
								terms.Add(et);
							}
							e["terms"] = new BsonItem(terms.ToArray());
							entries.Add(e);
						}
						t["entries"] = new BsonItem(entries.ToArray());
					}
				}

                var data = doc.ToByteArray(stringTableMode != BsonStringTableMode.None);
                if (compress)
                    data = EasyCompressor.Compress(data);
                writer.Write(Encoding.ASCII.GetBytes("RANT"));
                writer.Write((uint)2);
                writer.Write(compress);
                writer.Write(data.Length);
                writer.Write(data);
            }
        }
示例#32
0
 private static void WriteStringsToBinary(string path, string[] strs)
 {
     using (EasyWriter writer = new EasyWriter(path))
     {
         writer.Write(strs);
     }
 }
示例#33
0
 internal override void SerializeData(EasyWriter writer)
 {
     writer.Write(Name);
     RST.SerializeRST(SyntaxTree, writer);
 }
示例#34
0
 internal abstract void SerializeData(EasyWriter writer);
示例#35
0
        private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false)
        {
            if (!isTop)
            {
                writer.Write(item.Type);
                writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true);
            }

            if (item.HasValues) // object or array - we need to write a document
            {
                var stream  = new MemoryStream();
                var vWriter = new EasyWriter(stream);
                foreach (string key in item.Keys)
                {
                    WriteItem(vWriter, item[key], key, false, item.IsArray);
                }
                vWriter.Close();
                var data = stream.ToArray();
                // length of data + length of length + null terminator
                writer.Write(data.Length + 4 + 1);
                writer.Write(data);
                writer.Write((byte)0x00);
                return;
            }

            switch (item.Type)
            {
            case 0x01:     // double
                writer.Write((double)item.Value);
                break;

            case 0x02:                          // string
                var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true));
                writer.Write(bytes.Length + 1); // includes null terminator
                writer.WriteBytes(bytes);
                writer.Write((byte)0x00);
                break;

            case 0x05:     // binary
                var byteArray = (byte[])item.Value;
                writer.Write(byteArray.Length);
                writer.Write((byte)0x00);
                writer.Write(byteArray);
                break;

            case 0x07:     // objectid
                writer.Write((byte[])item.Value);
                break;

            case 0x08:     // bool
                writer.Write((bool)item.Value);
                break;

            case 0x09:     // UTC datetime
            case 0x11:     // timestamp
            case 0x12:     // 64 bit int
                writer.Write((long)item.Value);
                break;

            case 0x10:     // 32 bit int
                writer.Write((int)item.Value);
                break;

            default:
                throw new NotSupportedException($"Item type {item.Type} not supported.");
            }
        }
示例#36
0
 /// <summary>
 /// Writes this document as BSON to the specified stream.
 /// </summary>
 /// <param name="stream">The stream that will be written to.</param>
 public void Write(Stream stream, bool includeStringTable = false)
 {
     var memoryStream = new MemoryStream();
     using (var writer = new EasyWriter(memoryStream))
     {
         Write(writer);
     }
     memoryStream.Close();
     var bytes = memoryStream.ToArray();
     var tableWriter = new EasyWriter(stream);
     WriteStringTable(tableWriter, includeStringTable);
     stream.Write(bytes, 0, bytes.Length);
 }
示例#37
0
 /// <summary>
 /// Writes this BSON document to the specified path.
 /// </summary>
 /// <param name="path">The path to write to.</param>
 public void Write(string path, bool includeStringTable = false)
 {
     var stream = File.Open(path, FileMode.Create);
     var writer = new EasyWriter(stream);
     Write(writer);
     writer.Close();
     stream.Close();
 }
示例#38
0
 /// <summary>
 /// Writes this document using the specified EasyWriter.
 /// </summary>
 /// <param name="writer">The writer that will be used to write this document.</param>
 internal void Write(EasyWriter writer)
 {
     _stringTableIndex = 0;
     _stringTable.Clear();
     WriteItem(writer, Top, null, true);
     writer.Close();
 }
示例#39
0
        private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false)
        {
            if(!isTop)
            {
                writer.Write(item.Type);
                writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true);
            }

            if (item.HasValues) // object or array - we need to write a document
            {
                var stream = new MemoryStream();
                var vWriter = new EasyWriter(stream);
                foreach(string key in item.Keys)
                    WriteItem(vWriter, item[key], key, false, item.IsArray);
                vWriter.Close();
                var data = stream.ToArray();
                // length of data + length of length + null terminator
                writer.Write(data.Length + 4 + 1);
                writer.Write(data);
                writer.Write((byte)0x00);
                return;
            }

            switch(item.Type)
            {
                case 0x01: // double
                    writer.Write((double)item.Value);
                    break;
                case 0x02: // string
                    var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true));
                    writer.Write(bytes.Length + 1); // includes null terminator
                    writer.WriteBytes(bytes);
                    writer.Write((byte)0x00);
                    break;
                case 0x05: // binary
                    var byteArray = (byte[])item.Value;
                    writer.Write(byteArray.Length);
                    writer.Write((byte)0x00);
                    writer.Write(byteArray);
                    break;
                case 0x07: // objectid
                    writer.Write((byte[])item.Value);
                    break;
                case 0x08: // bool
                    writer.Write((bool)item.Value);
                    break;
                case 0x09: // UTC datetime
                case 0x11: // timestamp
                case 0x12: // 64 bit int
                    writer.Write((long)item.Value);
                    break;
                case 0x10: // 32 bit int
                    writer.Write((int)item.Value);
                    break;
                default:
                    throw new NotSupportedException($"Item type {item.Type} not supported.");
            }
        }
示例#40
0
 internal void WriteStringTable(EasyWriter writer, bool include)
 {
     writer.Write(include);
     if (include)
     {
         writer.Write((byte)_stringTableMode);
         writer.Write((byte)STRING_TABLE_VERSION);
         var stringTableBytes = GenerateStringTable();
         writer.Write(stringTableBytes.Length);
         writer.Write(stringTableBytes);
     }
 }