public override string ParseString(string strValue) { fHour = 0; fMinutes = 0; fSeconds = 0; fFraction = 0; if (!string.IsNullOrEmpty(strValue)) { var strTok = new StringTokenizer(strValue); strTok.IgnoreWhiteSpace = true; strTok.Next(); // initialize first token fHour = (byte)strTok.RequestInt(); strTok.Next(); strTok.RequestSymbol(':'); strTok.Next(); fMinutes = (byte)strTok.RequestInt(); strTok.Next(); var tok = strTok.CurrentToken; if (tok.Kind == TokenKind.Symbol && tok.Value[0] == ':') { strTok.Next(); fSeconds = (byte)strTok.RequestInt(); strTok.Next(); tok = strTok.CurrentToken; if (tok.Kind == TokenKind.Symbol && tok.Value[0] == '.') { strTok.Next(); fFraction = (short)strTok.RequestInt(); } } strValue = strTok.GetRest(); } return(strValue); }
public override string ParseString(string strValue) { GEDCOMFormat format = GEDCOMProvider.GetGEDCOMFormat(Owner); fApproximated = GEDCOMApproximated.daExact; fCalendar = GEDCOMCalendar.dcGregorian; fYear = UNKNOWN_YEAR; fYearBC = false; fYearModifier = ""; fMonth = ""; fDay = 0; if (!string.IsNullOrEmpty(strValue)) { if (format == GEDCOMFormat.gf_Ahnenblatt) { strValue = PrepareAhnenblattDate(strValue); } var strTok = new StringTokenizer(strValue); strTok.IgnoreWhiteSpace = false; strTok.Next(); strTok.SkipWhiteSpaces(); // extract approximated var token = strTok.CurrentToken; if (token.Kind == TokenKind.Word) { string su = token.Value.ToUpperInvariant(); int idx = SysUtils.IndexOf(GEDCOMDateApproximatedArray, su); if (idx >= 0) { fApproximated = (GEDCOMApproximated)idx; strTok.Next(); strTok.SkipWhiteSpaces(); } } // extract escape token = strTok.CurrentToken; if (token.Kind == TokenKind.Symbol && token.Value[0] == '@') { var escapeStr = token.Value; do { token = strTok.Next(); escapeStr += token.Value; } while (token.Kind != TokenKind.Symbol || token.Value[0] != '@'); // FIXME: check for errors int idx = SysUtils.IndexOf(GEDCOMDateEscapeArray, escapeStr); if (idx >= 0) { fCalendar = (GEDCOMCalendar)idx; } strTok.Next(); strTok.SkipWhiteSpaces(); } // extract day token = strTok.CurrentToken; if (token.Kind == TokenKind.Number && token.Value.Length <= 2) { fDay = (byte)(int)token.ValObj; token = strTok.Next(); } // extract delimiter if (token.Kind == TokenKind.WhiteSpace && token.Value[0] == ' ') { fDateFormat = GEDCOMDateFormat.dfGEDCOMStd; token = strTok.Next(); } else if (token.Kind == TokenKind.Symbol && token.Value[0] == '.') { fDateFormat = GEDCOMDateFormat.dfSystem; token = strTok.Next(); } // extract month string[] monthes = GetMonthNames(fCalendar); if (token.Kind == TokenKind.Word) { string mth = token.Value; int idx = SysUtils.IndexOf(monthes, mth); if (idx >= 0) { fMonth = mth; } token = strTok.Next(); } else if (fDateFormat == GEDCOMDateFormat.dfSystem && token.Kind == TokenKind.Number) { int idx = (int)token.ValObj; fMonth = monthes[idx - 1]; token = strTok.Next(); } // extract delimiter if (fDateFormat == GEDCOMDateFormat.dfSystem) { if (token.Kind == TokenKind.Symbol && token.Value[0] == '.') { token = strTok.Next(); } } else { if (token.Kind == TokenKind.WhiteSpace && token.Value[0] == ' ') { token = strTok.Next(); } } // extract year if (token.Kind == TokenKind.Number) { fYear = (short)(int)token.ValObj; token = strTok.Next(); // extract year modifier if (token.Kind == TokenKind.Symbol && token.Value[0] == '/') { token = strTok.Next(); if (token.Kind != TokenKind.Number) { // error } fYearModifier = token.Value; token = strTok.Next(); } // extract bc/ad if (token.Kind == TokenKind.Word && token.Value[0] == 'B') { token = strTok.Next(); if (token.Kind != TokenKind.Symbol || token.Value[0] != '.') { // error } token = strTok.Next(); if (token.Kind != TokenKind.Word || token.Value[0] != 'C') { // error } token = strTok.Next(); if (token.Kind != TokenKind.Symbol || token.Value[0] != '.') { // error } strTok.Next(); fYearBC = true; } } strValue = strTok.GetRest(); } DateChanged(); return(strValue); }
private void LoadFromStream(Stream fileStream, StreamReader reader) { fTree.State = GEDCOMState.osLoading; try { ProgressEventHandler progressHandler = fTree.OnProgress; fSourceEncoding = DEFAULT_ENCODING; fEncodingState = EncodingState.esUnchecked; long fileSize = fileStream.Length; int progress = 0; GEDCOMCustomRecord curRecord = null; GEDCOMTag curTag = null; int lineNum = 0; while (reader.Peek() != -1) { lineNum++; string str = reader.ReadLine(); str = GEDCOMUtils.TrimLeft(str); if (str.Length == 0) { continue; } if (!ConvertHelper.IsDigit(str[0])) { FixFTBLine(curRecord, curTag, lineNum, str); } else { int tagLevel; string tagXRef = "", tagName, tagValue = ""; try { var strTok = new StringTokenizer(str); strTok.RecognizeDecimals = false; strTok.IgnoreWhiteSpace = false; strTok.RecognizeIdents = true; var token = strTok.Next(); // already trimmed if (token.Kind != TokenKind.Number) { // syntax error throw new EGEDCOMException(string.Format("The string {0} doesn't start with a valid number", str)); } tagLevel = (int)token.ValObj; token = strTok.Next(); if (token.Kind != TokenKind.WhiteSpace) { // syntax error } token = strTok.Next(); if (token.Kind == TokenKind.Symbol && token.Value[0] == '@') { token = strTok.Next(); while (token.Kind != TokenKind.Symbol && token.Value[0] != '@') { tagXRef += token.Value; token = strTok.Next(); } // FIXME: check for errors //throw new EGEDCOMException(string.Format("The string {0} contains an unterminated XRef pointer", str)); //throw new EGEDCOMException(string.Format("The string {0} is expected to start with an XRef pointer", str)); token = strTok.Next(); strTok.SkipWhiteSpaces(); } token = strTok.CurrentToken; if (token.Kind != TokenKind.Word && token.Kind != TokenKind.Ident) { // syntax error } tagName = token.Value.ToUpperInvariant(); token = strTok.Next(); if (token.Kind == TokenKind.WhiteSpace) { tagValue = strTok.GetRest(); } } catch (EGEDCOMException ex) { throw new EGEDCOMException("Syntax error in line " + Convert.ToString(lineNum) + ".\r" + ex.Message); } // convert codepages if (!string.IsNullOrEmpty(tagValue) && fEncodingState == EncodingState.esChanged) { tagValue = ConvertStr(fSourceEncoding, tagValue); } if (tagLevel == 0) { if (curRecord == fTree.Header && fEncodingState == EncodingState.esUnchecked) { // beginning recognition of the first is not header record // to check for additional versions of the code page DefineEncoding(reader); } if (tagName == "INDI") { curRecord = fTree.AddRecord(new GEDCOMIndividualRecord(fTree, fTree, "", "")); } else if (tagName == "FAM") { curRecord = fTree.AddRecord(new GEDCOMFamilyRecord(fTree, fTree, "", "")); } else if (tagName == "OBJE") { curRecord = fTree.AddRecord(new GEDCOMMultimediaRecord(fTree, fTree, "", "")); } else if (tagName == "NOTE") { curRecord = fTree.AddRecord(new GEDCOMNoteRecord(fTree, fTree, "", tagValue)); } else if (tagName == "REPO") { curRecord = fTree.AddRecord(new GEDCOMRepositoryRecord(fTree, fTree, "", "")); } else if (tagName == "SOUR") { curRecord = fTree.AddRecord(new GEDCOMSourceRecord(fTree, fTree, "", "")); } else if (tagName == "SUBN") { curRecord = fTree.AddRecord(new GEDCOMSubmissionRecord(fTree, fTree, "", "")); } else if (tagName == "SUBM") { curRecord = fTree.AddRecord(new GEDCOMSubmitterRecord(fTree, fTree, "", "")); } else if (tagName == "_GROUP") { curRecord = fTree.AddRecord(new GEDCOMGroupRecord(fTree, fTree, "", "")); } else if (tagName == "_RESEARCH") { curRecord = fTree.AddRecord(new GEDCOMResearchRecord(fTree, fTree, "", "")); } else if (tagName == "_TASK") { curRecord = fTree.AddRecord(new GEDCOMTaskRecord(fTree, fTree, "", "")); } else if (tagName == "_COMM") { curRecord = fTree.AddRecord(new GEDCOMCommunicationRecord(fTree, fTree, "", "")); } else if (tagName == "_LOC") { curRecord = fTree.AddRecord(new GEDCOMLocationRecord(fTree, fTree, "", "")); } else if (tagName == "HEAD") { curRecord = fTree.Header; } else if (tagName == "TRLR") { break; } else { curRecord = null; } if (curRecord != null && tagXRef != "") { curRecord.XRef = tagXRef; } curTag = null; } else { if (curRecord != null) { if (curTag == null || tagLevel == 1) { curTag = curRecord.AddTag(tagName, tagValue, null); } else { while (tagLevel <= curTag.Level) { curTag = (curTag.Parent as GEDCOMTag); } curTag = curTag.AddTag(tagName, tagValue, null); } } } } if (progressHandler != null) { int newProgress = (int)Math.Min(100, (fileStream.Position * 100.0f) / fileSize); if (progress != newProgress) { progress = newProgress; progressHandler(fTree, progress); } } } } finally { fTree.State = GEDCOMState.osReady; } }