-
Notifications
You must be signed in to change notification settings - Fork 48
/
image.cpp
121 lines (104 loc) · 3.45 KB
/
image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Copyright (C) 2017 David Hansel
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#include "host.h"
#include "image.h"
#ifdef HOST_HAS_FILESYS
static const char *fname_template[4] = {"DISK%02X.DSK", "HDSK%02X.DSK", "TDISK%02X.DSK", "CDISK%02X.DSK"};
static const char *dirfilename[4] = {"DISKDIR.TXT", "HDSKDIR.TXT", "TDISKDIR.TXT", "CDISKDIR.TXT"};
const char *image_get_dir_content(byte image_type)
{
static byte contenttype = -1;
static char *contentcache = NULL;
if( image_type != contenttype )
{
contenttype = image_type;
if( contentcache!=NULL ) { free(contentcache); contentcache = NULL; }
}
if( contentcache==NULL )
{
int dirlen = host_filesys_file_size(dirfilename[image_type]);
if( dirlen>0 )
{
contentcache = (char *) malloc(dirlen+1);
if( contentcache!=NULL )
{
HOST_FILESYS_FILE_TYPE f = host_filesys_file_open(dirfilename[image_type], false);
if( f && host_filesys_file_read(f, dirlen, contentcache) )
{
contentcache[dirlen] = 0;
host_filesys_file_close(f);
}
else
{
free(contentcache);
contentcache = (char *) "";
}
}
else
{
free(contentcache);
contentcache = (char *) "";
}
}
else
contentcache = (char *) "";
}
return contentcache;
}
bool image_get_filename(byte image_type, byte image_num, char *filename, int buf_len, bool check_exist)
{
snprintf(filename, buf_len, fname_template[image_type], image_num);
return check_exist ? host_filesys_file_exists(filename) : true;
}
const char *image_get_filename(byte image_type, byte image_num, bool check_exist)
{
static char buf[13];
if( image_get_filename(image_type, image_num, buf, 13, check_exist) )
return buf;
else
return NULL;
}
const char *image_get_description(byte image_type, byte image_num)
{
static char *buf = NULL;
const char *fname = image_get_filename(image_type, image_num);
if( fname!=NULL )
{
const char *c = strstr(image_get_dir_content(image_type), fname);
if( c )
{
if( buf ) free(buf);
buf = (char *) malloc(strlen(c)+1);
if( buf )
{
char *b = buf;
while(*c != 13 && *c!=10 && *c!=0) *b++ = *c++;
*b = 0;
return buf;
}
else
return fname;
}
else
return fname;
}
else
return fname;
}
#endif