public override void Serialize(EasyWriter output) { output.Write(FILTER_REGEX); output.Write(Outcome); output.Write((int)Regex.Options); output.Write(Regex.ToString()); }
protected override IEnumerator <RST> Serialize(EasyWriter output) { output.Write(_codeHigh); output.Write(_codeLow); output.Write(_times); output.Write(_unicode); yield break; }
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)); }
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); }
protected override IEnumerator <RST> Serialize(EasyWriter output) { output.Write(_argc); output.Write(_funcInfo.Name); foreach (var arg in _args) { yield return(arg); } }
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); } }
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); } } }
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); } } }
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(); }
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()); }
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); } }
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(); }
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); } }
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(); } }
/// <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()); } } } } }
protected override IEnumerator <RST> Serialize(EasyWriter output) { output.Write(Actions.Count); foreach (var action in Actions) { yield return(action); } }
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); }
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); } }
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); } }
/// <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()); } } } } }
/// <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); } }
internal override void SerializeData(EasyWriter writer) { writer.Write(Name); RST.SerializeRST(SyntaxTree, writer); }
protected override IEnumerator <RST> Serialize(EasyWriter output) { output.Write(Name); yield return(Body); }
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()); } } } }
private static void WriteStringsToBinary(string path, string[] strs) { using (EasyWriter writer = new EasyWriter(path)) { writer.Write(strs); } }
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."); } }
/// <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(); } }
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."); } }
/// <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); } }
protected override IEnumerator <RST> Serialize(EasyWriter output) { output.Write(Text); yield break; }