DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
include/dos_system.h
00001 /*
00002  *  Copyright (C) 2002-2015  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 #ifndef DOSBOX_DOS_SYSTEM_H
00021 #define DOSBOX_DOS_SYSTEM_H
00022 
00023 #include <vector>
00024 #ifndef DOSBOX_DOSBOX_H
00025 #include "dosbox.h"
00026 #endif
00027 #ifndef DOSBOX_CROSS_H
00028 #include "cross.h"
00029 #endif
00030 #ifndef DOSBOX_SUPPORT_H
00031 #include "support.h"
00032 #endif
00033 #ifndef DOSBOX_MEM_H
00034 #include "mem.h"
00035 #endif
00036 
00037 #define DOS_NAMELENGTH 12
00038 #define DOS_NAMELENGTH_ASCII (DOS_NAMELENGTH+1)
00039 #define DOS_FCBNAME 15
00040 #define DOS_DIRDEPTH 8
00041 #define DOS_PATHLENGTH 80
00042 #define DOS_TEMPSIZE 1024
00043 
00044 enum {
00045     CPM_COMPAT_OFF=0,
00046     CPM_COMPAT_MSDOS2,
00047     CPM_COMPAT_MSDOS5,
00048     CPM_COMPAT_DIRECT
00049 };
00050 
00051 enum {
00052         DOS_ATTR_READ_ONLY=     0x01,
00053         DOS_ATTR_HIDDEN=        0x02,
00054         DOS_ATTR_SYSTEM=        0x04,
00055         DOS_ATTR_VOLUME=        0x08,
00056         DOS_ATTR_DIRECTORY=     0x10,
00057         DOS_ATTR_ARCHIVE=       0x20,
00058         DOS_ATTR_DEVICE=        0x40
00059 };
00060 
00061 struct FileStat_Block {
00062         Bit32u size;
00063         Bit16u time;
00064         Bit16u date;
00065         Bit16u attr;
00066 };
00067 
00068 class DOS_DTA;
00069 
00070 #ifdef WIN32 /* Shaddup MSVC! */
00071 # define stricmp _stricmp
00072 #endif
00073 
00074 class DOS_File {
00075 public:
00076         DOS_File():flags(0)             { name=0; refCtr = 0; hdrive=0xff; newtime=false;};
00077         DOS_File(const DOS_File& orig);
00078         DOS_File & operator= (const DOS_File & orig);
00079         virtual ~DOS_File(){if(name) delete [] name;};
00080         virtual bool    Read(Bit8u * data,Bit16u * size)=0;
00081         virtual bool    Write(const Bit8u * data,Bit16u * size)=0;
00082         virtual bool    Seek(Bit32u * pos,Bit32u type)=0;
00083         virtual bool    Close()=0;
00084         /* ert, 20100711: Locking extensions */
00085         virtual bool    LockFile(Bit8u mode, Bit32u pos, Bit16u size) { (void)mode; (void)pos; (void)size; return false; };
00086         virtual Bit16u  GetInformation(void)=0;
00087         virtual void    SetName(const char* _name)      { if (name) delete[] name; name = new char[strlen(_name)+1]; strcpy(name,_name); }
00088         virtual char*   GetName(void)                           { return name; };
00089         virtual bool    IsOpen()                                        { return open; };
00090         virtual bool    IsName(const char* _name)       { if (!name) return false; return strcasecmp(name,_name)==0; };
00091         virtual void    AddRef()                                        { refCtr++; };
00092         virtual Bits    RemoveRef()                                     { return --refCtr; };
00093         virtual bool    UpdateDateTimeFromHost()        { return true; }
00094         virtual Bit32u  GetSeekPos()    { return 0xffffffff; }
00095         void SetDrive(Bit8u drv) { hdrive=drv;}
00096         Bit8u GetDrive(void) { return hdrive;}
00097 
00098         char* name;
00099         Bit8u drive;
00100         Bit32u flags;
00101         bool open;
00102 
00103         Bit16u attr;
00104         Bit16u time;
00105         Bit16u date;
00106         Bits refCtr;
00107         bool newtime;
00108         /* Some Device Specific Stuff */
00109 private:
00110         Bit8u hdrive;
00111 };
00112 
00113 class DOS_Device : public DOS_File {
00114 public:
00115         DOS_Device(const DOS_Device& orig):DOS_File(orig) {
00116                 devnum=orig.devnum;
00117                 open=true;
00118         }
00119         DOS_Device & operator= (const DOS_Device & orig) {
00120                 DOS_File::operator=(orig);
00121                 devnum=orig.devnum;
00122                 open=true;
00123                 return *this;
00124         }
00125         DOS_Device():DOS_File(),devnum(0){};
00126         virtual ~DOS_Device() {};
00127         virtual bool    Read(Bit8u * data,Bit16u * size);
00128         virtual bool    Write(const Bit8u * data,Bit16u * size);
00129         virtual bool    Seek(Bit32u * pos,Bit32u type);
00130         virtual bool    Close();
00131         virtual Bit16u  GetInformation(void);
00132         virtual bool    ReadFromControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode);
00133         virtual bool    WriteToControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode);
00134         void SetDeviceNumber(Bitu num) { devnum=num;}
00135 private:
00136         Bitu devnum;
00137 };
00138 
00139 /* The following variable can be lowered to free up some memory.
00140  * The negative side effect: The stored searches will be turned over faster.
00141  * Should not have impact on systems with few directory entries. */
00142 class DOS_Drive;
00143 #define MAX_OPENDIRS 2048
00144 //Can be high as it's only storage (16 bit variable)
00145 
00146 class DOS_Drive_Cache {
00147 public:
00148         DOS_Drive_Cache                                 (void);
00149         DOS_Drive_Cache                                 (const char* path, DOS_Drive *drive); 
00150         ~DOS_Drive_Cache                                (void);
00151 
00152         enum TDirSort { NOSORT, ALPHABETICAL, DIRALPHABETICAL, ALPHABETICALREV, DIRALPHABETICALREV };
00153 
00154         void            SetBaseDir                      (const char* path, DOS_Drive *drive);
00155         void            SetDirSort                      (TDirSort sort) { sortDirType = sort; };
00156         bool            OpenDir                         (const char* path, Bit16u& id);
00157         bool            ReadDir                         (Bit16u id, char* &result);
00158 
00159         void            ExpandName                      (char* path);
00160         char*           GetExpandName           (const char* path);
00161         bool            GetShortName            (const char* fullname, char* shortname);
00162 
00163         bool            FindFirst                       (char* path, Bit16u& id);
00164         bool            FindNext                        (Bit16u id, char* &result);
00165 
00166         void            CacheOut                        (const char* path, bool ignoreLastDir = false);
00167         void            AddEntry                        (const char* path, bool checkExist = false);
00168         void            DeleteEntry                     (const char* path, bool ignoreLastDir = false);
00169 
00170         void            EmptyCache                      (void);
00171         void            MediaChange                     (void);
00172         void            SetLabel                        (const char* name,bool cdrom,bool allowupdate);
00173         char*           GetLabel                        (void) { return label; };
00174 
00175         class CFileInfo {
00176         public:
00177                 CFileInfo(void) {
00178                         orgname[0] = shortname[0] = 0;
00179                         isDir = false;
00180                         id = MAX_OPENDIRS;
00181                         nextEntry = shortNr = 0;
00182                 }
00183                 ~CFileInfo(void) {
00184                         for (Bit32u i=0; i<fileList.size(); i++) delete fileList[i];
00185                         fileList.clear();
00186                         longNameList.clear();
00187                 };
00188                 char            orgname         [CROSS_LEN];
00189                 char            shortname       [DOS_NAMELENGTH_ASCII];
00190                 bool            isDir;
00191                 Bit16u          id;
00192                 Bitu            nextEntry;
00193                 Bitu            shortNr;
00194                 // contents
00195                 std::vector<CFileInfo*> fileList;
00196                 std::vector<CFileInfo*> longNameList;
00197         };
00198 
00199 private:
00200         void ClearFileInfo(CFileInfo *dir);
00201         void DeleteFileInfo(CFileInfo *dir);
00202 
00203         bool            RemoveTrailingDot       (char* shortname);
00204         Bits            GetLongName             (CFileInfo* info, char* shortname);
00205         void            CreateShortName         (CFileInfo* dir, CFileInfo* info);
00206         Bitu            CreateShortNameID       (CFileInfo* dir, const char* name);
00207         int             CompareShortname        (const char* compareName, const char* shortName);
00208         bool            SetResult               (CFileInfo* dir, char * &result, Bitu entryNr);
00209         bool            IsCachedIn              (CFileInfo* dir);
00210         CFileInfo*      FindDirInfo             (const char* path, char* expandedPath);
00211         bool            RemoveSpaces            (char* str);
00212         bool            OpenDir                 (CFileInfo* dir, const char* path, Bit16u& id);
00213         void            CreateEntry             (CFileInfo* dir, const char* name, bool query_directory);
00214         void            CopyEntry               (CFileInfo* dir, CFileInfo* from);
00215         Bit16u          GetFreeID               (CFileInfo* dir);
00216         void            Clear                   (void);
00217 
00218         CFileInfo*      dirBase;
00219         char            dirPath                         [CROSS_LEN];
00220         DOS_Drive*      drive;
00221         char            basePath                        [CROSS_LEN];
00222         bool            dirFirstTime;
00223         TDirSort        sortDirType;
00224         CFileInfo*      save_dir;
00225         char            save_path                       [CROSS_LEN];
00226         char            save_expanded           [CROSS_LEN];
00227 
00228         Bit16u          srchNr;
00229         CFileInfo*      dirSearch                       [MAX_OPENDIRS];
00230         char            dirSearchName           [MAX_OPENDIRS];
00231         CFileInfo*      dirFindFirst            [MAX_OPENDIRS];
00232         Bit16u          nextFreeFindFirst;
00233 
00234         char            label                           [CROSS_LEN];
00235         bool            updatelabel;
00236 };
00237 
00238 class DOS_Drive {
00239 public:
00240         DOS_Drive();
00241         virtual ~DOS_Drive(){};
00242         virtual bool FileOpen(DOS_File * * file,const char * name,Bit32u flags)=0;
00243         virtual bool FileCreate(DOS_File * * file,const char * name,Bit16u attributes)=0;
00244         virtual bool FileUnlink(const char * _name)=0;
00245         virtual bool RemoveDir(const char * _dir)=0;
00246         virtual bool MakeDir(const char * _dir)=0;
00247         virtual bool TestDir(const char * _dir)=0;
00248         virtual bool FindFirst(const char * _dir,DOS_DTA & dta,bool fcb_findfirst=false)=0;
00249         virtual bool FindNext(DOS_DTA & dta)=0;
00250         virtual bool GetFileAttr(const char * name,Bit16u * attr)=0;
00251         virtual bool Rename(const char * oldname,const char * newname)=0;
00252         virtual bool AllocationInfo(Bit16u * _bytes_sector,Bit8u * _sectors_cluster,Bit16u * _total_clusters,Bit16u * _free_clusters)=0;
00253         virtual bool FileExists(const char* name)=0;
00254         virtual bool FileStat(const char* name, FileStat_Block * const stat_block)=0;
00255         virtual Bit8u GetMediaByte(void)=0;
00256         virtual void SetDir(const char* path) { strcpy(curdir,path); };
00257 //      virtual void EmptyCache(void) { dirCache.EmptyCache(); };
00258         virtual bool isRemote(void)=0;
00259         virtual bool isRemovable(void)=0;
00260         virtual Bits UnMount(void)=0;
00261 
00262         /* these 4 may only be used by DOS_Drive_Cache because they have special calling conventions */
00263         virtual void *opendir(const char *dir) { (void)dir; return NULL; };
00264         virtual void closedir(void *handle) { (void)handle; };
00265         virtual bool read_directory_first(void *handle, char* entry_name, bool& is_directory) { (void)handle; (void)entry_name; (void)is_directory; return false; };
00266         virtual bool read_directory_next(void *handle, char* entry_name, bool& is_directory) { (void)handle; (void)entry_name; (void)is_directory; return false; };
00267 
00268         virtual const char * GetInfo(void);
00269         char * GetBaseDir(void);
00270 
00271     bool readonly;
00272     bool nocachedir;
00273         char curdir[DOS_PATHLENGTH];
00274         char info[256];
00275         /* Can be overridden for example in iso images */
00276         virtual char const * GetLabel() {return "NOLABEL";}; 
00277         virtual void SetLabel(const char *label, bool iscdrom, bool updatable) { (void)label; (void)iscdrom; (void)updatable; };
00278         virtual void EmptyCache() {};
00279         virtual void MediaChange() {};
00280         // disk cycling functionality (request resources)
00281         virtual void Activate(void) {};
00282 };
00283 
00284 enum { OPEN_READ=0, OPEN_WRITE=1, OPEN_READWRITE=2, OPEN_READ_NO_MOD=4, DOS_NOT_INHERIT=128};
00285 enum { DOS_SEEK_SET=0,DOS_SEEK_CUR=1,DOS_SEEK_END=2};
00286 
00287 
00288 /*
00289  A multiplex handler should read the registers to check what function is being called
00290  If the handler returns false dos will stop checking other handlers
00291 */
00292 
00293 typedef bool (MultiplexHandler)(void);
00294 void DOS_AddMultiplexHandler(MultiplexHandler * handler);
00295 void DOS_DelMultiplexHandler(MultiplexHandler * handler);
00296 
00297 /* AddDevice stores the pointer to a created device */
00298 void DOS_AddDevice(DOS_Device * adddev);
00299 /* DelDevice destroys the device that is pointed to. */
00300 void DOS_DelDevice(DOS_Device * dev);
00301 
00302 void VFILE_Register(const char * name,Bit8u * data,Bit32u size);
00303 void VFILE_RegisterBuiltinFileBlob(const struct BuiltinFileBlob &b);
00304 #endif