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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Blobio.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "blob_io";
  7. static const char szTblType[] = szDBASE;
  8.  
  9. static DBIResult InsertRec(hDBICur hCur, UINT16 uID);
  10. static DBIResult FillBlobInfo(hDBICur hCur, pBYTE pRecBuf);
  11. static BOOL FillString(pBYTE pString);
  12.  
  13. static SNIPFAR FLDDesc fldDesc[] = {
  14.                             {
  15.                                 1,            // Field number
  16.                                 "Item_ID",    // Field name
  17.                                 fldINT16,     // Field type
  18.                                 0,            // Field subtype
  19.                                 1,            // Field size ( 1 or 0
  20.                                               // except BLOB or string )
  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.                             {
  30.                                 2, "BigText", fldBLOB, fldstMEMO,
  31.                                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  32.                             }
  33.                         };  // Array of field descriptors
  34.  
  35. static const unsigned uNumFields = sizeof(fldDesc) / sizeof(fldDesc[0]);
  36.  
  37. //=====================================================================
  38. //  Function:
  39. //          BlobIO(void);
  40. //
  41. //  Description:
  42. //          This example will show how to modify and insert a BLOB field
  43. //          that is greater than 64K.
  44. //=====================================================================
  45. void
  46. BlobIO (void)
  47. {
  48.     DBIResult   rslt;               // Return value from IDAPI functions
  49.     hDBIDb      hDb;                // database handle
  50.     hDBICur     hCur;               // Cursor handle
  51.     CRTblDesc   crTblDsc;           // Table descriptor
  52.     BOOL        bOverWrite = TRUE;  // Overwrite, yes/no flag
  53.  
  54.     Screen("*** Using BLOB's ***\r\n");
  55.  
  56.     BREAK_IN_DEBUGGER();
  57.  
  58.     Screen("    Initializing IDAPI...");
  59.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  60.     {                                        
  61.         Screen("\r\n*** End of Example ***");
  62.         return;
  63.     }
  64.  
  65.     Screen("    Setting the database directory...");
  66.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  67.     ChkRslt(rslt, "SetDirectory");
  68.  
  69.     // Initialize the table create descriptor.
  70.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  71.     strcpy(crTblDsc.szTblName, szTblName);
  72.     strcpy(crTblDsc.szTblType, szTblType);
  73.     crTblDsc.iFldCount     = uNumFields;
  74.     crTblDsc.pfldDesc      = fldDesc;
  75.  
  76.     Screen("    Creating the %s %s table...", szTblName, szTblType);
  77.  
  78.     rslt = DbiCreateTable(hDb, bOverWrite, &crTblDsc);
  79.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  80.     {
  81.         CloseDbAndExit(&hDb);
  82.         Screen("\r\n*** End of Example ***");
  83.         return;
  84.     }
  85.  
  86.     Screen("    Open the %s table...", szTblName);
  87.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  88.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  89.                         xltFIELD, FALSE, NULL, &hCur);
  90.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  91.     {
  92.         CloseDbAndExit(&hDb);
  93.         Screen("\r\n*** End of Example ***");
  94.         return;
  95.     }
  96.  
  97.     // Insert the records.
  98.     Screen("\r\n    Inserting the first record...");
  99.     InsertRec(hCur, 1);
  100.  
  101.     Screen("\r\n    Inserting the second record...");
  102.     InsertRec(hCur, 5);
  103.  
  104.     Screen("\r\n    Close the %s table...", szTblName);
  105.     rslt = DbiCloseCursor(&hCur);
  106.     ChkRslt(rslt, "CloseCursor");
  107.  
  108.     // Delete the table and its related files from disk.
  109.     Screen("    Delete the %s table...", szTblName);
  110.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  111.     ChkRslt(rslt, "DeleteTable");
  112.  
  113.     Screen("\r\n    Close the database and exit IDAPI...");
  114.     CloseDbAndExit(&hDb);
  115.  
  116.     Screen("\r\n*** End of Example ***");
  117. }
  118.  
  119. //=====================================================================
  120. //  Function:
  121. //          InsertRec(hCur, iID);
  122. //
  123. //  Input:  hCur    - Cursor to the table
  124. //          iID     - ID for the field
  125. //
  126. //  Return: Result of adding the record to the table.
  127. //
  128. //  Desc:   This function will add a record to the given table.
  129. //=====================================================================
  130. DBIResult
  131. InsertRec (hDBICur hCur, UINT16 iID)
  132. {
  133.     DBIResult   rslt;          // Value returned from IDAPI functions
  134.     CURProps    tblProps;      // Table properties
  135.     pBYTE       pRecBuf;       // Record buffer
  136.  
  137.     Screen("    Acquire the table's properties...");
  138.     rslt = DbiGetCursorProps(hCur, &tblProps);
  139.     ChkRslt(rslt, "GetCursorProps");
  140.  
  141.     // Allocate memory for the record buffer.
  142.     pRecBuf = (pBYTE) malloc(tblProps.iRecBufSize * sizeof(BYTE));
  143.     if (pRecBuf == NULL)
  144.     {
  145.         Screen("    Error - Out of memory");
  146.         return DBIERR_NOMEMORY;
  147.     }
  148.  
  149.     Screen("    Initialize record buffer...");
  150.     rslt = DbiInitRecord(hCur, pRecBuf);
  151.     ChkRslt(rslt, "InitRec");
  152.  
  153.     Screen("    Put the ID number into the record buffer...");
  154.     rslt = DbiPutField(hCur, 1, pRecBuf, (pBYTE) &iID);
  155.     ChkRslt(rslt, "PutField");
  156.  
  157.     // Fill the BLOB information pointer with 64K or more of text.
  158.     FillBlobInfo(hCur, pRecBuf);
  159.  
  160.     Screen("    Insert the record into the table...");
  161.     rslt = DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf);
  162.     ChkRslt(rslt, "InsertRecord");
  163.  
  164.     Screen("    Free the BLOB...");
  165.     rslt = DbiFreeBlob(hCur, pRecBuf, 2);
  166.     ChkRslt(rslt, "FreeBlob");
  167.  
  168.     // Free the record buffer.
  169.     free(pRecBuf);
  170.  
  171.     return rslt;
  172. }
  173.  
  174. //=====================================================================
  175. //  Function:
  176. //          FillBlobInfo(hCur, pRecBuf);
  177. //
  178. //  Input:  hCur        -   Cursor to the table
  179. //          pRecBuf     -   Record buffer
  180. //
  181. //  Return: Result of filling the BLOB field
  182. //
  183. //  Desc:   This function will fill the BLOB field of the given
  184. //          record buffer with a predefined string.
  185. //=====================================================================
  186. DBIResult
  187. FillBlobInfo (hDBICur hCur, pBYTE pRecBuf)
  188. {
  189.     pBYTE       Temp1 = NULL;
  190.     pBYTE       Temp2 = NULL;
  191.     pBYTE       Temp3 = NULL;
  192.     pBYTE       Temp4 = NULL;
  193.     UINT16      Len = 30000;
  194.     long        lWritePos = 0;
  195.     DBIResult   rslt;
  196.  
  197.     // Allocate memory for the four temporary buffers that will
  198.     //   total more than 64K of memory.
  199.     Temp1 = (pBYTE) malloc((sizeof(BYTE) * Len) + 1);
  200.     Temp2 = (pBYTE) malloc((sizeof(BYTE) * Len) + 1);
  201.     Temp3 = (pBYTE) malloc((sizeof(BYTE) * Len) + 1);
  202.     Temp4 = (pBYTE) malloc((sizeof(BYTE) * Len) + 1);
  203.     if ((Temp1 == NULL) || (Temp2 == NULL) ||
  204.         (Temp3 == NULL) || (Temp4 == NULL))
  205.     {
  206.         if (Temp1) free(Temp1);
  207.         if (Temp2) free(Temp2);
  208.         if (Temp3) free(Temp3);
  209.         if (Temp4) free(Temp4);
  210.         return DBIERR_NOMEMORY;
  211.     }
  212.  
  213.     Screen("    Filling the temporary strings with data...");
  214.     FillString(Temp1);
  215.     FillString(Temp2);
  216.     FillString(Temp3);
  217.     FillString(Temp4);
  218.  
  219.     Screen("    Open the BLOB for writing...");
  220.     rslt = DbiOpenBlob(hCur, pRecBuf, 2, dbiREADWRITE);
  221.     ChkRslt(rslt, "OpenBlob");
  222.  
  223.     Screen("    Put the first part of the BLOB into the BLOB field...");
  224.     rslt = DbiPutBlob(hCur, pRecBuf, 2, 0, strlen((pCHAR) Temp1), Temp1);
  225.     ChkRslt(rslt, "PutBlob1");
  226.  
  227.     // Set lWritePos to point to the position inside the BLOB.
  228.     lWritePos = (long)strlen((pCHAR) Temp1);
  229.  
  230.     Screen("    Put the second part of the BLOB into the BLOB field...");
  231.     rslt = DbiPutBlob(hCur, pRecBuf, 2, lWritePos, strlen((pCHAR) Temp2), Temp2);
  232.     ChkRslt(rslt, "PutBlob2");
  233.  
  234.     // Increment lWritePos to point to the position inside the BLOB.
  235.     lWritePos = lWritePos + (long)strlen((pCHAR) Temp2);
  236.  
  237.     Screen("    Put the third part of the BLOB into the BLOB field...");
  238.     rslt = DbiPutBlob(hCur, pRecBuf, 2, lWritePos, strlen((pCHAR) Temp3),
  239.                       Temp3);
  240.     ChkRslt(rslt, "PutBlob3");
  241.  
  242.     // Increment lWritePos to point to the position inside the BLOB.
  243.     lWritePos = lWritePos + (long)strlen((pCHAR) Temp3);
  244.  
  245.     Screen("    Put the final part of the BLOB into the BLOB field...");
  246.     rslt = DbiPutBlob(hCur, pRecBuf, 2, lWritePos, strlen((pCHAR) Temp4),
  247.                       Temp4);
  248.     ChkRslt(rslt, "PutBlob4");
  249.  
  250.     // Free all the buffers.
  251.     free(Temp1);
  252.     free(Temp2);
  253.     free(Temp3);
  254.     free(Temp4);
  255.  
  256.     return DBIERR_NONE;
  257. }
  258.  
  259. //=====================================================================
  260. //  Function:
  261. //          FillString(pString);
  262. //
  263. //  Input:  pString - String that will hold the random letters
  264. //
  265. //  Return: TRUE - String has been filled
  266. //
  267. //  Desc:   This function will fill the string with a predefined
  268. //          string.
  269. //=====================================================================
  270. BOOL
  271. FillString (pBYTE pString)
  272. {
  273.     CHAR    Filler[] = "This is a test";
  274.     UINT16  uNum  = 1900;   // Number of times to copy the string
  275.     UINT16  uLen;           // Length of the filler string
  276.     UINT16  i;              // Loop counter
  277.     pCHAR   pszTemp;        // Temporary pointer used in strcpy.
  278.  
  279.     // First copy the filler into the buffer.
  280.     pszTemp = strcpy((pCHAR)pString, Filler);
  281.  
  282.     uLen = strlen((pCHAR)pString);
  283.     
  284.     // Now fill the rest of the string.
  285.     for (i = 1; i < uNum; i++)
  286.     {
  287.         pszTemp = pszTemp + uLen;
  288.         pszTemp = strcpy((pCHAR)pszTemp, Filler);
  289.     }
  290.  
  291.     return TRUE;
  292. }
  293.  
  294.