DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/misc/support.cpp
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 #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     const char whitespace[] = " \r\t\f\n";
00050         const auto empty_pfx = str.find_first_not_of(whitespace);
00051         if (empty_pfx == std::string::npos) {
00052                 str.clear(); // whole string is filled with whitespace
00053                 return;
00054         }
00055         const auto empty_sfx = str.find_last_not_of(whitespace);
00056         str.erase(empty_sfx + 1);
00057         str.erase(0, empty_pfx);
00058 }
00059 
00060 /* 
00061         Ripped some source from freedos for this one.
00062 
00063 */
00064 
00065 
00066 /*
00067  * replaces all instances of character o with character c
00068  */
00069 
00070 
00071 void strreplace(char * str,char o,char n) {
00072         while (*str) {
00073                 if (*str==o) *str=n;
00074                 str++;
00075         }
00076 }
00077 char *ltrim(char *str) { 
00078         while (*str && isspace(*reinterpret_cast<unsigned char*>(str))) str++;
00079         return str;
00080 }
00081 
00082 char *rtrim(char *str) {
00083         char *p;
00084         p = strchr(str, '\0');
00085         while (--p >= str && *reinterpret_cast<unsigned char*>(p) != '\f' && isspace(*reinterpret_cast<unsigned char*>(p))) {};
00086 
00087         p[1] = '\0';
00088         return str;
00089 }
00090 
00091 char *trim(char *str) {
00092         return ltrim(rtrim(str));
00093 }
00094 
00095 char * upcase(char * str) {
00096     for (char* idx = str; *idx ; idx++) *idx = toupper(*reinterpret_cast<unsigned char*>(idx));
00097     return str;
00098 }
00099 
00100 char * lowcase(char * str) {
00101         for(char* idx = str; *idx ; idx++)  *idx = tolower(*reinterpret_cast<unsigned char*>(idx));
00102         return str;
00103 }
00104 
00105 
00106 
00107 bool ScanCMDBool(char * cmd,char const * const check) {
00108         char * scan=cmd;size_t c_len=strlen(check);
00109         while ((scan=strchr(scan,'/'))) {
00110                 /* found a / now see behind it */
00111                 scan++;
00112                 if (strncasecmp(scan,check,c_len)==0 && (scan[c_len]==' ' || scan[c_len]=='\t' || scan[c_len]=='/' || scan[c_len]==0)) {
00113                 /* Found a math now remove it from the string */
00114                         memmove(scan-1,scan+c_len,strlen(scan+c_len)+1);
00115                         trim(scan-1);
00116                         return true;
00117                 }
00118         }
00119         return false;
00120 }
00121 
00122 /* This scans the command line for a remaining switch and reports it else returns 0*/
00123 char * ScanCMDRemain(char * cmd) {
00124         char * scan,*found;
00125         if ((scan=found=strchr(cmd,'/'))) {
00126                 while ( *scan && !isspace(*reinterpret_cast<unsigned char*>(scan)) ) scan++;
00127                 *scan=0;
00128                 return found;
00129         } else return 0; 
00130 }
00131 
00132 char * StripWord(char *&line) {
00133         char * scan=line;
00134         scan=ltrim(scan);
00135         if (*scan=='"') {
00136                 char * end_quote=strchr(scan+1,'"');
00137                 if (end_quote) {
00138                         *end_quote=0;
00139                         line=ltrim(++end_quote);
00140                         return (scan+1);
00141                 }
00142         }
00143         char * begin=scan;
00144         for (char c = *scan ;(c = *scan);scan++) {
00145                 if (isspace(*reinterpret_cast<unsigned char*>(&c))) {
00146                         *scan++=0;
00147                         break;
00148                 }
00149         }
00150         line=scan;
00151         return begin;
00152 }
00153 
00154 char * StripArg(char *&line) {
00155        char * scan=line;
00156        int q=0;
00157        scan=ltrim(scan);
00158        char * begin=scan;
00159        for (char c = *scan ;(c = *scan);scan++) {
00160                if (*scan=='"') {
00161                        q++;
00162                } else if (q/2*2==q && isspace(*reinterpret_cast<unsigned char*>(&c))) {
00163                         *scan++=0;
00164                         break;
00165                 }
00166         }
00167         line=scan;
00168         return begin;
00169 }
00170 
00171 
00172 Bits ConvDecWord(char * word) {
00173         bool negative=false;Bitu ret=0;
00174         if (*word=='-') {
00175                 negative=true;
00176                 word++;
00177         }
00178         while (char c=*word) {
00179                 ret*=10u;
00180                 ret+=(Bitu)c-'0';
00181                 word++;
00182         }
00183         if (negative) return 0-(Bits)ret;
00184         else return (Bits)ret;
00185 }
00186 
00187 Bits ConvHexWord(char * word) {
00188         Bitu ret=0;
00189         while (char c=toupper(*reinterpret_cast<unsigned char*>(word))) {
00190                 ret*=16;
00191                 if (c>='0' && c<='9') ret+=(Bitu)c-'0';
00192                 else if (c>='A' && c<='F') ret+=10u+((Bitu)c-'A');
00193                 word++;
00194         }
00195         return (Bits)ret;
00196 }
00197 
00198 double ConvDblWord(char * word) {
00199     (void)word;//UNUSED
00200         return 0.0f;
00201 }
00202 
00203 #if C_DEBUG
00204 #include <curses.h>
00205 #endif
00206 
00207 static char buf[1024];           //greater scope as else it doesn't always gets thrown right (linux/gcc2.95)
00208 void E_Exit(const char * format,...) {
00209 #if C_DEBUG && C_HEAVY_DEBUG
00210         DEBUG_HeavyWriteLogInstruction();
00211 #endif
00212         va_list msg;
00213         va_start(msg,format);
00214         vsprintf(buf,format,msg);
00215         va_end(msg);
00216         strcat(buf,"\n");
00217         LOG_MSG("E_Exit: %s\n",buf);
00218 #if defined(WIN32)
00219         /* Most Windows users DON'T run DOSBox-X from the command line! */
00220         MessageBox(GetHWND(), buf, "E_Exit", MB_OK | MB_ICONEXCLAMATION);
00221 #endif
00222 #if C_DEBUG
00223         endwin();
00224 #endif
00225         fprintf(stderr, "E_Exit: %s\n", buf);
00226         SDL_Quit();
00227         exit(0);
00228 }
00229