CHAP4. Fundamental File Structure Concept by yurtgc548

VIEWS: 17 PAGES: 29

									File Structures by Folk, Zoellick, Riccardi




          Chap4. Fundamental File
            Structure Concepts

                           서울대학교 컴퓨터공학부
                            객체지향시스템연구실
                             SNU-OOPSLA-LAB
                               김 형 주 교수




         File Structures             SNU-OOPSLA Lab.   1
Objectives
                        Chapter Objectives
    Introduce file structure concepts dealing with
         Stream files
         Reading and writing fields and records
         Field and record boundaries
         Fixed-length and variable-length fields and records
         Packing and unpacking records and buffers
    Present an object-oriented approach to file structures
         Methods of encapsulating object value and behavior in classes
         Classes for buffer manipulation
         Class hierarchy for buffer and file objects and operations
         Inheritance and virtual functions
         Template classes




      File Structures                  SNU-OOPSLA Lab.                    2
Contents


                       Contents
  4.1 Field and Record Organization
  4.2 Using Classes to Manipulate Buffers
  4.3 Using Inheritance for Record Buffer Classes
  4.4 Managing Fixed-Length, Fixed-Field Buffers
  4.5 An Object-Oriented Class for Record Files




     File Structures      SNU-OOPSLA Lab.           3
4.1 Field and Record Organization

             A Stream file
    File structure ==> Persistency ==>
     Programs outlive the data in files
    Simple representation: a file organized as a stream of
     bytes
    Simple, but Reverse Humpty-Dumpty problem
        In case of putting all information as a byte
      of stream, there is no way to get it apart
        Solution : Use field structure




     File Structures              SNU-OOPSLA Lab.             4
   The Need of Field Concept
Consider the function “write Person as a stream of bytes”!

Ostream & operator << (ostream & outputFile, Person & p)
{ // insert (write) fields into stream
    outputFile << p.LastName << p.FirstName << p.Address
                 << p.City          << p.State        << p.ZipCode;
     return outputFile;
}
(input)
Mary Ames 123 Maple                 Stillwater, Ok 74074
Alan Mason 90 Eastgate              Ada,       Ok 74820
(output)
AmesMary123 MapleStillwaterOK74075MasonAlan90 Eastgate…..



 File Structures               SNU-OOPSLA Lab.                        5
4.1 Field and Record Organization


             Field Organization

       Field: The smallest logically meaningful unit of
        information in a file (not physical)
       Field structures (4 methods)
            Fix the length of fields
            Begin each field with a length indicator
            Separate the fields with delimiters
            Use a “Keyword = value” expression




                                                           Continued
    File Structures                SNU-OOPSLA Lab.            6
4.1 Field and Record Organization

                      Four methods for organizing files
                       Ames           John          123 Maple         Stillwater          OK74075377-1808
                      Mason          Alan          90 Eastgate       Ada                 OK74820


                    (a) Field lengths fixed. Place blanks in the spaces where the phone number would go.




             Ames|John|123 Maple|Stillwater|OK|74075|377-1808|
            Mason|Alan|90 Eastgate|Ada|OK|74820||


                    (b) Delimeters are used to indicate the end of a field. Place the delimeter for the "empty"field
                immediately after the delimiter for the previous field.



                Ames|...|Stillwater|OK|74075|377-1808|#Mason|... 90Eastgate|Ada|OK|74820|#...



                     (c) Place the field for business phone at the end of the record. If the end-of-record mark is encountered,
             assume that the field is missing.



                      SURNAME=Ames|FIRSTNAME=John|STREET=123 Maple|...|ZIP=74075|PHONE=377-1808|#...



                     (d) Use a keyword to identify each field each field. If the ketword is missing, the corresponding field is
            assumed to missing.



    File Structures                                        SNU-OOPSLA Lab.                                                        7
             RW files with Field Concept
      Extraction operator for delimited fields into a Person object
istream & operator >> (istream & stream, Person & p)
{ // read delimited fields from file
   char delim;
   stream.getline(p.LastName, 30, ‘|’);
   if (strlen(p.LastName) == 0) return stream;
   stream.getline(p.FirstName,30,’|’);
   stream.getline(p.Address,30,’|’);
   …..
   Return stream;}

** By    D.5 (pp588) 과 D.7 (pp590)

Last Name      ‘Ames’
First Name     ‘Mary’
Address        ‘123 Maple’
……….
Last Name     ‘Mason’
First Name    ‘Alan’
……...




    File Structures                         SNU-OOPSLA Lab.            8
