ENH: remove deps to vtkString by using KWSys (a handful of functions have been moved to KWSys)

This commit is contained in:
Sebastien Barre 2005-03-10 17:34:22 -05:00
parent 3ff6722934
commit 07bdc60045
2 changed files with 310 additions and 3 deletions

View File

@ -795,6 +795,33 @@ kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
return n; return n;
} }
// Return capitalized words
kwsys_stl::string SystemTools::CapitalizedWords(const kwsys_stl::string& s)
{
kwsys_stl::string n(s);
for (size_t i = 0; i < s.size(); i++)
{
if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1])))
{
n[i] = static_cast<kwsys_stl::string::value_type>(toupper(s[i]));
}
}
return n;
}
// Return uncapitalized words
kwsys_stl::string SystemTools::UnCapitalizedWords(const kwsys_stl::string& s)
{
kwsys_stl::string n(s);
for (size_t i = 0; i < s.size(); i++)
{
if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1])))
{
n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
}
}
return n;
}
// Return a lower case string // Return a lower case string
kwsys_stl::string SystemTools::LowerCase(const kwsys_stl::string& s) kwsys_stl::string SystemTools::LowerCase(const kwsys_stl::string& s)
@ -820,9 +847,133 @@ kwsys_stl::string SystemTools::UpperCase(const kwsys_stl::string& s)
return n; return n;
} }
// Count char in string
size_t SystemTools::CountChar(const char* str, char c)
{
size_t count = 0;
if (str)
{
while (*str)
{
if (*str == c)
{
++count;
}
++str;
}
}
return count;
}
// Remove chars in string
char* SystemTools::RemoveChars(const char* str, const char *toremove)
{
if (!str)
{
return NULL;
}
char *clean_str = new char [strlen(str) + 1];
char *ptr = clean_str;
while (*str)
{
const char *str2 = toremove;
while (*str2 && *str != *str2)
{
++str2;
}
if (!*str2)
{
*ptr++ = *str;
}
++str;
}
*ptr = '\0';
return clean_str;
}
// Remove chars in string
char* SystemTools::RemoveCharsButUpperHex(const char* str)
{
if (!str)
{
return 0;
}
char *clean_str = new char [strlen(str) + 1];
char *ptr = clean_str;
while (*str)
{
if ((*str >= '0' && *str <= '9') || (*str >= 'A' && *str <= 'H'))
{
*ptr++ = *str;
}
++str;
}
*ptr = '\0';
return clean_str;
}
// Replace chars in string
char* SystemTools::ReplaceChars(char* str, const char *toreplace, char replacement)
{
if (str)
{
char *ptr = str;
while (*ptr)
{
const char *ptr2 = toreplace;
while (*ptr2)
{
if (*ptr == *ptr2)
{
*ptr = replacement;
}
++ptr2;
}
++ptr;
}
}
return str;
}
// Returns a pointer to the last occurence of str2 in str1
const char* SystemTools::FindLastString(const char* str1, const char* str2)
{
if (!str1 || !str2)
{
return NULL;
}
size_t len1 = strlen(str1), len2 = strlen(str2);
if (len1 >= len2)
{
const char *ptr = str1 + len1 - len2;
do
{
if (!strncmp(ptr, str2, len2))
{
return ptr;
}
} while (ptr-- != str1);
}
return NULL;
}
// Duplicate string
char* SystemTools::DuplicateString(const char* str)
{
if (str)
{
char *newstr = new char [strlen(str) + 1];
return strcpy(newstr, str);
}
return NULL;
}
// Return a cropped string // Return a cropped string
kwsys_stl::string SystemTools::Crop(const kwsys_stl::string& s, kwsys_stl::string SystemTools::CropString(const kwsys_stl::string& s,
size_t max_len) size_t max_len)
{ {
if (!s.size() || max_len == 0 || max_len >= s.size()) if (!s.size() || max_len == 0 || max_len >= s.size())
{ {
@ -1278,6 +1429,108 @@ long int SystemTools::CreationTime(const char* filename)
} }
} }
bool SystemTools::ConvertDateMacroString(const char *str, time_t *tmt)
{
if (!str || !tmt || strlen(str) < 12)
{
return false;
}
struct tm tmt2;
// __DATE__
// The compilation date of the current source file. The date is a string
// literal of the form Mmm dd yyyy. The month name Mmm is the same as for
// dates generated by the library function asctime declared in TIME.H.
// index: 012345678901
// format: Mmm dd yyyy
// example: Dec 19 2003
static char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
char buffer[12];
strcpy(buffer, str);
buffer[3] = 0;
char *ptr = strstr(month_names, buffer);
if (!ptr)
{
return false;
}
int month = (ptr - month_names) / 3;
int day = atoi(buffer + 4);
int year = atoi(buffer + 7);
tmt2.tm_isdst = -1;
tmt2.tm_hour = 0;
tmt2.tm_min = 0;
tmt2.tm_sec = 0;
tmt2.tm_wday = 0;
tmt2.tm_yday = 0;
tmt2.tm_mday = day;
tmt2.tm_mon = month;
tmt2.tm_year = year - 1900;
*tmt = mktime(&tmt2);
return true;
}
bool SystemTools::ConvertTimeStampMacroString(const char *str, time_t *tmt)
{
if (!str || !tmt || strlen(str) < 27)
{
return false;
}
struct tm tmt2;
// __TIMESTAMP__
// The date and time of the last modification of the current source file,
// expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy,
/// where Ddd is the abbreviated day of the week and Date is an integer
// from 1 to 31.
// index: 0123456789
// 0123456789
// 0123456789
// format: Ddd Mmm Date hh:mm:ss yyyy
// example: Fri Dec 19 14:34:58 2003
static char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
char buffer[27];
strcpy(buffer, str);
buffer[7] = 0;
char *ptr = strstr(month_names, buffer + 4);
if (!ptr)
{
return false;
}
int month = (ptr - month_names) / 3;
int day = atoi(buffer + 8);
int hour = atoi(buffer + 11);
int min = atoi(buffer + 14);
int sec = atoi(buffer + 17);
int year = atoi(buffer + 20);
tmt2.tm_isdst = -1;
tmt2.tm_hour = hour;
tmt2.tm_min = min;
tmt2.tm_sec = sec;
tmt2.tm_wday = 0;
tmt2.tm_yday = 0;
tmt2.tm_mday = day;
tmt2.tm_mon = month;
tmt2.tm_year = year - 1900;
*tmt = mktime(&tmt2);
return true;
}
kwsys_stl::string SystemTools::GetLastSystemError() kwsys_stl::string SystemTools::GetLastSystemError()
{ {
int e = errno; int e = errno;

View File

@ -109,6 +109,18 @@ public:
*/ */
static kwsys_stl::string Capitalized(const kwsys_stl::string&); static kwsys_stl::string Capitalized(const kwsys_stl::string&);
/**
* Return capitalized words (i.e the first letter of each word is
* uppercased all other are left untouched though).
*/
static kwsys_stl::string CapitalizedWords(const kwsys_stl::string&);
/**
* Return uncapitalized words (i.e the first letter of each word is
* lowercased all other are left untouched though).
*/
static kwsys_stl::string UnCapitalizedWords(const kwsys_stl::string&);
/** /**
* Return a lower case string * Return a lower case string
*/ */
@ -119,11 +131,46 @@ public:
*/ */
static kwsys_stl::string UpperCase(const kwsys_stl::string&); static kwsys_stl::string UpperCase(const kwsys_stl::string&);
/**
* Count char in string
*/
static size_t CountChar(const char* str, char c);
/**
* Remove some characters from a string.
* Return a pointer to the new resulting string (allocated with 'new')
*/
static char* RemoveChars(const char* str, const char *toremove);
/**
* Remove remove all but 0->9, A->F from a string.
* Return a pointer to the new resulting string (allocated with 'new')
*/
static char* RemoveCharsButUpperHex(const char* str);
/**
* Replace some characters by another character in a string (in-place)
* Return a pointer to string
*/
static char* ReplaceChars(char* str, const char *toreplace,char replacement);
/**
* Returns a pointer to the last occurence of str2 in str1
*/
static const char* FindLastString(const char* str1, const char* str2);
/**
* Make a duplicate of the string similar to the strdup C function
* but use new to create the 'new' string, so one can use
* 'delete' to remove it. Returns 0 if the input is empty.
*/
static char* DuplicateString(const char* str);
/** /**
* Return the string cropped to a given length by removing chars in the * Return the string cropped to a given length by removing chars in the
* center of the string and replacing them with an ellipsis (...) * center of the string and replacing them with an ellipsis (...)
*/ */
static kwsys_stl::string Crop(const kwsys_stl::string&, size_t max_len); static kwsys_stl::string CropString(const kwsys_stl::string&, size_t max_len);
/** /**
* do a case-independent string comparison * do a case-independent string comparison
@ -339,6 +386,13 @@ public:
/** Return file's creation time (Win32: works only for NTFS, not FAT). */ /** Return file's creation time (Win32: works only for NTFS, not FAT). */
static long int CreationTime(const char* filename); static long int CreationTime(const char* filename);
/**
* Convert a string in __DATE__ or __TIMESTAMP__ format into a time_t.
* Return false on error, true on success
*/
static bool ConvertDateMacroString(const char *str, time_t *tmt);
static bool ConvertTimeStampMacroString(const char *str, time_t *tmt);
///! for windows return the short path for the given path, unix just a pass through ///! for windows return the short path for the given path, unix just a pass through
static bool GetShortPath(const char* path, kwsys_stl::string& result); static bool GetShortPath(const char* path, kwsys_stl::string& result);