private string toString(RawProto rp, string strIndent) { if (rp.Value.Length == 0 && rp.Children.Count == 0) { return(""); } string str = strIndent + rp.Name; if (rp.Value.Length > 0) { str += ": "; if (rp.Type == TYPE.STRING) { str += "\""; } str += rp.Value; if (rp.Type == TYPE.STRING) { str += "\""; } } else { str += " "; } str += Environment.NewLine; if (rp.Children.Count == 0) { return(str); } str += strIndent + "{"; str += Environment.NewLine; foreach (RawProto child in rp.m_rgChildren) { str += toString(child, strIndent + " "); } str += strIndent + "}"; str += Environment.NewLine; return(str); }
/// <summary> /// Parses a prototxt and places it in a new RawProto. /// </summary> /// <param name="str">Specifies the prototxt to parse.</param> /// <returns>The new RawProto is returned.</returns> public static RawProto Parse(string str) { List <RawProto> rgParent = new List <RawProto>() { new RawProto("root", "") }; RawProto child = new RawProto("", ""); List <KeyValuePair <string, int> > rgstrTokens = tokenize(str); parse(rgParent, child, rgstrTokens, 0, STATE.NAME); return(rgParent[0]); }
/// <summary> /// Parses a prototxt and places it in a new RawProto. /// </summary> /// <param name="str">Specifies the prototxt to parse.</param> /// <returns>The new RawProto is returned.</returns> public static RawProto Parse(string str) { List <RawProto> rgParent = new List <RawProto>() { new RawProto("root", "") }; RawProto child = new RawProto("", ""); str = Utility.Replace(str, '\t', ' '); str = strip_comments(str); List <KeyValuePair <string, int> > rgstrTokens = tokenize(str); parse(rgParent, child, rgstrTokens, 0, STATE.NAME); return(rgParent[0]); }
/// <summary> /// Saves the RawProto to a file. /// </summary> /// <param name="p">Specifies the RawProto.</param> /// <param name="strFileName">Specifies the file name where the RawProto is to be saved.</param> public static void SaveToFile(RawProto p, string strFileName) { string strBody = ""; if (p.Name != "root") { strBody = p.ToString(); } else { foreach (RawProto child in p.Children) { strBody += child.ToString(); strBody += Environment.NewLine; } } using (StreamWriter sw = new StreamWriter(strFileName)) { sw.Write(strBody); } }
/// <summary> /// The OverrideProjectArgs constructor. /// </summary> /// <param name="proto">Specifies the RawProtot.</param> public OverrideProjectArgs(RawProto proto) { m_proto = proto; }
private static void parse2(List <RawProto> rgParent, RawProto child, List <KeyValuePair <string, int> > rgstrTokens, int nIdx, STATE s) { if (nIdx >= rgstrTokens.Count) { return; } string strToken = rgstrTokens[nIdx].Key; if (s == STATE.NAME) { if (!char.IsLetter(strToken[0])) { throw new Exception("Expected a name and instead have: " + rgstrTokens[nIdx]); } STATE sNext; if (strToken[strToken.Length - 1] == ':') { child.m_strName = strToken.TrimEnd(':'); sNext = STATE.VALUE; } else { child.m_strName = strToken; sNext = STATE.BLOCKSTART; } nIdx++; if (nIdx >= rgstrTokens.Count) { return; } if (sNext == STATE.VALUE) { parse(rgParent, child, rgstrTokens, nIdx, STATE.VALUE); } else if (rgstrTokens[nIdx].Key == "{") { parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKSTART); } else { throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'"); } } else if (s == STATE.VALUE) { TYPE type = TYPE.NUMERIC; strToken = strToken.Trim(' ', '\t'); if (strToken[0] == '"' || strToken[0] == '\'') { type = TYPE.STRING; } child.m_strValue = strToken.Trim('"', '\''); child.m_type = type; nIdx++; rgParent[0].Children.Add(child); if (nIdx >= rgstrTokens.Count) { return; } if (char.IsLetter(rgstrTokens[nIdx].Key[0])) { child = new RawProto("", ""); parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME); } else if (rgstrTokens[nIdx].Key == "}") { parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND); } else { throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'"); } } else if (s == STATE.BLOCKSTART) { rgParent[0].Children.Add(child); rgParent.Insert(0, child); child = new RawProto("", ""); nIdx++; if (char.IsLetter(rgstrTokens[nIdx].Key[0])) { parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME); } else if (rgstrTokens[nIdx].Key == "}") { parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND); } else { throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'"); } } else if (s == STATE.BLOCKEND) { child = rgParent[0]; rgParent.RemoveAt(0); nIdx++; if (nIdx >= rgstrTokens.Count) { return; } if (char.IsLetter(rgstrTokens[nIdx].Key[0])) { child = new RawProto("", ""); parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME); } else if (rgstrTokens[nIdx].Key == "}") { parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND); } else { throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'"); } } }
/// <summary> /// Removes a given child from this node's children. /// </summary> /// <param name="p">Specifies the RawProto to remove.</param> /// <returns>If found and removed, <i>true</i> is returned, otherwise <i>false</i> is returned.</returns> public bool RemoveChild(RawProto p) { return(m_rgChildren.Remove(p)); }