1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ags/shared/util/string_compat.h"
#include "ags/shared/core/platform.h"
#include "ags/lib/allegro/error.h"
#include "common/str.h"
namespace AGS3 {
char *ags_strlwr(char *s) {
char *p = s;
for (; *p; p++)
*p = (char)tolower(*p);
return s;
}
char *ags_strupr(char *s) {
char *p = s;
for (; *p; p++)
*p = (char)toupper(*p);
return s;
}
int ags_stricmp(const char *s1, const char *s2) {
return scumm_stricmp(s1, s2);
}
int ags_strnicmp(const char *s1, const char *s2, size_t n) {
return scumm_strnicmp(s1, s2, n);
}
char *ags_strdup(const char *s) {
size_t len = strlen(s);
char *result = (char *)malloc(len + 1);
memcpy(result, s, len + 1);
return result;
}
int ags_strncpy_s(char *dest, size_t dest_sz, const char *src, size_t count) {
// NOTE: implementation approximately mimics explanation for "strncpy_s":
// https://en.cppreference.com/w/c/string/byte/strncpy
assert(dest && dest_sz > 0 && ((dest + dest_sz - 1 < src) || (dest > src + count)));
if (!dest || dest_sz == 0 || ((dest <= src) && (dest + dest_sz - 1 >= src)) || ((src <= dest) && (src + count - 1 >= dest)))
return AL_EINVAL; // null buffer, or dest and src overlap
if (!src) {
dest[0] = 0; // ensure null terminator
return AL_EINVAL;
}
const size_t copy_len = (count < dest_sz - 1) ? count : dest_sz - 1; // reserve null-terminator
const char *psrc = src;
const char *src_end = src + copy_len;
char *pdst = dest;
for (; *psrc && (psrc != src_end); ++psrc, ++pdst)
*pdst = *psrc;
*pdst = 0; // ensure null terminator
assert((*psrc == 0) || ((psrc - src) == (int)count)); // assert that no *unintended* truncation occurred
if ((*psrc != 0) && ((psrc - src) < (int)count))
return AL_ERANGE; // not enough dest buffer - error
return 0; // success
}
} // namespace AGS3
|