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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Callback.C
  4. #include "snipit.h"
  5.  
  6. static const DBIPATH szTblName      = "CUST2";
  7. static const char szTblType[]       = szDBASE;
  8. pCHAR  CallBackData2;
  9.  
  10. CBRType DBIFN _export CallBackFunc2(CBType ecbType, UINT32 iClientData,
  11.                                                 pVOID pCbInfo);
  12.  
  13. //=====================================================================
  14. //  Function:
  15. //          InputReqCallback();
  16. //
  17. //  Description:
  18. //          This example shows how to access a dBASE table which is missing
  19. //          it's .MDX file. This is accomplished with the use of the
  20. //          cbINPUTREQ callback. Note that this method can also be used
  21. //          to access FOX and CLIPPER tables.
  22. //=====================================================================
  23. void InputReqCallback(void)
  24. {
  25.      hDBIDb      hDb;                        // Handle to the database
  26.      hDBICur     hCur;                       // Handle to the table
  27.      UINT16      uNumOfRecs = 5;             // No. of records to display
  28.      CBInputDesc CbInfo;                     // Variable which is used within
  29.                                                           //   the callback
  30.      DBIResult   rslt;                       // Return value from IDAPI
  31.                                                           //   functions
  32.  
  33.      Screen("*** Open Table CallBack Example ***\r\n");
  34.  
  35.      BREAK_IN_DEBUGGER();
  36.  
  37.      Screen("    Initializing IDAPI...");
  38.      if (InitAndConnect(&hDb) != DBIERR_NONE)
  39.      {
  40.           Screen("*** End of Example ***");
  41.           return;
  42.      }
  43.  
  44.     Screen("    Setting the database directory...");
  45.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  46.     ChkRslt(rslt, "SetDirectory");
  47.  
  48.     Screen("    ERROR expected opening the %s table: \r\n"
  49.            "        'Index does not exist'...", szTblName);
  50.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  51.                         NULL, NULL, 0, dbiREADONLY, dbiOPENSHARED,
  52.                         xltFIELD, FALSE, NULL, &hCur);
  53.     ChkRslt(rslt, "OpenTable");
  54.  
  55.     if (hCur)
  56.     {
  57.         Screen("    Display the %s table...", szTblName);
  58.         DisplayTable(hCur, uNumOfRecs);
  59.  
  60.         Screen("\r\n    Close the %s table...", szTblName);
  61.         DbiCloseCursor(&hCur);
  62.     }
  63.  
  64.     // Allocate enough space for the data passed to the callback function.
  65.     CallBackData2 = (pCHAR)malloc(100 * sizeof(CHAR));
  66.     if (CallBackData2 == NULL)
  67.     {
  68.         CloseDbAndExit(&hDb);
  69.         Screen("\r\n*** End of Example ***");
  70.         return;
  71.     }
  72.  
  73.     // Register the callback.
  74.     rslt = DbiRegisterCallBack(NULL, cbINPUTREQ, (UINT32) CallBackData2,
  75.                                sizeof(CBInputDesc), &CbInfo, CallBackFunc2);
  76.     if(ChkRslt(rslt, "RegisterCallback") != DBIERR_NONE)
  77.     {
  78.           free(CallBackData2);
  79.         CloseDbAndExit(&hDb);
  80.         Screen("\r\n*** End of Example ***");
  81.         return;
  82.     }
  83.  
  84.     Screen("\r\n    Open the %s table...", szTblName);
  85.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  86.                         NULL, NULL, 0, dbiREADONLY, dbiOPENSHARED,
  87.                         xltFIELD, FALSE, NULL, &hCur);
  88.     ChkRslt(rslt, "OpenTable");
  89.  
  90.     if (hCur)
  91.     {
  92.         Screen("    Display the Table...");
  93.         DisplayTable(hCur, uNumOfRecs);
  94.  
  95.         Screen("\r\n    Close the %s table...", szTblName);
  96.         rslt = DbiCloseCursor(&hCur);
  97.         ChkRslt(rslt, "CloseCursor");
  98.     }
  99.  
  100.     // Unregister the callback by passing in NULL for the function.
  101.     rslt = DbiRegisterCallBack(NULL, cbINPUTREQ, NULL, 0, NULL, NULL);
  102.     if (ChkRslt(rslt, "RegisterCallBack") != DBIERR_NONE)
  103.     {
  104.         free(CallBackData2);
  105.         CloseDbAndExit(&hDb);
  106.         Screen("\r\n*** End of Example ***");
  107.         return;
  108.     }
  109.  
  110.     free(CallBackData2);
  111.  
  112.     Screen("\r\n    Close the database and exit IDAPI...");
  113.     CloseDbAndExit(&hDb);
  114.  
  115.     Screen("\r\n*** End of Example ***");
  116. }
  117.  
  118. //======================================================================
  119. //  Name:   CallBackFunc2(ecbType, iClientData, pCbInfo)
  120. //
  121. //  Input:  ecbType     - Callback type
  122. //          iClientData - Pointer to client information that is passed into
  123. //                        the callback function
  124. //          pCbInfo     - The callback structure that holds the information
  125. //                        about the current state
  126. //
  127. //  Return: The action that should be taken
  128. //
  129. //  Description:
  130. //          This function will be called from the BDE during the process
  131. //          of opening the table.
  132. //======================================================================
  133. CBRType DBIFN
  134. CallBackFunc2 (CBType ecbType, UINT32 iClientData, pVOID pCbInfo)
  135. {
  136.     CBInputDesc *eCBInputDesc; // Variable to contain passed-in
  137.                                //   information
  138.     int         i;
  139.  
  140.     // Set to stop an unused variable warning.
  141.     iClientData = iClientData;
  142.  
  143.     switch (ecbType)
  144.     {
  145.         // In case this is a restructure progress callback, display the
  146.         //   information.
  147.         case cbINPUTREQ:
  148.  
  149.             eCBInputDesc = (CBInputDesc far *)pCbInfo;
  150.  
  151.             Screen("\r\n    ### In the Callback function...\r\n");
  152.                 
  153.             Screen("    ### %s", eCBInputDesc->szMsg);
  154.             Screen("\r\n        Options are:");
  155.             for (i = 0; i < eCBInputDesc->iCount; i++)
  156.             {
  157.                 Screen("        ### %s: %s",
  158.                        eCBInputDesc->acbEntry[i].szKeyWord,
  159.                        eCBInputDesc->acbEntry[i].szHelp);
  160.             }
  161.  
  162.             Screen("        ### Default: %s",
  163.                    eCBInputDesc->acbEntry[eCBInputDesc->iSelection].szKeyWord);
  164.  
  165.             if (eCBInputDesc->eCbInputId == cbiMDXMISSING)
  166.             {
  167.                 i = 0;
  168.                 while (i < eCBInputDesc->iCount)
  169.                 {
  170.                     if (!strcmp(eCBInputDesc->acbEntry[i].szKeyWord,
  171.                              "Open Read Only"))
  172.                     {
  173.                         Screen("\r\n        ### Open the table read only...\r\n");
  174.                         eCBInputDesc->iSelection = i + 1;
  175.                         eCBInputDesc->bSave = FALSE;
  176.                         break;
  177.                     }
  178.                 }             
  179.             }
  180.             break;
  181.  
  182.         default:
  183.             Screen("### In the callback function");
  184.     }
  185.  
  186.     return cbrCHKINPUT;
  187. }
  188.