Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

pc64.c

00001 /*
00002  *      This program is free software; you can redistribute it and/or
00003  *      modify it under the terms of the GNU General Public License
00004  *      as published by the Free Software Foundation; either version
00005  *      2 of the License, or (at your option) any later version.
00006  *
00007  *  Copyright 2001 Michael Klein <michael(dot)klein(at)puffin(dot)lb(dot)shuttle(dot)de>
00008 */
00009 
00010 #ifdef SAVE_RCSID
00011 static char *rcsid =
00012     "@(#) $Id: pc64.c,v 1.3 2006/02/24 12:01:01 trikalio Exp $";
00013 #endif
00014 
00015 #include <ctype.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 
00019 #include "inputfiles.h"
00020 
00021 static int probe(FILE *file, const char *fname, cbmcopy_message_cb msg_cb)
00022 {
00023     char sig[8];
00024 
00025     msg_cb( sev_debug, "checking for pc64" );
00026 
00027     if(fread( sig, sizeof(sig), 1, file ) == 1 &&
00028        strncmp( sig, "C64File", 8 ) == 0)
00029     {
00030         msg_cb( sev_debug, "PC64 file detected: %s", fname );
00031         return 1;
00032     }
00033     rewind( file );
00034     return 0;
00035 }
00036 
00037 
00038 static int read(FILE *file, const char *fname, int entry,
00039                 char *cbmname, char *type,
00040                 unsigned char **data, size_t *size,
00041                 cbmcopy_message_cb msg_cb)
00042 {
00043     const char *ext;
00044 
00045     struct
00046     {
00047         unsigned char sig[8];
00048         unsigned char cbmname[16];
00049         unsigned char zero;
00050         unsigned char reclen;
00051     } pc64header;
00052 
00053     rewind( file );
00054 
00055     if(entry != 0)
00056     {
00057         msg_cb( sev_warning, "invalid PC64 entry" );
00058         return 1;
00059     }
00060 
00061     if(fread( &pc64header, sizeof(pc64header), 1, file ) != 1)
00062     {
00063         msg_cb( sev_warning, "could not read PC64 header" );
00064         return 1;
00065     }
00066 
00067     if(strlen( fname ) > 4)
00068     {
00069         ext = fname + strlen(fname) - 3;
00070         if(isdigit(ext[1]) && isdigit(ext[2]) &&
00071            strchr("PSDU", toupper(*ext)))
00072         {
00073             *type = (char) toupper(*ext);
00074         }
00075         else
00076         {
00077             msg_cb( sev_warning, "could not guess PC64 filetype: %s", fname );
00078             *type = 'P';
00079         }
00080     }
00081     else
00082     {
00083         msg_cb( sev_warning,
00084                 "name too short to guess PC64 filetype: %s", fname );
00085         *type = 'P';
00086     }
00087     memcpy(cbmname, pc64header.cbmname, 16 );
00088 
00089     *data = NULL;
00090     if(fseek(file, 0L, SEEK_END) == 0)
00091     {
00092         *size = ftell(file) - sizeof(pc64header);
00093         if(*size)
00094         {
00095             *data = malloc(*size);
00096             if(*data)
00097             {
00098                 if(fseek(file, sizeof(pc64header), SEEK_SET) == 0 &&
00099                    fread(*data, *size, 1, file) == 1)
00100                 {
00101                     return 0;
00102                 }
00103                 free(*data);
00104             }
00105         }
00106         else
00107         {
00108             return 0;
00109         }
00110     }
00111     return 1;
00112 }
00113 
00114 DECLARE_INPUT_READER(pc64);

Generated on Sun Apr 30 18:45:56 2006 for opencbm by  doxygen 1.4.2