DiskFile.cpp

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  * 
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00100 #include "StdAfx.h"
00101 #include "DiskFile.h"
00102 
00103 CDiskFile::CDiskFile(CConfigurator*  cfg, CSystem*  sys, CDiskController*  c,
00104                      int idebus, int idedev) : CDisk(cfg, sys, c, idebus, idedev)
00105 {
00106   filename = myCfg->get_text_value("file");
00107   if(!filename)
00108   {
00109     FAILURE_1(Configuration, "%s: Disk has no file attached!\n", devid_string);
00110   }
00111 
00112   if(read_only)
00113     handle = fopen(filename, "rb");
00114   else
00115     handle = fopen_large(filename, "rb+");
00116   if(!handle)
00117   {
00118     printf("%s: Could not open file %s!\n", devid_string, filename);
00119 
00120     int sz = myCfg->get_num_value("autocreate_size", false, 0) / 1024 / 1024;
00121     if(!sz)
00122       FAILURE_1(Runtime, "%s: File does not exist and no autocreate_size set",
00123                 devid_string);
00124 
00125     void*   crt_buf;
00126     handle = fopen_large(filename, "wb");
00127     if(!handle)
00128       FAILURE_1(Runtime, "%s: File does not exist and could not be created",
00129                 devid_string);
00130     crt_buf = calloc(1024, 1024);
00131     printf("%s: writing %d 1kB blocks:   0%%\b\b\b\b", devid_string, sz);
00132 
00133     int lastpc = 0;
00134     for(int a = 0; a < sz; a++)
00135     {
00136       fwrite(crt_buf, 1024, 1024, handle);
00137 
00138       int pc = a * 100 / sz;
00139       if(pc != lastpc)
00140       {
00141         printf("%3d\b\b\b", pc);
00142         lastpc = pc;
00143       }
00144 
00145       fflush(stdout);
00146     }
00147 
00148     printf("100%%\n");
00149     fclose(handle);
00150     free(crt_buf);
00151     if(read_only)
00152       handle = fopen_large(filename, "rb");
00153     else
00154       handle = fopen_large(filename, "rb+");
00155     if(!handle)
00156     {
00157       FAILURE_1(Runtime, "%s: File created could not be opened", devid_string);
00158     }
00159 
00160     printf("%s: %d MB file %s created.\n", devid_string, sz, filename);
00161   }
00162 
00163   // determine size...
00164   fseek_large(handle, 0, SEEK_END);
00165   byte_size = ftell_large(handle);
00166   fseek_large(handle, 0, SEEK_SET);
00167   state.byte_pos = ftell_large(handle);
00168 
00169   sectors = 32;
00170   heads = 8;
00171 
00172   //calc_cylinders();
00173   determine_layout();
00174 
00175   model_number = myCfg->get_text_value("model_number", filename);
00176 
00177   // skip to the filename portion of the path.
00178   char*   p = model_number;
00179 #if defined(_WIN32)
00180   char    x = '\\';
00181 #elif defined(__VMS)
00182   char    x = ']';
00183 #else
00184   char    x = '/';
00185 #endif
00186   while(*p)
00187   {
00188     if(*p == x)
00189       model_number = p + 1;
00190     p++;
00191   }
00192 
00193   printf("%s: Mounted file %s, %"LL "d %d-byte blocks, %"LL "d/%d/%d.\n",
00194          devid_string, filename, byte_size / state.block_size, state.block_size,
00195          cylinders, heads, sectors);
00196 }
00197 
00198 CDiskFile::~CDiskFile(void)
00199 {
00200   printf("%s: Closing file.\n", devid_string);
00201   fclose(handle);
00202 }
00203 
00204 bool CDiskFile::seek_byte(off_t_large byte)
00205 {
00206   if(byte >= byte_size)
00207   {
00208     FAILURE_1(InvalidArgument, "%s: Seek beyond end of file!\n", devid_string);
00209   }
00210 
00211   fseek_large(handle, byte, SEEK_SET);
00212   state.byte_pos = ftell_large(handle);
00213 
00214   return true;
00215 }
00216 
00217 size_t CDiskFile::read_bytes(void* dest, size_t bytes)
00218 {
00219   size_t  r;
00220   r = fread(dest, 1, bytes, handle);
00221   state.byte_pos = ftell_large(handle);
00222   return r;
00223 }
00224 
00225 size_t CDiskFile::write_bytes(void* src, size_t bytes)
00226 {
00227   if(read_only)
00228     return 0;
00229 
00230   size_t  r;
00231   r = fwrite(src, 1, bytes, handle);
00232   state.byte_pos = ftell_large(handle);
00233   return r;
00234 }

SourceForge.net Logo
Project space on SourceForge.net