Exemple #1
0
void
match_fuzzy(void) {
	int i;
	size_t len;
	Item *item;

	char *pos;

	len = strlen(text);

	matches = matchend = NULL;
	for(item = items; item && item->text; item++) {
		i = 0;
		for(pos = fstrchr(item->text, text[i]); pos && text[i]; i++, pos = fstrchr(pos+1, text[i]));
		if(i == len) appenditem(item, &matches, &matchend);
	}

	curr = sel = matches;
	calcoffsets();
}
void FUPluginManager::LoadPluginsInFolderName(const fstring& folderName, const fchar* _filter)
{
	// Append the wanted extension for the plugins.
	FUStringBuilder pluginFolder(folderName);
	fchar lastChar = folderName[pluginFolder.length() - 1];
	if (lastChar != '\\' && lastChar != '/') pluginFolder.append((fchar) '/');
	pluginFolder.append(FC("Plugins/"));
	pluginFolderName = pluginFolder.ToString();

	if (_filter == NULL || _filter[0] == 0) _filter = FC("*.*");
	do
	{
		const fchar* nextFilter = fstrchr(_filter, '|');
		fstring filter(_filter);
		if (nextFilter != NULL)
		{
			filter.erase(nextFilter - _filter);
			++nextFilter; // skip the pipe.
		}
		_filter = nextFilter;

		// Windows-only for now.
#if defined(WIN32)
		size_t filterLength = filter.length();
		// Iterate over all the filtered files within the given folder.
		ffinddata folderIterator;
		fstring searchString = pluginFolderName + filter;
		intptr_t folderHandle = ffindfirst(searchString.c_str(), &folderIterator);
		if (folderHandle != -1L)
		{
			int32 isDone = FALSE;
			while (isDone == FALSE)
			{
				bool keep = false;
				PluginLibrary* library = new PluginLibrary();
				library->filename = pluginFolderName + folderIterator.name;

				// work around for wildcards and 3 letter extensions that pick up 3+ letter extensions
				// e.g. "dir *.fvp" in command prompt on a directory with "a.fvp", "a.fvpa", and "a.dll" returns 
				// "a.fvp" and "a.fvpa"
				bool checkModule = true;
				if (filterLength > 3)
				{
					if ((filter.at(filterLength-4) == FC('.')) && (filter.at(filterLength-3) != FC('*')) &&
							(filter.at(filterLength-2) != FC('*')) && (filter.at(filterLength-1) != FC('*')))
					{
						size_t filepathLength = fstrlen(folderIterator.name);
						checkModule = (folderIterator.name[filepathLength-4] == filter.at(filterLength-4)) &&
								(folderIterator.name[filepathLength-3] == filter.at(filterLength-3)) &&
								(folderIterator.name[filepathLength-2] == filter.at(filterLength-2)) &&
								(folderIterator.name[filepathLength-1] == filter.at(filterLength-1));
					}
				}

				library->module = LoadLibrary(library->filename.c_str());
				if (checkModule && (library->module != NULL))
				{
					// Retrieve the necessary callbacks
					library->getPluginCount = (GetPluginCount) GetProcAddress(library->module, "GetPluginCount");
					library->getPluginType = (GetPluginType) GetProcAddress(library->module, "GetPluginType");
					library->createPlugin = (CreatePlugin) GetProcAddress(library->module, "CreatePlugin");
					keep = library->createPlugin != NULL && library->getPluginType != NULL && library->getPluginCount != NULL;
				}

				// This is a valid library.
				if (keep) loadedLibraries.push_back(library);
				else { SAFE_DELETE(library); }
				isDone = ffindnext(folderHandle, &folderIterator);
			}
			ffindclose(folderHandle);
		}

#elif defined(__APPLE__) || defined(LINUX)
		fm::string s_filter = TO_STRING(filter);
		if (s_filter.length() > 0 && s_filter.front() == '*') s_filter.erase(0, 1);
		if (s_filter.length() > 0 && s_filter.back() == '*') s_filter.pop_back();

		DIR* directory = opendir(TO_STRING(pluginFolderName).c_str());
		if (directory == NULL) continue;

		dirent* directoryEntry;
		while ((directoryEntry = readdir(directory)) != NULL)
		{
			if (directoryEntry->d_type == DT_DIR) continue; // skip sub-folders.
			if (strstr((const char*) directoryEntry->d_name, s_filter.c_str()) != NULL)
			{
				// We have a match.
				bool keep = false;
				PluginLibrary* library = new PluginLibrary();
				library->filename = pluginFolderName + TO_FSTRING((const char*) directoryEntry->d_name);
				fm::string libraryModuleFilename = TO_STRING(library->filename);
				DEBUG_OUT("Found dynamic library: %s\n", libraryModuleFilename.c_str());
				library->module = dlopen(libraryModuleFilename.c_str(), RTLD_NOW);
				if (library->module != NULL)
				{
					// Retrieve the necessary callbacks
					library->getPluginCount = (GetPluginCount) dlsym(library->module, "GetPluginCount");
					library->getPluginType = (GetPluginType) dlsym(library->module, "GetPluginType");
					library->createPlugin = (CreatePlugin) dlsym(library->module, "CreatePlugin");
					keep = library->createPlugin != NULL && library->getPluginType != NULL && library->getPluginCount != NULL;
				}

				// This is a valid library.
				if (keep) loadedLibraries.push_back(library);
				else { SAFE_DELETE(library); }
			}
		}
		closedir(directory);

#endif // WIN32
	} while (_filter != NULL);
}