Ejemplo n.º 1
0
        public IEnumerator <Record> GetEnumerator()
        {
            int bufferSize = 10 * 1024 * 1024;

            byte[] buffer = new byte[bufferSize + 1];

            while (_reader.Position < _reader.Length)
            {
                int delPosition, realReadSize;

                do
                {
                    realReadSize = _reader.Read(buffer, 0, bufferSize);

                    delPosition = Array.LastIndexOf(buffer, (byte)FileMARC.END_OF_RECORD, realReadSize - 1) + 1;

                    if (delPosition == 0 && realReadSize == bufferSize)
                    {
                        bufferSize *= 2;
                        buffer      = new byte[bufferSize + 1];
                    }
                } while (delPosition == 0 && realReadSize == bufferSize);

                _reader.Position = _reader.Position - (realReadSize - delPosition);

                FileMARC marc = new FileMARC(Encoding.Default.GetString(buffer, 0, delPosition));
                foreach (Record marcRecord in marc)
                {
                    yield return(marcRecord);
                }
            }
        }
Ejemplo n.º 2
0
        public void AddTest()
        {
            FileMARC target = new FileMARC();
            //This string is taken as a copy and paste from record.mrc in the Test Records directory.
            string source = "01754cam a2200397 a 45000010009000000050017000090080041000269060045000679250044001129550187001560100017003430200034003600200037003940350023004310350020004540370060004740400049005340420009005830500024005920820014006161000018006302450070006482600038007183000029007565860037007855200280008226500038011026500034011406500031011746500031012056500029012366500025012656500022012906500022013126500022013341460273320110216094643.0061020s2007    nyua   c      000 f eng    a7bcbccorignewd1eecipf20gy-gencatlg0 aacquireb2 shelf copiesxpolicy default  alb18 2006-10-20ilb18 2006-10-20elb18 2006-10-20 to CIPaps04 2007-05-17 1 copy rec'd., to CIP ver.fld11 2007-07-02 Z-CipVergld11 2007-07-02 to BCCDalf27 2007-07-11 cp. 2 to BCCD  a  2006031847  a0810993139 (paper over board)  a9780810993136 (paper over board)  a(OCoLC)ocm74029165  a(OCoLC)74029165  bJunior Library Guildnhttp://www.juniorlibraryguild.com  aDLCcDLCdBAKERdBTCTAdTEFdYDXCPdEHHdDLC  alcac00aPZ7.K6232bDia 200700a[Fic]2221 aKinney, Jeff.10aDiary of a wimpy kid :bGreg Heffley's journal /cby Jeff Kinney.  aNew York :bAmulet Books,cc2007.  a217 p. :bill. ;c22 cm.8 aA Junior Library Guild selection  aGreg records his sixth grade experiences in a middle school where he and his best friend, Rowley, undersized weaklings amid boys who need to shave twice daily, hope just to survive, but when Rowley grows more popular, Greg must take drastic measures to save their friendship. 0aMiddle schoolsvJuvenile fiction. 0aFriendshipvJuvenile fiction. 0aSchoolsvJuvenile fiction. 0aDiariesvJuvenile fiction. 1aMiddle schoolsvFiction. 1aFriendshipvFiction. 1aSchoolsvFiction. 1aDiariesvFiction. 1aHumorous stories.";
            target.Add(source);

            {
                int expected = 1;
                int actual;
                actual = target.RawSource.Count;
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = source;
                string actual;
                actual = target.RawSource[0];
                Assert.AreEqual(expected, actual);
            }
        }
