protected ProfileItem(Profile owner, int lineNumber)
		{
			if(owner == null)
				throw new ArgumentNullException("owner");

			_owner = owner;
			_lineNumber = Math.Max(lineNumber, -1);
		}
Example #2
0
		public static Profile Load(Stream stream, Encoding encoding = null)
		{
			if(stream == null)
				throw new ArgumentNullException("stream");

			Profile profile = new Profile(stream is FileStream ? ((FileStream)stream).Name : string.Empty);
			string text, content;
			ProfileSection section = null;

			using(var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
			{
				int lineNumber = 0;

				while((text = reader.ReadLine()) != null)
				{
					//解析读取到的行文本
					switch(ParseLine(text, out content))
					{
						case LineType.Section:
							var parts = content.Split(' ', '\t');
							var sections = profile.Sections;

							foreach(string part in parts)
							{
								section = sections[part];

								if(section == null)
									section = sections.Add(part, lineNumber);

								sections = section.Sections;
							}

							break;
						case LineType.Entry:
							//if(section == null)
							//	throw new ProfileException("Invalid format of the profile.");

							var index = content.IndexOf('=');

							if(section == null)
							{
								if(index < 0)
									profile.Items.Add(new ProfileEntry(lineNumber, content));
								else
									profile.Items.Add(new ProfileEntry(lineNumber, content.Substring(0, index), content.Substring(index + 1)));
							}
							else
							{
								if(index < 0)
									section.Entries.Add(lineNumber, content);
								else
									section.Entries.Add(lineNumber, content.Substring(0, index), content.Substring(index + 1));
							}

							break;
						case LineType.Comment:
							if(section == null)
								profile._items.Add(new ProfileComment(content, lineNumber));
							else
								section.Items.Add(new ProfileComment(content, lineNumber));
							break;
					}

					//递增行号
					lineNumber++;
				}
			}

			return profile;
		}