4.1 Field and Record Organization

             Record Organization
    Record: a set of fields that belong together
    Record organization(5 methods)
         Make records a predictable number of bytes
                       (Fixed-length records)        Fig4.5. (a)(b)
         Make records a predictable number of fields Fig4.5. (c)
         Begin each record with a length indicator Fig4.6. (a)
         Use an index to keep track of addresses Fig4.6. (b)
         Place a delimiter at the end of each record Fig4.6. (c)




     File Structures               SNU-OOPSLA Lab.                    9
4.1 Field and Record Organization

                     The method for organizing records (1)
       Three ways of making the lengths of records constant and predictable
            Fixed-length record w/ fixed-length fields
            Fixed-length record w/ variable-length fields
            Six fields per record


           Ames            John          123        Maple      Stillwater        OK74075


           Mason           Alan          90         Eastgate   Ada                 OK74820


                                              (a)


           Ames|John|123 Maple|Stillwater|OK|74075|               Unused space


           Mason|Alan|90 Eastgate|Ada|OK|74820|                 Unused space


                                              (b)


Ames|John|123 Maple|Stillwater|OK|74075| Mason|Alan|90 Eastgate|Ada|OK| . . . .


                                              (c)

       File Structures                   SNU-OOPSLA Lab.
                                                                        Fig. 4.5             10
