MyCaffe  1.11.8.27
Deep learning software for Windows C# programmers.
RawProto.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6
7namespace MyCaffe.basecode
8{
16 public class RawProto
17 {
18 TYPE m_type = TYPE.NONE;
19 string m_strName;
20 string m_strValue;
21 RawProtoCollection m_rgChildren = new RawProtoCollection();
22
26 public enum TYPE
27 {
31 NONE,
35 NUMERIC,
39 STRING
40 }
41
42 enum STATE
43 {
44 NAME,
45 VALUE,
46 BLOCKSTART,
47 BLOCKEND
48 }
49
57 public RawProto(string strName, string strValue, RawProtoCollection rgChildren = null, TYPE type = TYPE.NONE)
58 {
59 m_type = type;
60 m_strName = strName;
61 m_strValue = strValue;
62
63 if (rgChildren != null)
64 m_rgChildren = rgChildren;
65 }
66
70 public string Name
71 {
72 get { return m_strName; }
73 }
74
78 public string Value
79 {
80 get { return m_strValue; }
81 set { m_strValue = value; }
82 }
83
87 public TYPE Type
88 {
89 get { return m_type; }
90 }
91
96 {
97 get { return m_rgChildren; }
98 }
99
105 public string FindValue(string strName)
106 {
107 foreach (RawProto p in m_rgChildren)
108 {
109 if (p.Name == strName)
110 return p.Value.Trim('\"');
111 }
112
113 return null;
114 }
115
122 public object FindValue(string strName, Type t)
123 {
124 string strVal = FindValue(strName);
125
126 if (strVal == null)
127 return null;
128
129 return convert(strVal, t);
130 }
131
138 public List<T> FindArray<T>(string strName)
139 {
140 List<T> rg = new List<T>();
141
142 foreach (RawProto rp in m_rgChildren)
143 {
144 if (rp.Name == strName)
145 {
146 object obj = convert(rp.Value, typeof(T));
147 T tVal = (T)Convert.ChangeType(obj, typeof(T));
148 rg.Add(tVal);
149 }
150 }
151
152 return rg;
153 }
154
155 private object convert(string strVal, Type t)
156 {
157 strVal = strVal.TrimEnd('}');
158
159 if (t == typeof(string))
160 return strVal;
161
162 if (t == typeof(bool))
163 return bool.Parse(strVal);
164
165 if (t == typeof(double))
166 return BaseParameter.ParseDouble(strVal);
167
168 if (t == typeof(float))
169 return BaseParameter.ParseFloat(strVal);
170
171 if (t == typeof(long))
172 return long.Parse(strVal);
173
174 if (t == typeof(int))
175 return int.Parse(strVal);
176
177 if (t == typeof(uint))
178 return uint.Parse(strVal);
179
180 throw new Exception("The type '" + t.ToString() + "' is not supported by the FindArray<T> function!");
181 }
182
188 public bool RemoveChild(RawProto p)
189 {
190 return m_rgChildren.Remove(p);
191 }
192
200 public bool RemoveChild(string strName, string strValue, bool bContains = false)
201 {
202 int nIdx = -1;
203
204 for (int i = 0; i < m_rgChildren.Count; i++)
205 {
206 if (m_rgChildren[i].Name == strName)
207 {
208 if ((bContains && m_rgChildren[i].Value.Contains(strValue)) ||
209 (!bContains && m_rgChildren[i].Value == strValue))
210 {
211 nIdx = i;
212 break;
213 }
214 }
215 }
216
217 if (nIdx >= 0)
218 {
219 m_rgChildren.RemoveAt(nIdx);
220 return true;
221 }
222
223 return false;
224 }
225
231 public RawProto FindChild(string strName)
232 {
233 foreach (RawProto p in m_rgChildren)
234 {
235 if (p.Name == strName)
236 return p;
237 }
238
239 return null;
240 }
241
247 public int FindChildIndex(string strName)
248 {
249 for (int i = 0; i < m_rgChildren.Count; i++)
250 {
251 if (m_rgChildren[i].Name == strName)
252 return i;
253 }
254
255 return -1;
256 }
257
263 public RawProtoCollection FindChildren(params string[] rgstrName)
264 {
266
267 foreach (RawProto p in m_rgChildren)
268 {
269 if (rgstrName.Contains(p.Name))
270 rg.Add(p);
271 }
272
273 return rg;
274 }
275
281 public static RawProto FromFile(string strFileName)
282 {
283 using (StreamReader sr = new StreamReader(strFileName))
284 {
285 return Parse(sr.ReadToEnd());
286 }
287 }
288
293 public void ToFile(string strFileName)
294 {
295 using (StreamWriter sw = new StreamWriter(strFileName))
296 {
297 sw.Write(ToString());
298 }
299 }
300
306 public static RawProto Parse(string str)
307 {
308 List<RawProto> rgParent = new List<RawProto>() { new RawProto("root", "") };
309 RawProto child = new RawProto("", "");
310
311 str = strip_comments(str);
312
313 List<KeyValuePair<string, int>> rgstrTokens = tokenize(str);
314
315 parse(rgParent, child, rgstrTokens, 0, STATE.NAME);
316
317 return rgParent[0];
318 }
319
320 private static string strip_comments(string str)
321 {
322 if (string.IsNullOrEmpty(str))
323 return str;
324
325 if (!str.Contains('#'))
326 return str;
327
328 string[] rgstr = str.Split('\n', '\r');
329 string strOut = "";
330
331 for (int i = 0; i < rgstr.Length; i++)
332 {
333 if (rgstr[i].Length > 0)
334 {
335 int nPos = rgstr[i].IndexOf('#');
336 if (nPos >= 0)
337 rgstr[i] = rgstr[i].Substring(0, nPos);
338 }
339
340 if (rgstr[i].Length > 0)
341 strOut += rgstr[i] + "\r\n";
342 }
343
344 return strOut;
345 }
346
347 private static string strip_commentsOld(string str)
348 {
349 List<string> rgstr = new List<string>();
350 int nPos = str.IndexOf('\n');
351
352 while (nPos >= 0)
353 {
354 string strLine = str.Substring(0, nPos);
355 strLine = strLine.Trim('\n', '\r');
356
357 int nPosComment = strLine.IndexOf('#');
358 if (nPosComment >= 0)
359 strLine = strLine.Substring(0, nPosComment);
360
361 if (strLine.Length > 0)
362 rgstr.Add(strLine);
363
364 str = str.Substring(nPos + 1);
365 nPos = str.IndexOf('\n');
366 }
367
368 str = str.Trim('\n', '\r');
369 if (str.Length > 0)
370 rgstr.Add(str);
371
372 str = "";
373
374 foreach (string strLine in rgstr)
375 {
376 str += strLine;
377 str += " \r\n";
378 }
379
380 return str;
381 }
382
383 private static List<KeyValuePair<string, int>> tokenize(string str)
384 {
385 List<KeyValuePair<string, int>> rgstrTokens = new List<KeyValuePair<string, int>>();
386 string[] rgLines = str.Split('\n');
387
388 for (int i=0; i<rgLines.Length; i++)
389 {
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 = "";
395
396 foreach (string strItem in rgTokens)
397 {
398 if (strItem.Length > 0 && strItem[0] == '\'')
399 {
400 bAdding = true;
401 strItem1 = strItem.TrimStart('\'');
402
403 if (strItem1.Contains('\''))
404 {
405 strItem1.TrimEnd('\'');
406 rgFinalTokens.Add(strItem1);
407 bAdding = false;
408 }
409 }
410 else if (bAdding && strItem.Contains('\''))
411 {
412 int nPos = strItem.IndexOf('\'');
413 strItem1 += strItem.Substring(0, nPos);
414 rgFinalTokens.Add(strItem1);
415
416 strItem1 = strItem.Substring(nPos + 1);
417 strItem1 = strItem1.Trim(' ', '\n', '\r', '\t');
418 if (strItem1.Length > 0)
419 rgFinalTokens.Add(strItem1);
420
421 strItem1 = "";
422 bAdding = false;
423 }
424 else
425 {
426 rgFinalTokens.Add(strItem);
427 }
428 }
429
430 foreach (string strItem in rgFinalTokens)
431 {
432 string strItem0 = strItem.Trim(' ', '\n', '\r', '\t');
433
434 if (strItem0.Length > 0)
435 rgstrTokens.Add(new KeyValuePair<string, int>(strItem0, i));
436 }
437 }
438
439 return rgstrTokens;
440 }
441
442 private static string removeCommentsOld(string str)
443 {
444 string[] rgstr = str.Split('\n');
445 string strOut = "";
446
447 foreach (string strLine in rgstr)
448 {
449 string strLine0 = strLine.Trim(' ', '\n', '\r', '\t');
450
451 if (strLine0.Length > 0 && strLine0[0] != '#')
452 {
453 strOut += strLine;
454 strOut += Environment.NewLine;
455 }
456 }
457
458 return strOut;
459 }
460
461 private static void parse(List<RawProto> rgParent, RawProto child, List<KeyValuePair<string, int>> rgstrTokens, int nIdx, STATE s)
462 {
463 while (nIdx < rgstrTokens.Count)
464 {
465 KeyValuePair<string, int> kvToken = rgstrTokens[nIdx];
466 string strToken = kvToken.Key;
467 int nLine = kvToken.Value;
468
469 if (s == STATE.NAME)
470 {
471 if (!char.IsLetter(strToken[0]))
472 throw new Exception("Expected a name and instead have: " + rgstrTokens[nIdx]);
473
474 STATE sNext;
475
476 if (strToken[strToken.Length - 1] == ':')
477 {
478 child.m_strName = strToken.TrimEnd(':');
479 sNext = STATE.VALUE;
480 }
481 else
482 {
483 child.m_strName = strToken;
484 sNext = STATE.BLOCKSTART;
485 }
486
487 nIdx++;
488
489 if (nIdx >= rgstrTokens.Count)
490 return;
491
492 if (rgstrTokens[nIdx].Key == "{")
493 s = STATE.BLOCKSTART;
494 else if (sNext == STATE.VALUE)
495 s = STATE.VALUE;
496 else
497 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
498 }
499 else if (s == STATE.VALUE)
500 {
501 TYPE type = TYPE.NUMERIC;
502
503 strToken = strToken.Trim(' ', '\t');
504
505 if (strToken[0] == '"' || strToken[0] == '\'')
506 type = TYPE.STRING;
507
508 child.m_strValue = strToken.Trim('"', '\'');
509 child.m_type = type;
510 nIdx++;
511
512 rgParent[0].Children.Add(child);
513
514 if (nIdx >= rgstrTokens.Count)
515 return;
516
517 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
518 {
519 child = new RawProto("", "");
520 s = STATE.NAME;
521 }
522 else if (rgstrTokens[nIdx].Key == "}")
523 {
524 s = STATE.BLOCKEND;
525 }
526 else
527 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
528 }
529 else if (s == STATE.BLOCKSTART)
530 {
531 rgParent[0].Children.Add(child);
532 rgParent.Insert(0, child);
533 child = new RawProto("", "");
534 nIdx++;
535
536 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
537 s = STATE.NAME;
538 else if (rgstrTokens[nIdx].Key == "}")
539 s = STATE.BLOCKEND;
540 else
541 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
542 }
543 else if (s == STATE.BLOCKEND)
544 {
545 child = rgParent[0];
546 rgParent.RemoveAt(0);
547
548 nIdx++;
549
550 if (nIdx >= rgstrTokens.Count)
551 return;
552
553 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
554 {
555 child = new RawProto("", "");
556 s = STATE.NAME;
557 }
558 else if (rgstrTokens[nIdx].Key == "}")
559 {
560 s = STATE.BLOCKEND;
561 }
562 else
563 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
564 }
565 }
566 }
567
568 private static void parse2(List<RawProto> rgParent, RawProto child, List<KeyValuePair<string, int>> rgstrTokens, int nIdx, STATE s)
569 {
570 if (nIdx >= rgstrTokens.Count)
571 return;
572
573 string strToken = rgstrTokens[nIdx].Key;
574
575 if (s == STATE.NAME)
576 {
577 if (!char.IsLetter(strToken[0]))
578 throw new Exception("Expected a name and instead have: " + rgstrTokens[nIdx]);
579
580 STATE sNext;
581
582 if (strToken[strToken.Length - 1] == ':')
583 {
584 child.m_strName = strToken.TrimEnd(':');
585 sNext = STATE.VALUE;
586 }
587 else
588 {
589 child.m_strName = strToken;
590 sNext = STATE.BLOCKSTART;
591 }
592
593 nIdx++;
594
595 if (nIdx >= rgstrTokens.Count)
596 return;
597
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);
602 else
603 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
604 }
605 else if (s == STATE.VALUE)
606 {
607 TYPE type = TYPE.NUMERIC;
608
609 strToken = strToken.Trim(' ', '\t');
610
611 if (strToken[0] == '"' || strToken[0] == '\'')
612 type = TYPE.STRING;
613
614 child.m_strValue = strToken.Trim('"', '\'');
615 child.m_type = type;
616 nIdx++;
617
618 rgParent[0].Children.Add(child);
619
620 if (nIdx >= rgstrTokens.Count)
621 return;
622
623 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
624 {
625 child = new RawProto("", "");
626 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
627 }
628 else if (rgstrTokens[nIdx].Key == "}")
629 {
630 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
631 }
632 else
633 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
634 }
635 else if (s == STATE.BLOCKSTART)
636 {
637 rgParent[0].Children.Add(child);
638 rgParent.Insert(0, child);
639 child = new RawProto("", "");
640 nIdx++;
641
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);
646 else
647 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
648 }
649 else if (s == STATE.BLOCKEND)
650 {
651 child = rgParent[0];
652 rgParent.RemoveAt(0);
653
654 nIdx++;
655
656 if (nIdx >= rgstrTokens.Count)
657 return;
658
659 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
660 {
661 child = new RawProto("", "");
662 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
663 }
664 else if (rgstrTokens[nIdx].Key == "}")
665 {
666 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
667 }
668 else
669 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
670 }
671 }
672
677 public override string ToString()
678 {
679 if (m_strName != "root")
680 return toString(this, "");
681
682 string str = "";
683
684 foreach (RawProto child in m_rgChildren)
685 {
686 str += child.ToString();
687 }
688
689 return str;
690 }
691
692 private string toString(RawProto rp, string strIndent)
693 {
694 if ((rp.Value == null || rp.Value.Length == 0) && rp.Children.Count == 0)
695 return "";
696
697 string str = strIndent + rp.Name;
698
699 if (rp.Value.Length > 0)
700 {
701 str += ": ";
702
703 if (rp.Type == TYPE.STRING)
704 str += "\"";
705
706 str += rp.Value;
707
708 if (rp.Type == TYPE.STRING)
709 str += "\"";
710 }
711 else
712 {
713 str += " ";
714 }
715
716 str += Environment.NewLine;
717
718 if (rp.Children.Count == 0)
719 return str;
720
721 str += strIndent + "{";
722 str += Environment.NewLine;
723
724 foreach (RawProto child in rp.m_rgChildren)
725 {
726 str += toString(child, strIndent + " ");
727 }
728
729 str += strIndent + "}";
730 str += Environment.NewLine;
731
732 return str;
733 }
734 }
735}
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.
Definition: RawProto.cs:17
bool RemoveChild(string strName, string strValue, bool bContains=false)
Removes a given child with a set value from this node's children.
Definition: RawProto.cs:200
static RawProto FromFile(string strFileName)
Parses a prototxt from a file and returns it as a RawProto.
Definition: RawProto.cs:281
TYPE
Defines the type of a RawProto node.
Definition: RawProto.cs:27
RawProto(string strName, string strValue, RawProtoCollection rgChildren=null, TYPE type=TYPE.NONE)
The RawProto constructor.
Definition: RawProto.cs:57
string Name
Returns the name of the node.
Definition: RawProto.cs:71
RawProtoCollection Children
Returns a collection of this nodes child nodes.
Definition: RawProto.cs:96
void ToFile(string strFileName)
Saves a RawProto to a file.
Definition: RawProto.cs:293
string Value
Get/set the value of the node.
Definition: RawProto.cs:79
RawProto FindChild(string strName)
Searches for a given node.
Definition: RawProto.cs:231
override string ToString()
Returns the RawProto as its full prototxt string.
Definition: RawProto.cs:677
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
Definition: RawProto.cs:306
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.
Definition: RawProto.cs:138
TYPE Type
Returns the type of the node.
Definition: RawProto.cs:88
int FindChildIndex(string strName)
Searches for the index to a given node's child.
Definition: RawProto.cs:247
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
bool RemoveChild(RawProto p)
Removes a given child from this node's children.
Definition: RawProto.cs:188
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
Definition: RawProto.cs:263
object FindValue(string strName, Type t)
Searches for a value of a node within this nodes children and return it as a given type.
Definition: RawProto.cs:122
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ NONE
No training category specified.