MyCaffe  1.12.2.41
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
387 if (string.IsNullOrEmpty(str))
388 return rgstrTokens;
389
390 string[] rgLines = str.Split('\n');
391
392 for (int i=0; i<rgLines.Length; i++)
393 {
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 = "";
399
400 foreach (string strItem in rgTokens)
401 {
402 if (strItem.Length > 0 && strItem[0] == '\'')
403 {
404 bAdding = true;
405 strItem1 = strItem.TrimStart('\'');
406
407 if (strItem1.Contains('\''))
408 {
409 strItem1.TrimEnd('\'');
410 rgFinalTokens.Add(strItem1);
411 bAdding = false;
412 }
413 }
414 else if (bAdding && strItem.Contains('\''))
415 {
416 int nPos = strItem.IndexOf('\'');
417 strItem1 += strItem.Substring(0, nPos);
418 rgFinalTokens.Add(strItem1);
419
420 strItem1 = strItem.Substring(nPos + 1);
421 strItem1 = strItem1.Trim(' ', '\n', '\r', '\t');
422 if (strItem1.Length > 0)
423 rgFinalTokens.Add(strItem1);
424
425 strItem1 = "";
426 bAdding = false;
427 }
428 else
429 {
430 rgFinalTokens.Add(strItem);
431 }
432 }
433
434 foreach (string strItem in rgFinalTokens)
435 {
436 string strItem0 = strItem.Trim(' ', '\n', '\r', '\t');
437
438 if (strItem0.Length > 0)
439 rgstrTokens.Add(new KeyValuePair<string, int>(strItem0, i));
440 }
441 }
442
443 return rgstrTokens;
444 }
445
446 private static string removeCommentsOld(string str)
447 {
448 string[] rgstr = str.Split('\n');
449 string strOut = "";
450
451 foreach (string strLine in rgstr)
452 {
453 string strLine0 = strLine.Trim(' ', '\n', '\r', '\t');
454
455 if (strLine0.Length > 0 && strLine0[0] != '#')
456 {
457 strOut += strLine;
458 strOut += Environment.NewLine;
459 }
460 }
461
462 return strOut;
463 }
464
465 private static void parse(List<RawProto> rgParent, RawProto child, List<KeyValuePair<string, int>> rgstrTokens, int nIdx, STATE s)
466 {
467 while (nIdx < rgstrTokens.Count)
468 {
469 KeyValuePair<string, int> kvToken = rgstrTokens[nIdx];
470 string strToken = kvToken.Key;
471 int nLine = kvToken.Value;
472
473 if (s == STATE.NAME)
474 {
475 if (!char.IsLetter(strToken[0]))
476 throw new Exception("Expected a name and instead have: " + rgstrTokens[nIdx]);
477
478 STATE sNext;
479
480 if (strToken[strToken.Length - 1] == ':')
481 {
482 child.m_strName = strToken.TrimEnd(':');
483 sNext = STATE.VALUE;
484 }
485 else
486 {
487 child.m_strName = strToken;
488 sNext = STATE.BLOCKSTART;
489 }
490
491 nIdx++;
492
493 if (nIdx >= rgstrTokens.Count)
494 return;
495
496 if (rgstrTokens[nIdx].Key == "{")
497 s = STATE.BLOCKSTART;
498 else if (sNext == STATE.VALUE)
499 s = STATE.VALUE;
500 else
501 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
502 }
503 else if (s == STATE.VALUE)
504 {
505 TYPE type = TYPE.NUMERIC;
506
507 strToken = strToken.Trim(' ', '\t');
508
509 if (strToken[0] == '"' || strToken[0] == '\'')
510 type = TYPE.STRING;
511
512 child.m_strValue = strToken.Trim('"', '\'');
513 child.m_type = type;
514 nIdx++;
515
516 rgParent[0].Children.Add(child);
517
518 if (nIdx >= rgstrTokens.Count)
519 return;
520
521 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
522 {
523 child = new RawProto("", "");
524 s = STATE.NAME;
525 }
526 else if (rgstrTokens[nIdx].Key == "}")
527 {
528 s = STATE.BLOCKEND;
529 }
530 else
531 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
532 }
533 else if (s == STATE.BLOCKSTART)
534 {
535 rgParent[0].Children.Add(child);
536 rgParent.Insert(0, child);
537 child = new RawProto("", "");
538 nIdx++;
539
540 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
541 s = STATE.NAME;
542 else if (rgstrTokens[nIdx].Key == "}")
543 s = STATE.BLOCKEND;
544 else
545 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
546 }
547 else if (s == STATE.BLOCKEND)
548 {
549 child = rgParent[0];
550 rgParent.RemoveAt(0);
551
552 nIdx++;
553
554 if (nIdx >= rgstrTokens.Count)
555 return;
556
557 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
558 {
559 child = new RawProto("", "");
560 s = STATE.NAME;
561 }
562 else if (rgstrTokens[nIdx].Key == "}")
563 {
564 s = STATE.BLOCKEND;
565 }
566 else
567 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
568 }
569 }
570 }
571
572 private static void parse2(List<RawProto> rgParent, RawProto child, List<KeyValuePair<string, int>> rgstrTokens, int nIdx, STATE s)
573 {
574 if (nIdx >= rgstrTokens.Count)
575 return;
576
577 string strToken = rgstrTokens[nIdx].Key;
578
579 if (s == STATE.NAME)
580 {
581 if (!char.IsLetter(strToken[0]))
582 throw new Exception("Expected a name and instead have: " + rgstrTokens[nIdx]);
583
584 STATE sNext;
585
586 if (strToken[strToken.Length - 1] == ':')
587 {
588 child.m_strName = strToken.TrimEnd(':');
589 sNext = STATE.VALUE;
590 }
591 else
592 {
593 child.m_strName = strToken;
594 sNext = STATE.BLOCKSTART;
595 }
596
597 nIdx++;
598
599 if (nIdx >= rgstrTokens.Count)
600 return;
601
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);
606 else
607 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
608 }
609 else if (s == STATE.VALUE)
610 {
611 TYPE type = TYPE.NUMERIC;
612
613 strToken = strToken.Trim(' ', '\t');
614
615 if (strToken[0] == '"' || strToken[0] == '\'')
616 type = TYPE.STRING;
617
618 child.m_strValue = strToken.Trim('"', '\'');
619 child.m_type = type;
620 nIdx++;
621
622 rgParent[0].Children.Add(child);
623
624 if (nIdx >= rgstrTokens.Count)
625 return;
626
627 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
628 {
629 child = new RawProto("", "");
630 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
631 }
632 else if (rgstrTokens[nIdx].Key == "}")
633 {
634 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
635 }
636 else
637 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
638 }
639 else if (s == STATE.BLOCKSTART)
640 {
641 rgParent[0].Children.Add(child);
642 rgParent.Insert(0, child);
643 child = new RawProto("", "");
644 nIdx++;
645
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);
650 else
651 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
652 }
653 else if (s == STATE.BLOCKEND)
654 {
655 child = rgParent[0];
656 rgParent.RemoveAt(0);
657
658 nIdx++;
659
660 if (nIdx >= rgstrTokens.Count)
661 return;
662
663 if (char.IsLetter(rgstrTokens[nIdx].Key[0]))
664 {
665 child = new RawProto("", "");
666 parse(rgParent, child, rgstrTokens, nIdx, STATE.NAME);
667 }
668 else if (rgstrTokens[nIdx].Key == "}")
669 {
670 parse(rgParent, child, rgstrTokens, nIdx, STATE.BLOCKEND);
671 }
672 else
673 throw new Exception("line (" + rgstrTokens[nIdx].Value.ToString() + ") - Unexpected token: '" + rgstrTokens[nIdx].Key + "'");
674 }
675 }
676
681 public override string ToString()
682 {
683 if (m_strName != "root")
684 return toString(this, "");
685
686 string str = "";
687
688 foreach (RawProto child in m_rgChildren)
689 {
690 str += child.ToString();
691 }
692
693 return str;
694 }
695
696 private string toString(RawProto rp, string strIndent)
697 {
698 if ((rp.Value == null || rp.Value.Length == 0) && rp.Children.Count == 0)
699 return "";
700
701 string str = strIndent + rp.Name;
702
703 if (rp.Value.Length > 0)
704 {
705 str += ": ";
706
707 if (rp.Type == TYPE.STRING)
708 str += "\"";
709
710 str += rp.Value;
711
712 if (rp.Type == TYPE.STRING)
713 str += "\"";
714 }
715 else
716 {
717 str += " ";
718 }
719
720 str += Environment.NewLine;
721
722 if (rp.Children.Count == 0)
723 return str;
724
725 str += strIndent + "{";
726 str += Environment.NewLine;
727
728 foreach (RawProto child in rp.m_rgChildren)
729 {
730 str += toString(child, strIndent + " ");
731 }
732
733 str += strIndent + "}";
734 str += Environment.NewLine;
735
736 return str;
737 }
738 }
739}
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:681
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.