Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL Utilities
4 : * Purpose: Common utility routines
5 : * Author: Even Rouault, <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2011-2012, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #include "commonutils.h"
30 :
31 : #include <cstdio>
32 : #include <cstring>
33 :
34 : #include <string>
35 :
36 : #include "cpl_conv.h"
37 : #include "cpl_string.h"
38 : #include "gdal.h"
39 :
40 : /* -------------------------------------------------------------------- */
41 : /* GetOutputDriversFor() */
42 : /* -------------------------------------------------------------------- */
43 :
44 316 : std::vector<std::string> GetOutputDriversFor(const char *pszDestFilename,
45 : int nFlagRasterVector)
46 : {
47 632 : return CPLStringList(GDALGetOutputDriversForDatasetName(
48 : pszDestFilename, nFlagRasterVector, /* bSingleMatch = */ false,
49 632 : /* bEmitWarning = */ false));
50 : }
51 :
52 : /* -------------------------------------------------------------------- */
53 : /* GetOutputDriverForRaster() */
54 : /* -------------------------------------------------------------------- */
55 :
56 1030 : CPLString GetOutputDriverForRaster(const char *pszDestFilename)
57 : {
58 : const CPLStringList aosList(GDALGetOutputDriversForDatasetName(
59 : pszDestFilename, GDAL_OF_RASTER, /* bSingleMatch = */ true,
60 2060 : /* bEmitWarning = */ true));
61 1030 : if (!aosList.empty())
62 : {
63 1030 : CPLDebug("GDAL", "Using %s driver", aosList[0]);
64 1030 : return aosList[0];
65 : }
66 0 : return CPLString();
67 : }
68 :
69 : /* -------------------------------------------------------------------- */
70 : /* EarlySetConfigOptions() */
71 : /* -------------------------------------------------------------------- */
72 :
73 884 : void EarlySetConfigOptions(int argc, char **argv)
74 : {
75 : // Must process some config options before GDALAllRegister() or
76 : // OGRRegisterAll(), but we can't call GDALGeneralCmdLineProcessor() or
77 : // OGRGeneralCmdLineProcessor(), because it needs the drivers to be
78 : // registered for the --format or --formats options.
79 5628 : for (int i = 1; i < argc; i++)
80 : {
81 4744 : if (EQUAL(argv[i], "--config") && i + 2 < argc)
82 : {
83 30 : CPLSetConfigOption(argv[i + 1], argv[i + 2]);
84 :
85 30 : i += 2;
86 : }
87 4714 : else if (EQUAL(argv[i], "--debug") && i + 1 < argc)
88 : {
89 7 : CPLSetConfigOption("CPL_DEBUG", argv[i + 1]);
90 7 : i += 1;
91 : }
92 : }
93 884 : }
94 :
95 : /************************************************************************/
96 : /* GDALRemoveBOM() */
97 : /************************************************************************/
98 :
99 : /* Remove potential UTF-8 BOM from data (must be NUL terminated) */
100 6 : void GDALRemoveBOM(GByte *pabyData)
101 : {
102 6 : if (pabyData[0] == 0xEF && pabyData[1] == 0xBB && pabyData[2] == 0xBF)
103 : {
104 1 : memmove(pabyData, pabyData + 3,
105 1 : strlen(reinterpret_cast<char *>(pabyData) + 3) + 1);
106 : }
107 6 : }
108 :
109 : /************************************************************************/
110 : /* GDALRemoveSQLComments() */
111 : /************************************************************************/
112 :
113 5 : std::string GDALRemoveSQLComments(const std::string &osInput)
114 : {
115 : const CPLStringList aosLines(
116 10 : CSLTokenizeStringComplex(osInput.c_str(), "\r\n", FALSE, FALSE));
117 5 : std::string osSQL;
118 20 : for (const char *pszLine : aosLines)
119 : {
120 15 : char chQuote = 0;
121 15 : int i = 0;
122 173 : for (; pszLine[i] != '\0'; ++i)
123 : {
124 166 : if (chQuote)
125 : {
126 12 : if (pszLine[i] == chQuote)
127 : {
128 4 : if (pszLine[i + 1] == chQuote)
129 : {
130 2 : i++;
131 : }
132 : else
133 : {
134 2 : chQuote = 0;
135 : }
136 : }
137 : }
138 154 : else if (pszLine[i] == '\'' || pszLine[i] == '"')
139 : {
140 2 : chQuote = pszLine[i];
141 : }
142 152 : else if (pszLine[i] == '-' && pszLine[i + 1] == '-')
143 : {
144 8 : break;
145 : }
146 : }
147 15 : if (i > 0)
148 : {
149 9 : osSQL.append(pszLine, i);
150 : }
151 15 : osSQL += ' ';
152 : }
153 10 : return osSQL;
154 : }
155 :
156 : /************************************************************************/
157 : /* ArgIsNumeric() */
158 : /************************************************************************/
159 :
160 151 : int ArgIsNumeric(const char *pszArg)
161 :
162 : {
163 151 : return CPLGetValueType(pszArg) != CPL_VALUE_STRING;
164 : }
|