Represents a recursive string key to arbitrary value container.
コード例 #1
0
		public KvTextReader(KeyValue kv, Stream input)
			: base(input)
		{
			bool wasQuoted;
			bool wasConditional;

			var currentKey = kv;

			do
			{
				// bool bAccepted = true;

				string s = ReadToken(out wasQuoted, out wasConditional);

				if (string.IsNullOrEmpty(s))
					break;

				if (currentKey == null)
					currentKey = new KeyValue(s, null);
				else
					currentKey.Name = s;

				s = ReadToken(out wasQuoted, out wasConditional);

				if (wasConditional)
				{
					// bAccepted = ( s == "[$WIN32]" );

					// Now get the '{'
					s = ReadToken(out wasQuoted, out wasConditional);
				}

				if (s.StartsWith("{") && !wasQuoted)
				{
					// header is valid so load the file
					currentKey.RecursiveLoadFromBuffer(this);
				}
				else
				{
					throw new Exception("LoadFromBuffer: missing {");
				}

				currentKey = null;

			} while (!EndOfStream);
		}
コード例 #2
0
		/// <summary>
		/// Populate this instance from the given <see cref="Stream"/> as a binary <see cref="KeyValue"/>.
		/// </summary>
		/// <param name="input">The input <see cref="Stream"/> to read from.</param>
		/// <returns><c>true</c> if the read was successful; otherwise, <c>false</c>.</returns>
		public bool ReadAsBinary(Stream input)
		{
			Children = new List<KeyValue>();

			while (true)
			{

				var type = (Type)input.ReadByte();

				if (type == Type.End)
				{
					break;
				}

				var current = new KeyValue(null, null) {Name = input.ReadNullTermString(Encoding.UTF8)};

				try
				{
					switch (type)
					{
						case Type.None:
							{
								current.ReadAsBinary(input);
								break;
							}

						case Type.String:
							{
								current.Value = input.ReadNullTermString(Encoding.UTF8);
								break;
							}

						case Type.WideString:
							{
								throw new InvalidDataException("wstring is unsupported");
							}

						case Type.Int32:
						case Type.Color:
						case Type.Pointer:
							{
								current.Value = Convert.ToString(input.ReadInt32());
								break;
							}

						case Type.UInt64:
							{
								current.Value = Convert.ToString(input.ReadUInt64());
								break;
							}

						case Type.Float32:
							{
								current.Value = Convert.ToString(input.ReadFloat());
								break;
							}

						default:
							{
								throw new InvalidDataException("Unknown KV type encountered.");
							}
					}
				}
				catch (InvalidDataException ex)
				{
					throw new InvalidDataException(string.Format("An exception ocurred while reading KV '{0}'", current.Name), ex);
				}

				Children.Add(current);
			}

			return input.Position == input.Length;
		}
コード例 #3
0
		internal void RecursiveLoadFromBuffer(KvTextReader kvr)
		{
			bool wasQuoted;
			bool wasConditional;

			while (true)
			{
				// bool bAccepted = true;

				// get the key name
				var name = kvr.ReadToken(out wasQuoted, out wasConditional);

				if (string.IsNullOrEmpty(name))
				{
					throw new Exception("RecursiveLoadFromBuffer: got EOF or empty keyname");
				}

				if (name.StartsWith("}") && !wasQuoted)        // top level closed, stop reading
					break;

				var dat = new KeyValue(name, null) {Children = new List<KeyValue>()};
				Children.Add(dat);

				// get the value
				string value = kvr.ReadToken(out wasQuoted, out wasConditional);

				if (wasConditional && value != null)
				{
					// bAccepted = ( value == "[$WIN32]" );
					value = kvr.ReadToken(out wasQuoted, out wasConditional);
				}

				if (value == null)
					throw new Exception("RecursiveLoadFromBuffer:  got NULL key");

				if (value.StartsWith("}") && !wasQuoted)
					throw new Exception("RecursiveLoadFromBuffer:  got } in key");

				if (value.StartsWith("{") && !wasQuoted)
				{
					dat.RecursiveLoadFromBuffer(kvr);
				}
				else
				{
					if (wasConditional)
					{
						throw new Exception("RecursiveLoadFromBuffer:  got conditional between key and value");
					}

					dat.Value = value;
					// blahconditionalsdontcare
				}
			}
		}
コード例 #4
0
		/// <summary>
		/// Attempts to create an instance of <see cref="KeyValue"/> from the given input text.
		/// </summary>
		/// <param name="input">The input text to load.</param>
		/// <returns>a <see cref="KeyValue"/> instance if the load was successful, or <c>null</c> on failure.</returns>
		/// <remarks>
		/// This method will swallow any exceptions that occur when reading, use <see cref="ReadAsText"/> if you wish to handle exceptions.
		/// </remarks>
		public static KeyValue LoadFromString(string input)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(input);

			using (var stream = new MemoryStream(bytes))
			{
				var kv = new KeyValue(null, null);

				try
				{
					if (kv.ReadAsText(stream) == false)
						return null;

					return kv;
				}
				catch (Exception)
				{
					return null;
				}
			}
		}
コード例 #5
0
		static KeyValue LoadFromFile(string path, bool asBinary)
		{
			if (File.Exists(path) == false)
			{
				return null;
			}

			try
			{
				using (var input = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
				{
					var kv = new KeyValue(null, null);

					if (asBinary)
					{
						if (kv.ReadAsBinary(input) == false)
						{
							return null;
						}
					}
					else
					{
						if (kv.ReadAsText(input) == false)
						{
							return null;
						}
					}

					return kv;
				}
			}
			catch (Exception)
			{
				return null;
			}
		}