home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a006 / 1.ddi / CSAMP.ZIP / IMPORT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  11.7 KB  |  451 lines

  1. /* Copyright (c) 1990 by Borland International, Inc. */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include "pxengine.h"
  7.  
  8. /*
  9.  *  IMPORT.C
  10.  *  File Translation Example
  11.  *
  12.  *  Description:
  13.  *    This example program demonstrates how to translate
  14.  *    an ASCII file into an indexed Paradox file.
  15.  *
  16.  *  ASCII files are expected to be in the following format
  17.  *    Customer Number:     8 characters
  18.  *    Part Number:         8 characters
  19.  *    Quantity:            Integer
  20.  *    Date:                10 characters, format MM/DD/YYYY
  21.  *
  22.  *    Each field within the ASCII record should be separated by
  23.  *    a space.
  24.  *
  25.  *    Each record within the ASCII file should be separated by
  26.  *    a newline.
  27.  *
  28.  *  Compilation:
  29.  *    To compile and link the example program, make sure that
  30.  *    your C compiler has been correctly installed.    Compile
  31.  *    the program using the Large memory model specifications:
  32.  *    Turbo C:    Option -ml
  33.  *    MicroSoft:  Option /AL
  34.  *
  35.  *  Execution:
  36.  *    To run the example program enter the following command:
  37.  *    import <ascii_file> <paradox_file>
  38.  *    Where:
  39.  *    <ascii_file>     is the name of an existing ASCII input file
  40.  *
  41.  *    <paradox_file>   is the name of a paradox file that will be created
  42.  *             by the example program.  Note that the example
  43.  *             program checks to make sure that this file does
  44.  *             not yet exist.
  45.  */
  46.  
  47. /* If you are running on a network, uncomment the following statement
  48.  * and also check the network-specific information (such as NETPATH,
  49.  * NETUSERNAME, and NETTYPE)
  50. #define NETWORK */
  51.  
  52. #ifdef NETWORK
  53. #define NETUSERNAME    "username"    /* network username */
  54. #define NETPATH        ""            /* .net directory */
  55. #define NETTYPE        NOTONNET      /* network type */
  56. #endif
  57.  
  58. /* Field handles of Paradox table  */
  59. FIELDHANDLE FieldPartNbr,               /* Part Number */
  60.             FieldCustNbr,               /* Customer Number */
  61.             FieldQuantity,              /* Quantity */
  62.             FieldDate;                  /* Date */
  63.  
  64. /* Sizes of ASCII table fields */
  65. #define ASCIISIZECUSTNBR   9            /* Customer Number Length */
  66. #define ASCIISIZEPARTNBR   9            /* Part Number Length */
  67. #define ASCIISIZEDATE      11           /* Date Length */
  68.  
  69.  
  70. /* Function Returns */
  71. #define SUCCESS        0                /* function succeeded */
  72. #define FAIL          -1                /* function failed */
  73.  
  74. /* Structure for an ASCII fixed-length record */
  75. typedef struct
  76. {
  77.     char CustNbr[ASCIISIZECUSTNBR];     /* customer number */
  78.     char PartNbr[ASCIISIZEPARTNBR];     /* part number */
  79.     int quantity;                       /* quantity */
  80.     char date[ASCIISIZEDATE];           /* date */
  81. } AsciiRecord;
  82.  
  83. /* Field names of Paradox table */
  84. char *FieldNames[] =
  85. {
  86.     "Part Number",
  87.     "Cust Number",
  88.     "Quantity",
  89.     "Date"
  90. };
  91.  
  92. /* Field types of Paradox table */
  93. char *FieldTypes[] =
  94. {
  95.     "A8",
  96.     "A8",
  97.     "N",
  98.     "D"
  99. };
  100.  
  101. #define NBRFIELDS    (sizeof(FieldNames) / sizeof(char *))
  102. #define NBRKEYS      2
  103.  
  104.  
  105. /* Function Prototypes */
  106. int main(int, char**);
  107. int OpenFiles(char**,TABLEHANDLE *, FILE **);
  108. int OpenAsciiFile(char *,FILE **);
  109. int CreateParadoxFile(char *);
  110. int translate(TABLEHANDLE, FILE *);
  111. int TranslateBuffer(AsciiRecord *, RECORDHANDLE);
  112. void CloseFiles(TABLEHANDLE, FILE *);
  113. int InitFields(TABLEHANDLE);
  114. int Error(int);
  115.  
  116. /*
  117.  *  Function:
  118.  *    main
  119.  *
  120.  *  Arguments:
  121.  *    int argc            number of arguments on command line
  122.  *    char **argv         pointer to command line arguments
  123.  *
  124.  *  Description:
  125.  *    The following steps accomplish file translation:
  126.  *      1.    Initialize the Engine
  127.  *      2.    Open the ASCII data file
  128.  *      3.    Create and open the Paradox file
  129.  *      4.    Translate the data
  130.  *      5.    Close all files
  131.  *      6.    Exit the Engine
  132.  *
  133.  *  Returns:
  134.  *    None
  135.  */
  136.  
  137. int main(int argc, char **argv)
  138. {
  139.     FILE * fpAscii;                   /* file pointer to ASCII file */
  140.     TABLEHANDLE tblHandle;            /* table handle to paradox table */
  141.  
  142.     /* Expect two file names on the command line */
  143.     if (argc != 3)
  144.     {
  145.         printf("Usage: IMPORT <ascii_file> <paradox_file>\n");
  146.         return(FAIL);
  147.     }
  148.  
  149.     /* Initialize the Engine */
  150. #ifndef NETWORK
  151.     if (Error(PXInit()))
  152. #else
  153.     if (Error(PXNetInit(NETPATH, NETTYPE, NETUSERNAME)))
  154. #endif
  155.         exit(1);
  156.  
  157.  
  158.     if (CreateParadoxFile(argv[2]) == FAIL)
  159.         exit(1);
  160.  
  161.     /* Open ASCII file and Paradox file */
  162.  
  163.     if (OpenFiles(argv,&tblHandle,&fpAscii) == SUCCESS)
  164.     {
  165.         translate(tblHandle,fpAscii);
  166.         CloseFiles(tblHandle,fpAscii);
  167.     }
  168.  
  169.     return(Error(PXExit()));
  170. }
  171.  
  172.  
  173. /*
  174.  *    Function:
  175.  *        OpenFiles
  176.  *
  177.  *    Arguments:
  178.  *        argv            Pointer to command line arguments containing
  179.  *                        file names
  180.  *        tblHandlePtr    Pointer to a Paradox table handle
  181.  *        fpAscii         Pointer to a file pointer
  182.  *
  183.  *    Description:
  184.  *        OpenFiles verifies the existence of two command line
  185.  *        arguments expected to be file names and then attempts to
  186.  *        open the ASCII file as well as the Paradox file.
  187.  *
  188.  *    Returns:
  189.  *        SUCCESS         Files opened
  190.  *        FAIL            Error has occurred
  191.  *
  192.  */
  193. int OpenFiles(char **argv,TABLEHANDLE *tblHandlePtr, FILE **fpAscii)
  194. {
  195.  
  196.     if (OpenAsciiFile(argv[1],fpAscii) == FAIL)
  197.         return(FAIL);
  198.  
  199.     /* Open the Paradox file */
  200.     if (Error(PXTblOpen(argv[2],tblHandlePtr,0,0)))
  201.         return(FAIL);
  202.     if (InitFields(*tblHandlePtr) == FAIL)
  203.         return(FAIL);
  204.  
  205.     return(SUCCESS);
  206. }
  207.  
  208. /*
  209.  *    Function:
  210.  *        OpenAsciiFile
  211.  *
  212.  *    Arguments:
  213.  *        fileName            Pointer to ASCII input file
  214.  *        fpAscii             Pointer to a file pointer
  215.  *
  216.  *    Description:
  217.  *        Attempts to open the ASCII file for reading
  218.  *
  219.  *    Returns:
  220.  *        SUCCESS             File was opened
  221.  *        FAIL                Could not open file
  222.  */
  223. int OpenAsciiFile(char *fileName, FILE **fpAscii)
  224. {
  225.     if ((*fpAscii = fopen(fileName, "r")) == NULL)
  226.     {
  227.         perror(fileName);
  228.         return(FAIL);
  229.     }
  230.     else
  231.         return(SUCCESS);
  232. }
  233.  
  234. /*
  235.  *    Function:
  236.  *        CreateParadoxFile
  237.  *
  238.  *    Arguments:
  239.  *        fileName            Pointer to Paradox file names
  240.  *
  241.  *    Description:
  242.  *        CreateParadoxFile has three steps:
  243.  *            1. Make sure the file doesn't already exist
  244.  *            2. Create the file
  245.  *            3. Create a primary index file
  246.  *
  247.  *    Returns:
  248.  *        FAIL                Error has occurred
  249.  *        SUCCESS             File create successful
  250.  */
  251. int CreateParadoxFile(char *fileName)
  252. {
  253.     int exist,i;
  254.     FIELDHANDLE keys[NBRFIELDS];
  255.  
  256.     /* Do not create if it already exists */
  257.     if (Error(PXTblExist(fileName, &exist)))
  258.         return(FAIL);
  259.  
  260.     if (exist)
  261.     {
  262.         printf("IMPORT: Table %s already exists\n", fileName);
  263.         return(FAIL);
  264.     }
  265.  
  266.     /* Now attempt to create the table */
  267.     if (Error(PXTblCreate(fileName, NBRFIELDS, FieldNames, FieldTypes)))
  268.         return(FAIL);
  269.  
  270.     /* Add first two fields as primary key */
  271.     for (i=0;i<NBRKEYS;i++)
  272.       keys[i] = i + 1;
  273.     if (Error(PXKeyAdd(fileName, NBRKEYS, keys, PRIMARY)))
  274.         return(FAIL);
  275.  
  276.     return(SUCCESS);
  277. }
  278.  
  279. /*
  280.  *    Function:
  281.  *        translate
  282.  *
  283.  *    Arguments:
  284.  *        tblHandle       Handle to a Paradox table
  285.  *        fpAscii         File pointer to ASCII input file
  286.  *
  287.  *    Description:
  288.  *        translate is the translation driver.    While there is still
  289.  *        data available in the ASCII input file, it reads records from
  290.  *        it, translates them into its Paradox counterpart and writes
  291.  *        the translated record out to the Paradox table.
  292.  *
  293.  *    Returns:
  294.  *        SUCCESS         Translation successful
  295.  *        FAIL            Error in translation
  296.  */
  297. int translate(TABLEHANDLE tblHandle, FILE *fpAscii)
  298. {
  299.     AsciiRecord asciiBuf;
  300.     RECORDHANDLE recHandle;
  301.  
  302.     /* Setup a record handle */
  303.     if (Error(PXRecBufOpen(tblHandle, &recHandle)))
  304.         return(FAIL);
  305.  
  306.     /* Read records until end of ASCII file */
  307.     while (! feof(fpAscii))
  308.     {
  309.         /* Read the buffer and make sure we do not encounter an unexpected
  310.            end-of-file */
  311.         if (fscanf(fpAscii, "%s %s %d %s", asciiBuf.PartNbr, asciiBuf.CustNbr,
  312.         &asciiBuf.quantity, asciiBuf.date) != NBRFIELDS)
  313.             break;
  314.  
  315.         if (TranslateBuffer(&asciiBuf, recHandle) == FAIL)
  316.             return(FAIL);
  317.  
  318.         /* And write it to the Paradox table */
  319.         if (Error(PXRecAppend(tblHandle, recHandle)))
  320.             return(FAIL);
  321.     }
  322.  
  323.     /* File translated, close the record buffer */
  324.     if (Error(PXRecBufClose(recHandle)))
  325.         return(FAIL);
  326.  
  327.     return(SUCCESS);
  328. }
  329.  
  330.  
  331. /*
  332.  *    Function:
  333.  *      TranslateBuffer
  334.  *
  335.  *    Arguments:
  336.  *      buf        Pointer to an AsciiRecord buffer
  337.  *      recHandle  Paradox record handle
  338.  *
  339.  *    Description:
  340.  *        Translates a single ASCII table record into a Paradox
  341.  *        record buffer
  342.  *
  343.  *    Returns:
  344.  *        SUCCESS                 Translation successful
  345.  *        FAIL                    Translation failed
  346.  */
  347. int TranslateBuffer(AsciiRecord * buf, RECORDHANDLE recHandle)
  348. {
  349.     long PXDate;
  350.     int month, day, year;
  351.  
  352.     /* First the Customer Number */
  353.  
  354.     if (Error(PXPutAlpha(recHandle, FieldCustNbr, buf->CustNbr)))
  355.         return(FAIL);
  356.  
  357.     /* Next the Part Number */
  358.     if (Error(PXPutAlpha(recHandle, FieldPartNbr, buf->PartNbr)))
  359.         return(FAIL);
  360.  
  361.     /* Quantity */
  362.     if (Error(PXPutDoub(recHandle, FieldQuantity, (double) buf->quantity)))
  363.         return(FAIL);
  364.  
  365.     /* To translate the date, first get month, day, and year from buffer
  366.        then use PXEncode to translate into a Paradox date format. */
  367.     sscanf(buf->date, "%2d/%2d/%4d", &month, &day, &year);
  368.  
  369.     if (Error(PXDateEncode(month, day, year, &PXDate)))
  370.         return(FAIL);
  371.  
  372.     /* Now put the date into the record buffer */
  373.     if (Error(PXPutDate(recHandle, FieldDate, PXDate)))
  374.         return(FAIL);
  375.  
  376.     return(SUCCESS);
  377. }
  378.  
  379. /*
  380.  *    Function:
  381.  *      CloseFiles
  382.  *
  383.  *    Arguments:
  384.  *      None
  385.  *
  386.  *    Description:
  387.  *      Closes ASCII and Paradox files.    CloseFiles indicates any
  388.  *      problems by reporting an error condition
  389.  *
  390.  *    Returns:
  391.  *      None
  392.  */
  393. void CloseFiles(TABLEHANDLE tblHandle, FILE *fpAscii)
  394. {
  395.  
  396.     if (fclose(fpAscii) == EOF)
  397.         fprintf(stderr, "Cannot close ASCII file\n");
  398.  
  399.     Error(PXTblClose(tblHandle));
  400. }
  401.  
  402. /*
  403.  *    Function:
  404.  *      InitFields
  405.  *
  406.  *    Arguments:
  407.  *        tblHandle       Paradox table handle
  408.  *
  409.  *    Description:
  410.  *        Initializes global variables representing the correct field
  411.  *        handles for the corresponding fields.
  412.  *
  413.  *    Returns:
  414.  *      Current error code
  415.  */
  416. int InitFields(TABLEHANDLE tblHandle)
  417. {
  418.     if (Error(PXFldHandle(tblHandle,"Part Number",&FieldPartNbr)))
  419.       return(FAIL);
  420.     if (Error(PXFldHandle(tblHandle,"Cust Number",&FieldCustNbr)))
  421.       return(FAIL);
  422.     if (Error(PXFldHandle(tblHandle,"Quantity",&FieldQuantity)))
  423.       return(FAIL);
  424.     if (Error(PXFldHandle(tblHandle,"Date",&FieldDate)))
  425.       return(FAIL);
  426.  
  427.     return(SUCCESS);
  428. }
  429.  
  430. /*
  431.  *    Function:
  432.  *      Error
  433.  *
  434.  *    Arguments:
  435.  *        rc                  return code from a PX... function
  436.  *
  437.  *    Description:
  438.  *        Prints error message if an error has occurred.
  439.  *
  440.  *    Returns:
  441.  *      Current error code
  442.  */
  443. int Error(int rc)
  444. {
  445.  
  446.     if (rc != PXSUCCESS)
  447.       printf("IMPORT: %s\n",PXErrMsg(rc));
  448.  
  449.     return rc;
  450. }
  451.