⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexcmd.cpp

📁 These are precompiled demonstration programs of the Onix toolkit. They do not include the Onix tool
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	if ( ext1 != NULL )
		loc1 = ext1 + strlen(ext1) -1;
	
	if ( ext2 != NULL )
		loc2 = ext2 + strlen(ext2) -1;

	if ( ext3 != NULL )
		loc3 = ext3 + strlen(ext3) -1;


	while ( sloc >= string ) {
		

		// Basically we only check if the extension string is non-NULL
		// This way we can simply set to NULL those extensions that 
		// have a character that don't match.  We then see if we've
		// gone through the complete extension.  If so, then we're
		// done.  Otherwise decrement our pointer. 

		if ( ext1 != NULL ) {
			if ( *loc1 != *sloc ) {
				ext1 = NULL;
			}
			else {
				if ( loc1 == ext1 )
					return 1;

				loc1--;
			}
		}
			


		if ( ext2 != NULL ) {
			if ( *loc2 != *sloc ) {
				ext2 = NULL;
			}
			else {
				if ( loc2 == ext2 )
					return 1;

				loc2--;
			}
		}
			

		if ( ext3 != NULL ) {
			if ( *loc3 != *sloc ) {
				ext3 = NULL;
			}
			else {
				if ( loc3 == ext3 )
					return 1;

				loc3--;
			}
		}
		
		sloc--;
		
		// If all three extensions are NULL we retunr 0 (failure)

		if (( ext1 == NULL ) && ( ext2 == NULL ) && ( ext3 == NULL ) ){
			return 0;
		}

	}

}
#ifdef __ONIX_WINDOWS__

// isdir
// -----
// Windows only function that tells us if a file handle is a directory or
// not

