diff options
author | Sebastien Barre <sebastien.barre@kitware.com> | 2005-03-28 22:46:38 (GMT) |
---|---|---|
committer | Sebastien Barre <sebastien.barre@kitware.com> | 2005-03-28 22:46:38 (GMT) |
commit | 1816011791a1db7095e57d3f6e6bda6d51e89f7a (patch) | |
tree | 7ca752611ddb97cd1a78c5200823b2c60918d628 /Source/kwsys/SystemTools.cxx | |
parent | 11965ebd34a41c6c1c452ae87134dd8a2221d5e2 (diff) | |
download | CMake-1816011791a1db7095e57d3f6e6bda6d51e89f7a.zip CMake-1816011791a1db7095e57d3f6e6bda6d51e89f7a.tar.gz CMake-1816011791a1db7095e57d3f6e6bda6d51e89f7a.tar.bz2 |
ENH: move EstimateFormatLength to kwsys
Diffstat (limited to 'Source/kwsys/SystemTools.cxx')
-rw-r--r-- | Source/kwsys/SystemTools.cxx | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 5807111..25676ee 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -1096,6 +1096,75 @@ kwsys_stl::string SystemTools::CropString(const kwsys_stl::string& s, return n; } +//---------------------------------------------------------------------------- +int SystemTools::EstimateFormatLength(const char *format, va_list ap) +{ + if (!format) + { + return 0; + } + + // Quick-hack attempt at estimating the length of the string. + // Should never under-estimate. + + // Start with the length of the format string itself. + + int length = strlen(format); + + // Increase the length for every argument in the format. + + const char* cur = format; + while(*cur) + { + if(*cur++ == '%') + { + // Skip "%%" since it doesn't correspond to a va_arg. + if(*cur != '%') + { + while(!int(isalpha(*cur))) + { + ++cur; + } + switch (*cur) + { + case 's': + { + // Check the length of the string. + char* s = va_arg(ap, char*); + if(s) + { + length += strlen(s); + } + } break; + case 'e': + case 'f': + case 'g': + { + // Assume the argument contributes no more than 64 characters. + length += 64; + + // Eat the argument. + static_cast<void>(va_arg(ap, double)); + } break; + default: + { + // Assume the argument contributes no more than 64 characters. + length += 64; + + // Eat the argument. + static_cast<void>(va_arg(ap, int)); + } break; + } + } + + // Move past the characters just tested. + ++cur; + } + } + + return length; +} + // convert windows slashes to unix slashes void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path) { |