Previous 15994 Revisions Next

r12286 Wednesday 20th July, 2011 at 17:52:07 UTC by Miodrag Milanović
New floppy image WIP (no whatsnew)
[src/lib]lib.mak
[src/lib/formats]flopimg.c flopimg.h hxcmfm_dsk.c* hxcmfm_dsk.h*
[src/mess/tools]tools.mak
[src/mess/tools/floptool]floptool.mak* main.c*

src/lib/formats/hxcmfm_dsk.c
r0r12286
1#include "hxcmfm_dsk.h"
2
3#define MFM_FORMAT_HEADER   "HXCMFM"
4
5mfm_format::mfm_format(const char *name,const char *extensions,const char *description,const char *param_guidelines) :
6   floppy_image_format_t(name,extensions,description,param_guidelines)
7{
8}
9
10int mfm_format::identify(floppy_image *image)
11{
12   UINT8 header[7];
13
14   image->image_read(header, 0, 7);
15   if ( memcmp( header, MFM_FORMAT_HEADER, 6 ) ==0) {
16      return 100;
17   }
18   return 0;
19}
20
21
22const floppy_format_type FLOPPY_MFM_FORMAT = &floppy_image_format_creator<mfm_format>;
No newline at end of file
src/lib/formats/hxcmfm_dsk.h
r0r12286
1/*********************************************************************
2
3    formats/hxcmfm_dsk.h
4
5    HxC Floppy Emulator disk images
6
7*********************************************************************/
8
9#ifndef HXCMFM_DSK_H
10#define HXCMFM_DSK_H
11
12#include "flopimg.h"
13
14class mfm_format : public floppy_image_format_t
15{
16public:
17   mfm_format(const char *name,const char *extensions,const char *description,const char *param_guidelines);
18   
19   virtual int identify(floppy_image *image);
20};
21
22extern const floppy_format_type FLOPPY_MFM_FORMAT;
23
24#endif /* HXCMFM_DSK_H */
src/lib/formats/flopimg.c
r12285r12286
932932
933933LEGACY_FLOPPY_OPTIONS_START(default)
934934LEGACY_FLOPPY_OPTIONS_END
935
936
937//////////////////////////////////////////////////////////
938/// New implementation
939//////////////////////////////////////////////////////////
940
941floppy_image::floppy_image(void *fp, const struct io_procs *procs,const struct floppy_format_def *formats)
942{
943   m_io.file = fp;
944   m_io.procs = procs;
945   m_io.filler = 0xFF;
946   m_formats = formats;
947}
948
949floppy_image::~floppy_image()
950{
951   close();
952}
953
954void floppy_image::image_read(void *buffer, UINT64 offset, size_t length)
955{
956   io_generic_read(&m_io, buffer, offset, length);
957}
958
959void floppy_image::image_write(const void *buffer, UINT64 offset, size_t length)
960{
961   io_generic_write(&m_io, buffer, offset, length);
962}
963
964void floppy_image::image_write_filler(UINT8 filler, UINT64 offset, size_t length)
965{
966   io_generic_write_filler(&m_io, filler, offset, length);
967}
968
969UINT64 floppy_image::image_size()
970{
971   return io_generic_size(&m_io);
972}
973
974void floppy_image::close_internal(bool close_file)
975{
976   if (close_file)
977      io_generic_close(&m_io);
978}
979
980void floppy_image::close()
981{
982   close_internal(TRUE);
983}
984
985
986floppy_image_format_t::floppy_image_format_t(const char *name,const char *extensions,const char *description,const char *param_guidelines)
987{
988   m_name = name;
989   m_extensions = extensions;
990   m_description = description;
991   m_param_guidelines = param_guidelines;
992}
993floppy_image_format_t::~floppy_image_format_t()
994{
995}
996
997int floppy_image_format_t::identify(floppy_image *image)
998{
999   return 0;
1000}
1001
1002const struct floppy_format_def *floppy_image::identify()
1003{
1004   const struct floppy_format_def *retVal = NULL;
1005   int best_vote = 0;
1006   
1007   for (int i = 0; m_formats[i].type; i++)
1008   {
1009      floppy_image_format_t *t = (m_formats[i].type)(m_formats[i].name,m_formats[i].extensions,m_formats[i].description,m_formats[i].param_guidelines);
1010      int vote = t->identify(this);
1011      /* is this option a better one? */
1012      if (vote > best_vote)
1013      {
1014         best_vote = vote;
1015         retVal = &m_formats[i];
1016      }
1017   }
1018   return retVal;
1019}
src/lib/formats/flopimg.h
r12285r12286
213213/* misc */
214214const char *floppy_error(floperr_t err);
215215
216//////////////////////////////////////////////////////////
217/// New implementation
218//////////////////////////////////////////////////////////
219
220class floppy_image;
221
222class floppy_image_format_t
223{
224public:
225   floppy_image_format_t(const char *name,const char *extensions,const char *description,const char *param_guidelines);
226   virtual ~floppy_image_format_t();
227   
228   virtual int identify(floppy_image *image);
229   
230protected:   
231   const char *m_name;
232   const char *m_extensions;
233   const char *m_description;
234   const char *m_param_guidelines;
235};
236
237
238// a device_type is simply a pointer to its alloc function
239typedef floppy_image_format_t *(*floppy_format_type)(const char *name,const char *extensions,const char *description,const char *param_guidelines);
240
241// this template function creates a stub which constructs a image format
242template<class _FormatClass>
243floppy_image_format_t *floppy_image_format_creator(const char *name,const char *extensions,const char *description,const char *param_guidelines)
244{
245   return new _FormatClass(name, extensions, description, param_guidelines);
246}
247
248struct floppy_format_def
249{
250   const char *name;
251   const char *extensions;
252   const char *description;
253   const floppy_format_type type;
254   const char *param_guidelines;
255};
256
257#define FLOPPY_OPTIONS_START(name)                                    \
258   const struct floppy_format_def floppyoptions_##name[] =                        \
259   {                                                         \
260
261#define FLOPPY_OPTIONS_END0 \
262      { NULL, NULL, NULL, NULL, NULL }                     \
263   };
264
265#define FLOPPY_OPTIONS_EXTERN(name)                                    \
266   extern const struct floppy_format_def floppyoptions_##name[]                     \
267
268#define FLOPPY_OPTION(name, extensions_, description_, type_, ranges_)\
269   { #name, extensions_, description_, type_, ranges_ },            \
270
271#define FLOPPY_OPTIONS_END                                       \
272   FLOPPY_OPTIONS_END0
273   
274// ======================> floppy_image
275
276// class representing floppy image
277class floppy_image
278{
279public:
280   // construction/destruction
281   floppy_image(void *fp, const struct io_procs *procs,const struct floppy_format_def *formats);
282   virtual ~floppy_image();
283   
284   void image_read(void *buffer, UINT64 offset, size_t length);
285   void image_write(const void *buffer, UINT64 offset, size_t length);
286   void image_write_filler(UINT8 filler, UINT64 offset, size_t length);
287   UINT64 image_size();
288   void close();
289   
290   const struct floppy_format_def *identify();
291private:
292   void close_internal(bool close_file);
293   struct io_generic       m_io;
294   const struct floppy_format_def *m_formats;
295};
296
216297#endif /* FLOPIMG_H */
217298
src/lib/lib.mak
r12285r12286
152152   $(LIBOBJ)/formats/x1_tap.o      \
153153   $(LIBOBJ)/formats/z80ne_dsk.o   \
154154   $(LIBOBJ)/formats/zx81_p.o      \
155   $(LIBOBJ)/formats/hxcmfm_dsk.o   \
155156
156157$(OBJ)/libformats.a: $(FORMATSOBJS)
157158
src/mess/tools/tools.mak
r12285r12286
2424TOOLS += $(CASTOOL)
2525endif
2626
27include $(MESSSRC)/tools/floptool/floptool.mak
28TOOLS += $(FLOPTOOL)
29
2730#ifdef BUILD_MESSTEST
2831#include $(MESSSRC)/tools/messtest/messtest.mak
2932#TOOLS += $(MESSTEST)
src/mess/tools/floptool/floptool.mak
r0r12286
1###########################################################################
2#
3#   floptool.mak
4#
5#   MESS floptool makefile
6#
7###########################################################################
8
9
10# floptool executable name
11FLOPTOOL = floptool$(EXE)
12
13# add path to floptool headers
14INCPATH += -I$(SRC)/$(TARGET)/tools/floptool
15
16# floptool directories
17FLOPTOOLOBJ = $(MESS_TOOLS)/floptool
18
19
20#-------------------------------------------------
21# floptool objects
22#-------------------------------------------------
23
24OBJDIRS += \
25   $(FLOPTOOLOBJ)
26
27FLOPTOOL_OBJS = \
28   $(FLOPTOOLOBJ)/main.o \
29
30
31
32#-------------------------------------------------
33# rules to build the floptool executable
34#-------------------------------------------------
35
36$(FLOPTOOL): $(FLOPTOOL_OBJS) $(FORMATS_LIB) $(LIBUTIL) $(EXPAT) $(ZLIB) $(LIBOCORE)
37   @echo Linking $@...
38   $(LD) $(LDFLAGS) $^ $(LIBS) -o $@
src/mess/tools/floptool/main.c
r0r12286
1/***************************************************************************
2
3    main.c
4
5    Floptool command line front end
6
7    20/07/2011 Initial version by Miodrag Milanovic
8
9***************************************************************************/
10
11#include <stdio.h>
12#include <string.h>
13#include <ctype.h>
14#include <stdlib.h>
15#include <time.h>
16#include <assert.h>
17
18#include "corestr.h"
19
20#include "formats/flopimg.h"
21
22static FLOPPY_OPTIONS_START( supported )
23   FLOPPY_OPTION( mfm, "mfm", "HxCFloppyEmulator floppy disk image", FLOPPY_MFM_FORMAT, NULL )
24FLOPPY_OPTIONS_END
25
26static void display_usage(void)
27{
28   fprintf(stderr, "Usage: \n");
29   fprintf(stderr, "      floptool.exe identify <inputfile>\n");
30}
31
32static void display_formats(void)
33{
34   int i;
35   fprintf(stderr, "Supported formats:\n\n");
36   for (i = 0; floppyoptions_supported[i].name; i++) {
37      fprintf(stderr, "%15s - %s\n",floppyoptions_supported[i].name,floppyoptions_supported[i].description);
38      fprintf(stderr, "%20s [%s]\n","",floppyoptions_supported[i].extensions);
39   }   
40}
41
42int CLIB_DECL main(int argc, char *argv[])
43{
44   FILE *f;
45   
46   if (argc > 1)
47   {
48      if (!core_stricmp("identify", argv[1]))
49      {
50         // convert command
51         if (argc!=3) {
52            fprintf(stderr, "Wrong parameter number.\n\n");
53            display_usage();
54            return -1;
55         } else {
56            f = fopen(argv[2], "rb");
57            if (!f) {
58               fprintf(stderr, "File %s not found.\n",argv[2]);
59               return -1;
60            }
61            floppy_image *image = new floppy_image(f, &stdio_ioprocs_noclose, floppyoptions_supported);
62            const struct floppy_format_def *format = image->identify();
63            if (format) {
64               fprintf(stderr, "File identified as %s\n",format->description);
65            } else {
66               fprintf(stderr, "Unable to identified file type\n");
67            }
68            fclose(f);
69            goto theend;
70         }
71      }
72   }
73
74   /* Usage */
75   fprintf(stderr, "floptool - Generic floppy image manipulation tool for use with MESS\n\n");
76   display_usage();
77   fprintf(stderr, "\n");
78   display_formats();
79   fprintf(stderr, "\nExample usage:\n");
80   fprintf(stderr, "        floptool.exe identify image.dsk\n\n");
81
82theend :
83   return 0;
84}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team