public void SetTagStringValue(string tagName, string value) { string su = tagName; GEDCOMTag P = FindTag(su, 0); if (P != null) { P.StringValue = value; } else { GEDCOMTag O = this; while (su != "") { string S; int index = su.IndexOf('\\'); if (index >= 0) { S = su.Substring(0, index); su = su.Substring(index + 1); } else { S = su; su = ""; } P = O.FindTag(S, 0); if (P == null) { if (su == "") { P = O.AddTag(S, value, null); } else { P = O.AddTag(S, "", null); } } else { if (su == "") { P.StringValue = value; } } O = P; } } }
public static void SetTagStrings(GEDCOMTag tag, string[] strings) { if (tag == null) { return; } tag.StringValue = ""; for (int i = tag.Count - 1; i >= 0; i--) { string subtag = tag[i].Name; if (subtag == "CONT" || subtag == "CONC") { tag.Delete(i); } } if (strings != null) { bool isRecordTag = (tag is GEDCOMRecord); int num = strings.Length; for (int i = 0; i < num; i++) { string str = strings[i]; int len = ((str.Length > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : str.Length); string sub = str.Substring(0, len); str = str.Remove(0, len); if (i == 0 && !isRecordTag) { tag.StringValue = sub; } else { tag.AddTag("CONT", sub, null); } while (str.Length > 0) { len = ((str.Length > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : str.Length); tag.AddTag("CONC", str.Substring(0, len), null); str = str.Remove(0, len); } } } }
public void Test_FindTags() { var tag = new GEDCOMTag(null, null, "TEST", ""); Assert.IsNotNull(tag); tag.AddTag("_SCOPE", "Private", null); tag.AddTag("_SCOPE", "Friends", null); tag.AddTag("_SCOPE", "Research", null); var subTags = tag.FindTags("_SCOPE"); Assert.AreEqual(3, subTags.Count); tag.DeleteTag("_SCOPE"); subTags = tag.FindTags("_SCOPE"); Assert.AreEqual(0, subTags.Count); }
/// <summary> /// Fix of line errors that are in the files of FamilyTreeBuilder. /// </summary> private static void FixFTBLine(GEDCOMCustomRecord curRecord, GEDCOMTag curTag, int lineNum, string str) { try { if (curTag is GEDCOMNotes) { curTag.AddTag("CONT", str, null); } else { if (curRecord != null) { curRecord.AddTag("NOTE", str, null); } } } catch (Exception ex) { Logger.LogWrite("GEDCOMProvider.FixFTBLine(): Line " + lineNum.ToString() + " failed correct: " + ex.Message); } }
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; } }