public void SerializeAndDeserialize_Hash() { SValue hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value"); hash["field1"] = SValue.NewInt(10); Assert.AreEqual("{'field0': 'value', 'field1': 10}", SValue.Unserialize(SValue.Serialize(hash)).ToString()); }
public void Deserialize_NotFailOnIncorrectData() { Assert.AreEqual(SValue.None, SValue.Unserialize(new byte[] { (byte)'D', (byte)'S', (byte)'-' })); Assert.AreEqual(SValue.None, SValue.Unserialize(new byte[] { (byte)'D', (byte)'S', (byte)'V' })); Assert.AreEqual(SValue.None, SValue.Unserialize(new byte[] { (byte)'D', (byte)'S', (byte)'V', (byte)0xff, (byte)0xff, (byte)0xff, 0, (byte)0xff, (byte)0xff, (byte)0xff, 0, (byte)0xff, (byte)0xff, (byte)0xff, 0 })); Assert.AreEqual(SValue.None, SValue.Unserialize(null)); }
public void SerializeAndDeserialize_List() { SValue hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value"); hash["field1"] = SValue.NewInt(10); SValue list = SValue.NewList(); list.Add(SValue.NewInt(1)); list.Add(SValue.NewString("value2")); list.Add(hash); Assert.AreEqual("[1, 'value2', {'field0': 'value', 'field1': 10}]", SValue.Unserialize(SValue.Serialize(list)).ToString()); }
public void SerializeAndDeserialize_Simple() { Assert.AreEqual("'some text'", SValue.Unserialize(SValue.Serialize(SValue.NewString("some text"))).ToString()); Assert.AreEqual("''", SValue.Unserialize(SValue.Serialize(SValue.NewString(""))).ToString()); Assert.AreEqual("''", SValue.Unserialize(SValue.Serialize(SValue.NewString(null))).ToString()); Assert.AreEqual("10", SValue.Unserialize(SValue.Serialize(SValue.NewInt(10))).ToString()); Assert.AreEqual("-150", SValue.Unserialize(SValue.Serialize(SValue.NewInt(-150))).ToString()); Assert.AreEqual("10.1d", SValue.Unserialize(SValue.Serialize(SValue.NewDouble(10.1))).ToString()); Assert.AreEqual("-0.1d", SValue.Unserialize(SValue.Serialize(SValue.NewDouble(-0.1))).ToString()); Assert.AreEqual("True", SValue.Unserialize(SValue.Serialize(SValue.NewBool(true))).ToString()); Assert.AreEqual("False", SValue.Unserialize(SValue.Serialize(SValue.NewBool(false))).ToString()); Assert.AreEqual("1.5f", SValue.Unserialize(SValue.Serialize(SValue.NewFloat(1.5f))).ToString()); Assert.AreEqual("2000L", SValue.Unserialize(SValue.Serialize(SValue.NewLong(2000L))).ToString()); Assert.AreEqual("None", SValue.Unserialize(SValue.Serialize(SValue.None)).ToString()); }
public void AsDictionary() { byte[] bytes; { SValue value = SValue.NewHash(); value["x"] = SValue.NewInt(10); Assert.NotNull(value.AsDictionary, "before serialize"); Assert.AreEqual(1, value.AsDictionary.Count, "before serialize"); Assert.AreEqual(10, value.AsDictionary["x"].Int, "before serialize"); bytes = SValue.Serialize(value); } { SValue value = SValue.Unserialize(bytes); Assert.NotNull(value.AsDictionary, "after serialize"); Assert.AreEqual(1, value.AsDictionary.Count, "after serialize"); Assert.AreEqual(10, value.AsDictionary["x"].Int, "after serialize"); } }
public void SerializeAndDeserialize_CircleLinks() { SValue hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value"); hash["field1"] = SValue.NewInt(10); hash["field2"] = hash; SValue list = SValue.NewList(); list.Add(SValue.NewInt(1)); list.Add(SValue.NewString("value2")); list.Add(hash); hash["field3"] = list; SValue unserialized = SValue.Unserialize(SValue.Serialize(list)); Assert.AreEqual("[1, 'value2', {'field0': 'value', 'field1': 10, 'field2': …, 'field3': …}]", unserialized.ToString()); Assert.AreEqual(unserialized[2], unserialized[2]["field2"]); Assert.AreEqual(unserialized, unserialized[2]["field3"]); Assert.AreNotEqual(unserialized, unserialized[2]["field2"]); }
public void Rescan() { string tempFile = Path.Combine(Path.GetTempPath(), "typewriter-syntax.bin"); SValue temp = File.Exists(tempFile) ? SValue.Unserialize(File.ReadAllBytes(tempFile)) : SValue.None; Dictionary <string, bool> scanned = new Dictionary <string, bool>(); List <string> files = new List <string>(); foreach (string dir in dirs) { if (!Directory.Exists(dir)) { continue; } foreach (string fileI in Directory.GetFiles(dir, "*.xml")) { string fileName = Path.GetFileName(fileI); if (!scanned.ContainsKey(fileName)) { scanned[fileName] = true; files.Add(fileI); } } } scanned.Clear(); SValue newTemp = SValue.NewHash(); infos.Clear(); syntaxFileByName.Clear(); bool changed = false; foreach (string fileI in files) { SValue tempI = temp[fileI]; long newTicks = File.GetLastWriteTime(fileI).Ticks; long ticks = tempI["ticks"].Long; if (newTicks == ticks) { LanguageInfo info = new LanguageInfo(); info.syntax = tempI["syntax"].String; info.patterns = ParsePatterns(tempI["patterns"].String); info.priority = tempI["priority"].Int; syntaxFileByName[info.syntax] = fileI; infos.Add(info); newTemp[fileI] = tempI; } else { changed = true; XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; settings.XmlResolver = null; using (XmlReader reader = XmlReader.Create(fileI, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "language") { string syntax = ""; string patterns = ""; int priority = 0; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name == "name") { syntax = reader.Value.ToLowerInvariant(); } else if (reader.Name == "extensions") { patterns = reader.Value; } else if (reader.Name == "priority") { int.TryParse(reader.Value, out priority); } } if (!string.IsNullOrEmpty(syntax)) { LanguageInfo info = new LanguageInfo(); info.syntax = syntax; info.patterns = ParsePatterns(patterns); info.priority = priority; syntaxFileByName[info.syntax] = fileI; infos.Add(info); SValue newTempI = SValue.NewHash(); newTempI["syntax"] = SValue.NewString(info.syntax); newTempI["patterns"] = SValue.NewString(patterns); newTempI["priority"] = SValue.NewInt(priority); newTempI["ticks"] = SValue.NewLong(newTicks); newTemp[fileI] = newTempI; } break; } } } } } if (changed) { File.WriteAllBytes(tempFile, SValue.Serialize(newTemp)); } }
public void SerializeAndDeserialize_EqualTextsCovering() { SValue list = SValue.NewList(); SValue hash; hash = SValue.NewHash(); hash["field0"] = SValue.NewString("value1"); hash["field1"] = SValue.NewInt(10); list.Add(hash); hash = SValue.NewHash(); hash["field3"] = SValue.NewString("value2"); hash["field1"] = SValue.NewInt(100); list.Add(hash); Assert.AreEqual("[{'field0': 'value1', 'field1': 10}, {'field3': 'value2', 'field1': 100}]", SValue.Unserialize(SValue.Serialize(list)).ToString()); }
public void Load(string postfix, bool rememberOpenedFiles) { SValue state = SValue.None; string file = GetTempSettingsPath(postfix, AppPath.StartupDir); if (File.Exists(file)) { state = SValue.Unserialize(File.ReadAllBytes(file)); } NullableCurrentDir = state["currentDir"].String; int width = Math.Max(50, state["width"].GetInt(700)); int height = Math.Max(30, state["height"].GetInt(480)); int x = state["x"].Int; int y = state["y"].Int; mainForm.Size = new Size(width, height); mainForm.Location = new Point(x, y); mainForm.WindowState = state["maximized"].GetBool(false) ? FormWindowState.Maximized : FormWindowState.Normal; storage.Unserialize(state["storage"]); recently.Unserialize(state["recently"]); recentlyDirs.Unserialize(state["recentlyDirs"]); DecodeGlobalBookmarks(state["bm"]); if (rememberOpenedFiles) { { foreach (SValue valueI in state["openedTabs"].List) { string fullPath = valueI["fullPath"].String; if (fullPath != "" && File.Exists(fullPath)) { mainForm.LoadFile(fullPath); } } Buffer selectedTab = mainForm.MainNest.buffers.GetByFullPath(BufferTag.File, state["selectedTab"]["fullPath"].String); if (selectedTab != null) { mainForm.MainNest.buffers.list.Selected = selectedTab; } } { foreach (SValue valueI in state["openedTabs2"].List) { string fullPath = valueI["fullPath"].String; if (fullPath != "" && File.Exists(fullPath)) { mainForm.LoadFile(fullPath, null, mainForm.MainNest2); } } if (mainForm.MainNest2 != null) { Buffer selectedTab = mainForm.MainNest.buffers.GetByFullPath(BufferTag.File, state["selectedTab2"]["fullPath"].String); if (selectedTab != null) { mainForm.MainNest.buffers.list.Selected = selectedTab; } } } } ValuesUnserialize(state); commandHistory.Unserialize(state["commandHistory"]); findHistory.Unserialize(state["findHistory"]); findInFilesHistory.Unserialize(state["findInFilesHistory"]); findInFilesTempFilter.Unserialize(state["findInFilesTempFilter"]); findInFilesTempCurrentFilter.value = state["findInFilesTempCurrentFilter"].String; moveHistory.Unserialize(state["moveHistory"]); replacePatternHistory.Unserialize(state["replacePatternHistory"]); replaceHistory.Unserialize(state["replaceHistory"]); goToLineHistory.Unserialize(state["goToLineHistory"]); findParams.Unserialize(state["findParams"]); mainForm.FileTree.SetExpandedTemp(state["fileTreeExpanded"]); if (state["showFileTree"].Bool) { mainForm.OpenFileTree(); if (mainForm.MainNest.Frame != null) { mainForm.MainNest.Frame.Focus(); } } helpPosition = state["helpPosition"].Int; viHelpPosition = state["viHelpPosition"].Int; UnserializeSettings(ref state); }
private void Rescan() { infos.Clear(); snippetFiles.Clear(); string tempFile = Path.Combine(Path.GetTempPath(), "typewriter-snippet.bin"); SValue temp = File.Exists(tempFile) ? SValue.Unserialize(File.ReadAllBytes(tempFile)) : SValue.None; Dictionary <string, bool> scanned = new Dictionary <string, bool>(); List <string> files = new List <string>(); foreach (string dir in dirs) { if (!Directory.Exists(dir)) { continue; } foreach (string fileI in Directory.GetFiles(dir, "*.snippets")) { string fileName = Path.GetFileName(fileI); if (!scanned.ContainsKey(fileName)) { scanned[fileName] = true; files.Add(Path.Combine(dir, fileI)); } } } files.Sort(); scanned.Clear(); SValue newTemp = SValue.NewHash(); infos.Clear(); foreach (string pathI in files) { string fileName = Path.GetFileName(pathI); SValue tempI = temp[fileName]; long newTicks = File.GetLastWriteTime(pathI).Ticks; long ticks = tempI["ticks"].Long; if (newTicks == ticks) { SnippetInfo info = new SnippetInfo(); info.path = pathI; info.patterns = ParseExtenstions(tempI["patterns"].String); infos.Add(info); newTemp[fileName] = tempI; } else { string line = null; using (StreamReader reader = new StreamReader(pathI)) { line = reader.ReadLine(); } string patterns; if (line != null && line.StartsWith("extensions:")) { patterns = line.Substring("extensions:".Length).Trim(); } else { patterns = ""; } SValue newTempI = SValue.NewHash(); newTempI["patterns"] = SValue.NewString(patterns); newTempI["ticks"] = SValue.NewLong(newTicks); newTemp[fileName] = newTempI; SnippetInfo info = new SnippetInfo(); info.path = pathI; info.patterns = ParseExtenstions(patterns); infos.Add(info); } } File.WriteAllBytes(tempFile, SValue.Serialize(newTemp)); }