/// <summary> /// /// </summary> /// <param name="propsFileInfo"></param> /// <param name="propertyRead"></param> public static void Read(ConfigResource resource, PropertyReadDelegate propertyRead) { if (!resource.Exists) return; StreamReader reader = new StreamReader(resource.OpenRead()); char c = ' ', prev = '\\'; int lastStart = 0, ignoreCount = 0; bool ignoreProperties = false; string key = null, var = null; StringBuilder currentVar = new StringBuilder(); StringBuilder currentToken = new StringBuilder(); Queue<string> queue = new Queue<string>(); StringBuilder currentTokenPiece = new StringBuilder(); ReadMode mode = ReadMode.Key; ReadMode nextModeAfterSpaces = ReadMode.Key; string line = reader.ReadLine(); while (line != null) { int start = 0; bool skipLine = false; while ((start < line.Length) && char.IsWhiteSpace(line[start])) ++start; if (start >= line.Length) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else if (line[start] == '#') { propertyRead(resource, PropertyType.Comment, queue, "#", line); } else { if (ignoreProperties) { if ((ignoreCount == 0) || (start == lastStart)) { ignoreCount++; lastStart = start; skipLine = true; } else { ignoreCount = 0; ignoreProperties = false; } } if (skipLine) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else { for (int i = start; i < line.Length; i++) { c = line[i]; if (mode == ReadMode.Key) { if (c == '=') { if (currentTokenPiece.Length > 0) { queue.Enqueue(currentTokenPiece.ToString()); } currentTokenPiece.Remove(0, currentTokenPiece.Length); key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); mode = ReadMode.Value; continue; } else if (char.IsWhiteSpace(c)) { key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); currentTokenPiece.Remove(0, currentTokenPiece.Length); if (key == "if") { nextModeAfterSpaces = ReadMode.If; } else if (key == "import") { nextModeAfterSpaces = ReadMode.Import; } else { break; } mode = ReadMode.FlushWhiteSpace; continue; } else if (c == '.') { currentToken.Append(c); queue.Enqueue(currentTokenPiece.ToString()); currentTokenPiece.Remove(0, currentTokenPiece.Length); } else { currentTokenPiece.Append(c); currentToken.Append(c); } } else if (mode == ReadMode.FlushWhiteSpace) { if (!char.IsWhiteSpace(c)) { currentToken.Append(c); mode = nextModeAfterSpaces; } } else if (mode == ReadMode.Import) { currentToken.Append(c); } else if (mode == ReadMode.If) { currentToken.Append(c); } else if (mode == ReadMode.Value) { currentToken.Append(c); } prev = c; } if (prev != '\\') { var = currentToken.ToString(); if (mode == ReadMode.If) { ignoreProperties = propertyRead(resource, PropertyType.If, queue, key, var); } else if (mode == ReadMode.Import) { // Open another file inline with this one. if (propertyRead(resource, PropertyType.Import, queue, key, var)) { ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", var)); //success = res.Exists; //FileInfo fileToImport = new FileInfo(string.Format(@"{0}\{1}.properties", propsFileInfo.Directory.FullName, var)); if (res.Exists) { Read(res, propertyRead); } } } else if (mode == ReadMode.Value) { propertyRead(resource, PropertyType.Property, queue, key, var); } currentToken.Remove(0, currentToken.Length); queue.Clear(); key = null; mode = ReadMode.Key; } else { currentToken.Remove(currentToken.Length - 1, 1); } } } line = reader.ReadLine(); } reader.Close(); if (key != null) { var = currentToken.ToString(); propertyRead(resource, PropertyType.Property, queue, key, var); } }
/// <summary> /// /// </summary> /// <param name="propsFileInfo"></param> /// <param name="propertyRead"></param> public static void Read(ConfigResource resource, PropertyReadDelegate propertyRead) { if (!resource.Exists) { return; } StreamReader reader = new StreamReader(resource.OpenRead()); char c = ' ', prev = '\\'; int lastStart = 0, ignoreCount = 0; bool ignoreProperties = false; string key = null, var = null; StringBuilder currentVar = new StringBuilder(); StringBuilder currentToken = new StringBuilder(); Queue <string> queue = new Queue <string>(); StringBuilder currentTokenPiece = new StringBuilder(); ReadMode mode = ReadMode.Key; ReadMode nextModeAfterSpaces = ReadMode.Key; string line = reader.ReadLine(); while (line != null) { int start = 0; bool skipLine = false; while ((start < line.Length) && char.IsWhiteSpace(line[start])) { ++start; } if (start >= line.Length) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else if (line[start] == '#') { propertyRead(resource, PropertyType.Comment, queue, "#", line); } else { if (ignoreProperties) { if ((ignoreCount == 0) || (start == lastStart)) { ignoreCount++; lastStart = start; skipLine = true; } else { ignoreCount = 0; ignoreProperties = false; } } if (skipLine) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else { for (int i = start; i < line.Length; i++) { c = line[i]; if (mode == ReadMode.Key) { if (c == '=') { if (currentTokenPiece.Length > 0) { queue.Enqueue(currentTokenPiece.ToString()); } currentTokenPiece.Remove(0, currentTokenPiece.Length); key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); mode = ReadMode.Value; continue; } else if (char.IsWhiteSpace(c)) { key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); currentTokenPiece.Remove(0, currentTokenPiece.Length); if (key == "if") { nextModeAfterSpaces = ReadMode.If; } else if (key == "import") { nextModeAfterSpaces = ReadMode.Import; } else { break; } mode = ReadMode.FlushWhiteSpace; continue; } else if (c == '.') { currentToken.Append(c); queue.Enqueue(currentTokenPiece.ToString()); currentTokenPiece.Remove(0, currentTokenPiece.Length); } else { currentTokenPiece.Append(c); currentToken.Append(c); } } else if (mode == ReadMode.FlushWhiteSpace) { if (!char.IsWhiteSpace(c)) { currentToken.Append(c); mode = nextModeAfterSpaces; } } else if (mode == ReadMode.Import) { currentToken.Append(c); } else if (mode == ReadMode.If) { currentToken.Append(c); } else if (mode == ReadMode.Value) { currentToken.Append(c); } prev = c; } if (prev != '\\') { var = currentToken.ToString(); if (mode == ReadMode.If) { ignoreProperties = propertyRead(resource, PropertyType.If, queue, key, var); } else if (mode == ReadMode.Import) { // Open another file inline with this one. if (propertyRead(resource, PropertyType.Import, queue, key, var)) { ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", var)); //success = res.Exists; //FileInfo fileToImport = new FileInfo(string.Format(@"{0}\{1}.properties", propsFileInfo.Directory.FullName, var)); if (res.Exists) { Read(res, propertyRead); } } } else if (mode == ReadMode.Value) { propertyRead(resource, PropertyType.Property, queue, key, var); } currentToken.Remove(0, currentToken.Length); queue.Clear(); key = null; mode = ReadMode.Key; } else { currentToken.Remove(currentToken.Length - 1, 1); } } } line = reader.ReadLine(); } reader.Close(); if (key != null) { var = currentToken.ToString(); propertyRead(resource, PropertyType.Property, queue, key, var); } }
public static void Read(FileInfo propsFileInfo, PropertyReadDelegate propertyRead) { Read(new ConfigResource(propsFileInfo), propertyRead); }