/* Miscellaneous routines.
*
* (C) 2003-2010 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
*
*/
#include "services.h"
#include "language.h"
#include "hashcomp.h" // If this gets added to services.h or someplace else later, remove it from here -- CyberBotX
/* Cheaper than isspace() or isblank() */
#define issp(c) ((c) == 32)
struct arc4_stream {
uint8 i;
uint8 j;
uint8 s[256];
} rs;
/*************************************************************************/
/** Check if a file exists
* @param filename The file
* @return true if the file exists, false if it doens't
*/
bool IsFile(const std::string &filename)
{
struct stat fileinfo;
if (!stat(filename.c_str(), &fileinfo))
{
return true;
}
return false;
}
/**
* toupper: Like the ANSI functions, but make sure we return an
* int instead of a (signed) char.
* @param c Char
* @return int
*/
int toupper(char c)
{
if (islower(c)) {
return static_cast<int>(c) - ('a' - 'A');
} else {
return static_cast<int>(c);
}
}
/*************************************************************************/
/**
* tolower: Like the ANSI functions, but make sure we return an
* int instead of a (signed) char.
* @param c Char
* @return int
*/
int tolower(char c)
{
if (isupper(c)) {
return static_cast<int>(c) + ('a' - 'A');
} else {
return static_cast<int>(c);
}
}
/*************************************************************************/
/**
* Simple function to convert binary data to hex.
* Taken from hybrid-ircd ( http://ircd-hybrid.com/ )
*/
void binary_to_hex(unsigned char *bin, char *hex, int length)
{
static const char trans[] = "0123456789ABCDEF";
int i;
for (i = 0; i < length; i++) {
hex[i << 1] = trans[bin[i] >> 4];
hex[(i << 1) + 1] = trans[bin[i] & 0xf];
}
hex[i << 1] = '\0';
}
/*************************************************************************/
/**
* strscpy: Copy at most len-1 characters from a string to a buffer, and
* add a null terminator after the last character copied.
* @param d Buffer to copy into
* @param s Data to copy int
* @param len Length of data
* @return updated buffer
*/
char *strscpy(char *d, const char *s, size_t len)
{
char *d_orig = d;
if (!len) {
return d;
}
while (--len && (*d++ = *s++));
*d = '\0';
return d_orig;
}
/*************************************************************************/
/**
* stristr: Search case-insensitively for string s2 within string s1,
* returning the first occurrence of s2 or NULL if s2 was not
* found.
* @param s1 String 1
* @param s2 String 2
* @return first occurrence of s2
*/
const char *stristr(const char *s1, const char *s2)
{
register const char *s = s1, *d = s2;
while (*s1) {
if (tolower(*s1) == tolower(*d)) {
s1++;
d++;
if (*d == 0)
return s;
} else {
s = ++s1;
d = s2;
}
}
return NULL;
}
/*************************************************************************/
/**
* strnrepl: Replace occurrences of `old' with `new' in string `s'. Stop
* replacing if a replacement would cause the string to exceed
* `size' bytes (including the null terminator). Return the
* string.
* @param s String
* @param size size of s
* @param old character to replace
* @param newstr character to replace with
* @return updated s
*/
char *strnrepl(char *s, int32 size, const char *old, const char *newstr)
{
char *ptr = s;
int32 left = strlen(s);
int32 avail = size - (left + 1);
int32 oldlen = strlen(old);
int32 newlen = strlen(newstr);
int32 diff = newlen - oldlen;
while (left >= oldlen) {
if (strncmp(ptr, old, oldlen) != 0) {
left--;
ptr++;
continue;
}
if (diff > avail)
break;
if (diff != 0)
memmove(ptr + oldlen + diff, ptr + oldlen, left + 1 - oldlen);
strncpy(ptr, newstr, newlen);
ptr += newlen;
left -= oldlen;
}
return s;
}
/*************************************************************************/
/**
* merge_args: Take an argument count and argument vector and merge them
* into a single string in which each argument is separated by
* a space.
* @param int Number of Args
* @param argv Array
* @return string of the merged array
*/
const char *merge_args(int argc, const char **argv)
{
int i;
static char s[4096];
char *t;
t = s;
for (i = 0; i < argc; i++)
t += snprintf(t, sizeof(s) - (t - s), "%s%s", *argv++,
(i < argc - 1) ? " " : "");
return s;
}
/*
* XXX: temporary "safe" version to avoid casting, it's still ugly.
*/
const char *merge_args(int argc, char **argv)
{
int i;
static char s[4096];
char *t;
t = s;
for (i = 0; i < argc; i++)
t += snprintf(t, sizeof(s) - (t - s), "%s%s", *argv++,
(i < argc - 1) ? " " : "");
return s;
}
/*************************************************************************/
NumberList::NumberList(const std::string &list, bool descending) : desc(descending)
{
char *error;
commasepstream sep(list);
std::string token;
sep.GetToken(token);
if (token.empty())
token = list;
do
{
size_t t = token.find('-');
if (t == std::string::npos)
{
unsigned num = strtol(token.c_str(), &error, 10);
if (*error == '\0')
{
numbers.insert(num);
}
else
{
if (!this->InvalidRange(list))
{
delete this;
return;
}
}
}
else
{
char *error2;
unsigned num1 = strtol(token.substr(0, t).c_str(), &error, 10);
unsigned num2 = strtol(token.substr(t + 1).c_str(), &error2, 10);
if (*error == '\0' && *error2 == '\0')
{
for (unsigned i = num1; i <= num2; ++i)
{
numbers.insert(i);
}
}
else
{
if (!this->InvalidRange(list))
{
delete this;
return;
}
}
}
} while (sep.GetToken(token));
}
NumberList::~NumberList()
{
}
void NumberList::Process()
{
if (this->desc)
{
for (std::set<unsigned>::reverse_iterator it = numbers.rbegin(); it != numbers.rend(); ++it)
{
this->HandleNumber(*it);
}
}
else
{
for (std::set<unsigned>::iterator it = numbers.begin(); it != numbers.end(); ++it)
{
this->HandleNumber(*it);
}
}
delete this;
}
void NumberList::HandleNumber(unsigned)
{
}
bool NumberList::InvalidRange(const std::string &)
{
return true;
}
/**
* dotime: Return the number of seconds corresponding to the given time
* string. If the given string does not represent a valid time,
* return -1.
*
* A time string is either a plain integer (representing a number
* of seconds), or an integer followed by one of these characters:
* "s" (seconds), "m" (minutes), "h" (hours), or "d" (days).
* @param s String to convert
* @return time_t
*/
time_t dotime(const char *s)
{
int amount;
if (!s || !*s) {
return -1;
}
amount = strtol(s, const_cast<char **>(&s), 10);
if (*s) {
switch (*s) {
case 's':
return amount;
case 'm':
return amount * 60;
case 'h':
return amount * 3600;
case 'd':
return amount * 86400;
case 'w':
return amount * 86400 * 7;
case 'y':
return amount * 86400 * 365;
default:
return -1;
}
} else {
return amount;
}
}
/*
|