merged string function cleanup from RC_0_16

This commit is contained in:
Arvid Norberg 2012-08-26 15:26:17 +00:00
parent 2665d2a4e1
commit fdc25967c2
15 changed files with 206 additions and 118 deletions

View File

@ -21,6 +21,7 @@ set(sources
file_storage
lazy_bdecode
escape_string
string_util
file
gzip
http_connection

View File

@ -479,6 +479,7 @@ SOURCES =
file_storage
lazy_bdecode
escape_string
string_util
file
gzip
http_connection

View File

@ -99,6 +99,7 @@ nobase_include_HEADERS = \
stat.hpp \
storage.hpp \
storage_defs.hpp \
string_util.hpp \
struct_debug.hpp \
thread.hpp \
time.hpp \

View File

@ -144,9 +144,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(disable:4251)
// '_vsnprintf': This function or variable may be unsafe
#pragma warning(disable:4996)
// 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup
#pragma warning(disable: 4996)
#define strdup _strdup
#define TORRENT_DEPRECATED_PREFIX __declspec(deprecated)
@ -445,10 +442,6 @@ inline int snprintf(char* buf, int len, char const* fmt, ...)
#define TORRENT_USE_I2P 1
#endif
#ifndef TORRENT_HAS_STRDUP
#define TORRENT_HAS_STRDUP 1
#endif
#if !defined TORRENT_IOV_MAX
#ifdef IOV_MAX
#define TORRENT_IOV_MAX IOV_MAX
@ -502,17 +495,6 @@ inline int snprintf(char* buf, int len, char const* fmt, ...)
#endif
#if !TORRENT_HAS_STRDUP
inline char* strdup(char const* str)
{
if (str == 0) return 0;
char* tmp = (char*)malloc(strlen(str) + 1);
if (tmp == 0) return 0;
strcpy(tmp, str);
return tmp;
}
#endif
// for non-exception builds
#ifdef BOOST_NO_EXCEPTIONS
#define TORRENT_TRY if (true)

View File

@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/system/error_code.hpp>
#endif
#include <string.h> // strdup
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#include <stdlib.h> // free
namespace libtorrent
@ -383,7 +383,7 @@ namespace libtorrent
if (!m_msg)
{
std::string msg = m_error.message();
m_msg = strdup(msg.c_str());
m_msg = allocate_string_copy(msg.c_str());
}
return m_msg;

View File

@ -43,17 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
TORRENT_EXTRA_EXPORT boost::array<char, 4 + std::numeric_limits<size_type>::digits10> to_string(size_type n);
TORRENT_EXTRA_EXPORT bool is_alpha(char c);
TORRENT_EXPORT bool is_digit(char c);
TORRENT_EXTRA_EXPORT bool is_print(char c);
TORRENT_EXTRA_EXPORT bool is_space(char c);
TORRENT_EXTRA_EXPORT char to_lower(char c);
TORRENT_EXTRA_EXPORT int split_string(char const** tags, int buf_size, char* in);
TORRENT_EXTRA_EXPORT bool string_begins_no_case(char const* s1, char const* s2);
TORRENT_EXTRA_EXPORT bool string_equal_no_case(char const* s1, char const* s2);
TORRENT_EXTRA_EXPORT void url_random(char* begin, char* end);
TORRENT_EXTRA_EXPORT std::string unescape_string(std::string const& s, error_code& ec);
// replaces all disallowed URL characters by their %-encoding

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <algorithm>
#include <deque>
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#include "libtorrent/peer.hpp"
#include "libtorrent/piece_picker.hpp"
@ -487,7 +488,7 @@ namespace libtorrent
#if TORRENT_USE_I2P
inline policy::i2p_peer::i2p_peer(char const* dest, bool connectable, int src)
: peer(0, connectable, src), destination(strdup(dest))
: peer(0, connectable, src), destination(allocate_string_copy(dest))
{
#if TORRENT_USE_IPV6
is_v6_addr = false;

View File

@ -0,0 +1,58 @@
/*
Copyright (c) 2012, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_STRING_UTIL_HPP_INCLUDED
#define TORRENT_STRING_UTIL_HPP_INCLUDED
namespace libtorrent
{
TORRENT_EXTRA_EXPORT bool is_alpha(char c);
TORRENT_EXPORT bool is_digit(char c);
TORRENT_EXTRA_EXPORT bool is_print(char c);
TORRENT_EXTRA_EXPORT bool is_space(char c);
TORRENT_EXTRA_EXPORT char to_lower(char c);
TORRENT_EXTRA_EXPORT int split_string(char const** tags, int buf_size, char* in);
TORRENT_EXTRA_EXPORT bool string_begins_no_case(char const* s1, char const* s2);
TORRENT_EXTRA_EXPORT bool string_equal_no_case(char const* s1, char const* s2);
TORRENT_EXTRA_EXPORT void url_random(char* begin, char* end);
// strdup is not part of the C standard. Some systems
// don't have it and it won't be available when building
// in strict ansi mode
char* allocate_string_copy(char const* str);
}
#endif

View File

@ -84,6 +84,7 @@ libtorrent_rasterbar_la_SOURCES = \
socks5_stream.cpp \
stat.cpp \
storage.cpp \
string_util.cpp \
thread.cpp \
torrent.cpp \
torrent_handle.cpp \

View File

@ -84,87 +84,6 @@ namespace libtorrent
return ret;
}
bool is_alpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
bool is_print(char c)
{
return c >= 32 && c < 127;
}
bool is_space(char c)
{
const static char* ws = " \t\n\r\f\v";
return std::strchr(ws, c) != 0;
}
// generate a url-safe random string
void url_random(char* begin, char* end)
{
// http-accepted characters:
// excluding ', since some buggy trackers don't support that
static char const printable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz-_.!~*()";
// the random number
while (begin != end)
*begin++ = printable[random() % (sizeof(printable)-1)];
}
char to_lower(char c)
{
return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
}
int split_string(char const** tags, int buf_size, char* in)
{
int ret = 0;
char* i = in;
for (;*i; ++i)
{
if (!is_print(*i) || is_space(*i))
{
*i = 0;
if (ret == buf_size) return ret;
continue;
}
if (i == in || i[-1] == 0)
{
tags[ret++] = i;
}
}
return ret;
}
bool string_begins_no_case(char const* s1, char const* s2)
{
while (*s1 != 0)
{
if (to_lower(*s1) != to_lower(*s2)) return false;
++s1;
++s2;
}
return true;
}
bool string_equal_no_case(char const* s1, char const* s2)
{
while (to_lower(*s1) == to_lower(*s2))
{
if (*s1 == 0) return true;
++s1;
++s2;
}
return false;
}
std::string unescape_string(std::string const& s, error_code& ec)
{
std::string ret;

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pch.hpp"
#include "libtorrent/file_storage.hpp"
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#include "libtorrent/file.hpp"
#include "libtorrent/utf8.hpp"
#include <boost/bind.hpp>
@ -147,7 +148,7 @@ namespace libtorrent
}
else
{
name = borrow_chars ? n : strdup(n);
name = borrow_chars ? n : allocate_string_copy(n);
}
name_len = borrow_chars;
}

View File

@ -48,7 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/identify_client.hpp"
#include "libtorrent/fingerprint.hpp"
#include "libtorrent/escape_string.hpp"
#include "libtorrent/string_util.hpp"
namespace
{

133
src/string_util.cpp Normal file
View File

@ -0,0 +1,133 @@
/*
Copyright (c) 2012, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/config.hpp"
#include "libtorrent/string_util.hpp"
#include <stdlib.h> // for malloc/free
#include <string.h> // for strcpy/strlen
namespace libtorrent
{
bool is_alpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
bool is_print(char c)
{
return c >= 32 && c < 127;
}
bool is_space(char c)
{
const static char* ws = " \t\n\r\f\v";
return strchr(ws, c) != 0;
}
char to_lower(char c)
{
return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
}
int split_string(char const** tags, int buf_size, char* in)
{
int ret = 0;
char* i = in;
for (;*i; ++i)
{
if (!is_print(*i) || is_space(*i))
{
*i = 0;
if (ret == buf_size) return ret;
continue;
}
if (i == in || i[-1] == 0)
{
tags[ret++] = i;
}
}
return ret;
}
bool string_begins_no_case(char const* s1, char const* s2)
{
while (*s1 != 0)
{
if (to_lower(*s1) != to_lower(*s2)) return false;
++s1;
++s2;
}
return true;
}
bool string_equal_no_case(char const* s1, char const* s2)
{
while (to_lower(*s1) == to_lower(*s2))
{
if (*s1 == 0) return true;
++s1;
++s2;
}
return false;
}
// generate a url-safe random string
void url_random(char* begin, char* end)
{
// http-accepted characters:
// excluding ', since some buggy trackers don't support that
static char const printable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz-_.!~*()";
// the random number
while (begin != end)
*begin++ = printable[random() % (sizeof(printable)-1)];
}
char* allocate_string_copy(char const* str)
{
if (str == 0) return 0;
char* tmp = (char*)malloc(strlen(str) + 1);
if (tmp == 0) return 0;
strcpy(tmp, str);
return tmp;
}
}

View File

@ -81,6 +81,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/http_connection.hpp"
#include "libtorrent/gzip.hpp" // for inflate_gzip
#include "libtorrent/random.hpp"
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#ifdef TORRENT_USE_OPENSSL
#include "libtorrent/ssl_stream.hpp"
@ -2129,7 +2130,7 @@ namespace libtorrent
INVARIANT_CHECK;
m_net_interfaces.clear();
char* str = strdup(net_interfaces.c_str());
char* str = allocate_string_copy(net_interfaces.c_str());
char* ptr = str;
while (ptr)

View File

@ -34,9 +34,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket.hpp"
#include "libtorrent/udp_socket.hpp"
#include "libtorrent/connection_queue.hpp"
#include "libtorrent/escape_string.hpp"
#include "libtorrent/socket_io.hpp"
#include "libtorrent/error.hpp"
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#include <stdlib.h>
#include <boost/bind.hpp>
#include <boost/array.hpp>
@ -144,7 +144,7 @@ void udp_socket::send_hostname(char const* hostname, int port
m_queue.push_back(queued_packet());
queued_packet& qp = m_queue.back();
qp.ep.port(port);
qp.hostname = strdup(hostname);
qp.hostname = allocate_string_copy(hostname);
qp.buf.insert(qp.buf.begin(), p, p + len);
qp.flags = 0;
}