ENH: move EstimateFormatLength to kwsys

This commit is contained in:
Sebastien Barre 2005-03-28 17:46:38 -05:00
parent 11965ebd34
commit 1816011791
2 changed files with 81 additions and 0 deletions

View File

@ -1096,6 +1096,75 @@ kwsys_stl::string SystemTools::CropString(const kwsys_stl::string& s,
return n; 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 // convert windows slashes to unix slashes
void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path) void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path)
{ {

View File

@ -22,6 +22,7 @@
#include <@KWSYS_NAMESPACE@/Configure.h> #include <@KWSYS_NAMESPACE@/Configure.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdarg.h>
#if defined( _MSC_VER ) #if defined( _MSC_VER )
typedef unsigned short mode_t; typedef unsigned short mode_t;
@ -188,6 +189,17 @@ public:
static char* AppendStrings( static char* AppendStrings(
const char* str1, const char* str2, const char* str3); const char* str1, const char* str2, const char* str3);
/**
* Estimate the length of the string that will be produced
* from printing the given format string and arguments. The
* returned length will always be at least as large as the string
* that will result from printing.
* WARNING: since va_arg is called to iterate of the argument list,
* you will not be able to use this 'ap' anymore from the beginning.
* It's up to you to call va_end though.
*/
static int EstimateFormatLength(const char *format, va_list ap);
/** ----------------------------------------------------------------- /** -----------------------------------------------------------------
* Filename Manipulation Routines * Filename Manipulation Routines
* ----------------------------------------------------------------- * -----------------------------------------------------------------