From 85db2023cb033928a190ac7fa6f05b5d6cbad14a Mon Sep 17 00:00:00 2001 From: FlavioJS Date: Sat, 2 Jun 2007 20:19:40 +0000 Subject: * Displaying op names instead of numbers in script engine errors. * Fixed a bug introduced in the last rework of the fame ranking. * Created safestrncpy that ensures the string is nul-terminated. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@10667 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/db.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/strlib.c | 19 ++++++++++--- src/common/strlib.h | 9 +++++-- 3 files changed, 99 insertions(+), 6 deletions(-) (limited to 'src/common') diff --git a/src/common/db.h b/src/common/db.h index 992469f66..5c7d42d87 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -691,4 +691,81 @@ void* linkdb_search ( struct linkdb_node** head, void *key); void* linkdb_erase ( struct linkdb_node** head, void *key); void linkdb_final ( struct linkdb_node** head ); + + +/// Finds an entry in an array. +/// ex: ARR_FIND(0, size, i, list[i] == target); +/// +/// @param __start Starting index (ex: 0) +/// @param __end End index (ex: size of the array) +/// @param __var Index variable +/// @param __cmp Expression that returns true when the target entry is found +#define ARR_FIND(__start, __end, __var, __cmp) \ + do{ \ + for( (__var) = (__start); (__var) < (__end); ++(__var) ) \ + if( __cmp ) \ + break; \ + }while(0) + + + +/// Moves an entry of the array. +/// Use ARR_MOVERIGHT/ARR_MOVELEFT if __from and __to are direct numbers. +/// ex: ARR_MOVE(i, 0, list, int);// move index i to index 0 +/// +/// +/// @param __from Initial index of the entry +/// @param __end Target index of the entry +/// @param __arr Array +/// @param __type Type of entry +#define ARR_MOVE(__from, __to, __arr, __type) \ + do{ \ + if( (__from) != (__to) ) \ + { \ + __type __backup__; \ + memmove(&__backup__, (__arr)+(__from), sizeof(__type)); \ + if( (__from) < (__to) ) \ + memmove((__arr)+(__from), (__arr)+(__from)+1, ((__to)-(__from))*sizeof(__type)); \ + else if( (__from) > (__to) ) \ + memmove((__arr)+(__to)+1, (__arr)+(__to), ((__from)-(__to))*sizeof(__type)); \ + memmove((__arr)+(__to), &__backup__, sizeof(__type)); \ + } \ + }while(0) + + + +/// Moves an entry of the array to the right. +/// ex: ARR_MOVERIGHT(1, 4, list, int);// move index 1 to index 4 +/// +/// @param __from Initial index of the entry +/// @param __end Target index of the entry +/// @param __arr Array +/// @param __type Type of entry +#define ARR_MOVERIGHT(__from, __to, __arr, __type) \ + do{ \ + __type __backup__; \ + memmove(&__backup__, (__arr)+(__from), sizeof(__type)); \ + memmove((__arr)+(__from), (__arr)+(__from)+1, ((__to)-(__from))*sizeof(__type)); \ + memmove((__arr)+(__to), &__backup__, sizeof(__type)); \ + }while(0) + + + +/// Moves an entry of the array to the left. +/// ex: ARR_MOVELEFT(3, 0, list, int);// move index 3 to index 0 +/// +/// @param __from Initial index of the entry +/// @param __end Target index of the entry +/// @param __arr Array +/// @param __type Type of entry +#define ARR_MOVELEFT(__from, __to, __arr, __type) \ + do{ \ + __type __backup__; \ + memmove(&__backup__, (__arr)+(__from), sizeof(__type)); \ + memmove((__arr)+(__to)+1, (__arr)+(__to), ((__from)-(__to))*sizeof(__type)); \ + memmove((__arr)+(__to), &__backup__, sizeof(__type)); \ + }while(0) + + + #endif /* _DB_H_ */ diff --git a/src/common/strlib.c b/src/common/strlib.c index 977dc8306..a683dc100 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -1,14 +1,15 @@ // Copyright (c) Athena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder +#include "../common/cbasetypes.h" +#include "../common/malloc.h" +#include "../common/utils.h" +#include "strlib.h" + #include #include #include -#include "strlib.h" -#include "../common/cbasetypes.h" -#include "../common/utils.h" -#include "../common/malloc.h" #define J_MAX_MALLOC_SIZE 65535 @@ -299,3 +300,13 @@ int config_switch(const char* str) return (int)strtol(str, NULL, 0); } + +/// always nul-terminates the string +char* safestrncpy(char* dst, const char* src, size_t n) +{ + char* ret; + ret = strncpy(dst, src, n); + if( ret != NULL ) + ret[n - 1] = '\0'; + return ret; +} diff --git a/src/common/strlib.h b/src/common/strlib.h index a3753b918..42fb75ab2 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -4,7 +4,9 @@ #ifndef _STRLIB_H_ #define _STRLIB_H_ -#include // size_t +#ifndef _CBASETYPES_H_ +#include "../common/cbasetypes.h" +#endif char* jstrescape (char* pt); char* jstrescapecpy (char* pt, const char* spt); @@ -15,7 +17,7 @@ char* trim(char* str); char* normalize_name(char* str,const char* delims); const char *stristr(const char *haystack, const char *needle); -#ifdef __WIN32 +#ifdef WIN32 #define HAVE_STRTOK_R #define strtok_r(s,delim,save_ptr) _strtok_r((s),(delim),(save_ptr)) char* _strtok_r(char* s1, const char* s2, char** lasts); @@ -28,4 +30,7 @@ size_t strnlen (const char* string, size_t maxlen); int e_mail_check(char* email); int config_switch(const char* str); +/// always nul-terminates the string +char* safestrncpy(char* dst, const char* src, size_t n); + #endif /* _STRLIB_H_ */ -- cgit v1.2.3-60-g2f50