[Gdal-dev] Library function for listing formats

Ray Gardener rayg at daylongraphics.com
Mon Nov 13 18:12:34 EST 2006


Matt Hanson wrote:
> It seems that there should be a function for listing all the suported 
> formats GDAL was compiled with.  I can't find one, does such a think exist?
> 
> I have an exe file that uses GDAL.  When the user calls my exe with 
> --help I want to be able to print a list of supported formats.

You can use something like the code below (you'll need to tweak some 
items for your platform, and implement some funcs like dashed_line(), 
but the basic listing feature should be obvious):


	logfile.printf("Using GDAL %d.%d.%d.%d\r\n",
		GDAL_VERSION_MAJOR,
		GDAL_VERSION_MINOR,
		GDAL_VERSION_REV,
		GDAL_VERSION_BUILD);

	logfile.printf("GDAL drivers registered:\r\n");

	GDALDriverManager* drivers = GetGDALDriverManager();
	const size_t numGDALdrivers = drivers->GetDriverCount();

	size_t i, longestDesc = 0, longestName = 0, longestExt = 0;

	for(i = 0; i < numGDALdrivers; i++)
	{
		GDALDriver* driver = drivers->GetDriver(i);
		char** papszMetadata = driver->GetMetadata();
		const char* psz = driver->GetDescription();
		if(psz != NULL)
			longestDesc = max(longestDesc, strlen(psz));
		psz = driver->GetMetadataItem(GDAL_DMD_LONGNAME);
		if(psz != NULL)
			longestName = max(longestName, strlen(psz));
		psz = driver->GetMetadataItem(GDAL_DMD_EXTENSION);
		if(psz != NULL)
			longestExt = max(longestExt, strlen(psz));
	}

	logfile.printf("    %-*s  %-*s  %-*s  %s\r\n",
		longestDesc, "Name",
		longestName, "Description",
		longestExt, "Ext(s)",
		"Capabilities");

	std::vector<char> buf[4];
	buf[0].resize(1 + longestDesc);
	buf[1].resize(1 + longestName);
	buf[2].resize(1 + longestExt);
	buf[3].resize(1 + 15);
	logfile.printf("    %s  %s  %s  %s\r\n",
		dashed_line(longestDesc, buf[0].begin()),
		dashed_line(longestName, buf[1].begin()),
		dashed_line(longestExt, buf[2].begin()),
		dashed_line(15, buf[3].begin()));

	for(i = 0; i < numGDALdrivers; i++)
	{
		GDALDriver* driver = drivers->GetDriver(i);

		CString strCaps = "read-only";
		char** papszMetadata = driver->GetMetadata();
		const BOOL bCopy = CSLFetchBoolean(
			papszMetadata, GDAL_DCAP_CREATECOPY, FALSE);
		const BOOL bCreate = CSLFetchBoolean(
			papszMetadata, GDAL_DCAP_CREATE, FALSE);
		if(bCopy && bCreate)
			strCaps = "create and copy";
		else if(bCopy)
			strCaps = "copy";
		else if(bCreate)
			strCaps = "create";

		logfile.printf(
			"    %-*s  %-*s  %-*s  %s\r\n",
			longestDesc, driver->GetDescription(),
			longestName, driver->GetMetadataItem(GDAL_DMD_LONGNAME),
			longestExt, driver->GetMetadataItem(GDAL_DMD_EXTENSION),
			(const char*)strCaps);
	}
	logfile.printf("\r\n");



Ray



More information about the Gdal-dev mailing list