1 : /******************************************************************************
2 : * $Id: commonutils.cpp 24950 2012-09-22 13:54:36Z rouault $
3 : *
4 : * Project: GDAL Utilities
5 : * Purpose: Common utility routines
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, Even Rouault
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "commonutils.h"
31 : #include "cpl_string.h"
32 : #include "gdal.h"
33 :
34 : CPL_CVSID("$Id: commonutils.cpp 24950 2012-09-22 13:54:36Z rouault $");
35 :
36 : /* -------------------------------------------------------------------- */
37 : /* CheckExtensionConsistency() */
38 : /* */
39 : /* Check that the target file extension is consistant with the */
40 : /* requested driver. Actually, we only warn in cases where the */
41 : /* inconsistency is blatant (use of an extension declared by one */
42 : /* or several drivers, and not by the selected one) */
43 : /* -------------------------------------------------------------------- */
44 :
45 355 : void CheckExtensionConsistency(const char* pszDestFilename,
46 : const char* pszDriverName)
47 : {
48 :
49 355 : char* pszDestExtension = CPLStrdup(CPLGetExtension(pszDestFilename));
50 355 : if (pszDestExtension[0] != '\0')
51 : {
52 355 : int nDriverCount = GDALGetDriverCount();
53 355 : CPLString osConflictingDriverList;
54 837 : for(int i=0;i<nDriverCount;i++)
55 : {
56 836 : GDALDriverH hDriver = GDALGetDriver(i);
57 : const char* pszDriverExtension =
58 836 : GDALGetMetadataItem( hDriver, GDAL_DMD_EXTENSION, NULL );
59 836 : if (pszDriverExtension && EQUAL(pszDestExtension, pszDriverExtension))
60 : {
61 354 : if (GDALGetDriverByName(pszDriverName) != hDriver)
62 : {
63 0 : if (osConflictingDriverList.size())
64 0 : osConflictingDriverList += ", ";
65 0 : osConflictingDriverList += GDALGetDriverShortName(hDriver);
66 : }
67 : else
68 : {
69 : /* If the request driver allows the used extension, then */
70 : /* just stop iterating now */
71 354 : osConflictingDriverList = "";
72 354 : break;
73 : }
74 : }
75 : }
76 355 : if (osConflictingDriverList.size())
77 : {
78 : fprintf(stderr,
79 : "Warning: The target file has a '%s' extension, which is normally used by the %s driver%s,\n"
80 : "but the requested output driver is %s. Is it really what you want ?\n",
81 : pszDestExtension,
82 : osConflictingDriverList.c_str(),
83 : strchr(osConflictingDriverList.c_str(), ',') ? "s" : "",
84 0 : pszDriverName);
85 355 : }
86 : }
87 :
88 355 : CPLFree(pszDestExtension);
89 355 : }
|