Site hosted by Angelfire.com: Build your free website today!
undefined
undefined

.

CGI scripts and xBase

First we need to understand what a Database is and how they work.
A Database is a collection of organized data. If you created a file with organized entrees.
You could consider it a Database. Hear is an example.

Rogers
Joe
83

Johns
mick
14

Al-through most people think of a Database in a larger since.

  • A way to enter the data. Spreadsheet, HTML forms, and Forms.
  • A way to retrieve the data.
  • A way to search the data.
  • Automatic backups of the Database file.
  • And if using in a Multiuser Environments. -- File locking.
    This is called a database system.
    A Database is a collection of organized data "The file".
    How you put the data in or retrieve it is up to you.

    We are going to use xBase 3 as an example. To write a simple database.
    That uses HTML form to enter the data.

    *So what is xBase?
    It is a database format derived from dBase ®.
    *So why use xBase with CGI scripts?
    Because databases are fast, efficient, and proven.
    We are going to write a raw database system.
    Called a Flat-file database.
    *So what is a Flat-file database?
    A flat-file database is a database stored in a single file that follow a uniform format.
    And are not structured for indexing or "recognizing relationships between records" (Relational database).

    *Purpose -- Is not to show you how to manage a database (Database administrator).
    It is to show you how they work, and how to write your own.
    Using CGI scripts and HTML forms with them.

    Order of study:
    1) What a Database is and how they work.
    2) Database terms.
    3) Basic xBase 3 file structure.
    4) Database system structure: Read and write.
    5) Create an empty mollified Xbase 3 file.
    6) Compile and test it.

    Part 2: Howto Create a HTML form and read CGI script.
    1) Install Apache 2 link.
    2) Setup Apache 2 to execute CGI scripts.
    3) Test Apache 2 setup.
    4) Wright a CGI script to Receive, transcribe, And display the form's input.
    5) POST and GET: What the difference.

    Part 3: Howto add data to the Xbase 3 file.
    1)

    Fist you need to know a few terms:


    xBase file like all files start with a header. xBase header has two parts.
    The part that describes the xBase file. And the part that describes the columns.

    The file header part (DB_HEADER):

  • xBase version.
  • Whether there is a memo fields.
  • Year, Month, and Day last modified.
  • Number of records.
  • Offset to first record.
  • Each record length.
  • And a reserved fields.
  • This is a xBase 3 file header description.
    Byte Contents Description
    0 1 byte Valid dBASE III PLUS table file (03h without a memo 83h with a memo).
    1-3 3 bytes Date of last update; in YYMMDD format. Like 210415 == April, 15, 2021
    4-7 4 bytes Number of records in the table. (Rows in table) also used as a ID.
    8-9 2 bytes Number of bytes in the header. (32 bytes + column * 32 + terminator 1) data start.
    10-11 2 bytes Number of bytes in the record.
    12-31 20 bytes Reserved
    Side note: Having reserved bytes in a header was a common practise.
    It allowed for future expansion.

    This is xBase version 3. From ver 3 to 4 is similar.
    Each newer version add something to header;
    By version 7. The file and column headers had more then double.
    This structure (struct) is used to describe the xBase file.
    Version 3++ (03h without a memo, 83h with a memo). .dbt memo file.
    #pragma pack(1)
    
    typedef struct {
      unsigned int   Ver;             
      unsigned int   Year_m;           //Year last modified. 
      unsigned int   Month_m;          //Month last modified.
      unsigned int   Day_m;            //Day last modified.
      long int       Number_records;   //Number of records. And used as a ID number.
      ///File header (32bytes) + (32bytes * Number of columns) + (1byte 0Dh field terminator.)
      short int      R_offset;         //Offset to first record.
      short int      R_length;         //Each record length.
      unsigned int   Reserved[20];     //Do not use.
    } DB_HEADER;    
    

    The secound decribes the column or field (32bytes) each.
    char column_name[11] //Name of the column, upto 10 charters. In ver 3 and 4.
    So if you have 3 columns. You have 3 DB_COLUMN structs.
    typedef struct {
      char           column_name[11]  //Name of the column, upto 10 charters and terminator '\0'.
      char           char_type;       //The type of data in the column. N,C,L,M,D,F, OR P.
      long           res_field;       //Not used by most databases. 
      //The column length + 1 (delete byte) is the R_length. in the DB_HEADER.       
      unsigned char  Col_length;      //Column length. Not including the delete byte. 
      //The format of the number in the column. 0 = integer, if > 0 = floating point.
      unsigned char  decimalPlace;    
      char           Reserved[14];    //Do not use.
    } DB_COLUMN;
    
    In 1984 dBase 3 was grate. Provably the first really usable databases.
    Remember that in 1984.
    Computer's memory, hard drive space and processor power was limited.
    The DOS operating system had a 640K limit. That way you packed to 1 byte boundary.
    But not new. We going to use xBase 3 as a reference. Not as a template.


    This is the file header.


    This is a column template.


    Col_type is defined with 1 character. N, C, L, M, D, F, AND P.
    Number, character, logical, memo, date, float, or picture.
    After the column headers. There a Field (column) Descriptor terminator (0x0D).
    The record data starts directly after the field descriptor terminator.

    *Then the data starts. Records.
    Before each record is a delete byte. Witch is not part of the record.
    A space ' ' not deleted,and an asterisk '*' if the record is deleted.
    We are not going to use. The Pre-compiler detective #pragma pack(1).
    Most compiler's CPU, today, pad there structs to division of 4. It is more efficient.
    Because of the way modern CPU are structured.

    This is xBase 3 file structure

    New we are going to switch gears. And talk about CGI scripts.

    Lets write a CGI script.

    What does the CGI script need to do?

    To write.
  • Check to see if the write file is open. Being used by an other user.
    If so. Loop tell it is not open or it times out.
  • After you open the write file.
    Add the lock marker.
    Add new data to end of file (append) and close it.
    At intervals, make a backup of the file.
  • Remove the lock marker.
    Send a successful message. or Send a unsuccessful message.

    To read.
  • Check to see if the read file is open. May ot be needed!!!
    If so. Loop tell it is not open or it times out.
  • After you open the read file.
    Add the lock marker.
    Read it (read only) and close it.
  • Remove the lock marker.
    Send a copy successful message. or Send a copy unsuccessful message.

    This is the main.h file. We use the main.h for all the c++ files.
    main.h



    To append to a database file. We need a database file.
    This file create an empty database. File without records.
    main.cpp

    Coding

    In codeblocks. Create a console project called CreateDbFile.
    In codeblocks File > New > File... > choose C/C++ header > Go button.
    In the C/C++ header window.
    Click the small square box next to "Filename with full path:".
    In the Select Filename window > File name: type main.h and save.
    New your back to the C/C++ header window.
    Under the Debug / Release box choose the All button.
    Finish button.

    In the main.h code box above.
    Right-click in the box and selects "Select All" and copy.
    In codeblocks Editor window, click on the main.h tab selected.
    Overwrite the main.h code. By pasting the code from the main.h code box above.
    Click the save button.

    New "Build and run" button.
    A console window will pop-up with Hello world! in it. Good.

    In the main.cpp code box above.
    Right-click in the box and selects "Select All" and copy.
    In codeblocks Editor window, click on the main.cpp tab selected.
    Overwrite the main.cpp code. By pasting the code from the main.cpp code box above.
    Click the save button.

    New "Build and run" button.
    There will be a file called MyData.dbf.
    This is your empty database file.

    If you wont to check you MyData.dbf file. And do not have DBase file viewer.
    You can download a free one at Windows Dbf Table Manager Download.
    And Codeblocks at Windows CODEBLOCKS IDE Download.
    Windows Apache 2 web server.
    Choose either 32Bit or 64Bit. DO NOT FORGET to download the runtime too (vc_redist_x64/86.exe).
    Do not download any modules.
    LINUX -- Use the Software manager that comes with your distribution (distro) to install Apache 2 web server.
    Howto compile Console apps.

    Part 2 Create a HTML form and read CGI script.
    Howto Create a HTML form and read CGI script.

    .