| 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: drives.cpp,v 1.15 2009-05-27 09:15:41 qbix79 Exp $ */ |
| 20 | |
| 21 | #include "dosbox.h" |
| 22 | #include "dos_system.h" |
| 23 | #include "drives.h" |
| 24 | #include "mapper.h" |
| 25 | #include "support.h" |
| 26 | |
| 27 | bool WildFileCmp(const char * file, const char * wild) |
| 28 | { |
| 29 | char file_name[9]; |
| 30 | char file_ext[4]; |
| 31 | char wild_name[9]; |
| 32 | char wild_ext[4]; |
| 33 | const char * find_ext; |
| 34 | Bitu r; |
| 35 | |
| 36 | strcpy(file_name," "); |
| 37 | strcpy(file_ext," "); |
| 38 | strcpy(wild_name," "); |
| 39 | strcpy(wild_ext," "); |
| 40 | |
| 41 | find_ext=strrchr(file,'.'); |
| 42 | if (find_ext) { |
| 43 | Bitu size=(Bitu)(find_ext-file); |
| 44 | if (size>8) size=8; |
| 45 | memcpy(file_name,file,size); |
| 46 | find_ext++; |
| 47 | memcpy(file_ext,find_ext,(strlen(find_ext)>3) ? 3 : strlen(find_ext)); |
| 48 | } else { |
| 49 | memcpy(file_name,file,(strlen(file) > 8) ? 8 : strlen(file)); |
| 50 | } |
| 51 | upcase(file_name);upcase(file_ext); |
| 52 | find_ext=strrchr(wild,'.'); |
| 53 | if (find_ext) { |
| 54 | Bitu size=(Bitu)(find_ext-wild); |
| 55 | if (size>8) size=8; |
| 56 | memcpy(wild_name,wild,size); |
| 57 | find_ext++; |
| 58 | memcpy(wild_ext,find_ext,(strlen(find_ext)>3) ? 3 : strlen(find_ext)); |
| 59 | } else { |
| 60 | memcpy(wild_name,wild,(strlen(wild) > 8) ? 8 : strlen(wild)); |
| 61 | } |
| 62 | upcase(wild_name);upcase(wild_ext); |
| 63 | /* Names are right do some checking */ |
| 64 | r=0; |
| 65 | while (r<8) { |
| 66 | if (wild_name[r]=='*') goto checkext; |
| 67 | if (wild_name[r]!='?' && wild_name[r]!=file_name[r]) return false; |
| 68 | r++; |
| 69 | } |
| 70 | checkext: |
| 71 | r=0; |
| 72 | while (r<3) { |
| 73 | if (wild_ext[r]=='*') return true; |
| 74 | if (wild_ext[r]!='?' && wild_ext[r]!=file_ext[r]) return false; |
| 75 | r++; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void Set_Label(char const * const input, char * const output, bool cdrom) { |
| 81 | Bitu togo = 8; |
| 82 | Bitu vnamePos = 0; |
| 83 | Bitu labelPos = 0; |
| 84 | bool point = false; |
| 85 | |
| 86 | //spacepadding the filenamepart to include spaces after the terminating zero is more closely to the specs. (not doing this now) |
| 87 | // HELLO\0' '' ' |
| 88 | |
| 89 | while (togo > 0) { |
| 90 | if (input[vnamePos]==0) break; |
| 91 | if (!point && (input[vnamePos]=='.')) { togo=4; point=true; } |
| 92 | |
| 93 | //another mscdex quirk. Label is not always uppercase. (Daggerfall) |
| 94 | output[labelPos] = (cdrom?input[vnamePos]:toupper(input[vnamePos])); |
| 95 | |
| 96 | labelPos++; vnamePos++; |
| 97 | togo--; |
| 98 | if ((togo==0) && !point) { |
| 99 | if (input[vnamePos]=='.') vnamePos++; |
| 100 | output[labelPos]='.'; labelPos++; point=true; togo=3; |
| 101 | } |
| 102 | }; |
| 103 | output[labelPos]=0; |
| 104 | |
| 105 | //Remove trailing dot. except when on cdrom and filename is exactly 8 (9 including the dot) letters. MSCDEX feature/bug (fifa96 cdrom detection) |
| 106 | if((labelPos > 0) && (output[labelPos-1] == '.') && !(cdrom && labelPos ==9)) |
| 107 | output[labelPos-1] = 0; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | DOS_Drive::DOS_Drive() { |
| 113 | curdir[0]=0; |
| 114 | info[0]=0; |
| 115 | } |
| 116 | |
| 117 | char * DOS_Drive::GetInfo(void) { |
| 118 | return info; |
| 119 | } |
| 120 | |
| 121 | // static members variables |
| 122 | int DriveManager::currentDrive; |
| 123 | DriveManager::DriveInfo DriveManager::driveInfos[26]; |
| 124 | |
| 125 | void DriveManager::AppendDisk(int drive, DOS_Drive* disk) { |
| 126 | driveInfos[drive].disks.push_back(disk); |
| 127 | } |
| 128 | |
| 129 | void DriveManager::InitializeDrive(int drive) { |
| 130 | currentDrive = drive; |
| 131 | DriveInfo& driveInfo = driveInfos[currentDrive]; |
| 132 | if (driveInfo.disks.size() > 0) { |
| 133 | driveInfo.currentDisk = 0; |
| 134 | DOS_Drive* disk = driveInfo.disks[driveInfo.currentDisk]; |
| 135 | Drives[currentDrive] = disk; |
| 136 | disk->Activate(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | void DriveManager::CycleDrive(bool pressed) { |
| 142 | if (!pressed) return; |
| 143 | |
| 144 | // do one round through all drives or stop at the next drive with multiple disks |
| 145 | int oldDrive = currentDrive; |
| 146 | do { |
| 147 | currentDrive = (currentDrive + 1) % DOS_DRIVES; |
| 148 | int numDisks = driveInfos[currentDrive].disks.size(); |
| 149 | if (numDisks > 1) break; |
| 150 | } while (currentDrive != oldDrive); |
| 151 | } |
| 152 | |
| 153 | void DriveManager::CycleDisk(bool pressed) { |
| 154 | if (!pressed) return; |
| 155 | |
| 156 | int numDisks = driveInfos[currentDrive].disks.size(); |
| 157 | if (numDisks > 1) { |
| 158 | // cycle disk |
| 159 | int currentDisk = driveInfos[currentDrive].currentDisk; |
| 160 | DOS_Drive* oldDisk = driveInfos[currentDrive].disks[currentDisk]; |
| 161 | currentDisk = (currentDisk + 1) % numDisks; |
| 162 | DOS_Drive* newDisk = driveInfos[currentDrive].disks[currentDisk]; |
| 163 | driveInfos[currentDrive].currentDisk = currentDisk; |
| 164 | |
| 165 | // copy working directory, acquire system resources and finally switch to next drive |
| 166 | strcpy(newDisk->curdir, oldDisk->curdir); |
| 167 | newDisk->Activate(); |
| 168 | Drives[currentDrive] = newDisk; |
| 169 | } |
| 170 | } |
| 171 | */ |
| 172 | |
| 173 | void DriveManager::CycleAllDisks(void) { |
| 174 | for (int idrive=0; idrive<DOS_DRIVES; idrive++) { |
| 175 | int numDisks = (int)driveInfos[idrive].disks.size(); |
| 176 | if (numDisks > 1) { |
| 177 | // cycle disk |
| 178 | int currentDisk = driveInfos[idrive].currentDisk; |
| 179 | DOS_Drive* oldDisk = driveInfos[idrive].disks[currentDisk]; |
| 180 | currentDisk = (currentDisk + 1) % numDisks; |
| 181 | DOS_Drive* newDisk = driveInfos[idrive].disks[currentDisk]; |
| 182 | driveInfos[idrive].currentDisk = currentDisk; |
| 183 | |
| 184 | // copy working directory, acquire system resources and finally switch to next drive |
| 185 | strcpy(newDisk->curdir, oldDisk->curdir); |
| 186 | newDisk->Activate(); |
| 187 | Drives[idrive] = newDisk; |
| 188 | LOG_MSG("Drive %c: disk %d of %d now active", 'A'+idrive, currentDisk+1, numDisks); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | int DriveManager::UnmountDrive(int drive) { |
| 194 | int result = 0; |
| 195 | // unmanaged drive |
| 196 | if (driveInfos[drive].disks.size() == 0) { |
| 197 | result = Drives[drive]->UnMount(); |
| 198 | } else { |
| 199 | // managed drive |
| 200 | int currentDisk = driveInfos[drive].currentDisk; |
| 201 | result = driveInfos[drive].disks[currentDisk]->UnMount(); |
| 202 | // only delete on success, current disk set to NULL because of UnMount |
| 203 | if (result == 0) { |
| 204 | driveInfos[drive].disks[currentDisk] = NULL; |
| 205 | for (int i = 0; i < (int)driveInfos[drive].disks.size(); i++) { |
| 206 | delete driveInfos[drive].disks[i]; |
| 207 | } |
| 208 | driveInfos[drive].disks.clear(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | void DriveManager::Init(Section* /* sec */) { |
| 216 | |
| 217 | // setup driveInfos structure |
| 218 | currentDrive = 0; |
| 219 | for(int i = 0; i < DOS_DRIVES; i++) { |
| 220 | driveInfos[i].currentDisk = 0; |
| 221 | } |
| 222 | |
| 223 | // MAPPER_AddHandler(&CycleDisk, MK_f3, MMOD1, "cycledisk", "Cycle Disk"); |
| 224 | // MAPPER_AddHandler(&CycleDrive, MK_f3, MMOD2, "cycledrive", "Cycle Drv"); |
| 225 | } |
| 226 | |
| 227 | void DRIVES_Init(Section* sec) { |
| 228 | DriveManager::Init(sec); |
| 229 | } |