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>>();
387 if (
string.IsNullOrEmpty(str))
390 string[] rgLines = str.Split(
'\n');
392 for (
int i=0; i<rgLines.Length; i++)
394 string strLine = rgLines[i].Trim(
' ',
'\r',
'\t');
395 string[] rgTokens = strLine.Split(
' ',
'\t');
396 List<string> rgFinalTokens =
new List<string>();
397 bool bAdding =
false;
398 string strItem1 =
"";
400 foreach (
string strItem
in rgTokens)
402 if (strItem.Length > 0 && strItem[0] ==
'\'')
405 strItem1 = strItem.TrimStart(
'\'');
407 if (strItem1.Contains(
'\''))
409 strItem1.TrimEnd(
'\'');
410 rgFinalTokens.Add(strItem1);
414 else if (bAdding && strItem.Contains(
'\''))
416 int nPos = strItem.IndexOf(
'\'');
417 strItem1 += strItem.Substring(0, nPos);
418 rgFinalTokens.Add(strItem1);
420 strItem1 = strItem.Substring(nPos + 1);
421 strItem1 = strItem1.Trim(
' ',
'\n',
'\r',
'\t');
422 if (strItem1.Length > 0)
423 rgFinalTokens.Add(strItem1);
430 rgFinalTokens.Add(strItem);
434 foreach (
string strItem
in rgFinalTokens)
436 string strItem0 = strItem.Trim(
' ',
'\n',
'\r',
'\t');
438 if (strItem0.Length > 0)
439 rgstrTokens.Add(
new KeyValuePair<string, int>(strItem0, i));
446 private static string removeCommentsOld(
string str)
448 string[] rgstr = str.Split(
'\n');
451 foreach (
string strLine
in rgstr)
453 string strLine0 = strLine.Trim(
' ',
'\n',
'\r',
'\t');
455 if (strLine0.Length > 0 && strLine0[0] !=
'#')
458 strOut += Environment.NewLine;
465 private static void parse(List<RawProto> rgParent,
RawProto child, List<KeyValuePair<string, int>> rgstrTokens,
int nIdx, STATE s)
467 while (nIdx < rgstrTokens.Count)
469 KeyValuePair<string, int> kvToken = rgstrTokens[nIdx];
470 string strToken = kvToken.Key;
471 int nLine = kvToken.Value;
475 if (!
char.IsLetter(strToken[0]))
476 throw new Exception(
"Expected a name and instead have: " + rgstrTokens[nIdx]);
480 if (strToken[strToken.Length - 1] ==
':')
482 child.m_strName = strToken.TrimEnd(
':');
487 child.m_strName = strToken;
488 sNext = STATE.BLOCKSTART;
493 if (nIdx >= rgstrTokens.Count)
496 if (rgstrTokens[nIdx].Key ==
"{")
497 s = STATE.BLOCKSTART;
498 else if (sNext == STATE.VALUE)
501 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
503 else if (s == STATE.VALUE)
507 strToken = strToken.Trim(
' ',
'\t');
509 if (strToken[0] ==
'"' || strToken[0] ==
'\'')
512 child.m_strValue = strToken.Trim(
'"',
'\'');
516 rgParent[0].Children.Add(child);
518 if (nIdx >= rgstrTokens.Count)
521 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
526 else if (rgstrTokens[nIdx].Key ==
"}")
531 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
533 else if (s == STATE.BLOCKSTART)
535 rgParent[0].Children.Add(child);
536 rgParent.Insert(0, child);
540 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
542 else if (rgstrTokens[nIdx].Key ==
"}")
545 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
547 else if (s == STATE.BLOCKEND)
550 rgParent.RemoveAt(0);
554 if (nIdx >= rgstrTokens.Count)
557 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
562 else if (rgstrTokens[nIdx].Key ==
"}")
567 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
572 private static void parse2(List<RawProto> rgParent,
RawProto child, List<KeyValuePair<string, int>> rgstrTokens,
int nIdx, STATE s)
574 if (nIdx >= rgstrTokens.Count)
577 string strToken = rgstrTokens[nIdx].Key;
581 if (!
char.IsLetter(strToken[0]))
582 throw new Exception(
"Expected a name and instead have: " + rgstrTokens[nIdx]);
586 if (strToken[strToken.Length - 1] ==
':')
588 child.m_strName = strToken.TrimEnd(
':');
593 child.m_strName = strToken;
594 sNext = STATE.BLOCKSTART;
599 if (nIdx >= rgstrTokens.Count)
602 if (sNext == STATE.VALUE)
603 parse(rgParent, child, rgstrTokens, nIdx, STATE.VALUE);
604 else if (rgstrTokens[nIdx].Key ==
"{")
605 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKSTART);
607 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
609 else if (s == STATE.VALUE)
613 strToken = strToken.Trim(
' ',
'\t');
615 if (strToken[0] ==
'"' || strToken[0] ==
'\'')
618 child.m_strValue = strToken.Trim(
'"',
'\'');
622 rgParent[0].Children.Add(child);
624 if (nIdx >= rgstrTokens.Count)
627 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
630 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
632 else if (rgstrTokens[nIdx].Key ==
"}")
634 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
637 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
639 else if (s == STATE.BLOCKSTART)
641 rgParent[0].Children.Add(child);
642 rgParent.Insert(0, child);
646 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
647 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
648 else if (rgstrTokens[nIdx].Key ==
"}")
649 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
651 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
653 else if (s == STATE.BLOCKEND)
656 rgParent.RemoveAt(0);
660 if (nIdx >= rgstrTokens.Count)
663 if (
char.IsLetter(rgstrTokens[nIdx].Key[0]))
666 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
668 else if (rgstrTokens[nIdx].Key ==
"}")
670 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
673 throw new Exception(
"line (" + rgstrTokens[nIdx].
Value.ToString() +
") - Unexpected token: '" + rgstrTokens[nIdx].Key +
"'");
683 if (m_strName !=
"root")
684 return toString(
this,
"");
688 foreach (
RawProto child
in m_rgChildren)
696 private string toString(
RawProto rp,
string strIndent)
701 string str = strIndent + rp.
Name;
703 if (rp.
Value.Length > 0)
720 str += Environment.NewLine;
725 str += strIndent +
"{";
726 str += Environment.NewLine;
728 foreach (
RawProto child
in rp.m_rgChildren)
730 str += toString(child, strIndent +
" ");
733 str += strIndent +
"}";
734 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.