using System; using System.Collections; using System.IO; namespace CosmosDocumentifier { /// /// Summary description for StringDefinition. /// public class StringDefinition { private Hashtable mDefinitions; public Hashtable Definitions { get { return mDefinitions; } } public static string SOUGHT_LANGUAGE = "enUS"; public static string SOUGHT_LANGUAGE_ALT = "usUS"; public StringDefinition() { mDefinitions = new Hashtable(5000); } public void AddDefinition(string file) { StringDefinition stringDefinition = LoadDefinition(file); foreach(object key in stringDefinition.mDefinitions.Keys) { mDefinitions[key] = stringDefinition.mDefinitions[key]; } } public void AddDefinitions(string directory, string pattern) { string[] files = Directory.GetFiles(directory, pattern); foreach(string file in files) { AddDefinition(file); } string[] dirs = Directory.GetDirectories(directory); foreach(string dir in dirs) { AddDefinitions(dir, pattern); } } public static StringDefinition LoadDefinition(string file) { StreamReader streamReader = null; try { streamReader = File.OpenText(file); StringDefinition stringDefinition = LoadDefinition(streamReader); streamReader.Close(); return stringDefinition; } catch(Exception) { return new StringDefinition(); } } public static StringDefinition LoadDefinition(StreamReader streamReader) { StringDefinition stringDefinition = new StringDefinition(); try { string line = null; string store = null; int index = 0; bool ok = true; bool multiLineComment = false; bool unsoughtLanguage = true; int begin = 0; while(ok) { line = streamReader.ReadLine(); if(line == null) { break; } if(multiLineComment) { index = line.IndexOf("]]"); if(index > -1) { line = line.Substring(index+2); multiLineComment = false; } else { continue; } } index = line.IndexOf("[["); if(index > -1) { line = line.Substring(0, index); multiLineComment = true; } index = line.IndexOf("--"); if(index > -1) { line = line.Substring(0, index); } if((line.IndexOf("GetLocale()") > -1) ) { if((line.IndexOf(SOUGHT_LANGUAGE) <= -1)) { if((line.IndexOf(SOUGHT_LANGUAGE_ALT) <= -1)) { unsoughtLanguage = true; if(line.IndexOf("elseif") <= -1) { begin++; } continue; } } unsoughtLanguage = false; } if(unsoughtLanguage) { bool hasEnd = line.Trim().EndsWith("end"); if(hasEnd) { begin--; continue; } index = line.IndexOf("elseif"); if(index <= -1) { index = line.IndexOf("else"); if(index > -1) { try { line = line.Substring(index+4); } catch(Exception) { line = string.Empty; } begin--; } } if(begin > 0) { continue; } else { unsoughtLanguage = false; begin = 0; } } index = line.IndexOf("="); if(index > -1) { store = line.Substring(index+1).Trim(); if(store.EndsWith(";")) { store = store.Substring(0, store.Length-1).Trim(); } line = line.Substring(0, index).Trim(); if(line.EndsWith("=")) { line = line.Substring(0, line.Length-1).Trim(); } //stringDefinition.mDefinitions.Add(line, store); if ( !unsoughtLanguage ) { stringDefinition.mDefinitions[line] = store; } } } } catch(Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } return stringDefinition; } public override string ToString() { string str = ""; foreach(object obj in mDefinitions.Keys) { str += obj.ToString() + " => " + mDefinitions[obj].ToString() + "\n"; } return str; } } }