From 3d9c8f1b2d52fb815dc65366bfbbfb535aced6e8 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 10 Sep 2009 03:54:10 +0000 Subject: [PATCH] cleaned up usage of MAX_PATH and related macros --- ChangeLog | 1 + include/libtorrent/alert_types.hpp | 6 +++--- include/libtorrent/config.hpp | 30 +++++++++++++++++++++++------- src/torrent_info.cpp | 9 +-------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5d121e4ab..aca1f61a5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ * added support for i2p torrents + * cleaned up usage of MAX_PATH and related macros 0.15 release diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index f0aac9316..a51b38d9c 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -138,7 +138,7 @@ namespace libtorrent virtual char const* what() const { return "file completed"; } virtual std::string message() const { - char msg[200]; + char msg[200 + TORRENT_MAX_PATH]; snprintf(msg, sizeof(msg), "%s: file %d finished downloading" , torrent_alert::message().c_str(), index); return msg; @@ -164,7 +164,7 @@ namespace libtorrent virtual char const* what() const { return "file renamed"; } virtual std::string message() const { - char msg[200 + NAME_MAX]; + char msg[200 + TORRENT_MAX_PATH * 2]; snprintf(msg, sizeof(msg), "%s: file %d renamed to %s", torrent_alert::message().c_str() , index, name.c_str()); return msg; @@ -190,7 +190,7 @@ namespace libtorrent virtual char const* what() const { return "file rename failed"; } virtual std::string message() const { - char ret[200 + NAME_MAX]; + char ret[200 + TORRENT_MAX_PATH * 2]; snprintf(ret, sizeof(ret), "%s: failed to rename file %d: %s" , torrent_alert::message().c_str(), index, error.message().c_str()); return ret; diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 193872367..001acb6f3 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -157,20 +157,36 @@ POSSIBILITY OF SUCH DAMAGE. // (disables some float-dependent APIs) #define TORRENT_NO_FPU 0 -// make sure NAME_MAX is defined -#ifndef NAME_MAX -#ifdef MAXPATH -#define NAME_MAX MAXPATH + +// on windows, NAME_MAX refers to Unicode characters +// on linux it refers to bytes (utf-8 encoded) +// TODO: Make this count Unicode characters instead of bytes on windows + +// windows +#if defined FILENAME_MAX +#define TORRENT_MAX_PATH FILENAME_MAX + +// solaris +#elif defined MAXPATH +#define TORRENT_MAX_PATH MAXPATH + +// posix +#elif defined NAME_MAX +#define TORRENT_MAX_PATH NAME_MAX + +// none of the above #else // this is the maximum number of characters in a // path element / filename on windows #define NAME_MAX 255 -#endif // MAXPATH -#endif // NAME_MAX +#warning unknown platform, assuming the longest path is 255 + +#endif #ifdef TORRENT_WINDOWS -#pragma warning(disable:4251) // class X needs to have dll-interface to be used by clients of class Y +// class X needs to have dll-interface to be used by clients of class Y +#pragma warning(disable:4251) #include diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index e93b00b35..f5985b3e1 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -203,14 +203,7 @@ namespace libtorrent void trim_path_element(std::string& path_element) { -#ifdef FILENAME_MAX - const int max_path_len = FILENAME_MAX; -#else - // on windows, NAME_MAX refers to Unicode characters - // on linux it refers to bytes (utf-8 encoded) - // TODO: Make this count Unicode characters instead of bytes on windows - const int max_path_len = NAME_MAX; -#endif + const int max_path_len = TORRENT_MAX_PATH; if (path_element.size() > max_path_len) { // truncate filenames that are too long. But keep extensions!