Пример #1
0
 public bool EndOfFile()
 {
     if (filePos >= fileBuffer.GetSize())
     {
         return(true);
     }
     return(false);
 }
Пример #2
0
        private bool ReadFile(String fileName, ref IFamilyTreeStoreBaseClass inFamilyTree)
        {
            printMemory = false;// true;

            importResult = new FileImportResult();

            DebugStringAdd(ref importResult, "GedcomDecoder::Readfile(" + fileName + ") Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            familyTree = inFamilyTree;

            trace.TraceData(TraceEventType.Verbose, 0, familyTree.GetShortTreeInfo());

            if (printMemory)
            {
                memory = new MemoryClass();

                memory.PrintMemory();
            }


            fileBuffer = new FileBufferClass();

            fileBuffer.ReadFile(fileName);

            DebugStringAdd(ref importResult, "GedcomDecoder::Readfile() size " + fileBuffer.GetSize());
            if (printMemory)
            {
                memory.PrintMemory();
            }

            String HeadString = "";

            parsedLines = 0;

            if (fileBuffer.GetSize() < 12)
            {
                importResult = line.GetDebugString();
                DebugStringAdd(ref importResult, "gedcom file too small!: " + fileName + ", size:" + fileBuffer.GetSize());
                importResult.WriteToFile(fileName);
                return(false);
            }

            Byte[] fileDataBuffer = fileBuffer.GetBuffer();

            //DebugStringAdd(ref importResult, "");

            string hdrStrText    = "";
            string linefeed      = "";
            bool   linefeedFound = false;

            for (int i = 0; i < 20; i++)
            {
                char ch = (char)fileDataBuffer[i];
                //DebugStringAdd(ref importResult, fileDataBuffer[i].ToString());
                HeadString += ch;
                hdrStrText += ch.ToString() + " ";
                if (!linefeedFound)
                {
                    if ((ch == '\n') || (ch == '\r'))
                    {
                        linefeed += ch;
                    }
                    else if (linefeed.Length > 0)
                    {
                        linefeedFound = true;
                    }
                }
            }
            //DebugStringAdd(ref importResult, "");

            DebugStringAdd(ref importResult, "Header bytes: " + hdrStrText);

            if (!linefeedFound)
            {
                DebugStringAdd(ref importResult, " Did not find proper linefeed!: " + fileName + ", size:" + fileBuffer.GetSize() + ":" + HeadString);
                importResult.WriteToFile(fileName);
                return(false);
            }

            line = new GedcomLineStatus(ref importResult, linefeed);

            if (HeadString.IndexOf("HEAD") < 0)
            {
                importResult = line.GetDebugString();
                DebugStringAdd(ref importResult, "gedcom file header missing!: " + fileName + ", size:" + fileBuffer.GetSize() + ":" + HeadString);
                importResult.WriteToFile(fileName);
                return(false);
            }

            familyTree.SetSourceFileType("GEDCOM");

            if (printMemory)
            {
                memory.PrintMemory();
            }

            Parse(ref importResult);

            if (printMemory)
            {
                memory.PrintMemory();
            }
            trace.TraceData(TraceEventType.Verbose, 0, familyTree.GetShortTreeInfo());
            DebugStringAdd(ref importResult, "GedcomDecoder::Readfile() Done " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            importResult.WriteToFile(fileName);

            return(true);
        }
Пример #3
0
        private void Parse(ref FileImportResult importResult)
        {
            bool             bomFound       = false;
            GedcomLineObject rootLineObject = new GedcomLineObject(0);

            GedcomParserProgress progress = new GedcomParserProgress(fileBuffer.GetBuffer(), fileBuffer.GetSize());

            double           lastPrintPercent = 0.0;
            double           printPercent;
            GedcomLineObject currentLineObject = rootLineObject;
            GedcomLineData   prevLineData      = null;
            int  lineDiff = 0;
            uint lineLength;

            //string importResult = "";
            bomFound = CheckBomMark(ref progress, ref importResult);

            GedcomTreeDecoderClass treeDecoder = new GedcomTreeDecoderClass(ref familyTree, ref importResult);

            treeDecoder.SetCharacterSet(characterSet);

            while (!progress.IsEndOfFile())
            {
                GedcomLineData lineData;

                line.SetDebugString(importResult);
                lineLength   = line.ReadLine(ref progress);
                lineData     = line.DecodeLine();
                importResult = line.GetDebugString();
                parsedLines++;

                if ((lineData != null) && lineData.valid)
                {
                    if (lineData.level == (currentLineObject.GetLevel() + 1))
                    {
                        GedcomLineObject subLineObject = new GedcomLineObject(lineData.level);

                        subLineObject.parent = currentLineObject;

                        if (prevLineData != null)
                        {
                            prevLineData.child = subLineObject;
                        }

                        currentLineObject = subLineObject;
                    }

                    /*else if (currentLineObject.GetLevel() == lineData.level)
                     * {
                     * }*/
                    else if (lineData.level < currentLineObject.GetLevel())
                    {
                        bool decodeDone = false;
                        printPercent = 100.0 * (double)progress.position / (double)progress.size;

                        if ((printPercent - lastPrintPercent) > 0.10)
                        {
                            if (backgroundWorker != null)
                            {
                                backgroundWorker.ReportProgress((int)printPercent, "Working...");
                            }
                            lastPrintPercent = printPercent;

                            if (trace.Switch.Level.HasFlag(SourceLevels.Information))
                            {
                                trace.TraceInformation("Decode position 1 " + progress.position + " (" + progress.size + ") " + DateTime.Now.ToString());
                                trace.TraceInformation("Lines " + parsedLines + " (" + treeDecoder.GetDecodedLines() + ") " + printPercent.ToString("F") + "%");
                                trace.TraceData(TraceEventType.Verbose, 0, familyTree.GetShortTreeInfo());
                                //familyTree.PrintShort();
                            }
                        }
                        do
                        {
                            if (currentLineObject.parent != null)
                            {
                                currentLineObject = currentLineObject.parent;

                                if ((currentLineObject.GetLevel() == 0) && (parsedLines > 0))
                                {
                                    treeDecoder.DecodeObject(currentLineObject);

                                    currentLineObject.Clear();

                                    if (treeDecoder.GetCharacterSet() != characterSet)
                                    {
                                        if (bomFound)
                                        {
                                            treeDecoder.DebugStringAdd("Warning! BOM and character set in Gedcom part mismatches! " + treeDecoder.GetCharacterSet() + "," + characterSet);
                                        }
                                        SetCharacterSet(treeDecoder.GetCharacterSet());
                                    }
                                }
                            }
                            else
                            {
                                if ((parsedLines - 1) - treeDecoder.GetDecodedLines() - 1 > lineDiff)
                                {
                                    treeDecoder.DebugStringAdd("Decode position " + progress.position + " (" + progress.size + ")");
                                    treeDecoder.DebugStringAdd("Lines " + parsedLines + " (" + treeDecoder.GetDecodedLines() + "," + lineDiff + ")");
                                    treeDecoder.DebugStringAdd("New undecoded lines: " + (parsedLines - treeDecoder.GetDecodedLines() - lineDiff) + "!");
                                    lineDiff = parsedLines - treeDecoder.GetDecodedLines();
                                }
                                if (trace.Switch.Level.HasFlag(SourceLevels.Information))
                                {
                                    trace.TraceInformation("Decode position 2 " + progress.position + " (" + progress.size + ")");
                                    trace.TraceInformation("Lines " + parsedLines + " (" + treeDecoder.GetDecodedLines() + ")");
                                    trace.TraceData(TraceEventType.Information, 0, familyTree.GetShortTreeInfo());
                                    //familyTree.PrintShort();
                                }

                                currentLineObject.gedcomLines.Clear();
                                decodeDone = true;
                            }
                        } while ((currentLineObject.GetLevel() > lineData.level) && !decodeDone);
                    }

                    prevLineData = lineData;

                    currentLineObject.gedcomLines.Add(lineData);
                }
                else
                {
                    if (lineData != null)
                    {
                        treeDecoder.DebugStringAdd("Line:" + lineData.lineNo + " Error bad gedcom line [" + lineData + "]");
                    }
                    else
                    {
                        treeDecoder.DebugStringAdd("Line: " + parsedLines + ": Error bad gedcom line:no data found ");
                    }
                }

                if (progress.IsEndOfFile())
                {
                    treeDecoder.DecodeObject(currentLineObject);
                    if (treeDecoder.GetCharacterSet() != characterSet)
                    {
                        if (bomFound)
                        {
                            treeDecoder.DebugStringAdd("Warning! BOM and character set in Gedcom part mismatches! " + treeDecoder.GetCharacterSet() + "," + characterSet);
                        }
                        SetCharacterSet(treeDecoder.GetCharacterSet());
                    }
                    treeDecoder.DebugStringAdd("Line:" + lineData.lineNo + " end of file " + parsedLines);

                    if (lineData.level != 0)
                    {
                        treeDecoder.DebugStringAdd("Line:" + lineData.lineNo + "Error: The Gedcom file did not end correctly! Was it inomplete? = " + currentLineObject.gedcomLines.Count);
                    }
                    if (!treeDecoder.DecodingCompleted())
                    {
                        treeDecoder.DebugStringAdd("Line:" + lineData.lineNo + "Error: The Gedcom file did not end correctly! No trailer detected = " + currentLineObject.gedcomLines.Count);
                    }

                    treeDecoder.ShowUnknownTags();
                }
            }
            backgroundWorker = null;
            treeDecoder.DebugStringAdd("Gedcom file parsing finished at " + currentLineObject.gedcomLines.Count);
            importResult = treeDecoder.GetImportResult();
        }
Пример #4
0
        public bool ReadFile(String filename, ref IFamilyTreeStoreBaseClass inFamilyTree)
        {
            printMemory = false;// true;

            trace.TraceInformation("TextDecoder::Readfile(" + filename + ") Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            familyTree = inFamilyTree;

            trace.TraceInformation(familyTree.GetShortTreeInfo());
            //familyTree.Print();

            if (printMemory)
            {
                memory = new MemoryClass();

                memory.PrintMemory();
            }

            xrefMapLists = new XrefMapLists(inFamilyTree);

            fileBuffer = new FileBufferClass();

            fileBuffer.ReadFile(filename);

            trace.TraceInformation("TextDecoder::Readfile() size " + fileBuffer.GetSize());
            if (printMemory)
            {
                memory.PrintMemory();
            }

            String HeadString = "";

            trace.TraceInformation("Text file " + filename + " read ok, size " + fileBuffer.GetSize());

            if (fileBuffer.GetSize() < 12)
            {
                trace.TraceInformation("Text file too small!: " + filename + ", size:" + fileBuffer.GetSize());
                return(false);
            }

            Byte[] fileDataBuffer = fileBuffer.GetBuffer();

            for (int i = 0; i < 12; i++)
            {
                trace.TraceInformation(" data:" + (int)fileDataBuffer[i]);
                HeadString += (char)fileDataBuffer[i];
            }
            trace.TraceInformation("");

            familyTree.SetSourceFileType("Text");

            if (printMemory)
            {
                memory.PrintMemory();
            }

            if (printMemory)
            {
                memory.PrintMemory();
            }
            //familyTree.Print();
            trace.TraceData(TraceEventType.Verbose, 0, familyTree.GetShortTreeInfo());
            trace.TraceInformation("TextDecoder::Readfile() Done " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            string directory = "/tmp/";

            if (!Directory.Exists(directory))
            {
                //Directory.CreateDirectory(directory);
                directory = "";
            }

            if ((directory.Length > 0) && filename.Contains(directory))
            {
                filename = filename.Substring(directory.Length);
            }
            Parse(directory + FamilyUtility.MakeFilename(filename + "_text_parsed_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".txt"));

            xrefMapLists.Analyze(trace);
            return(true);
        }
Пример #5
0
        public bool ReadFile(String fileName, ref FamilyTreeStoreBaseClass inFamilyTree)
        {
            printMemory = false;// true;

            trace.TraceInformation("TextDecoder::Readfile(" + fileName + ") Start " + DateTime.Now);

            familyTree = inFamilyTree;

            familyTree.Print();

            if (printMemory)
            {
                memory = new MemoryClass();

                memory.PrintMemory();
            }

            xrefMapLists = new XrefMapLists(inFamilyTree);

            fileBuffer = new FileBufferClass();

            fileBuffer.ReadFile(fileName);

            trace.TraceInformation("TextDecoder::Readfile() size " + fileBuffer.GetSize());
            if (printMemory)
            {
                memory.PrintMemory();
            }

            String HeadString = "";

            trace.TraceInformation("Text file " + fileName + " read ok, size " + fileBuffer.GetSize());

            if (fileBuffer.GetSize() < 12)
            {
                trace.TraceInformation("Text file too small!: " + fileName + ", size:" + fileBuffer.GetSize());
                return(false);
            }

            Byte[] fileDataBuffer = fileBuffer.GetBuffer();

            for (int i = 0; i < 12; i++)
            {
                trace.TraceInformation(" data:" + (int)fileDataBuffer[i]);
                HeadString += (char)fileDataBuffer[i];
            }
            trace.TraceInformation("");

            familyTree.SetSourceFileType("Text");

            if (printMemory)
            {
                memory.PrintMemory();
            }

            if (printMemory)
            {
                memory.PrintMemory();
            }
            familyTree.Print();
            trace.TraceInformation("TextDecoder::Readfile() Done " + DateTime.Now);

            Parse(FamilyUtility.MakeFilename(fileName + "_parsed_" + DateTime.Now.ToString() + ".txt"));

            xrefMapLists.Analyze(trace);
            return(true);
        }