📄 demo1.cpp
字号:
} // while
FindClose( filehandle );
return 0;
}
// select_file
// -----------
//
// Presents a menu telling you about the current index and allows you
// to select a file to index and append to the current index.
void select_file()
{
printf("\n\n\n");
printf( "INDEX FILE\n");
printf( "----------\n");
printf( "\n");
// Print out the index info
printf( "Current Index: %s\n", IndexPath );
if ( New_Index == true ) {
printf( "Mode: Creating new index\n");
} else {
printf( "Mode: Adding to existing index\n");
}
if ( Store_Text == true ) {
printf( " Storing indexed text in the index\n");
} else {
printf( " Storing reference to document in the index\n");
}
printf( "\n\n");
printf( "Please type the path and name of the file you wish to index.\n");
printf( "Be sure to include the extension of the file in the file's\n");
printf( "name. (i.e. c:\\my documents\\mytext.doc)\n");
printf( "\n\n");
fgets( Choice, 80, stdin);
strcpy( (char*) FilePath, (char *) Choice );
indexfile( (char*) FilePath );
printf("\n\n\n");
}
// select_dir
// ----------
//
// Presents a menu telling you about the current index and allows you
// to select a directory to index. All the files in the directory
// that end with .txt are indexed and appended to the current index.
void select_dir()
{
printf("\n\n\n");
printf( "INDEX DIRECTORY\n");
printf( "---------------\n");
printf( "\n");
printf( "Current Index: %s\n", IndexPath );
if ( New_Index == true ) {
printf( "Mode: Creating new index\n");
} else {
printf( "Mode: Adding to existing index\n");
}
if ( Store_Text == true ) {
printf( " Storing indexed text in the index\n");
} else {
printf( " Storing reference to document in the index\n");
}
if ( Recurse == true ) {
printf( " Index all sub-directories\n");
} else {
printf( " Do not index sub-directories\n");
}
printf( "\n\n");
printf( "Please type the path to the directory you wish to index.\n");
printf( "This will only index files in that directory with an\n");
printf( "extension of .txt and no other files.\n");
printf( "\n");
fgets( Choice, 80, stdin);
strcpy( (char*) FilePath, (char*) Choice );
scandir( (char*) FilePath );
}
// add_files
// ---------
//
// A general menu that allows you to specify how you will add new indexed
// files to the indexer.
void add_files()
{
pick = -1; // Just a global we use to select our choice. By setting
// it to -1 we make sure we don't quit yet.
if ( IndexPath[0] == 0 ) {
printf ( "You must first select an index before indexing files.\n\n");
return;
}
if ( State == inIndexing ) {
printf("\nClosing and updating old index...\n");
close_indexer();
printf("Opening new index...\n");
prepare_indexer();
}
else if ( State == inQuery ) {
printf("\nEnding old retrieval session...\n");
close_retrieval();
printf("Opening new index...\n");
prepare_indexer();
State = inIndexing;
}
else if ( State == inNothing ) {
printf("Opening new index...\n");
prepare_indexer();
State = inIndexing;
}
while ( pick != 0 ) {
printf("\n\n\n");
printf( "INDEX FILES\n" );
printf( "-----------\n" );
printf( "\n");
printf( "Current Index: %s\n", IndexPath );
if ( New_Index == true ) {
printf( "Mode: Creating new index\n");
} else {
printf( "Mode: Adding to existing index\n");
}
if ( Store_Text == true ) {
printf( " Storing indexed text in the index\n");
} else {
printf( " Storing reference to document in the index\n");
}
printf( "\n\n");
printf( "(1) Add a single file to the index\n");
printf( "(2) Add all text files (.txt) in a directory to the index\n");
printf( "(3) Add all text files in a directory and all sub-directories\n");
printf( " to the index\n");
printf( "\n");
printf( "(0) Quit\n");
printf( "\n\n");
fgets( Choice, 80, stdin);
pick = atoi( Choice );
switch ( pick ) {
case 1:
Recurse = false;
select_file();
break;
case 2:
Recurse = false;
select_dir();
break;
case 3:
Recurse = true;
select_dir();
break;
default:
break;
}
} // while
// if we exit and were indexing, finish up the indexing session
if ( State == inIndexing ) {
printf("\nClosing and updating index...\n");
close_indexer();
State = inNothing;
}
}
//------------------------------------------------------------------------------------
// Main Menu
//------------------------------------------------------------------------------------
// main_prompt
// -----------
//
// Basically our main menu. The only thing to be aware of is that we only close
// the indexing session when we start searching or select a new index. This means
// that merging isn't done immediately when we add new information to the index.
// This is the most efficient way to add information to the index. Each indexing
// session is stored in an individual partition and you want to minimize the number
// of partitions in your index. Onix does have a function call that optimizes the
// index, but you should still try to minimize the amount it needs to optimize.
void main_prompt()
{
pick = -1; // just a nice value that means nothing but keeps us in the loop
while ( pick != 0 ) {
printf("\n\n\n\n");
printf( "SET INDEX\n" );
printf( "---------\n" );
printf( "\n" );
// print index state information
if ( IndexPath[0] == 0 ) {
printf("No Current Index Selected\n");
} else {
printf( "Current Index: %s\n", IndexPath );
if ( New_Index == true ) {
printf( "Mode: Creating new index\n");
} else {
printf( "Mode: Adding to existing index\n");
}
if ( Store_Text == true ) {
printf( " Storing indexed text in the index\n");
} else {
printf( " Storing reference to document in the index\n");
}
}
printf( "\n\n" );
printf( "(1) Select a new index that will be created\n");
printf( "(2) Select an existing index to open\n");
printf( "\n" );
printf( "(3) Add files or directories to this index\n");
printf( "\n" );
printf( "(4) Search this index\n");
printf( "\n" );
printf( "(0) Quit\n");
printf( "\n\n");
fgets( Choice, 80, stdin);
pick = atoi( Choice );
switch ( pick ) {
case 1:
// Create New Index
printf("\n\n\n");
printf("Please enter the path and filename for the index you\n");
printf("wish to create. If there is an index of the same\n");
printf("name and path it will be overwritten.\n");
printf("\n");
printf("Note that the index will not be created until you index\n");
printf("some files.\n");
printf("\n");
// New_Index tells us that we are creating a new index rather than
// appending. See prepare_indexer for more information
New_Index = true;
fgets( Choice, 80, stdin); // get the pathname
// If there was a previously opened index we must close it.
// Note that this merges all the previous indexing info into the
// general index.
strcpy( (char*) IndexPath, (char*) Choice );
if ( theStatus < 0 ) {
IndexPath[0] = 0;
State = inNothing;
}
printf("\n\n");
break;
case 2:
// Open New Index
printf("\n\n\n");
printf("Please enter the path and filename for the index you\n");
printf("wish to add additional files to.\n");
printf("\n");
// New_Index tells us that we are creating a new index rather than
// appending. See prepare_indexer for more information
New_Index = false;
fgets( Choice, 80, stdin); // get the pathname
// If there was a previously opened index we must close it.
// Note that this merges all the previous indexing info into the
// general index.
strcpy( (char*) IndexPath, (char*) Choice );
if ( theStatus < 0 ) {
IndexPath[0] = 0;
State = inNothing;
}
printf("\n\n");
break;
case 3:
// Index Files
add_files();
pick = -1; // set this so we don't accidentally exit loop
break;
case 4:
// Search Index
if ( State == inIndexing ) {
// Shouldn't ever get here - but just in case
// I changed the code elsewhere
printf("\nClosing and updating old index...\n");
close_indexer();
printf("Opening new index...\n");
prepare_retrieval();
State = inQuery;
}
else if ( State == inNothing ) {
printf("Opening new index...\n");
prepare_retrieval();
State = inQuery;
}
if ( theStatus < 0 ) {
IndexPath[0] = 0;
State = inNothing;
}
search();
pick = -1; // set this so we don't accidentally exit loop
break;
default:
// Other - note that if we entered 0 we'll exit the loop
break;
}
}
// If we are done and have opened an index, close that index
if ( State == inIndexing ) {
printf("\nClosing and updating old index...\n");
close_indexer();
State = inNothing;
}
else if ( State == inQuery ) {
printf("\nEnding old retrieval session...\n");
close_retrieval();
State = inNothing;
}
}
// main
// ----
int main()
{
// We put all our globals in here so that they can easily be kept track of.
set_globals();
// We open up the actual indexing object. This is only done once for the entire
// demo, so we moved it out of the initializing functions (which are called
// frequently). This *must* be done prior to using any Onix function. Basically
// this is creating Onix. Note that these passcodes are not the real ones and
// will not function. The passcodes determine what Onix features are available
// and when they time out (if at all)
theIndexManager = ixCreateIndexManager(PASSCODE1, PASSCODE2, &theStatus);
main_prompt();
// Now that we are finished with the demo, free our Onix object.
ixDeleteIndexManager(theIndexManager, &theStatus);
if ( theStatus < 0 ) {
printf( "\nError Destroying Index Manager: %d \n", theStatus);
}
// Print out a nice farewell
printf("\n\n\n");
printf("Thank you for trying Onix. Please visit our web page for more information.\n");
printf("www.lextek.com\n");
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -