home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a006 / 1.ddi / PASSAMP.ZIP / IMPORT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-03-11  |  14.7 KB  |  554 lines

  1. {$N+,E+,V-}
  2. (* Copyright (c) 1990 by Borland International, Inc. *)
  3. program Import;
  4.  
  5. uses
  6.   PXEngine;
  7. (*
  8.  *  IMPORT.PAS
  9.  *  File Translation Example
  10.  *
  11.  *  Description:
  12.  *    This example program demonstrates how to translate
  13.  *    an ASCII file into an indexed Paradox File.
  14.  *
  15.  *  ASCII Files are expected to be in the following format
  16.  *    Customer Number:     8 characters
  17.  *    Part Number:         8 characters
  18.  *    Quantity:            Double
  19.  *    Date:                10 characters, format MM/DD/YYYY
  20.  *
  21.  *    Each field within the ASCII record should be separated by
  22.  *    at least one space.
  23.  *
  24.  *    Each record within the ASCII file should be separated by
  25.  *    a newline.
  26.  *
  27.  *  Compilation:
  28.  *    To compile and link the example program, make sure that
  29.  *    your Turbo Pascal (version 5.5 or 6.0) compiler has
  30.  *    been correctly installed.
  31.  *
  32.  *  Execution:
  33.  *    To run the example program enter the following command:
  34.  *    IMPORT <AsciiFile> <ParadoxFile>
  35.  *    Where:
  36.  *    <AsciiFile>      is the name of an existing ASCII input file
  37.  *
  38.  *    <ParadoxFile>    is the name of a paradox file that will be created
  39.  *                     by the example program.  Note that the example
  40.  *                     program checks to make sure that this file does
  41.  *                     not yet exist.
  42.  *
  43.  *
  44.  *  If you are running on a network, uncomment the following
  45.  *  compiler statement and check the network-related variables,
  46.  *  such as NetUserName, NetPath, and NetType.
  47.  *)
  48.   (* {$DEFINE NETWORK } *)
  49.  
  50. const
  51.   {$IFDEF NETWORK }
  52.   NetUserName = 'username';             (* network username *)
  53.   NetPath = '';                         (* net directory *)
  54.   NetType = NOTONNET;                   (* network type *)
  55.   {$ENDIF }
  56.   (* Field numbers of Paradox Table Fields *)
  57.   (* Sizes of ASCII table fields *)
  58.   ASCIISIZECUSTNBR = 8;                 (* Customer Number Length *)
  59.   ASCIISIZEPARTNBR = 8;                 (* Part Number Length *)
  60.   ASCIISIZEDATE = 10;                   (* Date Length *)
  61.   (* Function Returns *)
  62.   SUCCESS = True;                       (* function succeeded *)
  63.   FAILURE = False;                      (* function failed *)
  64.  
  65.   NBRFIELDS = 4;                        (* number of fields in one table record *)
  66.   NBRKEYS = 2;                          (* number of fields for primary index *)
  67.  
  68.   FieldNames: array[1..NBRFIELDS] of NameString =
  69.     ('Part Number', 'Cust Number', 'Quantity','Date');
  70.   FieldTypes: array[1..NBRFIELDS] of NameString = ('A8', 'A8', 'N', 'D');
  71.  
  72. type
  73.   (* Structure for an ASCII fixed-length record *)
  74.   TAsciiRecord =
  75.     record
  76.       CustNbr: string[ASCIISIZECUSTNBR]; (* customer number *)
  77.       PartNbr: string[ASCIISIZEPARTNBR]; (* part number *)
  78.       Quantity: Double;                 (* quantity *)
  79.       Date: string[ASCIISIZEDATE];     (* date *)
  80.     end;
  81.  
  82.  
  83. var
  84.   F: Text;                              (* ASCII file *)
  85.   TblHandle:   TableHandle;             (* table handle for paradox table *)
  86.   Keys:        FieldHandleArray;
  87.   FieldPartNbr,                         (* Part Number *)
  88.   FieldCustNbr,                         (* Customer Number *)
  89.   FieldQuantity,                        (* Quantity *)
  90.   FieldDate:   FieldHandle;             (* Date *)
  91.  
  92.  
  93.  
  94. (*
  95.  *    Procedure:
  96.  *          Strip
  97.  *
  98.  *    Arguments:
  99.  *          S       String to be stripped of white space.
  100.  *
  101.  *    Description:
  102.  *          Strips a string of leading and trailing white space.
  103.  *)
  104. procedure Strip(var S: String );
  105.  
  106.   var
  107.     L1, L2: Byte;
  108.  
  109.   begin
  110.     L1 := 1;
  111.     while (L1 < Length(S)) and (S[L1] in [#9..#13, ' ']) do
  112.       Inc(L1);
  113.     L2 := Length(S);
  114.     while (L2 > 0) and (S[L2] in [#9..#13, ' ']) do
  115.       Dec(L2);
  116.     S := Copy(S, L1, L2 - L1 + 1);
  117.   end; (* Strip *)
  118.  
  119.  
  120. (*
  121.  *    Function:
  122.  *      Error
  123.  *
  124.  *    Arguments:
  125.  *        RC                  return code from a PX... function
  126.  *
  127.  *    Description:
  128.  *        Writes error message if an error has occurred.
  129.  *
  130.  *    Returns:
  131.  *        True if Error
  132.  *        else False
  133.  *)
  134. function Error(RC: Integer): Boolean;
  135.  
  136.   begin
  137.     if RC <> PXSUCCESS then
  138.       WriteLn('IMPORT: ', PXErrMsg(RC));
  139.     Error := RC <> PXSUCCESS;
  140.   end; (* Error *)
  141.  
  142.  
  143. (*
  144.  *    Function:
  145.  *        OpenAsciiFile
  146.  *
  147.  *    Arguments:
  148.  *        FileName            ASCII input file
  149.  *        F                   Text file
  150.  *
  151.  *    Description:
  152.  *        Attemps to open the ASCII file for reading
  153.  *
  154.  *    Returns:
  155.  *        SUCCESS             File was opened
  156.  *        FAILURE             Could not open file
  157.  *)
  158.  
  159. function OpenAsciiFile(FileName: String ;
  160.                        var F: Text): Boolean;
  161.  
  162.   begin
  163.     Assign(F, FileName);
  164. {$I-}
  165.     Reset(F);
  166. {$I+}
  167.     if IoResult <> 0 then
  168.       begin
  169.         WriteLn('Can''t open file : ', FileName);
  170.         OpenAsciiFile := FAILURE;
  171.       end
  172.     else
  173.       OpenAsciiFile := SUCCESS;
  174.   end; (* OpenAsciiFile *)
  175.  
  176. (*    Function:
  177.  *        InitFields
  178.  *
  179.  *    Arguments:
  180.  *        TblHandle       Paradox Table Handle
  181.  *
  182.  *    Description:
  183.  *        InitFields initializes global variables representing the correct
  184.  *        field handles for the table's fields.
  185.  *
  186.  *    Returns:
  187.  *        SUCCESS   If all field handles were initialized
  188.  *        FAILURE   If any of the field handles was not initialized
  189.  *
  190.  *)
  191. function InitFields(TblHandle: TABLEHANDLE):  boolean;
  192.  
  193.   begin
  194.  
  195.     InitFields := FAILURE;   (* default value for easy exit *)
  196.     if Error(PXFldHandle(tblHandle,'Part Number',FieldPartNbr)) then
  197.       Exit;
  198.     if Error(PXFldHandle(tblHandle,'Cust Number',FieldCustNbr)) then
  199.       Exit;
  200.     if Error(PXFldHandle(tblHandle,'Quantity',FieldQuantity)) then
  201.       Exit;
  202.     if Error(PXFldHandle(tblHandle,'Date',FieldDate)) then
  203.       Exit;
  204.   end;
  205.  
  206.  
  207. (*
  208.  *    Function:
  209.  *        OpenFiles
  210.  *
  211.  *    Arguments:
  212.  *        TblHandle       Paradox Table Handle
  213.  *        F               text file
  214.  *
  215.  *    Description:
  216.  *        OpenFiles verifies the existence of two command line
  217.  *        arguments expected to be file names and then attempts to
  218.  *        open the ASCII files as well as the Paradox file.
  219.  *
  220.  *    Returns:
  221.  *        SUCCESS         Files opened
  222.  *        FAILURE         Error has occurred
  223.  *
  224.  *)
  225. function OpenFiles(var TblHandle: TableHandle;
  226.                    var F: Text): Boolean;
  227.  
  228.   begin
  229.     if OpenAsciiFile(ParamStr(1), F) = FAILURE then
  230.       OpenFiles := FAILURE
  231.     else (* Open the Paradox file *)
  232.       if Error(PXTblOpen(ParamStr(2), TblHandle, 0, False)) then
  233.         OpenFiles := FAILURE
  234.       else
  235.       begin
  236.         OpenFiles := SUCCESS;
  237.         if InitFields(TblHandle) = FAILURE then
  238.           OpenFiles := FAILURE;
  239.       end;
  240.   end; (* OpenFiles *)
  241.  
  242.  
  243. (*
  244.  *    Function:
  245.  *        CreateParadoxFile
  246.  *
  247.  *    Arguments:
  248.  *        FileName            Paradox file names
  249.  *
  250.  *    Description:
  251.  *        CreateParadoxFile has three steps:
  252.  *            1. Make sure the file doesn't already exist.
  253.  *            2. Create the file
  254.  *            3. Create a primary index file
  255.  *
  256.  *    Returns:
  257.  *        FAILURE             Error has occurred
  258.  *        SUCCESS             File create successful
  259.  *)
  260. function CreateParadoxFile(FileName: String ): Boolean;
  261.  
  262.   var
  263.     Exist: Boolean;
  264.  
  265.   begin
  266.     (* Do not create if it already exists *)
  267.     if Error(PXTblExist(FileName, Exist)) then
  268.       CreateParadoxFile := FAILURE
  269.     else if Exist then
  270.       begin
  271.         WriteLn('IMPORT: Table ', FileName, ' already exists');
  272.         CreateParadoxFile := FAILURE
  273.       end
  274.     else (* Now attempt to create the table *)
  275.     if Error(PXTblCreate(FileName, NBRFIELDS,@ FieldNames,@ FieldTypes)) then
  276.       CreateParadoxFile := FAILURE
  277.     else (* Add first two fields as primary key *)
  278.       begin
  279.         Keys[1] := FieldPartNbr;
  280.         Keys[2] := FieldCustNbr;
  281.         if Error(PXKeyAdd(FileName, NBRKEYS, Keys, PRIMARY)) then
  282.           CreateParadoxFile := FAILURE
  283.         else
  284.           CreateParadoxFile := SUCCESS;
  285.       end;
  286.   end; (* CreateParadoxFile *)
  287.  
  288.  
  289. (*
  290.  *    Function:
  291.  *        TranslateBuffer
  292.  *
  293.  *    Arguments:
  294.  *        Buf        AsciiRecord buffer
  295.  *        RecHandle  Paradox Record Handle
  296.  *
  297.  *    Description:
  298.  *        Translates a single ASCII table record into a Paradox
  299.  *        record buffer
  300.  *
  301.  *    Returns:
  302.  *        SUCCESS                 Translation successful
  303.  *        FAILURE                 Translation failed
  304.  *)
  305. function TranslateBuffer(var Buf: TAsciiRecord;
  306.                          RecHandle: RecordHandle): Boolean;
  307.  
  308.   var
  309.     PXDate: TDate;
  310.     Month, Day, Year: Integer;
  311.  
  312.  
  313.  
  314.   function GetNextWVal(var S: String ): Word;
  315.  
  316.     const
  317.       Delim = '/';
  318.  
  319.     var
  320.       L: Byte;
  321.       Help: Word;
  322.       Code: Integer;
  323.  
  324.     begin
  325.       L := Pos(Delim, S);
  326.       if L = 0 then
  327.         L := Length(S) + 1;
  328.       Val(Copy(S, 1, L - 1), Help, Code);
  329.       S := Copy(S, L + 1, Length(S));
  330.       if Code = 0 then
  331.         GetNextWVal := Help
  332.       else
  333.         GetNextWVal := 0;
  334.     end; (* GetNextWVal *)
  335.   begin
  336.  
  337.     TranslateBuffer := FAILURE; (* default value for easy exit *)
  338.  
  339.     (* First the Customer Number *)
  340.     if Error(PXPutAlpha(RecHandle, FieldCustNbr, Buf.CustNbr)) then
  341.       Exit;
  342.  
  343.     (* Next the Part Number *)
  344.     if Error(PXPutAlpha(RecHandle, FieldPartNbr, Buf.PartNbr)) then
  345.       Exit;
  346.  
  347.     (* Quantity *)
  348.     if Error(PXPutDoub(RecHandle, FieldQuantity, Buf.Quantity)) then
  349.       Exit;
  350.  
  351.     (* To translate the date, first get month, day, and year from buffer
  352.        then use PXEncode to translate into a Paradox date format. *)
  353.  
  354.     Month := GetNextWVal(Buf.Date);
  355.     Day := GetNextWVal(Buf.Date);
  356.     Year := GetNextWVal(Buf.Date);
  357.     if Error(PXDateEncode(Month, Day, Year, PXDate)) then
  358.       Exit;
  359.  
  360.  
  361.     (* Now put the date into the record buffer *)
  362.     if Error(PXPutDate(RecHandle, FieldDate, PXDate)) then
  363.       Exit;
  364.     TranslateBuffer := SUCCESS;
  365.  
  366.   end; (* TranslateBuffer *)
  367.  
  368.  
  369. (*
  370.  *    Function:
  371.  *        Translate
  372.  *
  373.  *    Arguments:
  374.  *        TblHandle       Handle to a Paradox table
  375.  *        F               ASCII input file
  376.  *
  377.  *    Description:
  378.  *        translate is the translation driver.    While there is still
  379.  *        data available in the ASCII input file, it reads records from
  380.  *        it, translates them into its Paradox counterpart and writes
  381.  *        the translated record out to the Paradox table
  382.  *
  383.  *    Returns:
  384.  *        SUCCESS         Translation successful
  385.  *        FAILURE         Error in translation
  386.  *)
  387.  
  388. function Translate(TblHandle: TableHandle;
  389.                    var F: Text): Boolean;
  390.  
  391.   var
  392.     AsciiBuf: TAsciiRecord;
  393.     RecHandle: RecordHandle;
  394.     StrBuf: String;
  395.  
  396.  
  397.   function Split4(S: String ;
  398.                   var S1, S2: String ;
  399.                   var I1: Double;
  400.                   var S3: String ): Boolean;
  401.     (* extracts the first four values from a string separated by spaces
  402.      * these valuse are : 1. String
  403.      *                    2. String
  404.      *                    3. Double
  405.      *                    4. String
  406.      * returns False if it wasn't possible to get these values
  407.      *)
  408.  
  409.     var
  410.       Help: String ;
  411.       Code: Integer;
  412.  
  413.  
  414.  
  415.     function GetNextStr(var S: String ): String ;
  416.  
  417.       const
  418.         Delim = ' ';
  419.  
  420.       var
  421.         L: Byte;
  422.  
  423.       begin
  424.         Strip(S); (* remove leading and trailing blanks if any *)
  425.         L := Pos(Delim, S);
  426.         if L = 0 then
  427.           L := Length(S) + 1;
  428.         GetNextStr := Copy(S, 1, L - 1);
  429.         S := Copy(S, L + 1, Length(S));
  430.       end; (* GetNextStr *)
  431.     begin
  432.       S1 := '';
  433.       S2 := '';
  434.       S3 := '';
  435.       I1 := 0;
  436.       S1 := GetNextStr(S);
  437.       S2 := GetNextStr(S);
  438.       Help := GetNextStr(S);
  439.       Val(Help, I1, Code);
  440.       S3 := GetNextStr(S);
  441.       Split4 := (S3 <> '') and (Code = 0);
  442.     end; (* Split4 *)
  443.   begin
  444.     Translate := FAILURE;
  445.     (* Setup a record handle *)
  446.     if Error(PXRecBufOpen(TblHandle, RecHandle)) then
  447.       Translate := FAILURE
  448.     else
  449.       begin
  450.         (* Read records until end of ASCII file *)
  451.         while not Eof(F) do
  452.           begin
  453.           (* Read the buffer and make sure we do not encounter an unexpected
  454.              end-of-file *)
  455.             ReadLn(F, StrBuf);
  456.             if Split4(StrBuf, AsciiBuf.PartNbr, AsciiBuf.CustNbr,
  457.                       AsciiBuf.Quantity, AsciiBuf.Date)
  458.  
  459.  
  460.             (* only a complete record will be processed *)
  461.                then
  462.               begin
  463.                 if not TranslateBuffer(AsciiBuf, RecHandle) then
  464.                   Exit;
  465.                 (* And write it to the Paradox table *)
  466.                 if Error(PXRecAppend(TblHandle, RecHandle)) then
  467.                   Exit;
  468.               end; (* then *)
  469.  
  470.           end; (* while *)
  471.       end; (* else *)
  472.     (* File translated, close the record buffer *)
  473.  
  474.     if Error(PXRecBufClose(RecHandle)) then
  475.       Translate := FAILURE
  476.     else
  477.       Translate := SUCCESS;
  478.  
  479.   end; (* Translate *)
  480.  
  481.  
  482. (*
  483.  *    Procedure:
  484.  *        CloseFiles
  485.  *
  486.  *    Arguments:
  487.  *        None
  488.  *
  489.  *    Description:
  490.  *        Closes ASCII and Paradox files.    CloseFiles indicates any
  491.  *        problems by reporting an error condition
  492.  *
  493.  *    Returns:
  494.  *        None
  495.  *)
  496.  
  497. procedure CloseFiles(TblHandle: TableHandle;
  498.                      var F: Text);
  499.  
  500.   begin
  501. {$I-}
  502.     Close(F);
  503. {$I+}
  504.     if IoResult <> 0 then
  505.       WriteLn('cannot close ascii file');
  506.     if Error(PXTblClose(TblHandle)) then; (* ignore return code *)
  507.   end; (* CloseFiles *)
  508.  
  509.  
  510. (*
  511.  *  Main program
  512.  *
  513.  *  Description:
  514.  *    The following steps accomplish file translation:
  515.  *      1.    Initialize Engine specific variables
  516.  *      2.    Initialize the Engine
  517.  *      3.    Open the ASCII data file
  518.  *      4.    Create and open the Paradox File
  519.  *      5.    Translate the data
  520.  *      6.    Close All Files
  521.  *      7.    Exit the Engine
  522.  *
  523.  *  Returns:
  524.  *    Dos exitcode 0  : if IMPORT has done a good job
  525.  *                 1  : if IMPORT was unsuccessful
  526.  *)
  527. begin
  528.   (* Expect two filenames on the command line *)
  529.   if ParamCount <> 2 then
  530.     begin
  531.       WriteLn('usage: IMPORT <AsciiFile> <ParadoxFile>');
  532.       Halt(1);
  533.     end;
  534.   (* Initialize the Engine *)
  535.   {$IFNDEF NETWORK }
  536.   if Error( PXInit )
  537.   {$ELSE }
  538.   if Error(PXNetInit(NetPath, NetType, NetUserName))
  539.   {$ENDIF }
  540.      then
  541.     Halt(1);
  542.   if CreateParadoxFile(ParamStr(2)) = FAILURE then
  543.     Halt(1);
  544.   (* Open ASCII file and paradox file *)
  545.   if OpenFiles(TblHandle, F) = SUCCESS then
  546.     begin
  547.       if not Translate(TblHandle, F) then
  548.         WriteLn('can''t import');
  549.       CloseFiles(TblHandle, F);
  550.     end;
  551.   if Error(PXExit) then
  552.     Halt(1);
  553. end.
  554.