From e5f9569dcbe5b2444b892b6064a4dcdcccc2297c Mon Sep 17 00:00:00 2001 From: Tyler Dauwalder Date: Fri, 8 Nov 2002 00:37:46 +0000 Subject: [PATCH] Added escape_path() functions git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1873 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/storage/storage_support.h | 39 ++++++++++++++-- src/kits/storage/storage_support.cpp | 55 ++++++++++++++++++++--- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/headers/private/storage/storage_support.h b/headers/private/storage/storage_support.h index ddc2dfce68..befc6763cd 100644 --- a/headers/private/storage/storage_support.h +++ b/headers/private/storage/storage_support.h @@ -39,18 +39,51 @@ status_t check_entry_name(const char *entry); //! Checks whether a path name is a valid path name. status_t check_path_name(const char *path); -//! Returns a copy of \c str in which all alphabetic characters are lowercase. +/*! \brief Returns a copy of \c str in which all alphabetic characters + are lowercase. + + Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer. +*/ std::string to_lower(const char *str); -//! Places a copy of \c str in \c result in which all alphabetic characters are lowercase. +/*! \brief Places a copy of \c str in \c result in which all alphabetic + characters are lowercase. + + Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer. +*/ void to_lower(const char *str, std::string &result); -//! Copies \c str into \c result, converting any uppercase alphabetics to lowercase. +/*! \brief Copies \c str into \c result, converting any uppercase alphabetics + to lowercase. + + \a str and \a result may point to the same string. \a result is + assumed to be as long as or longer than \a str. +*/ void to_lower(const char *str, char *result); //! Converts \c str to lowercase. void to_lower(char *str); +/*! \brief Escapes any whitespace or other special characters in the path + + \a result must be large enough to accomodate the addition of + escape sequences to \a str. \a str and \a result may *NOT* point to + the same string. + + Note that this function was designed for use with the registrar's + RecentEntries class, and may not create escapes exactly like you're + hoping. Please double check the code for the function to see if this + is the case. +*/ +void escape_path(const char *str, char *result); + +/*! \brief Escapes any whitespace or other special characters in the path + + \a str must be large enough to accomodate the addition of + escape sequences. +*/ +void escape_path(char *str); + }; // namespace Storage }; // namespace BPrivate diff --git a/src/kits/storage/storage_support.cpp b/src/kits/storage/storage_support.cpp index c4eeea889e..0909ee9c29 100644 --- a/src/kits/storage/storage_support.cpp +++ b/src/kits/storage/storage_support.cpp @@ -300,8 +300,6 @@ check_path_name(const char *path) return error; } -/*! Returns "(null)" if you're a bonehead and pass in a \c NULL pointer. -*/ std::string to_lower(const char *str) { @@ -310,8 +308,6 @@ to_lower(const char *str) return result; } -/*! Returns "(null)" if you're a bonehead and pass in a \c NULL pointer. -*/ void to_lower(const char *str, std::string &result) { @@ -323,9 +319,6 @@ to_lower(const char *str, std::string &result) result = "(null)"; } -/*! \c str and \c result may point to the same string. \c result is - assumed to be as long as or longer than \c str. -*/ void to_lower(const char *str, char *result) { @@ -343,6 +336,54 @@ to_lower(char *str) to_lower(str, str); } +void escape_path(const char *str, char *result) +{ + if (str && result) { + int32 len = strlen(str); + + for (int32 i = 0; i < len; i++) { + char ch = str[i]; + char escapeChar = 0; + + switch (ch) { + case ' ': + case '\'': + case '"': + case '?': + case '\\': + case '(': + case ')': + case '[': + case ']': + case '*': + case '^': + escapeChar = ch; + break; + } + + if (escapeChar) { + *(result++) = '\\'; + *(result++) = escapeChar; + } else { + *(result++) = ch; + } + } + + *result = 0; + } +} + +void escape_path(char *str) +{ + if (str) { + char *copy = new(nothrow) char[strlen(str)+1]; + if (copy) { + strcpy(copy, str); + escape_path(copy, str); + } + delete [] copy; + } +} }; // namespace Storage }; // namespace BPrivate