DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/misc/support.cpp
00001 /*
00002  *  Copyright (C) 2002-2019  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA.
00017  */
00018 
00019 
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include <assert.h>
00023 #include <ctype.h>
00024 #include <stdarg.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <algorithm>
00028 #include <cctype>
00029 #include <string>
00030   
00031 #include "dosbox.h"
00032 #include "debug.h"
00033 #include "support.h"
00034 #include "video.h"
00035 #include "menu.h"
00036 #include "SDL.h"
00037 
00038 void upcase(std::string &str) {
00039         int (*tf)(int) = std::toupper;
00040         std::transform(str.begin(), str.end(), str.begin(), tf);
00041 }
00042 
00043 void lowcase(std::string &str) {
00044         int (*tf)(int) = std::tolower;
00045         std::transform(str.begin(), str.end(), str.begin(), tf);
00046 }
00047 
00048 void trim(std::string &str) {
00049         std::string::size_type loc = str.find_first_not_of(" \r\t\f\n");
00050         if (loc != std::string::npos) str.erase(0,loc);
00051         loc = str.find_last_not_of(" \r\t\f\n");
00052         if (loc != std::string::npos) str.erase(loc+1);
00053 }
00054 
00055 /* 
00056         Ripped some source from freedos for this one.
00057 
00058 */
00059 
00060 
00061 /*
00062  * replaces all instances of character o with character c
00063  */
00064 
00065 
00066 void strreplace(char * str,char o,char n) {
00067         while (*str) {
00068                 if (*str==o) *str=n;
00069                 str++;
00070         }
00071 }
00072 char *ltrim(char *str) { 
00073         while (*str && isspace(*reinterpret_cast<unsigned char*>(str))) str++;
00074         return str;
00075 }
00076 
00077 char *rtrim(char *str) {
00078         char *p;
00079         p = strchr(str, '\0');
00080         while (--p >= str && isspace(*reinterpret_cast<unsigned char*>(p))) {}
00081         p[1] = '\0';
00082         return str;
00083 }
00084 
00085 char *trim(char *str) {
00086         return ltrim(rtrim(str));
00087 }
00088 
00089 char * upcase(char * str) {
00090     for (char* idx = str; *idx ; idx++) *idx = toupper(*reinterpret_cast<unsigned char*>(idx));
00091     return str;
00092 }
00093 
00094 char * lowcase(char * str) {
00095         for(char* idx = str; *idx ; idx++)  *idx = tolower(*reinterpret_cast<unsigned char*>(idx));
00096         return str;
00097 }
00098 
00099 
00100 
00101 bool ScanCMDBool(char * cmd,char const * const check) {
00102         char * scan=cmd;size_t c_len=strlen(check);
00103         while ((scan=strchr(scan,'/'))) {
00104                 /* found a / now see behind it */
00105                 scan++;
00106                 if (strncasecmp(scan,check,c_len)==0 && (scan[c_len]==' ' || scan[c_len]=='\t' || scan[c_len]=='/' || scan[c_len]==0)) {
00107                 /* Found a math now remove it from the string */
00108                         memmove(scan-1,scan+c_len,strlen(scan+c_len)+1);
00109                         trim(scan-1);
00110                         return true;
00111                 }
00112         }
00113         return false;
00114 }
00115 
00116 /* This scans the command line for a remaining switch and reports it else returns 0*/
00117 char * ScanCMDRemain(char * cmd) {
00118         char * scan,*found;
00119         if ((scan=found=strchr(cmd,'/'))) {
00120                 while ( *scan && !isspace(*reinterpret_cast<unsigned char*>(scan)) ) scan++;
00121                 *scan=0;
00122                 return found;
00123         } else return 0; 
00124 }
00125 
00126 char * StripWord(char *&line) {
00127         char * scan=line;
00128         scan=ltrim(scan);
00129         if (*scan=='"') {
00130                 char * end_quote=strchr(scan+1,'"');
00131                 if (end_quote) {
00132                         *end_quote=0;
00133                         line=ltrim(++end_quote);
00134                         return (scan+1);
00135                 }
00136         }
00137         char * begin=scan;
00138         for (char c = *scan ;(c = *scan);scan++) {
00139                 if (isspace(*reinterpret_cast<unsigned char*>(&c))) {
00140                         *scan++=0;
00141                         break;
00142                 }
00143         }
00144         line=scan;
00145         return begin;
00146 }
00147 
00148 Bits ConvDecWord(char * word) {
00149         bool negative=false;Bitu ret=0;
00150         if (*word=='-') {
00151                 negative=true;
00152                 word++;
00153         }
00154         while (char c=*word) {
00155                 ret*=10u;
00156                 ret+=(Bitu)c-'0';
00157                 word++;
00158         }
00159         if (negative) return 0-(Bits)ret;
00160         else return (Bits)ret;
00161 }
00162 
00163 Bits ConvHexWord(char * word) {
00164         Bitu ret=0;
00165         while (char c=toupper(*reinterpret_cast<unsigned char*>(word))) {
00166                 ret*=16;
00167                 if (c>='0' && c<='9') ret+=(Bitu)c-'0';
00168                 else if (c>='A' && c<='F') ret+=10u+((Bitu)c-'A');
00169                 word++;
00170         }
00171         return (Bits)ret;
00172 }
00173 
00174 double ConvDblWord(char * word) {
00175     (void)word;//UNUSED
00176         return 0.0f;
00177 }
00178 
00179 #if C_DEBUG
00180 #include <curses.h>
00181 #endif
00182 
00183 static char buf[1024];           //greater scope as else it doesn't always gets thrown right (linux/gcc2.95)
00184 void E_Exit(const char * format,...) {
00185 #if C_DEBUG && C_HEAVY_DEBUG
00186         DEBUG_HeavyWriteLogInstruction();
00187 #endif
00188         va_list msg;
00189         va_start(msg,format);
00190         vsprintf(buf,format,msg);
00191         va_end(msg);
00192         strcat(buf,"\n");
00193         LOG_MSG("E_Exit: %s\n",buf);
00194 #if defined(WIN32) && !defined(C_SDL2)
00195         /* Most Windows users DON'T run DOSBox-X from the command line! */
00196         MessageBox(GetHWND(), buf, "E_Exit", MB_OK | MB_ICONEXCLAMATION);
00197 #endif
00198 #if C_DEBUG
00199         endwin();
00200 #endif
00201         fprintf(stderr, "E_Exit: %s\n", buf);
00202         SDL_Quit();
00203         exit(0);
00204 }
00205