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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // secdesc.c
  4. #include "snipit.h"
  5.  
  6. static DBIResult AddRecord(hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder,
  7.                            CHAR *szDateSent, CHAR *szDateDelivered);
  8.  
  9. static const char szTblName[] = "SecDesc";
  10. static const char szTblType[] = szPARADOX;
  11.  
  12. // Field descriptor used in creating a table.
  13. static SNIPFAR FLDDesc fldDesc[] = {
  14.               { // Field 1 - AUTOINC
  15.                 1,            // Field number
  16.                 "Status ID",  // Field name
  17.                 fldFLOAT,     // Field type
  18.                 fldUNKNOWN,   // Field subtype
  19.                 0,            // Field size
  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, "Order No", fldFLOAT, fldUNKNOWN,
  30.                 0, 0, 0, 0, 0,
  31.                 fldvNOCHECKS, fldrREADWRITE
  32.               },
  33.               { // Field 4 - DATE
  34.                 3, "Date Sent", fldDATE, fldUNKNOWN,
  35.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  36.               },
  37.               { // Field 4 - DATE
  38.                 4, "Date Received", fldDATE, fldUNKNOWN,
  39.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  40.               }
  41.              }; // Array of field descriptors.
  42.  
  43. // The number of fields in the table.
  44. static const UINT16 uNumFields = sizeof(fldDesc) / sizeof(fldDesc[0]);
  45.  
  46. static SNIPFAR SECDesc secDesc[] = {
  47.                 {
  48.                     1,              // Number to identify descriptor
  49.                     prvREADONLY,    // Table privileges
  50.                     NOFAMRIGHTS,    // Family rights
  51.                     "test",         // Password
  52.                     { prvNONE, prvREADONLY, prvREADONLY, prvREADONLY }            // Field Level Priviledges
  53.                 }
  54.               };
  55.  
  56. // Define the operation on the table:  add the security descriptor to the
  57. //   table.            
  58. static CROpType crOpType[] = { crADD };
  59.  
  60. // The number of security descriptors in the table.
  61. static const UINT16 uNumDescs = sizeof(secDesc) / sizeof(secDesc[0]);
  62.  
  63. //=====================================================================
  64. //  Function:
  65. //          SecDesc();
  66. //
  67. //  Desccription:
  68. //          This function shows how to use auxiliary passwords with a
  69. //          Paradox table.
  70. //=====================================================================
  71. void
  72. SecDesc (void)
  73. {
  74.     DBIResult   rslt;               // Return value from IDAPI functions
  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.  
  82.     Screen("*** Security Descriptor Example ***\r\n");
  83.  
  84.     BREAK_IN_DEBUGGER();
  85.  
  86.     Screen("    Initializing IDAPI...");
  87.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  88.     {
  89.         Screen("\r\n*** End of Example ***");
  90.         return;
  91.     }
  92.  
  93.     Screen("    Setting the database directory...");
  94.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  95.     ChkRslt(rslt, "SetDirectory");
  96.  
  97.     Screen("    Initializing the table descriptor...");
  98.     memset((void *)&TblDesc, 0, sizeof(CRTblDesc));
  99.     lstrcpy(TblDesc.szTblName, szTblName);
  100.     lstrcpy(TblDesc.szTblType, szTblType);
  101.     TblDesc.iFldCount   = uNumFields;
  102.     TblDesc.pfldDesc    = fldDesc;
  103.     TblDesc.bProtected  = TRUE;
  104.     strcpy(TblDesc.szPassword, szPassword);
  105.     TblDesc.iSecRecCount = uNumDescs;
  106.     TblDesc.pecrSecOp    = crOpType;
  107.     TblDesc.psecDesc     = (SECDesc *)secDesc;
  108.  
  109.     // Add the master password to the session. This is needed
  110.     //   before the create in case the table needs to be overwritten.
  111.     //   The master password is also required in order to open the table.
  112.     rslt = DbiAddPassword(szPassword);
  113.     ChkRslt(rslt, "AddPassword");
  114.  
  115.     Screen("    Creating the %s table...", szTblName);
  116.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  117.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  118.     {
  119.         CloseDbAndExit(&hDb);
  120.         Screen("\r\n*** End of Example ***");
  121.         return;
  122.     }
  123.  
  124.     Screen("    Open the %s table with the master password...",
  125.            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/1994", "08/12/1994");
  140.     AddRecord(hCur, 2, 1005, "08/02/1994", "08/12/1994");
  141.     AddRecord(hCur, 3, 1006, "08/04/1994", "08/12/1994");
  142.     AddRecord(hCur, 4, 1007, "09/28/1994", "11/12/1994");
  143.     AddRecord(hCur, 5, 1008, "10/28/1994", "11/12/1994");
  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(hCur, fStatus, fOrder, *DateOrdered, *DateDelivered)
  207. //
  208. //  Input:  hCur            - Cursor to the table 
  209. //          fStatus         - Value to write to the Status ID field
  210. //          fOrder          - Value to write to the Order ID field
  211. //          DateSent        - Value to write to the Date Sent field
  212. //          DateDelivered   - Value to write to the Date Received field
  213. //
  214. //  Return: Success of the function
  215. //
  216. //  Description:
  217. //          This function is used to add a record to the table.
  218. //=====================================================================
  219. DBIResult
  220. AddRecord (hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder, CHAR *szDateSent,
  221.            CHAR *szDateDelivered)
  222. {
  223.     DBIResult   rslt;               // Return value from IDAPI functions
  224.     pBYTE       pRecBuf;            // Record buffer
  225.     CURProps    TblProps;           // The properties of the table
  226.     UINT16      uMonth;             // Contains the month portion of the
  227.                                     //   date
  228.     UINT16      uDay;               // Contains the day portion of the
  229.                                     //   date
  230.     INT16       iYear;              // Contains the year portion of the
  231.                                     //   date
  232.     CHAR        szDate[30];         // Pointer to be allocated from the
  233.                                     //   heap
  234.     CHAR        *pszTemp;           // Temporary string used in parsing
  235.                                     //   the date
  236.     DBIDATE     Date;               // Date structure, used in
  237.                                     //   DbiPutField
  238.  
  239.     // Allocate a record buffer.
  240.     rslt = DbiGetCursorProps(hCur, &TblProps);
  241.     ChkRslt(rslt, "GetCursorProps");
  242.  
  243.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(pBYTE));
  244.     if (pRecBuf == NULL)
  245.     {
  246.         Screen("        Error - Out of memory!");
  247.         return DBIERR_NOMEMORY;
  248.     }
  249.  
  250.     // Make sure we're starting with a clean record buffer.
  251.     rslt = DbiInitRecord(hCur, pRecBuf);
  252.     ChkRslt(rslt, "InitRecord");
  253.  
  254.     // Add the Status ID to the record buffer.
  255.     rslt = DbiPutField(hCur, 1, pRecBuf, (pBYTE) &fStatus);
  256.     ChkRslt(rslt, "PutField");
  257.  
  258.     // Add the Order ID to the record buffer.
  259.     rslt = DbiPutField(hCur, 2, pRecBuf, (pBYTE) &fOrder);
  260.     ChkRslt(rslt, "PutField");
  261.  
  262.     strcpy(szDate, szDateSent);
  263.     pszTemp = strtok(szDate, "/");
  264.     if (pszTemp)
  265.     {
  266.         uMonth = atoi(pszTemp); // uMonth set to 0 if invalid date.
  267.         pszTemp = strtok(NULL, "/");
  268.         if (pszTemp)
  269.         {
  270.             uDay = atoi(pszTemp); // uDay set to 0 if invalid date.
  271.             pszTemp = strtok(NULL, "/");
  272.             if (pszTemp)
  273.             {
  274.                 iYear = atoi(pszTemp);  // iYear set to 0 if
  275.                                         //   invalid date.
  276.             }
  277.         }
  278.     }
  279.  
  280.     // Encode the date.
  281.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  282.     ChkRslt(rslt, "DateEncode");
  283.  
  284.     // Put the data into the record buffer.
  285.     rslt = DbiPutField(hCur, 3, pRecBuf, (pBYTE) &Date);
  286.     ChkRslt(rslt, "PutField");
  287.  
  288.     // Need to convert the string to a Date structure.
  289.     strcpy(szDate, szDateDelivered);
  290.     pszTemp = strtok(szDate, "/");
  291.     if (pszTemp)
  292.     {
  293.         uMonth = atoi(pszTemp); // uMonth set to 0 if invalid date.
  294.         pszTemp = strtok(NULL, "/");
  295.         if (pszTemp)
  296.         {
  297.             uDay = atoi(pszTemp); // uDay set to 0 if invalid date.
  298.             pszTemp = strtok(NULL, "/");
  299.             if (pszTemp)
  300.             {
  301.                 iYear = atoi(pszTemp);  // iYear set to 0 if
  302.                                         //   invalid date.
  303.             }
  304.         }
  305.     }
  306.  
  307.     // Encode the date.
  308.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  309.     ChkRslt(rslt, "DateEncode");
  310.  
  311.     // Put the data into the record buffer.
  312.     rslt = DbiPutField(hCur, 4, pRecBuf, (pBYTE) &Date);
  313.     ChkRslt(rslt,"PutField");
  314.  
  315.     // Insert the record into the table.
  316.     rslt = DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf);
  317.     ChkRslt(rslt, "InsertRecord");
  318.  
  319.     free(pRecBuf);
  320.  
  321.     return rslt;
  322. }
  323.