using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Gaia { public static class TemplateFrames { public static List List = new List(); } public class TemplateFrameVariable { public List Indicies; public List Positions; public TemplateFrameVariable(List indicies, List positions) { this.Indicies = indicies; this.Positions = positions; } } /// /// This class deals with the static part of the Template, the template variable names, indicies, and positions. /// The static part is separated so that the template can be parsed once and used multiple times by multiple /// requests. /// public class TemplateFrame { public Dictionary Variables = new Dictionary(); public string FilePath; public string Text; private const char BeginChar = '{'; private const char EndChar = '}'; private int VariableCount = 0; public TemplateFrame(string filePath, bool debug) { this.FilePath = filePath; this.Text = this.Build(filePath); if (!debug) { TemplateFrames.List.Add(this); } } public string Build(string filePath) { // Parse the template into a StringBuilder and TemplateFrameVariable array char[] text = PWCommon3.Utils.ReadAllText(filePath).ToCharArray(); string varName; int startIndex; for (int i = 0; i < text.Length; i++) { if (text[i] == BeginChar && text[i + 1] == BeginChar) { startIndex = i; i = i + 2; varName = getVarName(text, ref i); if (varName != null) { if (!this.Variables.ContainsKey(varName)) { this.Variables.Add(varName, new TemplateFrameVariable(new List(), new List())); } this.Variables[varName].Indicies.Add(startIndex); this.Variables[varName].Positions.Add(VariableCount++); } text = shiftCharArryLeft(text, startIndex, i); i = startIndex - 1; // Minus 1 since new characters have been shifted to start index and there could be consecutive variables. } } return new string(text); } /// /// Shifts left from endIndex to startIndex. /// private char[] shiftCharArryLeft(char[] arry, int startIndex, int endIndex) { char[] newArry = new char[arry.Length - (endIndex - startIndex + 1)]; for (int i = 0; i < startIndex; i++) { newArry[i] = arry[i]; } int j = 0; for (int i = endIndex + 1; i < arry.Length; i++) { newArry[startIndex + j] = arry[i]; j++; } return newArry; } private string getVarName(char[] text, ref int pos) { char[] ret; int startIndex; int endIndex; pos = skipSpaces(text, pos); startIndex = pos; while (text[pos] != ' ') { pos++; } endIndex = pos; ret = new char[endIndex - startIndex]; for (int i = 0; i < ret.Length; i++) { ret[i] = text[startIndex + i]; } pos = skipSpaces(text, pos); if (text[pos] != EndChar && text[++pos] != EndChar) { return null; } pos++; return new string(ret); } private static int skipSpaces(char[] text, int pos) { while (text[pos] == ' ') { pos++; } return pos; } } }