/// <summary> /// Tries the parse. /// </summary> /// <param name="formula">The formula.</param> /// <param name="evaluator">The evaluator.</param> /// <returns></returns> public bool TryParse(string formula, out Func <MediaFile, string> evaluator) { CounterEval rz = new CounterEval() { _fmt = "d" }; rz._error = null; evaluator = rz.GetError; StrParser prs = new StrParser(); char ch; try { prs.SetSource(formula.ToUpper()); if (!prs.ReadChar(out ch) || (ch != 'C')) { rz._error = string.Format("Error in formaula \"{0}\": must start from 'C'", formula); return(false); } if (prs.IsEol) { rz._start = 0; evaluator = rz.GetCounter; return(true); } ch = prs.NextChar; if (Char.IsDigit(ch)) // C10 { if (!prs.ReadInt(out rz._start)) { rz._error = string.Format("Error in formaula \"{0}\": cannot parse integer after 'C'", formula); return(false); } if (prs.IsEol) { evaluator = rz.GetCounter; return(true); } ch = prs.NextChar; } if (ch == 'F') // CF000 format { prs.Advance(); rz._fmt = prs.ReadToEnd(); if (string.IsNullOrWhiteSpace(rz._fmt)) { rz._error = string.Format("Error in formaula \"{0}\": format must be integer after 'CF'", formula); return(false); } else { evaluator = rz.GetCounter; return(true); } } rz._error = string.Format("Error in formaula \"{0}\". Must be in form: 'C'|'Cnn'|'CnnFfmt'|'CFfmt' where 'nn' is integer and fmt is integer format, e.g. 000", formula); return(false); } catch (Exception x) { rz._error = string.Format("Error in formaula \"{0}\". {1}: {2}", formula, x.GetType().Name, x.Message); return(false); } }
/// <summary> /// Tries the parse. /// </summary> /// <param name="formula">The formula.</param> /// <param name="evaluator">The evaluator.</param> /// <returns></returns> public bool TryParse(string formula, out Func <MediaFile, string> evaluator) { SubNameEval rz = new SubNameEval(); rz._error = null; evaluator = rz.GetError; StrParser prs = new StrParser(); char ch; try { prs.SetSource(formula.ToUpper()); if (!prs.ReadChar(out ch) || (ch != 'N')) { rz._error = string.Format("Error in formaula \"{0}\": must start from 'N'", formula); return(false); } if (prs.IsEol) { evaluator = rz.GetOriginalName; return(true); } ch = prs.NextChar; if ((ch == 'L') || (ch == 'R')) // NL10 or NR10 format { prs.Advance(); if (prs.ReadInt(out rz._ix)) { if (ch == 'L') { evaluator = rz.GetLeft; } else { evaluator = rz.GetRight; } return(true); } else { rz._error = string.Format("Error in formaula \"{0}\": must be integer after 'N{1}'", formula, ch); return(false); } } else if (Char.IsDigit(ch)) // N10-20 format { int rx; if (prs.ReadInt(out rz._ix) && prs.ReadChar(out ch) && (ch == '-') && prs.ReadInt(out rx)) { if (Char.ToLower(prs.NextChar) == 't') // N10-3T (Trim right form) { rz._tr = rx; evaluator = rz.GetSubT; } else { rz._len = rx - rz._ix + 1; if (rz._len <= 0) { rz._error = string.Format("Error in formaula \"{0}\": bad requested substring length {1}. Must look like 'N10-20' to get substring from position 10 to 20", formula, rz._len); return(false); } evaluator = rz.GetSub; } return(true); } else { rz._error = string.Format("Error in formaula \"{0}\": must look like 'N10-20' to get substring from position 10 to 20", formula); return(false); } } rz._error = string.Format("Error in formaula \"{0}\". Must be in form: 'N'|'NLnn'|'NRnn'|'Nnn-nn' where 'nn' is integer", formula); return(false); } catch (Exception x) { rz._error = string.Format("Error in formaula \"{0}\". {1}: {2}", formula, x.GetType().Name, x.Message); return(false); } }