|
The  FATS matchcode commands create a full-text index with the content of your data tables or -files. This index enables FATS to find each data record by specification of any terms in fractions of a second. The following C/C++ example demonstrates the use of the FATS matchcode commands:
Every word and every number is incorporated into the index, an "inverted list" is generated.
"MC" Create Matchcode File (Define, create and open a Matchcode-Index-File.) | |
"MB" Build Matchcode (Build the Matchcode-Index.) | |
"O" Open Indexfile (Makes a index file available for access.) | |
"MS" Search in Matchcode (Query the matchcode index, and setup an result set.) |
/* FATS Extended Demo for Watcom-C FATS 02.30 (c) GCS Software, Udo Gertz 1993-1998 This program needs the extended version of FATS (FATSXWE.EXE) It tests the 'MC', 'MB', 'MS' and 'MA' commands */ #include <stdio.h> #include <io.h> #include <fcntl.h> #include <sys\types.h> #include <sys\stat.h> #include <stdlib.h> #include <string.h> #include <mem.h> #include "FATS.h" /* Calling FATS All commands provided by FATS can be executed with one function: unsigned long __cdecl FATSCALL(char *szCmnd, unsigned short *uErrorcode, char *szFATSkey); The meaning of the used parameters: szCmnd With this command string you specify the actual FATS command. The available commands are described in the user manual. nErrorcode Your application must always pass this variable as the status parameter on a FATS call. After the FATS call, the application should always check the value of this variable. FATS returns a errorcode of 0 after a successful operation. FATS indicates any errors which occur during processing by returning a nonzero value in the errorcode variable. In the manual you can find a list of all FATS errorcodes and their possible causes. szReturnKey This variable will contain the key value of a found key after any normal search command (S,G,F,L,N,P,A,E). in the case of a matchcode command: If a primary key was generated during the creation of the matchcode index file with the "K#:#" flag, it is made available via this variable to the application program. Return Value: Record Number */ struct custdat { char DELETEDMARK; char ID[6]; char NAME[26]; char JOB[26]; char STREET[26]; char ZIP[6]; char CITY[21]; }; char szCmnd[512]; char szFATSkey[256]; unsigned short uFATSError; unsigned long dwFATSRecno; struct custdat cust; int main () { int hCustomer; unsigned long dwCurRecno; char cChar; printf( "\nFATS Extended testprogram (Command 'MC', 'MB', 'MS' and 'MA')\n" ); printf( "\nThis Test Program was designed for Watcom C\n\n" ); printf( "To compile and link the test program, use the following command:\n\n" ); printf( " WCL /m[?] /l=dos /bt=dos TST2_ENG.C FATSXC[?]R.LIB \n" ); printf( " [?] memory model: s (small) , m (medium), c (compact), l (large)\n\n" ); printf( "\nPlease press the [ENTER] key ...\n\n" ); cChar = getchar(); /* Load/Unload the FATS Extended Workstation Engine: Load: FATSXWE Unload: FATSXWE -unload */ /* -------> Open data file */ printf( "Opening data file ...\n" ); hCustomer = open( "CUSTOMER.DAT", O_RDWR ); setmode( hCustomer, O_BINARY ); /* ======================================================================== Creating Matchcode Index ======================================================================== */ /* -------> Creating Matchcode Index File With the command "MC" Create Matchcode File, the most important query facilities are determined already while creating the matchcode index file. With the search-group flag ("I#"), several logically related data columns can be registered in a common index so that a query to this index resp. search group extends automatically over all these columns. A matchcode file manages up to 32 search groups that can be used for joined queries (using the "AND"-operator). In this example the following search groups are defined: Search Group Fields I1 NAME I2 JOB I3 ZIP & CITY The syntax of the command string: szCmnd = "MC\{FileName}\{Flags}\{FileNo}\{Col1def}[\{Col2def}]" FileName Filename, perhaps with an additional path (i.e. C:/DATA/CUSTOMER.FMS or CUSTOMER.FMS) Flags Reserved, not used at the moment FileNo File number Col#def Definition of data column # (flags, separated by comma). The content of the corresponding data columns is transferred the commands "MB", "MI" and "MD" later in the order determined by this command. I# The content of the data column becomes part of search group #. You can combine several columns into a logical search group (e.g. first name, surname). C The content of the data column is edited for word overall searching, i.e. a search for "motorca" e.g. finds "motorcar" and "motor caravan". N Numbers are handled as words, i.e. during a search according to "150", "12150" e.g. is also found. Further adjustments are possible and described in detail in the user manual. */ printf( "\n" ); printf( "Creating matchcode index file ...\n" ); dwFATSRecno = FATSCALL( "MC\\CUSTOMER.FTS\\\\1\\I1\\I2\\I3", &uFATSError, szFATSkey ); if ( uFATSError ) { printf( "\n" ); if ( uFATSError == -1 ) { printf( "FATS workstation engine is not activated.\n" ); printf( "Please start the program by entering FATSXWE at the command line.\n" ); } else { printf( "FATS Errorcode: %i (Command: MC)\n", uFATSError ); } return ( uFATSError ); } /* -------> Insert text into the matchcode index After the matchcode file was generated, the content of the data columns may be with the command "MB" Build Matchcode inserted into the matchcode index. The position of the data columns within the command string ("Col#data") corresponds to that with the call of command Create Matchcode File ("MC") determined definition. The syntax of the command string: szCmnd = "MB\{FileNo}\{RecNo}\{Col1data}[\{Col2data}[\{Col3data}]]" FileNo File number RecNo <> 0 Record- resp. id-number == 0 Stop Build, no more records Col#data Content of data column # The following sample program code indexes contents of the entire data file within a loop: */ printf( "\n" ); printf( "Building matchcode index\n" ); printf( "\nPlease press the [ENTER] key ...\n\n" ); cChar = getchar(); dwCurRecno = 0L; /* While indexing sequential data resources, the number specified via "RecNo" should correspond to the physical record number i.e. the first data record has number 1, the second record number 2.., in the case of data records marked as deleted, the content of the data columns is not specified ("MB\{FileNo}\{RecNo}"). The Build Matchcode (MB) command demands an ascending number as a "RecNo" argument i.e. the matchcode can not usually be indexed in the logical order of an index-key. The example program MCSORT.c shows how an application program might use the "MC" command to create the matchcode index in sorted order. */ for ( ;; ) { lseek( hCustomer, dwCurRecno * sizeof(cust), SEEK_SET ); if ( read(hCustomer, &cust, sizeof(cust)) == sizeof(cust) ) { dwCurRecno++; sprintf( szCmnd, "MB\\1\\%lu", dwCurRecno ); if ( !cust.DELETEDMARK ) { sprintf( &szCmnd[ strlen(szCmnd) ], "\\%s\\%s\\%s %s", cust.NAME, cust.JOB, cust.ZIP, cust.CITY ); } dwFATSRecno = FATSCALL( szCmnd, &uFATSError, szFATSkey ); if ( uFATSError ) { /* If an error occurred during the execution of the "MB" command, the matchcode index file is already closed by FATS. */ printf( "FATS Errorcode: %i (Command: MB)\n", uFATSError ); break; } printf( " %s --> RecNo %lu\n", cust.NAME, dwCurRecno ); } else { /* After the last record was inserted the creating process has to be terminated with the command "MB\{FileNo}\0". Because this command closes the matchcode index file you don't have to do a close command. */ dwFATSRecno = FATSCALL( "MB\\1\\0", &uFATSError, szFATSkey ); break; } } if ( !dwCurRecno ) { printf( "\n" ); printf( "Error opening the file CUSTOMER.DAT\n" ); printf( "Please create the file using the test program TST1_ENG.C\n" ); return ( -1 ); } /* ======================================================================== Matchcode Search ======================================================================== */ /* -------> Open matchcode index file With the command "O" Open Indexfile you open an existing matchcode index file with the opening flags defined with the command "Y" Auto Refresh. After the file was opened it can be accessed under the file number you specified. */ dwFATSRecno = FATSCALL( "O\\CUSTOMER.FTS\\1", &uFATSError, szFATSkey ); /* -------> Search in Search-Group 1 (NAME) The command "MS" Search in Matchcode searches the matchcode index and fills the result set with the record- resp. ID-numbers of data records which include the searched terms. If FATS finds the requested terms, it returns the record- resp. ID-number of the first hit in the "dwRecno" variable and a errorcode of 0. The result set can then be read out with the browser commands ("MA", "ME", "MN", "MP" ...). The syntax of the command string: szCmnd = "MS\{FileNo}\{Flags}\{Total}\{SearchGrp1}[\{SearchGrp2}...]" FileNo File number Flags The search procedure can be adjusted by the specification of different flags. You find a description of the flags in the command reference. Total Maximum number of hits in the result set: 0 All hits are set into the result set. This regulation works very fast, however, no sorting occurs by means of hit quality. >0 It is attempted to put the specified number of hits into the result set. These hits are subjected to a sorting concerning their quality and then put into one of 7 sorting groups. SearchGrp# The in search group # searched terms. Different FATS commands can be used for further processing of the result table, depending on the program logic. The following example prints all hits on the output device: */ printf( "\n" ); printf( "We now search for all customers with the first name Michael.\n" ); printf( "\nPlease press the [ENTER] key ...\n\n" ); cChar = getchar(); strcpy( szCmnd, "MS\\1\\\\0\\michael" ); do { dwFATSRecno = FATSCALL( szCmnd, &uFATSError, szFATSkey ); if ( !uFATSError ) { lseek( hCustomer, (dwFATSRecno - 1) * sizeof(cust), SEEK_SET ); if ( read(hCustomer, &cust, sizeof(cust)) == sizeof(cust) ) { printf( "%s | %s\n%s %s (#%lu)\n\n", cust.NAME, cust.JOB, cust.ZIP, cust.CITY, dwFATSRecno ); } sprintf( szCmnd, "MA\\1\\%lu", dwFATSRecno ); } } while ( !uFATSError ); /* The following browser commands are supported by FATS: "MF" - Get First Result "ML" - Get Last Result "MP" - Get Previous Result "MN" - Get Next Result "MA" - Get Next Result After "ME" - Get Previous Result Before */ /* --------> Joined search in Search-Groups 2 (JOB) and 3 (ZIP/CITY) */ printf( "\n" ); printf( "We are now doing a joined search over the search groups\n" ); printf( "2 (Job) and 3 (Zip, City) to find all people from nevada\n" ); printf( "who are related to the transportation industry.\n" ); printf( "\nPlease press the [ENTER] key ...\n\n" ); cChar = getchar(); strcpy( szCmnd, "MS\\1\\\\0\\\\trans\\nev" ); do { dwFATSRecno = FATSCALL( szCmnd, &uFATSError, szFATSkey ); if ( !uFATSError ) { lseek( hCustomer, (dwFATSRecno - 1) * sizeof(cust), SEEK_SET ); if ( read(hCustomer, &cust, sizeof(cust)) == sizeof(cust) ) { printf( "%s | %s\n%s %s (#%lu)\n\n", cust.NAME, cust.JOB, cust.ZIP, cust.CITY, dwFATSRecno ); } sprintf( szCmnd, "MA\\1\\%lu", dwFATSRecno ); } } while ( !uFATSError ); /* --------> Close matchcode index file */ dwFATSRecno = FATSCALL( "K\\1", &uFATSError, szFATSkey ); close( hCustomer ); return ( 0 ); }
© 2008 GCS Software, Udo Gertz