public static bool TryRead(string FileName, out BuildUserMetadata OutMetadata, TextWriter Log) { // Try to read the file string[] Lines; try { Lines = File.ReadAllLines(FileName); } catch (Exception Ex) { Log.WriteException(Ex, "Couldn't read {0}", FileName); OutMetadata = null; return(false); } // Read all the key-value pairs from the file Dictionary <string, string> Pairs = new Dictionary <string, string>(); foreach (string Line in Lines) { int EqualsIdx = Line.IndexOf('='); if (EqualsIdx != -1) { Pairs.Add(Line.Substring(0, EqualsIdx).Trim(), Line.Substring(EqualsIdx + 1).Trim()); } } // Separate out the strings BuildUserMetadata Metadata = new BuildUserMetadata(); if (Pairs.TryGetValue("User", out Metadata.User) && Pairs.TryGetValue("Message", out Metadata.Message)) { string Quality; if (Pairs.TryGetValue("Quality", out Quality) && Enum.TryParse <BuildQuality>(Quality, out Metadata.Quality)) { OutMetadata = Metadata; return(true); } } // OutMetadata = null; return(false); }
public static bool TryRead(string FileName, out BuildUserMetadata OutMetadata, TextWriter Log) { // Try to read the file string[] Lines; try { Lines = File.ReadAllLines(FileName); } catch(Exception Ex) { Log.WriteException(Ex, "Couldn't read {0}", FileName); OutMetadata = null; return false; } // Read all the key-value pairs from the file Dictionary<string, string> Pairs = new Dictionary<string,string>(); foreach(string Line in Lines) { int EqualsIdx = Line.IndexOf('='); if(EqualsIdx != -1) { Pairs.Add(Line.Substring(0, EqualsIdx).Trim(), Line.Substring(EqualsIdx + 1).Trim()); } } // Separate out the strings BuildUserMetadata Metadata = new BuildUserMetadata(); if(Pairs.TryGetValue("User", out Metadata.User) && Pairs.TryGetValue("Message", out Metadata.Message)) { string Quality; if(Pairs.TryGetValue("Quality", out Quality) && Enum.TryParse<BuildQuality>(Quality, out Metadata.Quality)) { OutMetadata = Metadata; return true; } } // OutMetadata = null; return false; }
bool CacheMetadata(FileInfo File, out BuildUserMetadata OutMetadata, TextWriter Log) { // Check if it's already in the cache Tuple <DateTime, BuildUserMetadata> CacheItem; if (CachedMetadata.TryGetValue(File.FullName, out CacheItem) && CacheItem.Item1 == File.LastWriteTimeUtc) { OutMetadata = CacheItem.Item2; return(true); } // Try to read it from disk if (BuildUserMetadata.TryRead(File.FullName, out OutMetadata, Log)) { CachedMetadata.Add(File.FullName, new Tuple <DateTime, BuildUserMetadata>(File.LastWriteTimeUtc, OutMetadata)); return(true); } // Update the cache return(false); }
bool CacheMetadata(FileInfo File, out BuildUserMetadata OutMetadata, TextWriter Log) { // Check if it's already in the cache Tuple<DateTime, BuildUserMetadata> CacheItem; if(CachedMetadata.TryGetValue(File.FullName, out CacheItem) && CacheItem.Item1 == File.LastWriteTimeUtc) { OutMetadata = CacheItem.Item2; return true; } // Try to read it from disk if(BuildUserMetadata.TryRead(File.FullName, out OutMetadata, Log)) { CachedMetadata.Add(File.FullName, new Tuple<DateTime,BuildUserMetadata>(File.LastWriteTimeUtc, OutMetadata)); return true; } // Update the cache return false; }