| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: support.cpp,v 1.37 2009-05-27 09:15:42 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <assert.h> |
| 24 | #include <ctype.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <algorithm> |
| 29 | #include <cctype> |
| 30 | #include <string> |
| 31 | |
| 32 | #include "dosbox.h" |
| 33 | #include "debug.h" |
| 34 | #include "support.h" |
| 35 | #include "video.h" |
| 36 | |
| 37 | |
| 38 | void upcase(std::string &str) { |
| 39 | int (*tf)(int) = std::toupper; |
| 40 | std::transform(str.begin(), str.end(), str.begin(), tf); |
| 41 | } |
| 42 | |
| 43 | void lowcase(std::string &str) { |
| 44 | int (*tf)(int) = std::tolower; |
| 45 | std::transform(str.begin(), str.end(), str.begin(), tf); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | Ripped some source from freedos for this one. |
| 51 | |
| 52 | */ |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * replaces all instances of character o with character c |
| 57 | */ |
| 58 | |
| 59 | |
| 60 | void strreplace(char * str,char o,char n) { |
| 61 | while (*str) { |
| 62 | if (*str==o) *str=n; |
| 63 | str++; |
| 64 | } |
| 65 | } |
| 66 | char *ltrim(char *str) { |
| 67 | while (*str && isspace(*reinterpret_cast<unsigned char*>(str))) str++; |
| 68 | return str; |
| 69 | } |
| 70 | |
| 71 | char *rtrim(char *str) { |
| 72 | char *p; |
| 73 | p = strchr(str, '\0'); |
| 74 | while (--p >= str && isspace(*reinterpret_cast<unsigned char*>(p))) {}; |
| 75 | p[1] = '\0'; |
| 76 | return str; |
| 77 | } |
| 78 | |
| 79 | char *trim(char *str) { |
| 80 | return ltrim(rtrim(str)); |
| 81 | } |
| 82 | |
| 83 | char * upcase(char * str) { |
| 84 | for (char* idx = str; *idx ; idx++) *idx = toupper(*reinterpret_cast<unsigned char*>(idx)); |
| 85 | return str; |
| 86 | } |
| 87 | |
| 88 | char * lowcase(char * str) { |
| 89 | for(char* idx = str; *idx ; idx++) *idx = tolower(*reinterpret_cast<unsigned char*>(idx)); |
| 90 | return str; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | bool ScanCMDBool(char * cmd,char const * const check) { |
| 96 | char * scan=cmd;size_t c_len=strlen(check); |
| 97 | while ((scan=strchr(scan,'/'))) { |
| 98 | /* found a / now see behind it */ |
| 99 | scan++; |
| 100 | if (strncasecmp(scan,check,c_len)==0 && (scan[c_len]==' ' || scan[c_len]=='\t' || scan[c_len]=='/' || scan[c_len]==0)) { |
| 101 | /* Found a math now remove it from the string */ |
| 102 | memmove(scan-1,scan+c_len,strlen(scan+c_len)+1); |
| 103 | trim(scan-1); |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /* This scans the command line for a remaining switch and reports it else returns 0*/ |
| 111 | char * ScanCMDRemain(char * cmd) { |
| 112 | char * scan,*found;; |
| 113 | if ((scan=found=strchr(cmd,'/'))) { |
| 114 | while ( *scan && !isspace(*reinterpret_cast<unsigned char*>(scan)) ) scan++; |
| 115 | *scan=0; |
| 116 | return found; |
| 117 | } else return 0; |
| 118 | } |
| 119 | |
| 120 | char * StripWord(char *&line) { |
| 121 | char * scan=line; |
| 122 | scan=ltrim(scan); |
| 123 | if (*scan=='"') { |
| 124 | char * end_quote=strchr(scan+1,'"'); |
| 125 | if (end_quote) { |
| 126 | *end_quote=0; |
| 127 | line=ltrim(++end_quote); |
| 128 | return (scan+1); |
| 129 | } |
| 130 | } |
| 131 | char * begin=scan; |
| 132 | for (char c = *scan ;(c = *scan);scan++) { |
| 133 | if (isspace(*reinterpret_cast<unsigned char*>(&c))) { |
| 134 | *scan++=0; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | line=scan; |
| 139 | return begin; |
| 140 | } |
| 141 | |
| 142 | Bits ConvDecWord(char * word) { |
| 143 | bool negative=false;Bitu ret=0; |
| 144 | if (*word=='-') { |
| 145 | negative=true; |
| 146 | word++; |
| 147 | } |
| 148 | while (char c=*word) { |
| 149 | ret*=10; |
| 150 | ret+=c-'0'; |
| 151 | word++; |
| 152 | } |
| 153 | if (negative) return 0-ret; |
| 154 | else return ret; |
| 155 | } |
| 156 | |
| 157 | Bits ConvHexWord(char * word) { |
| 158 | Bitu ret=0; |
| 159 | while (char c=toupper(*reinterpret_cast<unsigned char*>(word))) { |
| 160 | ret*=16; |
| 161 | if (c>='0' && c<='9') ret+=c-'0'; |
| 162 | else if (c>='A' && c<='F') ret+=10+(c-'A'); |
| 163 | word++; |
| 164 | } |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | double ConvDblWord(char * word) { |
| 169 | return 0.0f; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static char buf[1024]; //greater scope as else it doesn't always gets thrown right (linux/gcc2.95) |
| 174 | void E_Exit(const char * format,...) { |
| 175 | #if C_DEBUG && C_HEAVY_DEBUG |
| 176 | DEBUG_HeavyWriteLogInstruction(); |
| 177 | #endif |
| 178 | va_list msg; |
| 179 | va_start(msg,format); |
| 180 | vsprintf(buf,format,msg); |
| 181 | va_end(msg); |
| 182 | strcat(buf,"\n"); |
| 183 | |
| 184 | throw(buf); |
| 185 | } |