DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
include/support.h
00001 /*
00002  *  Copyright (C) 2002-2013  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
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 
00048 bool ScanCMDBool(char * cmd,char const * const check);
00049 char * ScanCMDRemain(char * cmd);
00050 char * StripWord(char *&cmd);
00051 bool IsDecWord(char * word);
00052 bool IsHexWord(char * word);
00053 Bits ConvDecWord(char * word);
00054 Bits ConvHexWord(char * word);
00055 
00056 void upcase(std::string &str);
00057 void lowcase(std::string &str);
00058 
00059 static inline bool is_power_of_2(Bitu val) {
00060         return (val != 0) && ((val&(val-1)) == 0);
00061         /* To explain: if val is a power of 2, then only one bit is set.
00062          * Decrementing val would change that one bit to 0, and all bits to the right to 1.
00063          * Example:
00064          *
00065          * Power of 2: val = 1024
00066          *
00067          *      1024 = 0000 0100 0000 0000
00068          *  AND 1023 = 0000 0011 1111 1111
00069          *  ------------------------------
00070          *         0 = 0000 0000 0000 0000
00071          *
00072          * Non-power of 2: val = 713
00073          *
00074          *       713 = 0000 0010 1100 1001
00075          *   AND 712 = 0000 0010 1100 1000
00076          *  ------------------------------
00077          *       712 = 0000 0010 1100 1000
00078          *
00079          * See how that works?
00080          *
00081          * For more info see https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2*/
00082 }
00083 
00084 #endif