home *** CD-ROM | disk | FTP | other *** search
- // BDE32 3.x - (C) Copyright 1996 by Borland International
-
- // TextExp.c
- #include "snipit.h"
-
- static const char szPXTblName[] = "CUSTOMER.DB";
- static const char szPXTblType[] = szPARADOX;
-
- static const char szTXTTblName[] = "CUSTOMER.TXT";
- static const char szTXTTblType[] = szASCII;
-
- //=====================================================================
- // Function:
- // TextExport();
- //
- // Description:
- // This example shows exporting data from a Paradox table
- // to a text file. The "customer.db" table will be used as the
- // source table, and a "customer.txt" table will be created as
- // the destination table. The DbiBatchMove function is used to
- // copy the records from one table to another.
- //=====================================================================
- void
- TextExport (void)
- {
- CURProps PDTblProps; // Table properties
- hDBIDb hDb; // Handle to the database
- hDBICur PDhCur; // Paradox cursor handle
- hDBICur TXThCur; // Text cursor handle
- pFLDDesc pPDFlds; // List of Paradox fields
- pFLDDesc pTXTFlds; // List of text fields
- CRTblDesc crTblDsc; // Table descriptor
- BOOL bOverWrite = TRUE; // Overwrite, yes/no flag
- DBIPATH filename; // Text table name.
- DBIResult rslt; // Return value from IDAPI functions
-
- Screen("*** Text Export Example ***\r\n");
-
- BREAK_IN_DEBUGGER();
-
- Screen(" Initializing IDAPI...");
- if (InitAndConnect(&hDb) != DBIERR_NONE)
- {
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen(" Setting the database directory...");
- rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
- ChkRslt(rslt, "SetDirectory");
-
- // Initialize the table descriptor
- memset(&crTblDsc, 0, sizeof(CRTblDesc));
- strcpy(crTblDsc.szTblName, szTXTTblName) ;
- strcpy(crTblDsc.szTblType, szTXTTblType) ;
-
- Screen(" Creating the text table...");
- rslt = DbiCreateTable(hDb, bOverWrite, &crTblDsc);
- if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen("\r\n Opening the %s Paradox table...", szPXTblName);
- rslt = DbiOpenTable(hDb, (pCHAR) szPXTblName, (pCHAR) szPXTblType,
- NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &PDhCur);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Note that the table type has to be specified with the delimiter
- // and separator since we are opening the table as a delimited text
- // file.
- Screen(" Opening the %s Text table...\r\n", szTXTTblName);
- rslt = DbiOpenTable(hDb, (pCHAR) szTXTTblName, "ASCIIDRV-\"-,",
- NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &TXThCur);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- rslt = DbiCloseCursor(&PDhCur);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- rslt = DbiGetCursorProps(PDhCur, &PDTblProps);
- ChkRslt(rslt, "GetProps");
-
- // Allocate memory for field descriptors.
- pPDFlds = (pFLDDesc) malloc(PDTblProps.iFields * sizeof( FLDDesc));
- pTXTFlds = (pFLDDesc) malloc(PDTblProps.iFields * sizeof( FLDDesc));
- if ((pTXTFlds == NULL) || (pPDFlds == NULL))
- {
- Screen(" Error - Out of memory");
- if (pPDFlds) free(pPDFlds);
- if (pTXTFlds) free(pTXTFlds);
- DbiCloseCursor(&PDhCur);
- DbiCloseCursor(&TXThCur);
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Change the translation mode to xltNONE to get the physical
- // field descriptors.
- rslt = DbiSetProp(PDhCur, curXLTMODE, xltNONE);
- ChkRslt(rslt, "SetProp");
-
- // Get the physical field descriptors of the Paradox table.
- rslt = DbiGetFieldDescs(PDhCur, pPDFlds);
- ChkRslt(rslt, "GetFieldDescs");
-
- // Change the translation mode to back to xltFIELD.
- rslt = DbiSetProp(PDhCur, curXLTMODE, xltFIELD);
- ChkRslt(rslt, "SetProp");
-
- // Convert the record structure to the text table format.
- rslt = DbiTranslateRecordStructure(PDTblProps.szTableType,
- PDTblProps.iFields, pPDFlds,
- (pCHAR) szTXTTblType,
- NULL, pTXTFlds, FALSE);
- ChkRslt(rslt, "TranslateRecordStruct");
-
- rslt = DbiSetFieldMap(TXThCur, PDTblProps.iFields, pTXTFlds);
- if (ChkRslt(rslt, "SetFieldMap") != DBIERR_NONE)
- {
- free(pTXTFlds);
- free(pPDFlds);
- DbiCloseCursor(&PDhCur);
- DbiCloseCursor(&TXThCur);
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- rslt = DbiSetToBegin(PDhCur);
- ChkRslt(rslt, "SetToBegin");
-
- rslt = DbiSetToBegin(TXThCur);
- ChkRslt(rslt, "SetToBegin");
-
- rslt = DbiBatchMove(NULL, PDhCur, NULL, TXThCur, batAPPEND,
- 0, NULL, NULL, NULL, 0, NULL, NULL,
- NULL, NULL, NULL, NULL,
- FALSE, FALSE, NULL, TRUE);
- ChkRslt(rslt, "Batch Move");
-
- rslt = DbiSetToBegin(TXThCur);
- ChkRslt(rslt, "SetToBegin");
-
- Screen(" Displaying the first 10 records of the %s text table...",
- szTXTTblType);
- DisplayInMemoryTable(TXThCur, 10);
-
- rslt = DbiGetProp(TXThCur, curFILENAME, &filename, sizeof(DBIPATH),
- NULL);
- ChkRslt(rslt, "GetProp");
-
- Screen("\r\n The TEXT table is at: %s", filename);
-
- // Cleanup
- free(pPDFlds);
- free(pTXTFlds);
-
- Screen("\r\n Closing the TEXT table...");
- rslt = DbiCloseCursor(&TXThCur);
- ChkRslt(rslt, "CloseCur");
-
- Screen("\r\n Closing the PARADOX table...");
- rslt = DbiCloseCursor(&PDhCur);
- ChkRslt(rslt, "CloseCur");
-
- Screen(" Close the database and exit IDAPI...");
- CloseDbAndExit(&hDb);
-
- Screen("\r\n*** End of Example ***");
- }
-