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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // prdxsort.c
  4. #include "snipit.h"
  5.  
  6. // Default length of the mapped fields
  7. #define NAMELEN 30
  8.  
  9. static const char szTblName[]       = "vendors";
  10. static const char szSortedTblName[] = "SortVend";
  11. static const char szTblType[]       = "PARADOX";
  12.  
  13. // Field descriptor used in mapping the fields of the table.
  14. //   Used in order to display the fields in the order that they
  15. //   are sorted: 'State/Prov' and 'Street'. The 'Vendor Name' is
  16. //   included as additional information.
  17. //   See the fieldmap.c file for a description of field mapping
  18. static SNIPFAR FLDDesc FldDescs1[] = {
  19.               {
  20.                 1,            // Field number
  21.                 "State/Prov", // Field name
  22.                 fldZSTRING,   // Field type
  23.                 fldUNKNOWN,   // Field subtype
  24.                 NAMELEN,      // Field size (1 or 0, except
  25.                               //   BLOB or CHAR field)
  26.                 0,            // Decimal places   ( 0 )
  27.                               //   computed
  28.                 0,            // Offset in record ( 0 )
  29.                 0,            // Length in bytes  ( 0 )
  30.                 0,            // For NULL Bits    ( 0 )
  31.                 fldvNOCHECKS, // Validiy checks   ( 0 )
  32.                 fldrREADWRITE // Rights
  33.               },
  34.               {
  35.                 2, "Street", fldZSTRING, fldUNKNOWN,
  36.                 NAMELEN, 0, 0, 0, 0,
  37.                 fldvNOCHECKS, fldrREADWRITE
  38.               },
  39.               {
  40.                 3, "Vendor Name", fldZSTRING, fldUNKNOWN,
  41.                 NAMELEN, 0, 0, 0, 0,
  42.                 fldvNOCHECKS, fldrREADWRITE
  43.               }
  44.              }; // FldDescs1.
  45.  
  46. INT16 DBIFN StreetCompare(pVOID pLdObj, pVOID pValue1, pVOID pValue2,
  47.                           INT16 iLen);
  48. pCHAR DBIFN FindFirstChar(const char *pszString);
  49.  
  50. //=====================================================================
  51. //  Function:
  52. //          ParadoxSort();
  53. //
  54. //  Description:
  55. //          This example shows how to sort on a field in a PARADOX
  56. //          table.  The example will use the DbiSortTable() function.
  57. //=====================================================================
  58. void
  59. ParadoxSort (void)
  60. {
  61.     hDBIDb          hDb;            // Handle to the database
  62.     hDBICur         hCur;           // Handle to the table
  63.     hDBICur         hSortCur;       // Handle to the sorted table
  64.     UINT16          uSortFields[] = { 5, 3 };   // Which fields to sort
  65.     BOOL            bCaseIns[]    = { FALSE, FALSE};
  66.     SORTOrder       SortOrder[]   = { sortASCEND, sortASCEND };
  67.     pfSORTCompFn    pfSortFn[]    = { NULL, StreetCompare };
  68.     UINT16          NOSRTFLDS;      // Number of fields to sort
  69.     UINT32          uNumToSort = 10;// Number of record to display
  70.     UINT16          uNumFields;     // Number of fields in the table
  71.     DBIResult       rslt;           // Return value from IDAPI functions
  72.  
  73.     Screen("*** Sorting Example ***\r\n");
  74.  
  75.     BREAK_IN_DEBUGGER();
  76.  
  77.     Screen("    Initializing IDAPI...");
  78.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  79.     {                                        
  80.         Screen("\r\n*** End of Example ***");
  81.         return;
  82.     }
  83.  
  84.     Screen("    Setting the database directory...");
  85.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  86.     ChkRslt(rslt, "SetDirectory");
  87.  
  88.     Screen("    Open the %s table...", szTblName);
  89.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  90.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  91.                         xltFIELD, FALSE, NULL, &hCur);
  92.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  93.     {
  94.         CloseDbAndExit(&hDb);
  95.         Screen("\r\n*** End of Example ***");
  96.         return;
  97.     }
  98.  
  99.     rslt = DbiSetToBegin(hCur);
  100.     ChkRslt(rslt, "SetToBegin");
  101.  
  102.     Screen("    Display the unsorted %s table...", szTblName);
  103.     DisplayTable(hCur, uNumToSort);
  104.  
  105.     Screen("\r\n    Sort the %s table, using first the 'State/Prov'"
  106.            " field,", szTblName);
  107.     Screen("     and then the 'Street' field. The sorted"
  108.            " records are saved in the %s table",  szSortedTblName);
  109.  
  110.     // Number of sort fields on which to sort the table.
  111.     NOSRTFLDS = sizeof(uSortFields) / sizeof(uSortFields[0]);
  112.  
  113.     rslt = DbiSortTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, 0,
  114.                         (pCHAR) szSortedTblName, NULL, 0, NOSRTFLDS,
  115.                         uSortFields, bCaseIns, SortOrder, pfSortFn,
  116.                         FALSE, NULL, &uNumToSort);
  117.     if (ChkRslt(rslt, "SortTable") != DBIERR_NONE)
  118.     {
  119.         rslt = DbiDeleteTable(hDb, (pCHAR) szSortedTblName, (pCHAR) szTblType);
  120.         ChkRslt(rslt, "DeleteTable");
  121.         CloseDbAndExit(&hDb);
  122.         Screen("\r\n*** End of Example ***");
  123.         return;
  124.     }
  125.  
  126.     Screen("    Open the %s sorted table...", szSortedTblName);
  127.     rslt = DbiOpenTable(hDb, (pCHAR) szSortedTblName, (pCHAR) szTblType,
  128.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  129.                         xltFIELD, FALSE, NULL, &hSortCur);
  130.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  131.     {
  132.         rslt = DbiDeleteTable(hDb, (pCHAR) szSortedTblName, (pCHAR) szTblType);
  133.         ChkRslt(rslt, "DeleteTable");
  134.         CloseDbAndExit(&hDb);
  135.         Screen("\r\n*** End of Example ***");
  136.         return;
  137.     }
  138.  
  139.     Screen("    Change which fields of the table are displayed:"
  140.            "\r\n        'State/Prov', 'Street', and 'Vendor Name'");
  141.  
  142.     // Determine the number of fields that are part of the descriptor.
  143.     uNumFields = sizeof(FldDescs1) / sizeof (FldDescs1[0]);
  144.  
  145.     // Set the field mapping.
  146.     rslt = DbiSetFieldMap(hSortCur, uNumFields, FldDescs1);
  147.     ChkRslt(rslt, "SetFieldMap");
  148.  
  149.     rslt = DbiSetToBegin(hSortCur);
  150.     ChkRslt(rslt, "SetToBegin");
  151.  
  152.     Screen("    Display the %s sorted table...", szSortedTblName);
  153.     DisplayTable(hSortCur, uNumToSort);
  154.  
  155.     Screen("\r\n    Close the %s original table...", szTblName);
  156.     rslt = DbiCloseCursor(&hCur);
  157.     ChkRslt(rslt, "CloseCursor");
  158.  
  159.     Screen("    Close the %s sorted table...", szSortedTblName);
  160.     rslt = DbiCloseCursor(&hSortCur);
  161.     ChkRslt(rslt, "CloseCursor");
  162.  
  163.     Screen("    Delete the %s sorted table...", szSortedTblName);
  164.     rslt = DbiDeleteTable(hDb, (pCHAR) szSortedTblName, (pCHAR) szTblType);
  165.     ChkRslt(rslt, "DeleteTable");
  166.  
  167.     Screen("    Close the database and exit IDAPI...");
  168.     CloseDbAndExit(&hDb);
  169.  
  170.     Screen("\r\n*** End of Example ***");
  171. }
  172.  
  173. //=====================================================================
  174. //  Function:
  175. //          StreetCompare(pLdObj, pValue1, pValue2, iLen);
  176. //
  177. //  Input:
  178. //          pLdObj  - Language driver, not used
  179. //          pValue1 - First value to compare
  180. //          pValue2 - Second value to compare
  181. //          iLen    - Length, not used
  182. //
  183. //  Return: -1 if (pValue1 <  pValue2)
  184. //          0  if (pValue1 == pValue2)
  185. //          1  if (pValue1 >  pValue2)
  186. //
  187. //  Description:
  188. //          This function is used in comparing the 'Street'
  189. //          field of the vendor table. This function will
  190. //          sort according to the street name, not the address.
  191. //=====================================================================
  192. INT16 DBIFN
  193. StreetCompare (pVOID pLdObj, pVOID pValue1, pVOID pValue2,
  194.                INT16 iLen)
  195. {
  196.     INT16       rslt;   // Value returned from IDAPI functions
  197.     pCHAR       szOne;  // First value to compare
  198.     pCHAR       szTwo;  // Second value to compare
  199.  
  200.     // Dummy assignments to avoid warnings.
  201.     iLen = iLen;
  202.     pLdObj = pLdObj;
  203.  
  204.     // Skip leading numeric values - used to skip the house number
  205.     //   portion of the street address.
  206.     szOne = FindFirstChar((const char *)pValue1);
  207.     szTwo = FindFirstChar((const char *)pValue2);
  208.  
  209.     // Compare the values in the 'Street' field, starting the
  210.     //   strings at the first non-numeric character - sort on
  211.     //   Street name, not the house number on a given street.
  212.     rslt = strcmp(szOne, szTwo);
  213.  
  214.     // If on the same street (pValue1 == pValue2),
  215.     //   compare House Number on the street.
  216.     if (rslt == DBIERR_NONE)
  217.     {
  218.         rslt = strcmp((pCHAR)pValue1, (pCHAR)pValue2);
  219.     }
  220.     if (0 < rslt)
  221.     {
  222.         rslt = 1;
  223.     }
  224.     if (rslt < 0)
  225.     {
  226.         rslt = -1;
  227.     }
  228.     return rslt;
  229. }
  230.  
  231. //=====================================================================
  232. //  Code:   FindFirstChar(pszString);
  233. //
  234. //  Input:  pszString   - String to search
  235. //
  236. //  Return: Pointer to the location in the string after the house
  237. //          number
  238. //
  239. //  Description:
  240. //          This function is used to skip the house number part
  241. //          of the street address when sorting the table. This
  242. //          allows the 'Street' field to be sorted according to
  243. //          the street name.
  244. //=====================================================================
  245. pCHAR DBIFN
  246. FindFirstChar (const char *pszString)
  247. {
  248.     UINT16  uWhere = 0; // Counter to keep track of the current character
  249.                         //   within the string
  250.  
  251.     // Search for the first blank as part of the string -
  252.     //   this should be the start of the street name.
  253.     while ((pszString[uWhere] != 0) &&
  254.            (pszString[uWhere] != ' '))
  255.     {
  256.         uWhere++;
  257.     }
  258.     // Return the character after the space
  259.     return (pCHAR)&(pszString[uWhere+1]);
  260. }
  261.