4.1 Field and Record Organization

                     The method for organizing records (2)
                          Record structure for variable record
                                  with a length indicator
                                  using a index file
                                  with delimiter(#)


            Ames|John|123 Maple|Stillwater|OK|74075|Mason|Alan|90 Eastgate . . .


                                                   (a)


        Data file:        Ames|John|123 Maple|Stillwater|OK|74075|Mason|Alan . . .




        Index file:       00   40 . . .

                                                   (b)


            Ames|John|123 Maple|Stillwater|OK|74075|#Mason|Alan|90 Eastgate|Ada|OK . . .


                                                   (c)
   File Structures                           SNU-OOPSLA Lab.             Fig. 4.6      11
Write a var-length delimited buffer to a file
(from memory to disk)
Const int MaxBUfferSize = 200;
int WritePerson(ostream & stream, Person & p)
{char buffer [MaxBufferSize];
strcpy(buffer, p.LastName); strcat(buffer, “l”);
strcpy(buffer, p.FistName); strcat(buffer, “I”);
…..
Strcpy(buffer,p.Zipcode); strcat(buffer, “l”);
short length=strlen(buffer);
stream.write (&length, sizeof(length));
stream.write(&buffer, length)
}
Figure 4.7 (pp 129)


   File Structures            SNU-OOPSLA Lab.      12
 Reading Variable Records
  Records preceded by lengths (variable length records)
40 Ames|Mary|123 Maple|Stillwater|OK|74075|
36 Mason|Alan|90 Eastgate|Ada|OK|74820

int ReadVariablePerson (istream & stream, Person & p)
{ // read a variable sized record from stream and store it in p
short length;
stream . Read (&length, sizeof(lenth));
char * buffer = new char[length + 1]; // create a buffer space
stream . Read (buffer, length);
buffer [ length] = 0; // treminate buffer with null
istrstream strbuff (buffer); // create a string stream
strbuff >> p; // use the istream extraction operator
return 1;
}



    File Structures                   SNU-OOPSLA Lab.             13
4.1 Field and Record Organization

                        Read-file using File Dump
      File-dump gives us the ability to look inside a file at the actual
       bytes that are stored
      Octal Dump: od -xc filename
      e.g. The number 40, stored as ASCII characters and as a short
       integer

                                      Decimal value of       Hex value stored       ASCII
                                          number                 in bytes       character form

(a) 40 stored as ASCII chars:               40                     34 30          '4'      '0'




 (b) 40 stored as a 2-byte integer:         40                     00 28          '\0'     "("




      File Structures                            SNU-OOPSLA Lab.                                 14
4.2 Using Classes to Manipulate Buffers


  Using Classes to Manipulate Buffers
       Examples of three C++ classes to encapsulate
        operation of buffer object
            Function : Pack, Unpack, Read, Write
                  Output: pack into a buffer & write a buffer to a file
                  Input: read into a buffer from a file & unpack a buffer
                  ‘pack and unpack’ deals with only one field
            DelimTextBuffer class for delimited fields
            LengthTextBuffer class for length-based fields
            FixedTextBuffer class for fixed-length fields
       Appendix E : Full implementation




       File Structures                 SNU-OOPSLA Lab.                       15
4.2 Using Classes to Manipulate Buffers

  Buffer Class for Delimited Text Fields(1)
    Variable-length buffer
    Fields are represented as delimited text

             Class DelimTextBuffer
             { public:
                  DelimTextBuffer (char Delim = ‘|’, int maxBtytes = 1000);
                  int Read(istream & file);
                  int Write (ostream & file) const;
                  int Pack(const char * str, int size = -1);
                  int Unpack(char * str);
              private:
                  char Delim;         // delimiter character
                  char * Buffer;      // character array to hold field values
                  int BufferSize;     // current size of packed fields
                  int MaxBytes;       // maximum # of characters in the buffer
                  int NextByte;       // packing/unpacking position in buffer
             };


     File Structures                  SNU-OOPSLA Lab.                            16
4.2 Using Classes to Manipulate Buffers

  Buffer Class for Delimited Text Fields(2)
    int DelimTextBuffer::Unpack(char *str)
    // extract the value of the next field of the buffer
    {
        int len = -1;     // length of packed string
        int start = NextByte; // first character to be unpacked
        for(int i = start; i < BufferSize; i++)
               if(Buffer[i] == Delim)
                          {len = i-start; break;}
        if(len == -1) return FALSE;           // delimiter not found
        NextByte += len + 1;
        if(NextByte > BufferSize) return FALSE;
        strncpy (str, &Buffer[start], len);
        str[len] = 0;     // zero termination for string
        return TRUE;
    }
    Unpack() is extracking one field from a record in a buffer.
    Pack() method copies the characters of its argument to the buffer and then
       adds the delimiter characters.



     File Structures                   SNU-OOPSLA Lab.                       17
4.2 Using Classes to Manipulate Buffers


    Buffer Class for Delimited Text Fields(3)
     Read method of DelimTextBuffer
           Clears the current buffer contents
           Extracts the record size
           Read the proper number of bytes into buffer
           Set the buffer size
    int DelimTextBuffer::Read(istream & stream)
    {
        Clear();
        stream.read((char *)&BufferSize, sizeof(BufferSize));
        if (Stream.fail()) return FALSE;
        if (BubberSize > MaxBytes) return FALSE; // buffer overflow
        stream.read(Buffer, BufferSize);
        return stream.good();
    }




     File Structures               SNU-OOPSLA Lab.                    18
4.2 Using Classes to Manipulate Buffers

Extending Class Person with Buffer Operations
   class Person{public:
       char lastname[11]; char firstname[11];
             …
       char zipcode[10];
   // method
       …
       int Pack(DelimTextBuffer &buf) const; // buffer operation Pack
       ...
   }
   int Person::Pack(DelimTextBuffer &buf) const
   { // pack the fields into a DelimTextBuffer
        int result;
        result = buf.Pack(lastname);
        result = result && buf.Pack(firstname);
        …
        return result = result && buf.Pack(zipcode);
   }
   * pack deals with only one field!


      File Structures                  SNU-OOPSLA Lab.                  19
Buffer Class for Delimitted Text Field(Reminder)
       Class DelimTextBuffer
       { public:
          DelimTextBuffer (char Delim = ‘|’, int maxBtytes = 1000);
          int Read(istream & file);
          int Write (ostream & file) const;
          int Pack(const char * str, int size = -1);
          int Unpack(char * str);
        private:
          char Delim;         // delimiter character
          char * Buffer;      // character array to hold field values
          int BufferSize;     // current size of packed fields
          int MaxBytes;       // maximum # of characters in the buffer
          int NextByte;       // packing/unpacking position in buffer
       };




    File Structures               SNU-OOPSLA Lab.                        20
4.2 Using Classes to Manipulate Buffers

 Buffer Classes for Length-Based Fields
       Almost same as the delimited field class (compare with the
        previous page)
       Change in the implementations of the Pack and Unpack

   class LengthTextBuffer
   { public:
       LengthTextBuffer(int maxBytes = 1000);
       int Read(istream & file);
       int Write(ostream & file) const;
       int Pack(const char * field, int size = -1);
       int Unpack(char * field);
   private:
       char * Buffer; // character array to hold field values
       int BufferSize; // size of packed fields
       int MaxBytyes; // maximum # of characters in the buffer
       int NextByte;      // packing/unpacking position in buffer
   };




       File Structures                  SNU-OOPSLA Lab.              21
4.2 Using Classes to Manipulate Buffers

  Buffer Classes for Fixed-length Fields

   Class FixedTextBuffer
   { public:
       FixedTextBuffer (int maxBytes = 1000);
       int AddField (int fieldSize);
       int Read(isteram * file);
       int Write(ostream *file) const;
       int Pack(const char * field);
       int Unpack (char * field);
   private:
       char * Buffer; // character array to hold field values
       int BufferSize; // size of packed fields
       int MaxBytes; // Max # of chars in the buffer
       int NextByte; // packing/unpacking position in buffer
       int * FieldSizes; // array of field sizes
   }




      File Structures                SNU-OOPSLA Lab.            22
4.3 Using Inheritance for Record Buffer Classes

  Inheritance in the C++ Stream Classes
    class istream: virtual public ios { …
    class ostream: virtual public ios { …
    class iostream: virtual istream, public ostream { …
    class ifstream: public fstreambase, public istream { …
    class ostream: public fstreambase, public ostream {…
    class fstream: public fstreambase, public iostream { …



       Operations that work on base class objects also work
        on derived class objects




     File Structures               SNU-OOPSLA Lab.             23
4.3 Using Inheritance for Record Buffer Classes

 Class Hierarchy for Record Buffer Objects(1)
     Inheritance allows multiple classes share members
    and methods

                                                  IOBuffer
                                         char array for buffer value

                          VariableLengthBuffer                                FixedLengthBuffer
                       read and write operations                          read and write operations
                       for variable length records                         for fixed length records

        DelimitedFieldBuffer                LengthFieldBuffer                 FixedFieldBuffer
     pack and unpack operations        pack and unpack operations      pack and unpack operations for
         for delimited fields            for length- based fields             fixed sized fields

    Appendix F : full implementation

     File Structures                           SNU-OOPSLA Lab.                                          24
4.3 Using Inheritance for Record Buffer Classes

  Class Hierarchy for Record Buffer Objects(2)


     class IOBuffer
     { public:
         IOBuffer (int maxBytes = 1000); // a MAX of maxByte
         virtual int Read (istream &) = 0; // read a buffer
         virtual int Write (ostream &) = 0; // write a buffer
         virtual int Pack (const void * field, int size = -1) = 0;
         virtual int Unpack (void * field, int maxbytes = -1) = 0;
     protected:
         char * Bufffer; // character array to hold field values
         int BufferSize; // sum of the sizes of packed fields
         int MaxBytes; // MAX # of characters in the buffer
     };




     File Structures                 SNU-OOPSLA Lab.                 25
4.3 Using Inheritance for Record Buffer Classes

 Class Hierarchy for Record Buffer Objects(3)
   Class VariableLengthBuffer: public IOBuffer
   { public:
       VariableLengthBuffer (int MaxBytes = 1000);
       int Read (istream &);
       int Write (ostream &) const;
       int SizeOfBuffer () const; // return current size of buffer
   };


   class DelimFieldBuffer: public VariableLengthBuffer
   { public:
       DelimFieldBuffer (char Delim = -1, int maxBytes = 1000);
       int Pack (const void *, int size = -1);
       int Unpack (void *field, int maxBytes = -1);
   protected:
       char Delim;
   };



     File Structures                  SNU-OOPSLA Lab.                26
4.4 Managing Fixed-Length, Fixed_Field Buffers

Managing Fixed-Length, Fixed-Field Buffers

   class FixedFieldBuffer: public FixedLengthBuffer
   { public:
       FixedFieldBuffer (int maxFields, int RecordSzie = 1000);
       FixedFieldBuffer (int maxFields, int *fieldSize);
       int AddField (int fieldSize); // define the next field
       int Pack(const void * field, int size = -1);
       int Unpack(void * field, int maxBytes = -1);
       int NumberOfFields () const; // return # of defined fields
   protected:
       int * FieldSzie; // array to hold field sizes
       int MaxFields; // MAX # of fields
       int NumFields; // actual # of defined fields
   };




      File Structures                SNU-OOPSLA Lab.                27
4.5 An Object-Oriented Class for Record Files

 Object-Oriented Class for Record Files
       So far, we defined buffer classes
       Now, we encapsulate all of our file operations!
   class BufferFile // file with buffers
   { public:
       BufferFile (IOBuffer &); // create with a buffer
       int Open(char * fname, int MODE); // open an existing file
       int Create (char * fname, int MODE); // create a new file
       int Close();
       int Rewind();         // reset to the first data record
       // Input and Output operations
       int Read(int recaddr = -1);
       int Write(int recaddr = -1);
       int Append(); // write the current buffer at the end of file
   protected:
       IOBuffer & Buffer; // reference to the file’s buffer
       fstream File;         // the C++ stream of the file
   };
   Usage: DelimFieldBuffer buffer;
          BufferFile file(buffer);
          file.open(myfile);
          file.Read();
          buffer.Unpack(myobject);




       File Structures                        SNU-OOPSLA Lab.         28
Contents


                       Let’s Review!!!
  4.1 Field and Record Organization
  4.2 Using Classes to Manipulate Buffers
  4.3 Using Inheritance for Record Buffer Classes
  4.4 Managing Fixed-Length, Fixed-Field Buffers
  4.5 An Object-Oriented Class for Record Files




     File Structures        SNU-OOPSLA Lab.         29

								
To top