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