home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / OPTPARAM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  11.6 KB  |  318 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // optparam.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "OPTPARAM";
  7. static const char szTblType[] = szPARADOX;
  8.  
  9. #define NAMELEN  10 // Length of the name field
  10.  
  11. // Field descriptor used in creating a table.
  12. static SNIPFAR FLDDesc fldDesc[] = {
  13.               { // Field 1 - AUTOINC
  14.                 1,            // Field number
  15.                 "NUMBER",     // Field name
  16.                 fldFLOAT,     // Field type
  17.                 fldUNKNOWN,   // Field subtype
  18.                 0,            // Field size (1 or 0, except
  19.                               //   BLOB or CHAR field)
  20.                 0,            // Decimal places   ( 0 )
  21.                               //   computed
  22.                 0,            // Offset in record ( 0 )
  23.                 0,            // Length in bytes  ( 0 )
  24.                 0,            // For NULL bits    ( 0 )
  25.                 fldvNOCHECKS, // Validity checks  ( 0 )
  26.                 fldrREADWRITE // Rights
  27.               },
  28.               { // Field 2 - ALPHA
  29.                 2, "ALPHA", fldZSTRING, fldUNKNOWN,
  30.                 NAMELEN, 0, 0, 0, 0,
  31.                 fldvNOCHECKS, fldrREADWRITE
  32.               }
  33.             }; // Array of field descriptors.
  34.  
  35. // The number of fields in the table.
  36. static const unsigned uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  37.  
  38. static DBIResult GetOptionalParams(pCHAR szDriver, UINT16 *iFields,
  39.                                    pFLDDesc pfldDesc, pCHAR szData);
  40.  
  41. //=====================================================================
  42. //  Function:
  43. //          OptParam();
  44. //
  45. //  Description:
  46. //          This function shows how to use the optional parameters
  47. //          on some of the BDE functions.
  48. //=====================================================================
  49. void
  50. OptParam (void)   
  51. {
  52.     DBIResult   rslt;               // Return value from IDAPI functions
  53.     hDBIDb      hDb;                // Handle to the database
  54.     hDBICur     hCur;               // Handle to the table
  55.     CRTblDesc   TblDesc;            // Create table descriptor
  56.     UINT16      uDispNumRecs = 10 ; // Number of records to add and
  57.                                     //   display
  58.     CURProps    curProps;           // Cursor properties
  59.     pFLDDesc    pfldDesc;           // Field descriptor for optional
  60.                                     //   parameters
  61.     UINT16      iFields;            // Number of fields (optional
  62.                                     //   parameters)
  63.     UINT16      i;                  // Loop counter
  64.     UINT16      iOffset;            // Offset in the default values buffer
  65.     CHAR        szFieldName[DBIMAXSCRECSIZE];   // Field name
  66.     CHAR        szDefault[DBIMAXSCRECSIZE];     // Default values for
  67.                                                 //   optional parameters
  68.     pCHAR       szTemp;                         // Temporary buffer
  69.     pCHAR       szData;                         // Data to display
  70.  
  71.     Screen("*** Using Optional Parameters ***\r\n");
  72.  
  73.     BREAK_IN_DEBUGGER();
  74.  
  75.     Screen("    Initializing IDAPI...");
  76.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  77.     {
  78.         Screen("\r\n*** End of Example ***");
  79.         return;
  80.     }
  81.  
  82.     Screen("    Setting the database directory...");
  83.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  84.     ChkRslt(rslt, "SetDirectory");
  85.  
  86.     szTemp = (pCHAR) malloc(DBIMAXSCFLDLEN);
  87.     szData = (pCHAR) malloc(DBIMAXSCRECSIZE);
  88.     pfldDesc = (pFLDDesc) malloc(DBIMAXSCFIELDS * sizeof(FLDDesc));
  89.     if ((szTemp == NULL) || (szData == NULL) || (pfldDesc == NULL))
  90.     {
  91.         Screen("        Error - Out of Memory!");
  92.         if (szTemp) free(szTemp);
  93.         if (szData) free(szData);
  94.         if (pfldDesc) free(pfldDesc);
  95.         CloseDbAndExit(&hDb);
  96.         Screen("\r\n*** End of Example ***");
  97.         return;
  98.     }
  99.  
  100.     // Initialize the pfldDesc variable to all zeroes.
  101.     memset((void*) pfldDesc , 0, sizeof(FLDDesc) * DBIMAXSCFIELDS);
  102.  
  103.     // Get the field types and default values of the optional
  104.     //   parameters available when creating a Paradox table.
  105.     GetOptionalParams("PARADOX", &iFields, pfldDesc, szData);
  106.  
  107.     Screen("\r\n    Default values for optional parameters...\r\n");
  108.     strcpy(szFieldName, "    ");
  109.     strcpy(szDefault, "    ");
  110.     iOffset = 0;
  111.     for (i = 0; i < iFields; i++)
  112.     {
  113.         // Note: Fields are formatted to be 20 characters wide
  114.         //   in order to better fit on the screen (Field widths
  115.         //   default to 128).
  116.         sprintf(szTemp, "%-*s\t", 20, pfldDesc[i].szName);
  117.         strcat(szFieldName, szTemp);
  118.         sprintf(szTemp, " %-*s\t", 20, &szData[iOffset]);
  119.         strcat(szDefault, szTemp);
  120.  
  121.         // Create the table as a 3.5 table.
  122.         if (! strcmp(pfldDesc[i].szName, "LEVEL"))
  123.         {
  124.             strcpy(&szData[iOffset], "3");
  125.         }
  126.  
  127.         // Set the maximum size of the table to 256MB.
  128.         if (! strcmp(pfldDesc[i].szName, "BLOCK SIZE"))
  129.         {
  130.             strcpy(&szData[iOffset], "4096");
  131.         }
  132.  
  133.         // Increment to point to the next field within the
  134.         //   array of default values.
  135.         iOffset += pfldDesc[i].iLen;
  136.     }
  137.  
  138.     Screen(szFieldName);
  139.     Screen(szDefault);
  140.  
  141.     Screen("\r\n    Change optional parameters to...\r\n");
  142.     strcpy(szFieldName, "    ");
  143.     strcpy(szDefault, "    ");
  144.     iOffset = 0;
  145.     for (i = 0; i < iFields; i++)
  146.     {
  147.         // Note: Fields are formatted to be 20 characters wide
  148.         //   in order to better fit on the screen (field widths
  149.         //   default to 128).
  150.         sprintf(szTemp, "%-*s\t", 20, pfldDesc[i].szName);
  151.         strcat(szFieldName, szTemp);
  152.         sprintf(szTemp, " %-*s\t", 20, &szData[iOffset]);
  153.         strcat(szDefault, szTemp);
  154.  
  155.         // Increment to point to the next field within the
  156.         //   array of default values.
  157.         iOffset += pfldDesc[i].iLen;
  158.     }
  159.  
  160.     Screen(szFieldName);
  161.     Screen(szDefault);
  162.  
  163.     Screen("\r\n    Initializing the table descriptor...");
  164.     memset((void *) &TblDesc, 0, sizeof(CRTblDesc));
  165.     lstrcpy(TblDesc.szTblName, szTblName);
  166.     lstrcpy(TblDesc.szTblType, szTblType);
  167.     TblDesc.iFldCount = uNumFields;
  168.     TblDesc.pfldDesc = fldDesc;
  169.     TblDesc.iOptParams = iFields;
  170.     TblDesc.pfldOptParams = pfldDesc;
  171.     TblDesc.pOptData = (pBYTE) szData;
  172.  
  173.     Screen("\r\n    Creating the Paradox table...");
  174.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  175.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  176.     {
  177.         free(szTemp);
  178.         free(szData);
  179.         free(pfldDesc);
  180.         CloseDbAndExit(&hDb);
  181.         Screen("\r\n*** End of Example ***");
  182.         return;
  183.     }
  184.  
  185.     free(pfldDesc);
  186.  
  187.     Screen("    Fill the table with random data...");
  188.     FillTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, uDispNumRecs);
  189.  
  190.     Screen("    Open the %s table...", szTblName);
  191.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  192.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  193.                         xltFIELD, FALSE, NULL, &hCur);
  194.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  195.     {
  196.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  197.         ChkRslt(rslt, "DeleteTable");
  198.         CloseDbAndExit(&hDb);
  199.         Screen("\r\n*** End of Example ***");
  200.         return;
  201.     }
  202.  
  203.     Screen("\r\n    Determine the properties of the table:");
  204.     rslt = DbiGetCursorProps(hCur, &curProps);
  205.     ChkRslt(rslt, "GetCursorProps");
  206.  
  207.     Screen("        Table Level:      %d", curProps.iTblLevel);
  208.     Screen("        Table block size: %d", (curProps.iBlockSize * 1024));
  209.  
  210.     Screen("\r\n    Display the %s table which we just created...", szTblName);
  211.     DisplayTable(hCur, uDispNumRecs);
  212.                                  
  213.     Screen("\r\n    Close the table...");
  214.     rslt = DbiCloseCursor(&hCur);
  215.     ChkRslt(rslt, "CloseCursor");
  216.  
  217.     Screen("    Deleting the table...");
  218.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  219.     ChkRslt(rslt, "DeleteTable");
  220.  
  221.     Screen("\r\n    Close the database and exit IDAPI...");
  222.     CloseDbAndExit(&hDb);
  223.  
  224.     free(szTemp);
  225.     free(szData);
  226.     Screen("\r\n*** End of Example ***");
  227. }
  228.  
  229. //=====================================================================
  230. //  Function:
  231. //          GetOptionalParams(pCHAR szDriver, UINT16 *iFields,
  232. //                            pFLDDesc pfldDesc, pCHAR szData);
  233. //
  234. //  Input:  szDriver    - Name of the driver
  235. //          iFields     - Used to return the number of fields
  236. //          pfldDesc    - Returns the descriptors of the fields
  237. //                          Memory for this variable must be allocated by
  238. //                          the calling function
  239. //          szData      - Contains the values of the fields.  Memory for
  240. //                          this variable must be allocated by the calling
  241. //                          function
  242. //
  243. //  Return: DBIResult - success of the function
  244. //
  245. //  Description:
  246. //          This function is used to return the available optional
  247. //          parameters when creating a table of the given type. This
  248. //          information includes the number of optional parameters
  249. //          (iFields), an array of field descriptors describing those
  250. //          parameters, and the default values for the parameters.
  251. //          Optional parameters are available only for dBASE and
  252. //          Paradox tables.
  253. //=====================================================================
  254. DBIResult
  255. GetOptionalParams(pCHAR szDriver, UINT16 *iFields,
  256.                   pFLDDesc pfldDesc, pCHAR szData)
  257. {
  258.     DBIResult   rslt;       // Return value from IDAPI functions
  259.     hDBICur     hCur;       // Handle to the cursor
  260.     pCHAR       szNode;     // String which contains the name of the node
  261.     pCFGDesc    CfgDesc;    // Configuration descriptor
  262.     DBIPATH     szCfgPath;  // Maximum length of the path
  263.                             //   in the configuration file
  264.     UINT16      iOffset = 0; // Offset in the buffer
  265.  
  266.     *iFields = 0;
  267.  
  268.     // Set up the option to get from the configuration file.
  269.     strcpy(szCfgPath, "\\DRIVERS\\");
  270.     strcat(szCfgPath, szDriver);
  271.     strcat(szCfgPath, "\\TABLE CREATE\\");
  272.  
  273.     rslt = DbiOpenCfgInfoList(NULL, dbiREADONLY, cfgPersistent, szCfgPath,
  274.                               &hCur);
  275.     if (ChkRslt(rslt, "OpenCfgInfoList") != DBIERR_NONE)
  276.     {
  277.         return rslt;
  278.     }
  279.  
  280.     // Allocate resources.
  281.     szNode = (pCHAR) malloc((DBIMAXPATHLEN * sizeof(CHAR)) + 1);
  282.     CfgDesc = (pCFGDesc) malloc(sizeof(CFGDesc));
  283.     if ((szNode == NULL) || (CfgDesc == NULL))
  284.     {
  285.         if (szNode) free(szNode);
  286.         if (CfgDesc) free(CfgDesc);
  287.         rslt = DbiCloseCursor(&hCur);
  288.         ChkRslt(rslt, "CloseCursor");
  289.     }
  290.  
  291.     // Process all nodes.
  292.     //   Get the next record in the table - contains the next option
  293.     //   of the given level in the tree.
  294.     while (DbiGetNextRecord(hCur, dbiNOLOCK, (pBYTE)CfgDesc, NULL)
  295.            == DBIERR_NONE)
  296.     {
  297.         pfldDesc[*iFields].iFldNum = *iFields + 1;
  298.         pfldDesc[*iFields].iFldType = CfgDesc->iDataType;
  299.         pfldDesc[*iFields].iUnits1 = DBIMAXSCFLDLEN - 1;
  300.         pfldDesc[*iFields].iLen = DBIMAXSCFLDLEN;
  301.         strcpy(pfldDesc[*iFields].szName, CfgDesc->szNodeName);
  302.  
  303.         sprintf(szNode, "%s", CfgDesc->szValue);
  304.         strcpy(&szData[iOffset], szNode);
  305.         iOffset += pfldDesc[*iFields].iLen;
  306.         (*iFields)++;
  307.     }
  308.  
  309.     // Clean up.
  310.     free(szNode);
  311.     free((pCHAR) CfgDesc);
  312.     rslt = DbiCloseCursor(&hCur);
  313.     ChkRslt(rslt, "CloseCursor");
  314.  
  315.     return rslt;
  316. }
  317.  
  318.