Ejemplo n.º 3
0
        //Interface functions
        #region IEnumerator Members

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            int bufferSize = 10 * 1024 * 1024;             // Read 10 MB at a time

            if (bufferSize > reader.Length)
            {
                bufferSize = Convert.ToInt32(reader.Length);
            }

            while (reader.Position < reader.Length)
            {
                byte[] ByteArray = new byte[bufferSize];
                int    DelPosition, RealReadSize;

                do
                {
                    RealReadSize = reader.Read(ByteArray, 0, bufferSize);

                    if (RealReadSize != bufferSize)
                    {
                        Array.Resize(ref ByteArray, RealReadSize);
                    }

                    DelPosition = Array.LastIndexOf(ByteArray, Convert.ToByte(FileMARC.END_OF_RECORD)) + 1;

                    if (DelPosition == 0 & RealReadSize == bufferSize)
                    {
                        bufferSize *= 2;
                        ByteArray   = new byte[bufferSize];
                    }
                } while (DelPosition == 0 & RealReadSize == bufferSize);

                //Some files will have trailer characters, usually a hex code 1A.
                //The record has to at least be longer than the leader length of a MARC record, so it's a good place to make sure we have enough to at least try and make a record
                //Otherwise we will relying error checking in the FileMARC class
                if (ByteArray.Length > FileMARC.LEADER_LEN)
                {
                    string encoded;

                    reader.Position = reader.Position - (RealReadSize - DelPosition);
                    char marc8utf8Flag = Convert.ToChar(ByteArray[9]);

                    if (marc8utf8Flag == ' ' && !forceUTF8)
                    {
                        Encoding encoding = new MARC8();
                        encoded = encoding.GetString(ByteArray, 0, DelPosition);
                    }
                    else
                    {
                        encoded = Encoding.UTF8.GetString(ByteArray, 0, DelPosition);

                        if (encoded.StartsWith(byteOrderMarkUtf8))
                        {
                            encoded = encoded.Remove(0, byteOrderMarkUtf8.Length); //remove UTF8 Byte Order Mark
                        }
                    }

                    FileMARC marc = new FileMARC(encoded);
                    foreach (Record marcRecord in marc)
                    {
                        yield return(marcRecord);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            //Read raw MARC record from a file.
            //The example .mrc files are in the project's root.
            string rawMarc = File.ReadAllText("..\\..\\..\\record.mrc");

            //The FileMARC class does the actual decoding of a record and splits a string of multiple records into a list object.
            //Decoding is not done until you actually access a single record from the FileMARC object.

            //You can import records straight from a string in memory. The string can have one or many MARC records.
            FileMARC marcRecords = new FileMARC(rawMarc);

            //Or you can import it straight from a file
            marcRecords.ImportMARC("..\\..\\..\\record2.mrc");

            //You can get how many records were found by using the Count property
            Console.WriteLine("Found " + marcRecords.Count + " records.");

            //You can access each individual record in it's native MARC format using the RawSource object
            Console.WriteLine("Here is the first record:");
            Console.WriteLine(marcRecords.RawSource[0]);

            //You can access each record manually using array notation.
            Record firstRecord = marcRecords[0];

            //Or you can loop through them as an Enumerable object
            //Note: I recommend only retrieving each record from the FileMARC object once as each time you do it will be decoded.
            int i = 0;
            foreach (Record record in marcRecords)
            {
                //The Warnings property contains a list of issues that the decoder found with the record.
                //The decoder attempts to return a valid MARC record to the best of it's ability.
                Console.WriteLine("Book #" + ++i + " has been decoded with " + record.Warnings.Count +" errors!");

                //Once decoded you can easily access specific data within the record, as well as make changes.

                //Array notation we will get the first requested tag in the record, or null if one does not exist.
                //First we'll get the Author.  Since there should only be one author tag array notation is the easiest to use.
                Field authorField = record["100"];

                //Each tag in the record is a field object. To get the data we have to know if it is a DataField or a ControlField and act accordingly.
                if (authorField.IsDataField())
                {
                    DataField authorDataField = (DataField)authorField;
                    //The author's name is in subfield a.  Once again since there should only be one we can use array notation.
                    Subfield authorName = authorDataField['a'];
                    Console.WriteLine("The author of this book is " + authorName.Data);
                }
                else if (authorField.IsControlField())
                {
                    //Unreachable code!
                    Console.WriteLine("Something went horribly wrong. The author field should never be a Control Field.");
                }

                //Now we will get the subjects for this record. Since a book can have multiple subjects we will use GetFields() which returns a List<Field> object.
                //Note: Not passing in a tag number to GetFields will return all the tags in the record.
                List<Field> subjects = record.GetFields("650");

                Console.WriteLine("Here are the subjects for Book #" + i);
                //Here we will assume each Field is actually a DataField since ISBNs should always be a DataField.
                foreach (DataField subject in subjects)
                {
                    string subjectText = string.Empty;

                    //We also want to loop through each subfield.
                    //Just like with GetFields() you can either pass in a subfield value, or nothing to get all the subfields
                    foreach (Subfield subfield in subject.GetSubfields())
                        subjectText += subfield.Data + " ";

                    Console.WriteLine(subjectText);
                }
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 5
0
 public void ResetTest()
 {
     FileMARC target = new FileMARC();
     target.ImportMARC("record.mrc");
     target.ImportMARC("record2.mrc");
     target.MoveNext();
     target.MoveNext();
     target.MoveNext();
     target.Reset();
     target.MoveNext();
     bool expected = true;
     bool actual;
     actual = target.MoveNext();
     Assert.AreEqual(expected,  actual);
 }
Ejemplo n.º 6
0
 public void RawSourceTest()
 {
     FileMARC target = new FileMARC();
     target.ImportMARC("record.mrc");
     target.ImportMARC("record2.mrc");
     List<string> expected = new List<string>();
     //This string is taken as a copy and paste from record.mrc in the Test Records directory.
     expected.Add("01754cam a2200397 a 45000010009000000050017000090080041000269060045000679250044001129550187001560100017003430200034003600200037003940350023004310350020004540370060004740400049005340420009005830500024005920820014006161000018006302450070006482600038007183000029007565860037007855200280008226500038011026500034011406500031011746500031012056500029012366500025012656500022012906500022013126500022013341460273320110216094643.0061020s2007    nyua   c      000 f eng    a7bcbccorignewd1eecipf20gy-gencatlg0 aacquireb2 shelf copiesxpolicy default  alb18 2006-10-20ilb18 2006-10-20elb18 2006-10-20 to CIPaps04 2007-05-17 1 copy rec'd., to CIP ver.fld11 2007-07-02 Z-CipVergld11 2007-07-02 to BCCDalf27 2007-07-11 cp. 2 to BCCD  a  2006031847  a0810993139 (paper over board)  a9780810993136 (paper over board)  a(OCoLC)ocm74029165  a(OCoLC)74029165  bJunior Library Guildnhttp://www.juniorlibraryguild.com  aDLCcDLCdBAKERdBTCTAdTEFdYDXCPdEHHdDLC  alcac00aPZ7.K6232bDia 200700a[Fic]2221 aKinney, Jeff.10aDiary of a wimpy kid :bGreg Heffley's journal /cby Jeff Kinney.  aNew York :bAmulet Books,cc2007.  a217 p. :bill. ;c22 cm.8 aA Junior Library Guild selection  aGreg records his sixth grade experiences in a middle school where he and his best friend, Rowley, undersized weaklings amid boys who need to shave twice daily, hope just to survive, but when Rowley grows more popular, Greg must take drastic measures to save their friendship. 0aMiddle schoolsvJuvenile fiction. 0aFriendshipvJuvenile fiction. 0aSchoolsvJuvenile fiction. 0aDiariesvJuvenile fiction. 1aMiddle schoolsvFiction. 1aFriendshipvFiction. 1aSchoolsvFiction. 1aDiariesvFiction. 1aHumorous stories.");
     //This string is taken as a copy and paste from record2.mrc in the Test Records directory.
     expected.Add("02265cam a2200493 a 45000010009000000050017000090080041000269060045000679250044001129550266001560100017004220200027004390200030004660200025004960200022005210350024005430350021005670370060005880400074006480420009007220500023007310820014007541000018007682450061007862460018008472600037008653000029009024900030009315860037009615200247009986500038012456500031012836500032013146500031013466500029013776500022014066500026014286500022014546500022014766550030014988000046015288560091015748560106016651499856020100927211213.0070907s2008    nyua   c      000 f eng    a7bcbccorignewd1eecipf20gy-gencatlg0 aacquireb2 shelf copiesxpolicy default  alb24 2007-09-07clb24 2007-09-07dlb07 2007-09-13 ret. to CIP for complete textdlb07 2007-10-11elb07 2007-10-11 to CIPaps09 2008-07-29 2 copies rec'd., to CIP ver.fld11 2008-07-30 Z-CipVergld11 2008-07-30 copy 1 & 2 to BCCDexc09 2010-09-27 modified call #  a  2007032296  a0810994739 (hardcover)  a9780810994737 (hardcover)  a9780810995529 (pbk.)  a0810995522 (pbk.)  a(OCoLC)ocn166872907  a(OCoLC)166872907  bJunior Library Guildnhttp://www.juniorlibraryguild.com  aDLCcDLCdBTCTAdBAKERdUPZdYDXCPdDAJdTEFdXY4dEHHdGK8dJEDdDLC  alcac00aPZ7.K6232bDk 200800a[Fic]2221 aKinney, Jeff.10aDiary of a wimpy kid :bRodrick rules /cby Jeff Kinney.30aRodrick rules  aNew York :bAmulet Books,c2008.  a216 p. :bill. ;c21 cm.1 aDiary of a wimpy kid ;v28 aA Junior Library Guild selection  aGreg Heffley tells about his summer vacation and his attempts to steer clear of trouble when he returns to middle school and tries to keep his older brother Rodrick from telling everyone about Greg's most humiliating experience of the summer. 0aMiddle schoolsvJuvenile fiction. 0aSchoolsvJuvenile fiction. 0aFamiliesvJuvenile fiction. 0aDiariesvJuvenile fiction. 1aMiddle schoolsvFiction. 1aSchoolsvFiction. 1aFamily lifevFiction. 1aDiariesvFiction. 1aHumorous stories. 7aChildren's stories.2lcsh1 aKinney, Jeff.tDiary of a wimpy kid ;v2.423Publisher descriptionuhttp://www.loc.gov/catdir/enhancements/fy0801/2007032296-d.html423Contributor biographical informationuhttp://www.loc.gov/catdir/enhancements/fy0807/2007032296-b.html");
     List<string> actual;
     actual = target.RawSource;
     Assert.AreEqual(expected.Count, actual.Count);
     Assert.AreEqual(expected[0], actual[0]);
     Assert.AreEqual(expected[1], actual[1]);
 }
Ejemplo n.º 7
0
 public void GetEnumeratorTest()
 {
     FileMARC target = new FileMARC();
     IEnumerator actual;
     actual = target.GetEnumerator();
     Assert.IsInstanceOfType(actual, typeof(IEnumerator));
 }
Ejemplo n.º 8
0
 public void FileMARCConstructorTest()
 {
     FileMARC target = new FileMARC();
     int expected = 0;
     int actual;
     actual = target.RawSource.Count;
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 9
0
 public void CountTest()
 {
     FileMARC target = new FileMARC();
     target.ImportMARC("record.mrc");
     target.ImportMARC("record2.mrc");
     int expected = 2;
     int actual;
     actual = target.Count;
     Assert.AreEqual(expected, actual);
 }