cleaned up usage of MAX_PATH and related macros

This commit is contained in:
Arvid Norberg 2009-09-10 03:54:10 +00:00
parent 2fd925a4ae
commit 3d9c8f1b2d
4 changed files with 28 additions and 18 deletions

View File

@ -1,4 +1,5 @@
* added support for i2p torrents * added support for i2p torrents
* cleaned up usage of MAX_PATH and related macros
0.15 release 0.15 release

View File

@ -138,7 +138,7 @@ namespace libtorrent
virtual char const* what() const { return "file completed"; } virtual char const* what() const { return "file completed"; }
virtual std::string message() const virtual std::string message() const
{ {
char msg[200]; char msg[200 + TORRENT_MAX_PATH];
snprintf(msg, sizeof(msg), "%s: file %d finished downloading" snprintf(msg, sizeof(msg), "%s: file %d finished downloading"
, torrent_alert::message().c_str(), index); , torrent_alert::message().c_str(), index);
return msg; return msg;
@ -164,7 +164,7 @@ namespace libtorrent
virtual char const* what() const { return "file renamed"; } virtual char const* what() const { return "file renamed"; }
virtual std::string message() const 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() snprintf(msg, sizeof(msg), "%s: file %d renamed to %s", torrent_alert::message().c_str()
, index, name.c_str()); , index, name.c_str());
return msg; return msg;
@ -190,7 +190,7 @@ namespace libtorrent
virtual char const* what() const { return "file rename failed"; } virtual char const* what() const { return "file rename failed"; }
virtual std::string message() const 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" snprintf(ret, sizeof(ret), "%s: failed to rename file %d: %s"
, torrent_alert::message().c_str(), index, error.message().c_str()); , torrent_alert::message().c_str(), index, error.message().c_str());
return ret; return ret;

View File

@ -157,20 +157,36 @@ POSSIBILITY OF SUCH DAMAGE.
// (disables some float-dependent APIs) // (disables some float-dependent APIs)
#define TORRENT_NO_FPU 0 #define TORRENT_NO_FPU 0
// make sure NAME_MAX is defined
#ifndef NAME_MAX // on windows, NAME_MAX refers to Unicode characters
#ifdef MAXPATH // on linux it refers to bytes (utf-8 encoded)
#define NAME_MAX MAXPATH // 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 #else
// this is the maximum number of characters in a // this is the maximum number of characters in a
// path element / filename on windows // path element / filename on windows
#define NAME_MAX 255 #define NAME_MAX 255
#endif // MAXPATH #warning unknown platform, assuming the longest path is 255
#endif // NAME_MAX
#endif
#ifdef TORRENT_WINDOWS #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 <stdarg.h> #include <stdarg.h>

View File

@ -203,14 +203,7 @@ namespace libtorrent
void trim_path_element(std::string& path_element) void trim_path_element(std::string& path_element)
{ {
#ifdef FILENAME_MAX const int max_path_len = TORRENT_MAX_PATH;
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
if (path_element.size() > max_path_len) if (path_element.size() > max_path_len)
{ {
// truncate filenames that are too long. But keep extensions! // truncate filenames that are too long. But keep extensions!