int isDir( WIN32_FIND_DATA *theFile )
{


	return ( ((theFile->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ) && ( theFile->cFileName[0] != '.' ));
}

#endif


// scandir
// -------
//
// If it is given a string ending in a '\' it assumes that it is indexing a
// directory (all files ending in .txt).  Otherwise it assumes that it is
// indexing a file path.  It uses some windows functions to work - this is
// based on the MVC compilier and may need modified slightly for other 
// compiliers or platforms.  However the main code is fairly self-explanatory.
// It returns 0 for success and -1 for failure.

int scandir( char *dir)
{

	printf("Scanning directory: %s  \n", dir );


#ifdef _WINDOWS_VERSION_
	WIN32_FIND_DATA filedata;		// file data structure
	HANDLE			filehandle;		// search handle
	int				filesleft;
	char			tempfile[255], passfile[255];
	int				path_size = strlen( dir );

	// check to see if it is a directory or a file

	if ( dir[ path_size -1 ] != '\\' ) {

		filehandle = FindFirstFile ( dir, &filedata);

		if ( ! isDir( &filedata ) ) {

			FindClose( filehandle );
			// It's a file so just index the file (first file)

			indexfile( dir );
			return 0;
		}

		FindClose( filehandle );


	}

	// We have a directory, so get a list of all the
	// files ending in .txt in this directory.

	strcpy(tempfile, dir);

	strcpy(passfile, dir);


	// add a \ character to the end if it isn't already there

	if ( dir[ path_size -1 ] != '\\' ) {
		strcpy( tempfile + strlen( tempfile ), "\\");
		strcpy( passfile + strlen( passfile ), "\\");
		path_size = path_size + 1;
	}

	// Uncomment this line if you are looking at just one type of file
	// It will speed up the scanning of the directory a lot. . .
	
	// strcpy( tempfile + strlen( tempfile ), "*.txt");

	// Comment this line if you don't want to search every file.  If your
	// directory has a mixture of files then this will slow it down a 
	// fair bit.

	strcpy( tempfile + path_size, "*");

	filehandle = FindFirstFile ( tempfile, &filedata);
	
	if (filehandle == INVALID_HANDLE_VALUE ) {
		FindClose ( filehandle );
		return -1;
	}

	// Generate the path
	strcpy( passfile + path_size, filedata.cFileName );


	if ( isDir( &filedata) ) {
		// it is a directory - if appropriate recurse

		if ( Recurse )
			scandir( passfile );
	}
	else {

		if ( CompareExt(filedata.cFileName, ".txt", ".text", NULL ) ){
			// Index the first file 
			indexfile( passfile );
		}
	}

	// Iterate through all the other files.
	filesleft = FindNextFile( filehandle, &filedata );
	while ( filesleft ) {

		// Generate the path
		strcpy( passfile + path_size, filedata.cFileName );

		if ( isDir( &filedata) ) {
			// it is a directory - if appropriate recurse

			if ( Recurse )
				scandir( passfile );

			
		}
		else {
			// We check the extensions.  Not checking will speed things up somewhat.

			if ( CompareExt(filedata.cFileName, ".txt", ".text", NULL ) ){

				indexfile( passfile ); // note pass 0 as not first file
			}
		}

		filesleft = FindNextFile( filehandle, &filedata );

	} // while

	FindClose( filehandle );

#else
	// POSIX version 

	// OK, this recurses the directories getting all the files

	DIR		*the_dir;			// the directory information
	struct	stat	dir_stats;	// info about the directory inode
	struct	dirent	*the_file;	// a file/directoy

	char	pathname[255];	// full path

	int		len;			// length of path

	int		err_status;		// error flag



	// first find out if we have a directory
	the_dir = opendir( dir );

	if ( the_dir == NULL ) {
		// not a directory - return
		printf("\nCouldn't Open directory");
		return -1;
	}

	// iterate through all the files in the directory
	while ( ( the_file = readdir( the_dir ) ) != NULL ) {
		
		// get the full pathname

		strcpy( pathname, dir );

		len = strlen(pathname);

		pathname[len] = '/';

		strcpy( pathname + len + 1, the_file->d_name);

		printf( "\n%s\n", pathname );

		// ignore special directories

		if ( strcmp( the_file->d_name, "." ) && strcmp( the_file->d_name, ".." ) ) {
			
			if ( stat( pathname, &dir_stats ) ) {
				// couldn't get stats
				printf("\nError getting stats\n");
				closedir( the_dir );
				return -1;
			}

			// check to see if it is a directory

			if ( S_ISDIR( dir_stats.st_mode) ) {
				// call ourselves recursively on directories
				scandir( pathname );

			}	
			else {
				// it's a file

				// if you want to filter for only a certain kind of file
				// put your filter here.

				indexfile( pathname );

				first_file = 0;		// first file is 1 for the first file
									// indexed and 0 there after.  This has
									// to do with whether we create a new
									// record or not for the file.  See
									// indexfile for more info.
			}

		}
	}

	closedir( the_dir );

#endif
	return 0;
}


// print_help
// ----------
//
// Prints out help information.

void print_help()
{
	printf( "\n");
	printf( "ONIX COMMAND LINE DEMO: Indexer\n");
	printf( "-------------------------------\n");
	printf( "\n");
	printf( "index [-nvt] [-b #] [-a #] [help] indexname files\n");
	printf( "\n");
	printf( "  -n    (optional) create a new index \n");
	printf( "          (old index is overwritten if it is the same name)\n");
	printf( "  -v    (optional) verbose mode (displays more information)\n");
	printf( "  -t    (optional) store text in index\n");
	printf( "  -b #  (optional) define record break character \n");
	printf( "           (default = 10 '\\n') \n");
	printf( "  -a #  (optional) define alternative break character \n");
	printf( "           (default = 0) \n");
    printf( "  help  (optional) prints out this help information \n");
	printf( "\n");
	printf( "  indexname   full path to the index being created or opened \n");
	printf( "  files    a list of files to index or a directory to index \n");
	printf( "\n");
	printf( "Examples\n");
	printf( "\n");
	printf( "index -n c:\\index\\newindex.ix c:\\texts\\myfile.txt \n");
	printf( "   Index myfile.txt and put it in a new index named newindex.\n");
	printf( "\n");
	printf( "index c:\\index\\myindex.ix C:\\texts\\ \n");
	printf( "   Index all the files in c:\\texts and store the index in myindex.ix.\n");
	printf( "\n");
	printf( "index -a 16 c:\\index\\myindex.ix c:\\texts\\myfile.txt \n");
	printf( "   Index the file but use ascii 16 as the record break character.\n");
	printf( "\n");

}


// test_code
// ---------
//
// Run if there are no options given to the command - generally if it is 
// being run in an IDE.  Generally you can use this to tweak things for 
// your own understanding.  Just set the IndexPath here.
//
// This makes for a nice for an automated test program.  

void test_code()
{
	New_Index = true;
	Store_Text = true;
	Verbose_Mode = true;
	Recurse = true;

	printf( "\n Self Running Demo\n");
	printf(   "-------------------\n");
	printf("\n");
	printf("Indexing files to ");

	// Set the path for your index here
	strcpy( (char *) IndexPath, ".\\test_index.idx");

	printf( (char*) IndexPath );

	printf("\n");



	// Set the record break characters
	Record_Break1 = 16;
	Record_Break2 = '\n';
	Record_Break3 = '\r';

	if( prepare_indexer() != 0 ) 
		return ;

	// Index everything in our directory - change the directory to
	// whatever you wish to test on

	scandir( "z:\\texts\\" );

	if( close_indexer() ) {
		printf( "error closing indexer\n");
	}

	New_Index = false;

}



// indexcmd [-n] [-v] [help] indexname filelist
//
// -n			(optional) create new index 
// -v			(optional) verbose mode (displays diagnostic info)
// -t			(optional) store text in index
// -b #			(optional) character to break on
// -a #			(optional) second character to break on
// -c #			(optional) third character to break on
//
// help			print help
// indexname	the name of the index to be created or opened
// list     a list of file names to be indexed

int main(int argc, char *argv[])
{
	Sleep(5);

	// if there are no arguments run the test program
	if ( argc == 1 ) {
		test_code();
		return 0;
	}

	enum arguments { in_options, in_index, in_files };
	int  where_in = in_options;

	// default break characters
	Record_Break1 = 16;
	Record_Break2 = 0;
	Record_Break3 = 0;

	Recurse = false;		// by default don't recurse
	New_Index = false;		// by default open existing index
	Store_Text = false;		// by default don't store text in index

	// check to see if it is asking for help
	if ( argc <= 2 ) {
		print_help();
		return 0;
	}



	//---------------------------------------------------------
	// Handle Arguments
	//---------------------------------------------------------

	char	*loc;
	int		count;
	for ( count = 1; count < argc; count++ ) {

		if ( argv[ count ][0] == '-' ) {
			
			loc = argv[ count ];
			loc++;

			while ( isalpha( *loc ) ) {
				switch (*loc) {
				case 'v':		Verbose_Mode = true;
								break;

				case 'n':		New_Index = true;
								break;
		
				case 't':		Store_Text = true;
								break;

				case 'r':		Recurse = true;
								break;

				case '?':		print_help();
								break;

				case 'c':		count++;
								Record_Break3 = atoi( argv[ count ] );
								break;

				case 'b':		count++;
								Record_Break1 = atoi( argv[ count ] );
								break;

				case 'a':		count++;
								Record_Break2 = atoi( argv[ count ] );
								break;

				} // switch
				loc++;

			} // while isalpha

			continue; 

		} // if arv = '-'


		// our first non-optional argument: the index path
		if ( where_in != in_files ) {

			strcpy( (char *) IndexPath, argv[ count ]);

			// mark that all arguments are now file names
			where_in = in_files;	

			// setup the indexer
			if( prepare_indexer() != 0 )
				return 0;

			continue;

		} // index name

		// handle the file names / directories
		if ( where_in == in_files ) {

			scandir( argv[ count ] );	
		} 
	
	} // for argv

	

	if( close_indexer() ) {
		printf( "error closing indexer\n");
	}
	

	return 0;

}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -