(svn r26485) -Codechange: Replace ttd_strlcpy and ttd_strlcat with strecpy and strecat.

This commit is contained in:
frosch 2014-04-23 20:44:42 +00:00
parent 56e8ea6dde
commit ef4c2ce031
19 changed files with 58 additions and 108 deletions

View File

@ -98,7 +98,7 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo
if (nameParam == NULL) return NULL;
char ai_name[1024];
ttd_strlcpy(ai_name, nameParam, sizeof(ai_name));
strecpy(ai_name, nameParam, lastof(ai_name));
strtolower(ai_name);
AIInfo *info = NULL;

View File

@ -400,7 +400,7 @@ char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
}
/* Could not find the directory, fall back to a base path */
ttd_strlcpy(buf, _personal_dir, buflen);
strecpy(buf, _personal_dir, &buf[buflen - 1]);
return buf;
}
@ -550,7 +550,7 @@ static void FioCreateDirectory(const char *name)
mkdir(OTTD2FS(name));
#elif defined(__MORPHOS__) || defined(__AMIGAOS__)
char buf[MAX_PATH];
ttd_strlcpy(buf, name, MAX_PATH);
strecpy(buf, name, lastof(buf));
size_t len = strlen(name) - 1;
if (buf[len] == '/') {
@ -594,7 +594,8 @@ bool AppendPathSeparator(char *buf, size_t buflen)
char *BuildWithFullPath(const char *dir)
{
char *dest = MallocT<char>(MAX_PATH);
ttd_strlcpy(dest, dir, MAX_PATH);
char *last = dest + MAX_PATH - 1;
strecpy(dest, dir, last);
/* Check if absolute or relative path */
const char *s = strchr(dest, PATHSEPCHAR);
@ -603,7 +604,7 @@ char *BuildWithFullPath(const char *dir)
if (s == NULL || dest != s) {
if (getcwd(dest, MAX_PATH) == NULL) *dest = '\0';
AppendPathSeparator(dest, MAX_PATH);
ttd_strlcat(dest, dir, MAX_PATH);
strecat(dest, dir, last);
}
AppendPathSeparator(dest, MAX_PATH);
@ -791,15 +792,15 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
/* The prefix contains the directory-name */
if (th.prefix[0] != '\0') {
ttd_strlcpy(name, th.prefix, lengthof(name));
ttd_strlcat(name, PATHSEP, lengthof(name));
strecpy(name, th.prefix, lastof(name));
strecat(name, PATHSEP, lastof(name));
}
/* Copy the name of the file in a safe way at the end of 'name' */
ttd_strlcat(name, th.name, lengthof(name));
strecat(name, th.name, lastof(name));
/* Calculate the size of the file.. for some strange reason this is stored as a string */
ttd_strlcpy(buf, th.size, lengthof(buf));
strecpy(buf, th.size, lastof(buf));
size_t skip = strtoul(buf, &end, 8);
switch (th.typeflag) {
@ -828,7 +829,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
case '1': // hard links
case '2': { // symbolic links
/* Copy the destination of the link in a safe way at the end of 'linkname' */
ttd_strlcpy(link, th.linkname, lengthof(link));
strecpy(link, th.linkname, lastof(link));
if (strlen(name) == 0 || strlen(link) == 0) break;
@ -844,7 +845,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
/* Process relative path.
* Note: The destination of links must not contain any directory-links. */
ttd_strlcpy(dest, name, lengthof(dest));
strecpy(dest, name, lastof(dest));
char *destpos = strrchr(dest, PATHSEPCHAR);
if (destpos == NULL) destpos = dest;
*destpos = '\0';

View File

@ -39,7 +39,7 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo
if (nameParam == NULL) return NULL;
char game_name[1024];
ttd_strlcpy(game_name, nameParam, sizeof(game_name));
strecpy(game_name, nameParam, lastof(game_name));
strtolower(game_name);
GameInfo *info = NULL;

View File

@ -276,7 +276,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta
char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkClientSocket *new_cs;
this->GetClientName(client_name, sizeof(client_name));
this->GetClientName(client_name, lastof(client_name));
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
@ -382,7 +382,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyInfo()
FOR_ALL_CLIENT_SOCKETS(csi) {
char client_name[NETWORK_CLIENT_NAME_LENGTH];
((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, sizeof(client_name));
((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, lastof(client_name));
ci = csi->GetInfo();
if (ci != NULL && Company::IsValidID(ci->client_playas)) {
@ -444,7 +444,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode err
NetworkClientSocket *new_cs;
char client_name[NETWORK_CLIENT_NAME_LENGTH];
this->GetClientName(client_name, sizeof(client_name));
this->GetClientName(client_name, lastof(client_name));
DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str);
@ -1044,7 +1044,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet *
char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkClientSocket *new_cs;
this->GetClientName(client_name, sizeof(client_name));
this->GetClientName(client_name, lastof(client_name));
NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, NULL, this->client_id);
@ -1162,7 +1162,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p
return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
}
this->GetClientName(client_name, sizeof(client_name));
this->GetClientName(client_name, lastof(client_name));
StringID strid = GetNetworkErrorMsg(errorno);
GetString(str, strid, lastof(str));
@ -1194,7 +1194,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p)
return this->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
}
this->GetClientName(client_name, sizeof(client_name));
this->GetClientName(client_name, lastof(client_name));
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
@ -1710,7 +1710,7 @@ bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
char original_name[NETWORK_CLIENT_NAME_LENGTH];
/* We use NETWORK_CLIENT_NAME_LENGTH in here, because new_name is really a pointer */
ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH);
strecpy(original_name, new_name, lastof(original_name));
while (!found_name) {
const NetworkClientInfo *ci;
@ -2138,16 +2138,16 @@ bool NetworkCompanyHasClients(CompanyID company)
/**
* Get the name of the client, if the user did not send it yet, Client #<no> is used.
* @param client_name The variable to write the name to.
* @param size The amount of bytes we can write.
* @param last The pointer to the last element of the destination buffer
*/
void ServerNetworkGameSocketHandler::GetClientName(char *client_name, size_t size) const
void ServerNetworkGameSocketHandler::GetClientName(char *client_name, const char *last) const
{
const NetworkClientInfo *ci = this->GetInfo();
if (ci == NULL || StrEmpty(ci->client_name)) {
snprintf(client_name, size, "Client #%4d", this->client_id);
seprintf(client_name, last, "Client #%4d", this->client_id);
} else {
ttd_strlcpy(client_name, ci->client_name, size);
strecpy(client_name, ci->client_name, last);
}
}

View File

@ -83,7 +83,7 @@ public:
virtual Packet *ReceivePacket();
NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
void GetClientName(char *client_name, size_t size) const;
void GetClientName(char *client_name, const char *last) const;
NetworkRecvStatus SendMap();
NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);

View File

@ -1495,7 +1495,7 @@ void ShowMissingContentWindow(const GRFConfig *list)
ContentInfo *ci = new ContentInfo();
ci->type = CONTENT_TYPE_NEWGRF;
ci->state = ContentInfo::DOES_NOT_EXIST;
ttd_strlcpy(ci->name, c->GetName(), lengthof(ci->name));
strecpy(ci->name, c->GetName(), lastof(ci->name));
ci->unique_id = BSWAP32(c->ident.grfid);
memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
*cv.Append() = ci;

View File

@ -155,11 +155,11 @@ const char *GetCurrentLocale(const char *)
/**
* Return the contents of the clipboard (COCOA).
*
* @param buffer Clipboard content..
* @param buff_len Length of the clipboard content..
* @param buffer Clipboard content.
* @param last The pointer to the last element of the destination buffer
* @return Whether clipboard is empty or not.
*/
bool GetClipboardContents(char *buffer, size_t buff_len)
bool GetClipboardContents(char *buffer, const char *last)
{
NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
NSArray *types = [ NSArray arrayWithObject:NSStringPboardType ];
@ -171,7 +171,7 @@ bool GetClipboardContents(char *buffer, size_t buff_len)
NSString *string = [ pb stringForType:NSStringPboardType ];
if (string == nil || [ string length ] == 0) return false;
ttd_strlcpy(buffer, [ string UTF8String ], buff_len);
strecpy(buffer, [ string UTF8String ], last);
return true;
}

View File

@ -177,7 +177,7 @@ int CDECL main(int argc, char *argv[])
return openttd_main(argc, argv);
}
bool GetClipboardContents(char *buffer, size_t buff_len)
bool GetClipboardContents(char *buffer, const char *last)
{
/* XXX -- Currently no clipboard support implemented with GCC */
#ifndef __INNOTEK_LIBC__
@ -189,7 +189,7 @@ bool GetClipboardContents(char *buffer, size_t buff_len)
if (text != NULL)
{
ttd_strlcpy(buffer, text, buff_len);
strecpy(buffer, text, last);
WinCloseClipbrd(hab);
return true;
}

View File

@ -284,7 +284,7 @@ int CDECL main(int argc, char *argv[])
}
#ifndef WITH_COCOA
bool GetClipboardContents(char *buffer, size_t buff_len)
bool GetClipboardContents(char *buffer, const char *last)
{
return false;
}

View File

@ -496,7 +496,7 @@ void DetermineBasePaths(const char *exe)
if (SUCCEEDED(OTTDSHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path))) {
strecpy(tmp, FS2OTTD(path), lastof(tmp));
AppendPathSeparator(tmp, MAX_PATH);
ttd_strlcat(tmp, PERSONAL_DIR, MAX_PATH);
strecat(tmp, PERSONAL_DIR, lastof(tmp));
AppendPathSeparator(tmp, MAX_PATH);
_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
} else {
@ -506,7 +506,7 @@ void DetermineBasePaths(const char *exe)
if (SUCCEEDED(OTTDSHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path))) {
strecpy(tmp, FS2OTTD(path), lastof(tmp));
AppendPathSeparator(tmp, MAX_PATH);
ttd_strlcat(tmp, PERSONAL_DIR, MAX_PATH);
strecat(tmp, PERSONAL_DIR, lastof(tmp));
AppendPathSeparator(tmp, MAX_PATH);
_searchpaths[SP_SHARED_DIR] = strdup(tmp);
} else {
@ -544,7 +544,7 @@ void DetermineBasePaths(const char *exe)
}
bool GetClipboardContents(char *buffer, size_t buff_len)
bool GetClipboardContents(char *buffer, const char *last)
{
HGLOBAL cbuf;
const char *ptr;
@ -554,7 +554,7 @@ bool GetClipboardContents(char *buffer, size_t buff_len)
cbuf = GetClipboardData(CF_UNICODETEXT);
ptr = (const char*)GlobalLock(cbuf);
int out_len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)ptr, -1, buffer, (int)buff_len, NULL, NULL);
int out_len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)ptr, -1, buffer, (last - buffer) + 1, NULL, NULL);
GlobalUnlock(cbuf);
CloseClipboard();
@ -565,7 +565,7 @@ bool GetClipboardContents(char *buffer, size_t buff_len)
cbuf = GetClipboardData(CF_TEXT);
ptr = (const char*)GlobalLock(cbuf);
ttd_strlcpy(buffer, FS2OTTD(ptr), buff_len);
strecpy(buffer, FS2OTTD(ptr), last);
GlobalUnlock(cbuf);
CloseClipboard();

View File

@ -41,7 +41,7 @@ static void SaveReal_AIPL(int *index_ptr)
AIConfig *config = AIConfig::GetConfig(index);
if (config->HasScript()) {
ttd_strlcpy(_ai_saveload_name, config->GetName(), lengthof(_ai_saveload_name));
strecpy(_ai_saveload_name, config->GetName(), lastof(_ai_saveload_name));
_ai_saveload_version = config->GetVersion();
} else {
/* No AI is configured for this so store an empty string as name. */

View File

@ -40,7 +40,7 @@ static void SaveReal_GSDT(int *index_ptr)
GameConfig *config = GameConfig::GetConfig();
if (config->HasScript()) {
ttd_strlcpy(_game_saveload_name, config->GetName(), lengthof(_game_saveload_name));
strecpy(_game_saveload_name, config->GetName(), lastof(_game_saveload_name));
_game_saveload_version = config->GetVersion();
} else {
/* No GameScript is configured for this so store an empty string as name. */

View File

@ -133,7 +133,7 @@ ScriptController::~ScriptController()
LoadedLibraryList::iterator iter = controller->loaded_library.find(library_name);
if (iter != controller->loaded_library.end()) {
ttd_strlcpy(fake_class, (*iter).second, sizeof(fake_class));
strecpy(fake_class, (*iter).second, lastof(fake_class));
} else {
int next_number = ++controller->loaded_library_count;

View File

@ -117,7 +117,7 @@ bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirect
Searchpath sp;
FOR_ALL_SEARCHPATHS(sp) {
FioAppendDirectory(buf, MAX_PATH, sp, dir);
ttd_strlcat(buf, script_name, MAX_PATH);
strecat(buf, script_name, lastof(buf));
if (!FileExists(buf)) continue;
if (this->engine->LoadScript(buf)) return true;

View File

@ -526,7 +526,7 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
if (p != NULL) strecpy((char*)ptr, (const char*)p, (char*)ptr + sld->length - 1);
break;
case SLE_VAR_STR:
@ -1929,7 +1929,7 @@ bool SetSettingValue(uint index, const char *value, bool force_newgame)
*var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
} else {
char *var = (char*)GetVariableAddress(NULL, &sd->save);
ttd_strlcpy(var, value, sd->save.length);
strecpy(var, value, &var[sd->save.length - 1]);
}
if (sd->desc.proc != NULL) sd->desc.proc(0);

View File

@ -386,13 +386,13 @@ static inline void ottd_mkdir(const char *directory)
* path separator and the filename. The separator is only appended if the path
* does not already end with a separator
*/
static inline char *mkpath(char *buf, size_t buflen, const char *path, const char *file)
static inline char *mkpath(char *buf, const char *last, const char *path, const char *file)
{
ttd_strlcpy(buf, path, buflen); // copy directory into buffer
strecpy(buf, path, last); // copy directory into buffer
char *p = strchr(buf, '\0'); // add path separator if necessary
if (p[-1] != PATHSEPCHAR && (size_t)(p - buf) + 1 < buflen) *p++ = PATHSEPCHAR;
ttd_strlcpy(p, file, buflen - (size_t)(p - buf)); // concatenate filename at end of buffer
if (p[-1] != PATHSEPCHAR && p != last) *p++ = PATHSEPCHAR;
strecpy(p, file, last); // concatenate filename at end of buffer
return buf;
}
@ -522,7 +522,7 @@ int CDECL main(int argc, char *argv[])
* with a (free) parameter the program will translate that language to destination
* directory. As input english.txt is parsed from the source directory */
if (mgo.numleft == 0) {
mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
mkpath(pathbuf, lastof(pathbuf), src_dir, "english.txt");
/* parse master file */
StringData data(TAB_COUNT);
@ -532,7 +532,7 @@ int CDECL main(int argc, char *argv[])
/* write strings.h */
ottd_mkdir(dest_dir);
mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
mkpath(pathbuf, lastof(pathbuf), dest_dir, "strings.h");
HeaderFileWriter writer(pathbuf);
writer.WriteHeader(data);
@ -540,7 +540,7 @@ int CDECL main(int argc, char *argv[])
} else if (mgo.numleft >= 1) {
char *r;
mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
mkpath(pathbuf, lastof(pathbuf), src_dir, "english.txt");
StringData data(TAB_COUNT);
/* parse master file and check if target file is correct */
@ -558,12 +558,12 @@ int CDECL main(int argc, char *argv[])
/* get the targetfile, strip any directories and append to destination path */
r = strrchr(mgo.argv[i], PATHSEPCHAR);
mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : mgo.argv[i]);
mkpath(pathbuf, lastof(pathbuf), dest_dir, (r != NULL) ? &r[1] : mgo.argv[i]);
/* rename the .txt (input-extension) to .lng */
r = strrchr(pathbuf, '.');
if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
strecpy(r, ".lng", lastof(pathbuf));
LanguageFileWriter writer(pathbuf);
writer.WriteLang(data);

View File

@ -51,56 +51,6 @@ static int CDECL vseprintf(char *str, const char *last, const char *format, va_l
return min((int)diff, vsnprintf(str, diff + 1, format, ap));
}
/**
* Appends characters from one string to another.
*
* Appends the source string to the destination string with respect of the
* terminating null-character and the maximum size of the destination
* buffer.
*
* @note usage ttd_strlcat(dst, src, lengthof(dst));
* @note lengthof() applies only to fixed size arrays
*
* @param dst The buffer containing the target string
* @param src The buffer containing the string to append
* @param size The maximum size of the destination buffer
*/
void ttd_strlcat(char *dst, const char *src, size_t size)
{
assert(size > 0);
while (size > 0 && *dst != '\0') {
size--;
dst++;
}
ttd_strlcpy(dst, src, size);
}
/**
* Copies characters from one buffer to another.
*
* Copies the source string to the destination buffer with respect of the
* terminating null-character and the maximum size of the destination
* buffer.
*
* @note usage ttd_strlcpy(dst, src, lengthof(dst));
* @note lengthof() applies only to fixed size arrays
*
* @param dst The destination buffer
* @param src The buffer containing the string to copy
* @param size The maximum size of the destination buffer
*/
void ttd_strlcpy(char *dst, const char *src, size_t size)
{
assert(size > 0);
while (--size > 0 && *src != '\0') {
*dst++ = *src++;
}
*dst = '\0';
}
/**
* Appends characters from one string to another.
*

View File

@ -29,9 +29,6 @@
#include "core/bitmath_func.hpp"
#include "string_type.h"
void ttd_strlcat(char *dst, const char *src, size_t size);
void ttd_strlcpy(char *dst, const char *src, size_t size);
char *strecat(char *dst, const char *src, const char *last);
char *strecpy(char *dst, const char *src, const char *last);

View File

@ -26,9 +26,11 @@
* Try to retrieve the current clipboard contents.
*
* @note OS-specific function.
* @param buffer Clipboard content.
* @param last The pointer to the last element of the destination buffer
* @return True if some text could be retrieved.
*/
bool GetClipboardContents(char *buffer, size_t buff_len);
bool GetClipboardContents(char *buffer, const char *last);
int _caret_timer;
@ -226,7 +228,7 @@ bool Textbuf::InsertClipboard()
{
char utf8_buf[512];
if (!GetClipboardContents(utf8_buf, lengthof(utf8_buf))) return false;
if (!GetClipboardContents(utf8_buf, lastof(utf8_buf))) return false;
return this->InsertString(utf8_buf, false);
}
@ -406,7 +408,7 @@ void Textbuf::Assign(StringID string)
*/
void Textbuf::Assign(const char *text)
{
ttd_strlcpy(this->buf, text, this->max_bytes);
strecpy(this->buf, text, &this->buf[this->max_bytes - 1]);
this->UpdateSize();
}