1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/******************************************************************************
*
* Project: GDAL
* Purpose: CPLString implementation.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
* Copyright (c) 2011, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "cpl_string.h"
#include <cctype>
#include <cstdarg>
#include <cstddef>
#include <cstring>
#include <string>
#include "cpl_config.h"
#include "cpl_conv.h"
#if !defined(va_copy) && defined(__va_copy)
#define va_copy __va_copy
#endif
/*
* The CPLString class is derived from std::string, so the vast majority
* of the implementation comes from that. This module is just the extensions
* we add.
*/
/************************************************************************/
/* Printf() */
/************************************************************************/
/** Assign the content of the string using sprintf() */
CPLString &CPLString::Printf(CPL_FORMAT_STRING(const char *pszFormat), ...)
{
va_list args;
va_start(args, pszFormat);
vPrintf(pszFormat, args);
va_end(args);
return *this;
}
/************************************************************************/
/* vPrintf() */
/************************************************************************/
/** Assign the content of the string using vsprintf() */
CPLString &CPLString::vPrintf(CPL_FORMAT_STRING(const char *pszFormat),
va_list args)
{
/* -------------------------------------------------------------------- */
/* This implementation for platforms without vsnprintf() will */
/* just plain fail if the formatted contents are too large. */
/* -------------------------------------------------------------------- */
#if !defined(HAVE_VSNPRINTF)
char *pszBuffer = static_cast<char *>(CPLMalloc(30000));
if (CPLvsnprintf(pszBuffer, 30000, pszFormat, args) > 29998)
{
CPLError(CE_Fatal, CPLE_AppDefined,
"CPLString::vPrintf() ... buffer overrun.");
}
*this = pszBuffer;
CPLFree(pszBuffer);
/* -------------------------------------------------------------------- */
/* This should grow a big enough buffer to hold any formatted */
/* result. */
/* -------------------------------------------------------------------- */
#else
va_list wrk_args;
#ifdef va_copy
va_copy(wrk_args, args);
#else
wrk_args = args;
#endif
char szModestBuffer[500] = {};
szModestBuffer[0] = '\0';
int nPR = CPLvsnprintf(szModestBuffer, sizeof(szModestBuffer), pszFormat,
wrk_args);
if (nPR == -1 || nPR >= static_cast<int>(sizeof(szModestBuffer)) - 1)
{
int nWorkBufferSize = 2000;
char *pszWorkBuffer = static_cast<char *>(CPLMalloc(nWorkBufferSize));
#ifdef va_copy
va_end(wrk_args);
va_copy(wrk_args, args);
#else
wrk_args = args;
#endif
while ((nPR = CPLvsnprintf(pszWorkBuffer, nWorkBufferSize, pszFormat,
wrk_args)) >= nWorkBufferSize - 1 ||
nPR == -1)
{
nWorkBufferSize *= 4;
pszWorkBuffer =
static_cast<char *>(CPLRealloc(pszWorkBuffer, nWorkBufferSize));
#ifdef va_copy
va_end(wrk_args);
va_copy(wrk_args, args);
#else
wrk_args = args;
#endif
}
*this = pszWorkBuffer;
CPLFree(pszWorkBuffer);
}
else
{
*this = szModestBuffer;
}
#ifdef va_copy
va_end(wrk_args);
#endif
#endif /* !defined(HAVE_VSNPRINTF) */
return *this;
}
/************************************************************************/
/* FormatC() */
/************************************************************************/
/**
* Format double in C locale.
*
* The passed value is formatted using the C locale (period as decimal
* separator) and appended to the target CPLString.
*
* @param dfValue the value to format.
* @param pszFormat the sprintf() style format to use or omit for default.
* Note that this format string should only include one substitution argument
* and it must be for a double (%f or %g).
*
* @return a reference to the CPLString.
*/
CPLString &CPLString::FormatC(double dfValue, const char *pszFormat)
{
if (pszFormat == nullptr)
pszFormat = "%g";
// presumably long enough for any number.
const size_t buf_size = 512;
char szWork[buf_size] = {};
CPLsnprintf(szWork, buf_size, pszFormat, dfValue);
*this += szWork;
return *this;
}
/************************************************************************/
/* Trim() */
/************************************************************************/
/**
* Trim white space.
*
* Trims white space off the let and right of the string. White space
* is any of a space, a tab, a newline ('\\n') or a carriage control ('\\r').
*
* @return a reference to the CPLString.
*/
CPLString &CPLString::Trim()
{
constexpr char szWhitespace[] = " \t\r\n";
const size_t iLeft = find_first_not_of(szWhitespace);
const size_t iRight = find_last_not_of(szWhitespace);
if (iLeft == std::string::npos)
{
erase();
return *this;
}
assign(substr(iLeft, iRight - iLeft + 1));
return *this;
}
/************************************************************************/
/* Recode() */
/************************************************************************/
/** Recode the string */
CPLString &CPLString::Recode(const char *pszSrcEncoding,
const char *pszDstEncoding)
{
if (pszSrcEncoding == nullptr)
pszSrcEncoding = CPL_ENC_UTF8;
if (pszDstEncoding == nullptr)
pszDstEncoding = CPL_ENC_UTF8;
if (strcmp(pszSrcEncoding, pszDstEncoding) == 0)
return *this;
char *pszRecoded = CPLRecode(c_str(), pszSrcEncoding, pszDstEncoding);
if (pszRecoded == nullptr)
return *this;
assign(pszRecoded);
CPLFree(pszRecoded);
return *this;
}
/************************************************************************/
/* ifind() */
/************************************************************************/
/**
* Case insensitive find() alternative.
*
* @param str substring to find.
* @param pos offset in the string at which the search starts.
* @return the position of substring in the string or std::string::npos if not
* found.
* @since GDAL 1.9.0
*/
size_t CPLString::ifind(const std::string &str, size_t pos) const
{
return ifind(str.c_str(), pos);
}
/**
* Case insensitive find() alternative.
*
* @param s substring to find.
* @param nPos offset in the string at which the search starts.
* @return the position of the substring in the string or std::string::npos if
* not found.
* @since GDAL 1.9.0
*/
size_t CPLString::ifind(const char *s, size_t nPos) const
{
const char *pszHaystack = c_str();
const char chFirst = static_cast<char>(::tolower(s[0]));
const size_t nTargetLen = strlen(s);
if (nPos > size())
nPos = size();
pszHaystack += nPos;
while (*pszHaystack != '\0')
{
if (chFirst == ::tolower(*pszHaystack))
{
if (EQUALN(pszHaystack, s, nTargetLen))
return nPos;
}
nPos++;
pszHaystack++;
}
return std::string::npos;
}
/************************************************************************/
/* toupper() */
/************************************************************************/
/**
* Convert to upper case in place.
*/
CPLString &CPLString::toupper()
{
for (size_t i = 0; i < size(); i++)
(*this)[i] = static_cast<char>(::toupper((*this)[i]));
return *this;
}
/************************************************************************/
/* tolower() */
/************************************************************************/
/**
* Convert to lower case in place.
*/
CPLString &CPLString::tolower()
{
for (size_t i = 0; i < size(); i++)
(*this)[i] = static_cast<char>(::tolower((*this)[i]));
return *this;
}
/************************************************************************/
/* replaceAll() */
/************************************************************************/
/**
* Replace all occurrences of osBefore with osAfter.
*/
CPLString &CPLString::replaceAll(const std::string &osBefore,
const std::string &osAfter)
{
const size_t nBeforeSize = osBefore.size();
const size_t nAfterSize = osAfter.size();
if (nBeforeSize)
{
size_t nStartPos = 0;
while ((nStartPos = find(osBefore, nStartPos)) != std::string::npos)
{
replace(nStartPos, nBeforeSize, osAfter);
nStartPos += nAfterSize;
}
}
return *this;
}
/**
* Replace all occurrences of chBefore with osAfter.
*/
CPLString &CPLString::replaceAll(char chBefore, const std::string &osAfter)
{
return replaceAll(std::string(&chBefore, 1), osAfter);
}
/**
* Replace all occurrences of osBefore with chAfter.
*/
CPLString &CPLString::replaceAll(const std::string &osBefore, char chAfter)
{
return replaceAll(osBefore, std::string(&chAfter, 1));
}
/**
* Replace all occurrences of chBefore with chAfter.
*/
CPLString &CPLString::replaceAll(char chBefore, char chAfter)
{
return replaceAll(std::string(&chBefore, 1), std::string(&chAfter, 1));
}
/************************************************************************/
/* endsWith() */
/************************************************************************/
/**
* Returns whether the string ends with another string
* @param osStr other string.
* @return true if the string ends with osStr.
*/
bool CPLString::endsWith(const std::string &osStr) const
{
if (size() < osStr.size())
return false;
return substr(size() - osStr.size()) == osStr;
}
/************************************************************************/
/* CPLURLGetValue() */
/************************************************************************/
/**
* Return the value matching a key from a key=value pair in a URL.
*
* @param pszURL the URL.
* @param pszKey the key to find.
* @return the value of empty string if not found.
* @since GDAL 1.9.0
*/
CPLString CPLURLGetValue(const char *pszURL, const char *pszKey)
{
CPLString osKey(pszKey);
osKey += "=";
size_t nKeyPos = CPLString(pszURL).ifind(osKey);
if (nKeyPos != std::string::npos && nKeyPos > 0 &&
(pszURL[nKeyPos - 1] == '?' || pszURL[nKeyPos - 1] == '&'))
{
CPLString osValue(pszURL + nKeyPos + osKey.size());
const char *pszValue = osValue.c_str();
const char *pszSep = strchr(pszValue, '&');
if (pszSep)
{
osValue.resize(pszSep - pszValue);
}
return osValue;
}
return "";
}
/************************************************************************/
/* CPLURLAddKVP() */
/************************************************************************/
/**
* Return a new URL with a new key=value pair.
*
* @param pszURL the URL.
* @param pszKey the key to find.
* @param pszValue the value of the key (may be NULL to unset an existing KVP).
* @return the modified URL.
* @since GDAL 1.9.0
*/
CPLString CPLURLAddKVP(const char *pszURL, const char *pszKey,
const char *pszValue)
{
CPLString osURL(pszURL);
if (strchr(osURL, '?') == nullptr)
osURL += "?";
pszURL = osURL.c_str();
CPLString osKey(pszKey);
osKey += "=";
size_t nKeyPos = osURL.ifind(osKey);
if (nKeyPos != std::string::npos && nKeyPos > 0 &&
(pszURL[nKeyPos - 1] == '?' || pszURL[nKeyPos - 1] == '&'))
{
CPLString osNewURL(osURL);
osNewURL.resize(nKeyPos);
if (pszValue)
{
osNewURL += osKey;
osNewURL += pszValue;
}
const char *pszNext = strchr(pszURL + nKeyPos, '&');
if (pszNext)
{
if (osNewURL.back() == '&' || osNewURL.back() == '?')
osNewURL += pszNext + 1;
else
osNewURL += pszNext;
}
return osNewURL;
}
else
{
if (pszValue)
{
if (osURL.back() != '&' && osURL.back() != '?')
osURL += '&';
osURL += osKey;
osURL += pszValue;
}
return osURL;
}
}
/************************************************************************/
/* CPLOPrintf() */
/************************************************************************/
/** Return a CPLString with the content of sprintf() */
CPLString CPLOPrintf(CPL_FORMAT_STRING(const char *pszFormat), ...)
{
va_list args;
va_start(args, pszFormat);
CPLString osTarget;
osTarget.vPrintf(pszFormat, args);
va_end(args);
return osTarget;
}
/************************************************************************/
/* CPLOvPrintf() */
/************************************************************************/
/** Return a CPLString with the content of vsprintf() */
CPLString CPLOvPrintf(CPL_FORMAT_STRING(const char *pszFormat), va_list args)
{
CPLString osTarget;
osTarget.vPrintf(pszFormat, args);
return osTarget;
}
/************************************************************************/
/* CPLQuotedSQLIdentifer() */
/************************************************************************/
/** Return a CPLString of the SQL quoted identifier */
CPLString CPLQuotedSQLIdentifier(const char *pszIdent)
{
CPLString osIdent;
if (pszIdent)
{
char *pszQuotedIdent = CPLEscapeString(pszIdent, -1, CPLES_SQLI);
osIdent.Printf("\"%s\"", pszQuotedIdent);
CPLFree(pszQuotedIdent);
}
return osIdent;
}
|