2using System.Collections.Generic;
61 m_strValue = strValue;
63 if (rgChildren !=
null)
64 m_rgChildren = rgChildren;
72 get {
return m_strName; }
80 get {
return m_strValue; }
81 set { m_strValue = value; }
89 get {
return m_type; }
97 get {
return m_rgChildren; }
107 foreach (
RawProto p
in m_rgChildren)
109 if (p.
Name == strName)
110 return p.
Value.Trim(
'\"');
129 return convert(strVal, t);
140 List<T> rg =
new List<T>();
142 foreach (
RawProto rp
in m_rgChildren)
144 if (rp.
Name == strName)
146 object obj = convert(rp.
Value, typeof(T));
147 T tVal = (T)Convert.ChangeType(obj, typeof(T));
155 private object convert(
string strVal,
Type t)
157 strVal = strVal.TrimEnd(
'}');
159 if (t == typeof(
string))
162 if (t == typeof(
bool))
163 return bool.Parse(strVal);
165 if (t == typeof(
double))
168 if (t == typeof(
float))
171 if (t == typeof(
long))
172 return long.Parse(strVal);
174 if (t == typeof(
int))
175 return int.Parse(strVal);
177 if (t == typeof(uint))
178 return uint.Parse(strVal);
180 throw new Exception(
"The type '" + t.ToString() +
"' is not supported by the FindArray<T> function!");
190 return m_rgChildren.
Remove(p);
200 public bool RemoveChild(
string strName,
string strValue,
bool bContains =
false)
204 for (
int i = 0; i < m_rgChildren.
Count; i++)
206 if (m_rgChildren[i].
Name == strName)
208 if ((bContains && m_rgChildren[i].
Value.Contains(strValue)) ||
209 (!bContains && m_rgChildren[i].Value == strValue))
233 foreach (
RawProto p
in m_rgChildren)
235 if (p.
Name == strName)
249 for (
int i = 0; i < m_rgChildren.
Count; i++)
251 if (m_rgChildren[i].
Name == strName)
267 foreach (
RawProto p
in m_rgChildren)
269 if (rgstrName.Contains(p.
Name))
283 using (StreamReader sr =
new StreamReader(strFileName))
285 return Parse(sr.ReadToEnd());
295 using (StreamWriter sw =
new StreamWriter(strFileName))
308 List<RawProto> rgParent =
new List<RawProto>() {
new RawProto(
"root",
"") };
311 str = strip_comments(str);
313 List<KeyValuePair<string, int>> rgstrTokens = tokenize(str);
315 parse(rgParent, child, rgstrTokens, 0, STATE.NAME);
320 private static string strip_comments(
string str)
322 if (
string.IsNullOrEmpty(str))
325 if (!str.Contains(
'#'))
328 string[] rgstr = str.Split(
'\n',
'\r');
331 for (
int i = 0; i < rgstr.Length; i++)
333 if (rgstr[i].Length > 0)
335 int nPos = rgstr[i].IndexOf(
'#');
337 rgstr[i] = rgstr[i].Substring(0, nPos);
340 if (rgstr[i].Length > 0)
341 strOut += rgstr[i] +
"\r\n";
347 private static string strip_commentsOld(
string str)
349 List<string> rgstr =
new List<string>();
350 int nPos = str.IndexOf(
'\n');
354 string strLine = str.Substring(0, nPos);
355 strLine = strLine.Trim(
'\n',
'\r');
357 int nPosComment = strLine.IndexOf(
'#');
358 if (nPosComment >= 0)
359 strLine = strLine.Substring(0, nPosComment);
361 if (strLine.Length > 0)
364 str = str.Substring(nPos + 1);
365 nPos = str.IndexOf(
'\n');
368 str = str.Trim(
'\n',
'\r');
374 foreach (
string strLine
in rgstr)
383 private static List<KeyValuePair<string, int>> tokenize(
string str)
385 List<KeyValuePair<string, int>> rgstrTokens =
new List<KeyValuePair<string, int>>();
386 string[] rgLines = str.Split(
'\n');
388 for (
int i=0; i<rgLines.Length; i++)
390 string strLine = rgLines[i].Trim(
' ',
'\r',
'\t');
391 string[] rgTokens = strLine.Split(
' ',
'\t');
392 List<string> rgFinalTokens =
new List<string>();
393 bool bAdding =
false;
394 string strItem1 =
"";
396 foreach (
string strItem
in rgTokens)
398 if (strItem.Length > 0 && strItem[0] ==
'\'')
401 strItem1 = strItem.TrimStart(
'\'');
403 if (strItem1.Contains(
'\''))
405 strItem1.TrimEnd(
'\'');
406 rgFinalTokens.Add(strItem1);
410 else if (bAdding && strItem.Contains(
'\''))
412 int nPos = strItem.IndexOf(
'\'');
413 strItem1 += strItem.Substring(0, nPos);
414 rgFinalTokens.Add(strItem1);
416 strItem1 = strItem.Substring(nPos + 1);
417 strItem1 = strItem1.Trim(
' ',
'\n',
'\r',
'\t');
418 if (strItem1.Length > 0)
419 rgFinalTokens.Add(strItem1);
426 rgFinalTokens.Add(strItem);
430 foreach (
string strItem
in rgFinalTokens)
432 string strItem0 = strItem.Trim(
' ',
'\n',
'\r',
'\t');
434 if (strItem0.Length > 0)
435 rgstrTokens.Add(
new KeyValuePair<string, int>(strItem0, i));
442 private static string removeCommentsOld(
string str)
444 string[] rgstr = str.Split(
'\n');
447 foreach (
string strLine
in rgstr)
449 string strLine0 = strLine.Trim(
' ',
'\n',
'\r',
'\t');
451 if (strLine0.Length > 0 && strLine0[0] !=
'#')
454 strOut += Environment.NewLine;
461 private static void parse(List<RawProto> rgParent,
RawProto child, List<KeyValuePair<string, int>> rgstrTokens,
int nIdx, STATE s)
463 while (nIdx < rgstrTokens.Count)
465 KeyValuePair<string, int> kvToken = rgstrTokens[nIdx];
466 string strToken = kvToken.Key;
467 int nLine = kvToken.Value;
471 if (!
char.IsLetter(strToken[0]))
472 throw new Exception(
"Expected a name and instead have: " + rgstrTokens[nIdx]);
476 if (strToken[strToken.Length - 1] ==
':')
478 child.m_strName = strToken.TrimEnd(
':');
483 child.m_strName = strToken;
484 sNext = STATE.BLOCKSTART;
489 if (nIdx >= rgstrTokens.Count)
492 if (rgstrTokens[nIdx].Key ==
"{")
493 s = STATE.BLOCKSTART;
494 else if (sNext == STATE.VALUE)
497 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
499 else if (s == STATE.VALUE)
503 strToken = strToken.Trim(
' ',
'\t');
505 if (strToken[0] ==
'"' || strToken[0] ==
'\'')
508 child.m_strValue = strToken.Trim(
'"',
'\'');
512 rgParent[0].Children.Add(child);
514 if (nIdx >= rgstrTokens.Count)
517 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
522 else if (rgstrTokens[nIdx].Key ==
"}")
527 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
529 else if (s == STATE.BLOCKSTART)
531 rgParent[0].Children.Add(child);
532 rgParent.Insert(0, child);
536 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
538 else if (rgstrTokens[nIdx].Key ==
"}")
541 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
543 else if (s == STATE.BLOCKEND)
546 rgParent.RemoveAt(0);
550 if (nIdx >= rgstrTokens.Count)
553 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
558 else if (rgstrTokens[nIdx].Key ==
"}")
563 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
568 private static void parse2(List<RawProto> rgParent,
RawProto child, List<KeyValuePair<string, int>> rgstrTokens,
int nIdx, STATE s)
570 if (nIdx >= rgstrTokens.Count)
573 string strToken = rgstrTokens[nIdx].Key;
577 if (!
char.IsLetter(strToken[0]))
578 throw new Exception(
"Expected a name and instead have: " + rgstrTokens[nIdx]);
582 if (strToken[strToken.Length - 1] ==
':')
584 child.m_strName = strToken.TrimEnd(
':');
589 child.m_strName = strToken;
590 sNext = STATE.BLOCKSTART;
595 if (nIdx >= rgstrTokens.Count)
598 if (sNext == STATE.VALUE)
599 parse(rgParent, child, rgstrTokens, nIdx, STATE.VALUE);
600 else if (rgstrTokens[nIdx].Key ==
"{")
601 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKSTART);
603 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
605 else if (s == STATE.VALUE)
609 strToken = strToken.Trim(
' ',
'\t');
611 if (strToken[0] ==
'"' || strToken[0] ==
'\'')
614 child.m_strValue = strToken.Trim(
'"',
'\'');
618 rgParent[0].Children.Add(child);
620 if (nIdx >= rgstrTokens.Count)
623 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
626 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
628 else if (rgstrTokens[nIdx].Key ==
"}")
630 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
633 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
635 else if (s == STATE.BLOCKSTART)
637 rgParent[0].Children.Add(child);
638 rgParent.Insert(0, child);
642 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
643 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
644 else if (rgstrTokens[nIdx].Key ==
"}")
645 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
647 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
649 else if (s == STATE.BLOCKEND)
652 rgParent.RemoveAt(0);
656 if (nIdx >= rgstrTokens.Count)
659 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
662 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
664 else if (rgstrTokens[nIdx].Key ==
"}")
666 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
669 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
679 if (m_strName !=
"root")
680 return toString(
this,
"");
684 foreach (
RawProto child
in m_rgChildren)
692 private string toString(
RawProto rp,
string strIndent)
697 string str = strIndent + rp.
Name;
699 if (rp.
Value.Length > 0)
716 str += Environment.NewLine;
721 str += strIndent +
"{";
722 str += Environment.NewLine;
724 foreach (
RawProto child
in rp.m_rgChildren)
726 str += toString(child, strIndent +
" ");
729 str += strIndent +
"}";
730 str += Environment.NewLine;
The BaseParameter class is the base class for all other parameter classes.
static float ParseFloat(string strVal)
Parse float values using the US culture if the decimal separator = '.', then using the native culture...
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The RawProtoCollection class is a list of RawProto objects.
bool Remove(RawProto p)
Removes a RawProto from the collection.
void RemoveAt(int nIdx)
Removes the RawProto at a given index in the collection.
int Count
Returns the number of items in the collection.
The RawProto class is used to parse and output Google prototxt file data.
bool RemoveChild(string strName, string strValue, bool bContains=false)
Removes a given child with a set value from this node's children.
static RawProto FromFile(string strFileName)
Parses a prototxt from a file and returns it as a RawProto.
TYPE
Defines the type of a RawProto node.
RawProto(string strName, string strValue, RawProtoCollection rgChildren=null, TYPE type=TYPE.NONE)
The RawProto constructor.
string Name
Returns the name of the node.
RawProtoCollection Children
Returns a collection of this nodes child nodes.
void ToFile(string strFileName)
Saves a RawProto to a file.
string Value
Get/set the value of the node.
RawProto FindChild(string strName)
Searches for a given node.
override string ToString()
Returns the RawProto as its full prototxt string.
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
List< T > FindArray< T >(string strName)
Searches for all values of a given name within this nodes children and return it as a generic List.
TYPE Type
Returns the type of the node.
int FindChildIndex(string strName)
Searches for the index to a given node's child.
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
bool RemoveChild(RawProto p)
Removes a given child from this node's children.
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
object FindValue(string strName, Type t)
Searches for a value of a node within this nodes children and return it as a given type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
@ NONE
No training category specified.