Move GDALRemoveSQLComments() to CPLRemoveSQLComments()

This commit is contained in:
Even Rouault 2024-11-25 18:39:58 +01:00
parent 65a47f45ea
commit 7ab4e4fa46
No known key found for this signature in database
GPG Key ID: 33EBBFC47B3DD87D
7 changed files with 65 additions and 51 deletions

View File

@ -154,53 +154,6 @@ void GDALRemoveBOM(GByte *pabyData)
}
}
/************************************************************************/
/* GDALRemoveSQLComments() */
/************************************************************************/
std::string GDALRemoveSQLComments(const std::string &osInput)
{
const CPLStringList aosLines(
CSLTokenizeStringComplex(osInput.c_str(), "\r\n", FALSE, FALSE));
std::string osSQL;
for (const char *pszLine : aosLines)
{
char chQuote = 0;
int i = 0;
for (; pszLine[i] != '\0'; ++i)
{
if (chQuote)
{
if (pszLine[i] == chQuote)
{
if (pszLine[i + 1] == chQuote)
{
i++;
}
else
{
chQuote = 0;
}
}
}
else if (pszLine[i] == '\'' || pszLine[i] == '"')
{
chQuote = pszLine[i];
}
else if (pszLine[i] == '-' && pszLine[i + 1] == '-')
{
break;
}
}
if (i > 0)
{
osSQL.append(pszLine, i);
}
osSQL += ' ';
}
return osSQL;
}
/************************************************************************/
/* ArgIsNumeric() */
/************************************************************************/

View File

@ -94,7 +94,6 @@ std::vector<std::string> CPL_DLL
GetOutputDriversFor(const char *pszDestFilename, int nFlagRasterVector);
CPLString CPL_DLL GetOutputDriverForRaster(const char *pszDestFilename);
void GDALRemoveBOM(GByte *pabyData);
std::string GDALRemoveSQLComments(const std::string &osInput);
int ArgIsNumeric(const char *pszArg);

View File

@ -186,7 +186,7 @@ GDALRasterizeOptionsGetParser(GDALRasterizeOptions *psOptions,
char *pszSQLStatement =
reinterpret_cast<char *>(pabyRet);
psOptions->osSQL = CPLStrdup(
GDALRemoveSQLComments(pszSQLStatement).c_str());
CPLRemoveSQLComments(pszSQLStatement).c_str());
VSIFree(pszSQLStatement);
}
})

View File

@ -7176,7 +7176,7 @@ static std::unique_ptr<GDALArgumentParser> GDALVectorTranslateOptionsGetParser(
GDALRemoveBOM(pabyRet);
char *pszSQLStatement = reinterpret_cast<char *>(pabyRet);
psOptions->osSQLStatement =
GDALRemoveSQLComments(pszSQLStatement);
CPLRemoveSQLComments(pszSQLStatement);
VSIFree(pszSQLStatement);
}
else

View File

@ -2589,7 +2589,7 @@ GDALVectorInfoOptionsNew(char **papszArgv,
GDALRemoveBOM(pabyRet);
char *pszSQLStatement = reinterpret_cast<char *>(pabyRet);
psOptions->osSQLStatement =
GDALRemoveSQLComments(pszSQLStatement);
CPLRemoveSQLComments(pszSQLStatement);
VSIFree(pabyRet);
}
else

View File

@ -3161,3 +3161,56 @@ int CPLTolower(int c)
{
return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
}
/************************************************************************/
/* CPLRemoveSQLComments() */
/************************************************************************/
/** Remove SQL comments from a string
*
* @param osInput Input string.
* @since GDAL 3.11
*/
std::string CPLRemoveSQLComments(const std::string &osInput)
{
const CPLStringList aosLines(
CSLTokenizeStringComplex(osInput.c_str(), "\r\n", FALSE, FALSE));
std::string osSQL;
for (const char *pszLine : aosLines)
{
char chQuote = 0;
int i = 0;
for (; pszLine[i] != '\0'; ++i)
{
if (chQuote)
{
if (pszLine[i] == chQuote)
{
if (pszLine[i + 1] == chQuote)
{
i++;
}
else
{
chQuote = 0;
}
}
}
else if (pszLine[i] == '\'' || pszLine[i] == '"')
{
chQuote = pszLine[i];
}
else if (pszLine[i] == '-' && pszLine[i + 1] == '-')
{
break;
}
}
if (i > 0)
{
if (!osSQL.empty())
osSQL += ' ';
osSQL.append(pszLine, i);
}
}
return osSQL;
}

View File

@ -270,6 +270,15 @@ int CPL_DLL CPLCanRecode(const char *pszTestStr, const char *pszSrcEncoding,
const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
CPL_C_END
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
extern "C++"
{
std::string CPL_DLL CPLRemoveSQLComments(const std::string &osInput);
}
#endif
/************************************************************************/
/* CPLString */
/************************************************************************/