home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / SECDESC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.9 KB  |  325 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // secdesc.c
  4. #include "snipit.h"
  5.  
  6. // Function used to add a record to the table
  7. static DBIResult AddRecord(hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder,
  8.                            CHAR *szDateSent, CHAR *szDateDelivered);
  9.  
  10. static const char szTblName[] = "SecDesc";
  11. static const char szTblType[] = szPARADOX;
  12.  
  13. // Field descriptor used in creating a table
  14. static SNIPFAR FLDDesc fldDesc[] = {
  15.               { // Field 1 - AUTOINC
  16.                 1,            // Field number
  17.                 "Status ID",    // Field name
  18.                 fldFLOAT,     // Field type
  19.                 fldUNKNOWN,   // Field subtype
  20.                 0,            // Field size
  21.                 0,            // Decimal places ( 0 )
  22.                               //     computed
  23.                 0,            // Offset in record ( 0 )
  24.                 0,            // Length in bytes  ( 0 )
  25.                 0,            // For Null bits    ( 0 )
  26.                 fldvNOCHECKS, // Validity checks   ( 0 )
  27.                 fldrREADWRITE // Rights
  28.               },
  29.               { // Field 2 - ALPHA
  30.                 2, "Order No", fldFLOAT, fldUNKNOWN,
  31.                 0, 0, 0, 0, 0,
  32.                 fldvNOCHECKS, fldrREADWRITE
  33.               },
  34.               { // Field 4 - DATE
  35.                 3, "Date Sent", fldDATE, fldUNKNOWN,
  36.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  37.               },
  38.               { // Field 4 - DATE
  39.                 4, "Date Received", fldDATE, fldUNKNOWN,
  40.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  41.               }
  42.              }; // Array of field descriptors
  43.  
  44. // The number of fields in the table.
  45. static const UINT16 uNumFields = sizeof(fldDesc) / sizeof(fldDesc[0]);
  46.  
  47. static SNIPFAR SECDesc secDesc[] = {
  48.                 {
  49.                     1,              // # to identify descriptor
  50.                     prvREADONLY,    // Table privileges
  51.                     NOFAMRIGHTS,    // Family rights
  52.                     "test",         // Password
  53.                     { prvNONE, prvREADONLY, prvREADONLY, prvREADONLY }            // Field Level Priviledges
  54.                 }
  55.               };
  56.  
  57. // Define the operation on the table:  add the security descriptor to the
  58. // table.            
  59. static CROpType crOpType[] = { crADD };
  60.  
  61. // The number of security descriptors in the table
  62. static const UINT16 uNumDescs = sizeof(secDesc) / sizeof(secDesc[0]);
  63.  
  64. //=====================================================================
  65. //  Function:
  66. //          SecDesc();
  67. //
  68. //  Desccription:
  69. //          This function shows how to use auxiliary passwords with a
  70. //          Paradox table.
  71. //=====================================================================
  72. void
  73. SecDesc (void)
  74. {
  75.     hDBIDb      hDb;                // Handle to the database
  76.     hDBICur     hCur;               // Handle to the table
  77.     CRTblDesc   TblDesc;            // Create table descriptor
  78.     UINT16      uDispNumRecs = 10;  // Number of records to add and
  79.                                     // display.
  80.     CHAR        szPassword[] = "MyPass"; // Password for the table
  81.     DBIResult   rslt;
  82.  
  83.     Screen("*** Security Descriptor Example ***\r\n");
  84.  
  85.     BREAK_IN_DEBUGGER();
  86.  
  87.     Screen("    Initializing IDAPI...");
  88.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  89.     {
  90.         Screen("\r\n*** End of Example ***");
  91.         return;
  92.     }
  93.  
  94.     Screen("    Setting the database directory...");
  95.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  96.     ChkRslt(rslt, "SetDirectory");
  97.  
  98.     Screen("    Initializing the table descriptor...");
  99.     memset((void *)&TblDesc, 0, sizeof(CRTblDesc));
  100.     lstrcpy(TblDesc.szTblName, szTblName);
  101.     lstrcpy(TblDesc.szTblType, szTblType);
  102.     TblDesc.iFldCount   = uNumFields;
  103.     TblDesc.pfldDesc    = fldDesc;
  104.     TblDesc.bProtected  = TRUE;
  105.     strcpy(TblDesc.szPassword, szPassword);
  106.     TblDesc.iSecRecCount = uNumDescs;
  107.     TblDesc.pecrSecOp    = crOpType;
  108.     TblDesc.psecDesc     = (SECDesc *)secDesc;
  109.  
  110.     // Add the master password to the session. This is needed
  111.     // before the create in case the table needs to be overwritten.
  112.     // The master password is also required in order to open the table.
  113.     rslt = DbiAddPassword(szPassword);
  114.     ChkRslt(rslt, "AddPassword");
  115.  
  116.     Screen("    Creating the %s table...", szTblName);
  117.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  118.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  119.     {
  120.         CloseDbAndExit(&hDb);
  121.         Screen("\r\n*** End of Example ***");
  122.         return;
  123.     }
  124.  
  125.     Screen("    Open the \"%s\" table with the master password...", szTblName);
  126.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  127.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  128.                         xltFIELD, FALSE, NULL, &hCur);
  129.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  130.     {
  131.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  132.         ChkRslt(rslt, "DeleteTable");
  133.         CloseDbAndExit(&hDb);
  134.         Screen("\r\n*** End of Example ***");
  135.         return;
  136.     }
  137.  
  138.     Screen("\r\n    Add five records to the table...");
  139.     AddRecord(hCur, 1, 1004, "07/28/1996", "08/12/1996");
  140.     AddRecord(hCur, 2, 1005, "08/02/1996", "08/12/1996");
  141.     AddRecord(hCur, 3, 1006, "08/04/1996", "08/12/1996");
  142.     AddRecord(hCur, 4, 1007, "09/28/1996", "11/12/1996");
  143.     AddRecord(hCur, 5, 1008, "10/28/1996", "11/12/1996");
  144.  
  145.     Screen("\r\n    Close the table...");
  146.     rslt = DbiCloseCursor(&hCur);
  147.     ChkRslt(rslt, "CloseCursor");
  148.  
  149.     // Remove the master password from the session
  150.     rslt = DbiDropPassword(szPassword);
  151.     ChkRslt(rslt, "DropPassword");
  152.  
  153.     // Add the auxiliary password to the session
  154.     rslt = DbiAddPassword("test");
  155.     ChkRslt(rslt, "AddPassword");
  156.  
  157.     // Open the table.
  158.     // Note that the table has to be opened read only as the
  159.     // security descriptor only supports read operations.
  160.     Screen("\r\n    Open the \"%s\" table with the auxiliary password...",
  161.            szTblName);
  162.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  163.                         NULL, NULL, 0, dbiREADONLY, dbiOPENSHARED,
  164.                         xltFIELD, FALSE, NULL, &hCur);
  165.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  166.     {
  167.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  168.         ChkRslt(rslt, "DeleteTable");
  169.         CloseDbAndExit(&hDb);
  170.         Screen("\r\n*** End of Example ***");
  171.         return;
  172.     }
  173.  
  174.     // Go to the beginning of the table.
  175.     rslt = DbiSetToBegin(hCur);
  176.     ChkRslt(rslt, "SetToBegin");
  177.  
  178.     Screen("    Display the \"%s\" table...", szTblName);
  179.     Screen("        Notice that the \"Status ID\" field is blank:"
  180.            " the auxiliary password does\r\n        not have access to"
  181.            " the \"Status ID\" field"); 
  182.     DisplayTable(hCur, uDispNumRecs);
  183.  
  184.     Screen("\r\n    Close the table...");
  185.     rslt = DbiCloseCursor(&hCur);
  186.     ChkRslt(rslt, "CloseCursor");
  187.  
  188.     Screen("    Need to add the master password in order to delete"
  189.            " the table.\r\n    The auxiliary password does not have"
  190.            " sufficient rights");
  191.     rslt = DbiAddPassword(szPassword);
  192.     ChkRslt(rslt, "AddPassword");
  193.                        
  194.     Screen("    Deleting the table...");
  195.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  196.     ChkRslt(rslt, "DeleteTable");
  197.  
  198.     Screen("    Close the database and exit IDAPI...");
  199.     CloseDbAndExit(&hDb);
  200.  
  201.     Screen("\r\n*** End of Example ***");
  202. }
  203.  
  204. //=====================================================================
  205. //  Function:
  206. //          AddRecord (hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder,
  207. //                     CHAR *DateOrdered, CHAR *DateDelivered)
  208. //
  209. //  Input:  hCur            - Cursor to the table 
  210. //          fStatus         - Value to write to the Status ID field
  211. //          fOrder          - Value to write to the Order ID field
  212. //          DateSent        - Value to write to the Date Sent field
  213. //          DateDelivered   - Value to write to the Date Received field
  214. //
  215. //  Return: Success of the function.
  216. //
  217. //  Description:
  218. //          This function is used to add a record to the table.
  219. //=====================================================================
  220. DBIResult
  221. AddRecord (hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder, CHAR *szDateSent,
  222.            CHAR *szDateDelivered)
  223. {
  224.     DBIResult   rslt;               // Return value from IDAPI functions
  225.     pBYTE       pRecBuf;            // Record buffer
  226.     CURProps    TblProps;           // The properties of the table
  227.     UINT16      uMonth;             // Contains the month portion of the
  228.                                     //   date
  229.     UINT16      uDay;               // Contains the day portion of the
  230.                                     //   date
  231.     INT16       iYear;              // Contains the year portion of the
  232.                                     //   date
  233.     CHAR        szDate[30];         // Pointer to be allocated from the
  234.                                     //   heap
  235.     CHAR        *pszTemp;            // Temporary string used in parsing
  236.                                     //   the date
  237.     DBIDATE     Date;               // Date structure - used in
  238.                                     //   DbiPutField.
  239.  
  240.     // Get the record size for the table
  241.     rslt = DbiGetCursorProps(hCur, &TblProps);
  242.     ChkRslt(rslt, "GetCursorProps");
  243.  
  244.     // Allocate a record buffer
  245.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(pBYTE));
  246.     if (pRecBuf == NULL)
  247.     {
  248.         Screen("    Error - Out of memory");
  249.         return DBIERR_NOMEMORY;
  250.     }
  251.  
  252.     // Make sure we're starting with a clean record buffer
  253.     rslt = DbiInitRecord(hCur, pRecBuf);
  254.     ChkRslt(rslt, "InitRecord");
  255.  
  256.     // Add the Status ID to the record buffer
  257.     rslt = DbiPutField(hCur, 1, pRecBuf, (pBYTE) &fStatus);
  258.     ChkRslt(rslt, "PutField");
  259.  
  260.     // Add the Order ID to the record buffer
  261.     rslt = DbiPutField(hCur, 2, pRecBuf, (pBYTE) &fOrder);
  262.     ChkRslt(rslt, "PutField");
  263.  
  264.     strcpy(szDate, szDateSent);
  265.     pszTemp = strtok(szDate, "/");
  266.     if (pszTemp)
  267.     {
  268.         uMonth = (UINT16)atoi(pszTemp); // uMonth set to 0 if invalid date.
  269.         pszTemp = strtok(NULL, "/");
  270.         if (pszTemp)
  271.         {
  272.             uDay = (UINT16)atoi(pszTemp); // uDay set to 0 if invalid date.
  273.             pszTemp = strtok(NULL, "/");
  274.             if (pszTemp)
  275.             {
  276.                 iYear = (UINT16)atoi(pszTemp);   // iYear set to 0 if
  277.                                         //   invalid date.
  278.             }
  279.         }
  280.     }
  281.  
  282.     // Encode the date
  283.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  284.     ChkRslt(rslt, "DateEncode");
  285.  
  286.     // Put the data into the record buffer
  287.     rslt = DbiPutField(hCur, 3, pRecBuf, (pBYTE) &Date);
  288.     ChkRslt(rslt, "PutField");
  289.  
  290.     // Need to convert the string to a Date structure.
  291.     strcpy(szDate, szDateDelivered);
  292.     pszTemp = strtok(szDate, "/");
  293.     if (pszTemp)
  294.     {
  295.         uMonth = (UINT16)atoi(pszTemp); // uMonth set to 0 if invalid date.
  296.         pszTemp = strtok(NULL, "/");
  297.         if (pszTemp)
  298.         {
  299.             uDay = (UINT16)atoi(pszTemp); // uDay set to 0 if invalid date.
  300.             pszTemp = strtok(NULL, "/");
  301.             if (pszTemp)
  302.             {
  303.                 iYear = (UINT16)atoi(pszTemp);  // iYear set to 0 if
  304.                                         //   invalid date.
  305.             }
  306.         }
  307.     }
  308.  
  309.     // Encode the date.
  310.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  311.     ChkRslt(rslt, "DateEncode");
  312.  
  313.     // Put the data into the record buffer.
  314.     rslt = DbiPutField(hCur, 4, pRecBuf, (pBYTE) &Date);
  315.     ChkRslt(rslt,"PutField");
  316.  
  317.     // Insert the record into the table.
  318.     rslt = DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf);
  319.     ChkRslt(rslt, "InsertRecord");
  320.  
  321.     free(pRecBuf);
  322.  
  323.     return rslt;
  324. }
  325.