summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/SystemTools.cxx
diff options
context:
space:
mode:
authorSebastien Barre <sebastien.barre@kitware.com>2005-03-10 22:34:22 (GMT)
committerSebastien Barre <sebastien.barre@kitware.com>2005-03-10 22:34:22 (GMT)
commit07bdc600455744e608b1af720af5c29a32c38da8 (patch)
tree2ffea71598939291e555851f021c04c5c0a8848b /Source/kwsys/SystemTools.cxx
parent3ff6722934521151793e5ad798a48305fc5ec2fa (diff)
downloadCMake-07bdc600455744e608b1af720af5c29a32c38da8.zip
CMake-07bdc600455744e608b1af720af5c29a32c38da8.tar.gz
CMake-07bdc600455744e608b1af720af5c29a32c38da8.tar.bz2
ENH: remove deps to vtkString by using KWSys (a handful of functions have been moved to KWSys)
Diffstat (limited to 'Source/kwsys/SystemTools.cxx')
-rw-r--r--Source/kwsys/SystemTools.cxx257
1 files changed, 255 insertions, 2 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx
index d15e5ab..6e57bb6 100644
--- a/Source/kwsys/SystemTools.cxx
+++ b/Source/kwsys/SystemTools.cxx
@@ -795,6 +795,33 @@ kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
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
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;
}
+// 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
-kwsys_stl::string SystemTools::Crop(const kwsys_stl::string& s,
- size_t max_len)
+kwsys_stl::string SystemTools::CropString(const kwsys_stl::string& s,
+ size_t max_len)
{
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()
{
int e = errno;