DOSBox-X
|
00001 /* 00002 * Copyright (C) 2002-2020 The DOSBox Team 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License along 00015 * with this program; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 */ 00018 00019 00020 #ifndef DOSBOX_SUPPORT_H 00021 #define DOSBOX_SUPPORT_H 00022 00023 #include <string.h> 00024 #include <string> 00025 #include <ctype.h> 00026 #ifndef DOSBOX_DOSBOX_H 00027 #include "dosbox.h" 00028 #endif 00029 00030 #if defined (_MSC_VER) /* MS Visual C++ */ 00031 #define strcasecmp(a,b) stricmp(a,b) 00032 #define strncasecmp(a,b,n) _strnicmp(a,b,n) 00033 #endif 00034 00035 #define safe_strncpy(a,b,n) do { strncpy((a),(b),(size_t)((n)-1)); (a)[(size_t)((n)-1)] = 0; } while (0) 00036 00037 #ifdef HAVE_STRINGS_H 00038 #include <strings.h> 00039 #endif 00040 00041 void strreplace(char * str,char o,char n); 00042 char *ltrim(char *str); 00043 char *rtrim(char *str); 00044 char *trim(char * str); 00045 char * upcase(char * str); 00046 char * lowcase(char * str); 00047 char * StripArg(char *&cmd); 00048 bool ScanCMDBool(char * cmd,char const * const check); 00049 char * ScanCMDRemain(char * cmd); 00050 char * StripWord(char *&line); 00051 Bits ConvDecWord(char * word); 00052 Bits ConvHexWord(char * word); 00053 00054 void trim(std::string& str); 00055 void upcase(std::string &str); 00056 void lowcase(std::string &str); 00057 00058 static inline bool is_power_of_2(Bitu val) { 00059 return (val != 0) && ((val&(val-1)) == 0); 00060 /* To explain: if val is a power of 2, then only one bit is set. 00061 * Decrementing val would change that one bit to 0, and all bits to the right to 1. 00062 * Example: 00063 * 00064 * Power of 2: val = 1024 00065 * 00066 * 1024 = 0000 0100 0000 0000 00067 * AND 1023 = 0000 0011 1111 1111 00068 * ------------------------------ 00069 * 0 = 0000 0000 0000 0000 00070 * 00071 * Non-power of 2: val = 713 00072 * 00073 * 713 = 0000 0010 1100 1001 00074 * AND 712 = 0000 0010 1100 1000 00075 * ------------------------------ 00076 * 712 = 0000 0010 1100 1000 00077 * 00078 * See how that works? 00079 * 00080 * For more info see https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2*/ 00081 } 00082 00097 00098 template<size_t N> 00099 char * safe_strcpy(char (& dst)[N], const char * src) noexcept { 00100 snprintf(dst, N, "%s", src); 00101 return & dst[0]; 00102 } 00103 00104 template<size_t N> 00105 char * safe_strcat(char (& dst)[N], const char * src) noexcept { 00106 strncat(dst, src, N - strnlen(dst, N) - 1); 00107 return & dst[0]; 00108 } 00109 00110 #endif