summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/floppycntrl.cpp
blob: 3f6f9bdfb619ed857670d8f42c1c2c6fee342a39 (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert
/***************************************************************************

    ui/floppycntrl.cpp

***************************************************************************/

#include "emu.h"

#include "ui/filesel.h"
#include "ui/filecreate.h"
#include "ui/floppycntrl.h"

#include "formats/flopimg.h"
#include "formats/fsmgr.h"

#include "zippath.h"

#include <tuple>


namespace ui {

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

menu_control_floppy_image::menu_control_floppy_image(mame_ui_manager &mui, render_container &container, device_image_interface &image) :
	menu_control_device_image(mui, container, image),
	fd(dynamic_cast<floppy_image_device &>(image)),
	input_format(nullptr),
	output_format(nullptr),
	create_fs(nullptr),
	input_filename(),
	output_filename()
{
}

menu_control_floppy_image::~menu_control_floppy_image()
{
}

void menu_control_floppy_image::do_load_create()
{
	if(input_filename.empty()) {
		auto [err, message] = fd.create(output_filename, nullptr, nullptr);
		if (err) {
			machine().popmessage(_("Error creating floppy image: %1$s"), !message.empty() ? message : err.message());
			return;
		}
		if (create_fs) {
			// HACK: ensure the floppy_image structure is created since device_image_interface may not otherwise do so during "init phase"
			err = fd.finish_load().first;
			if (!err) {
				fs::meta_data meta;
				fd.init_fs(create_fs, meta);
			}
		}
	} else {
		auto [err, message] = fd.load(input_filename);
		if (!err && !output_filename.empty()) {
			message.clear();
			err = fd.reopen_for_write(output_filename);
		}
		if (err) {
			machine().popmessage(_("Error opening floppy image: %1$s"), !message.empty() ? message : err.message());
			return;
		}
	}
	if(output_format)
		fd.setup_write(output_format);
}

void menu_control_floppy_image::hook_load(const std::string &filename)
{
	std::error_condition err;
	input_filename = filename;
	std::tie(err, input_format) = static_cast<floppy_image_device &>(m_image).identify(filename);

	if (!input_format)
	{
		machine().popmessage("Error: %s", err.message());
		stack_pop();
	}
	else
	{
		bool can_in_place = input_format->supports_save();
		if (can_in_place)
		{
			std::string tmp_path;
			util::core_file::ptr tmp_file;
			// attempt to open the file for writing but *without* create
			std::error_condition const filerr = util::zippath_fopen(filename, OPEN_FLAG_READ | OPEN_FLAG_WRITE, tmp_file, tmp_path);
			if(!filerr)
				tmp_file.reset();
			else
				can_in_place = false;
		}
		menu::stack_push<menu_select_rw>(
				ui(),
				container(),
				can_in_place,
				[this] (menu_select_rw::result result)
				{
					switch (result)
					{
					case menu_select_rw::result::READONLY:
						do_load_create();
						fd.setup_write(nullptr);
						stack_pop();
						break;

					case menu_select_rw::result::READWRITE:
						output_format = input_format;
						do_load_create();
						stack_pop();
						break;

					case menu_select_rw::result::WRITE_DIFF:
						machine().popmessage("Sorry, diffs are not supported yet\n");
						stack_pop();
						break;

					case menu_select_rw::result::WRITE_OTHER:
						menu::stack_push<menu_file_create>(ui(), container(), &m_image, m_current_directory, m_current_file, m_create_ok);
						m_state = CHECK_CREATE;
						break;

					case menu_select_rw::result::INVALID:
						m_state = START_FILE;
						break;
					}
				});
	}
}

bool menu_control_floppy_image::can_format(const floppy_image_device::fs_info &fs)
{
	return !fs.m_manager || fs.m_manager->can_format();
}

void menu_control_floppy_image::menu_activated()
{
	switch (m_state) {
	case DO_CREATE: {
		std::vector<const floppy_image_format_t *> format_array;
		for(const floppy_image_format_t *i : fd.get_formats()) {
			if(!i->supports_save())
				continue;
			if (i->extension_matches(m_current_file.c_str()))
				format_array.push_back(i);
		}
		int ext_match = format_array.size();
		for(const floppy_image_format_t *i : fd.get_formats()) {
			if(!i->supports_save())
				continue;
			if (!i->extension_matches(m_current_file.c_str()))
				format_array.push_back(i);
		}
		output_format = nullptr;
		menu::stack_push<menu_select_format>(ui(), container(), format_array, ext_match, &output_format);

		m_state = SELECT_FORMAT;
		break;
	}

	case SELECT_FORMAT:
		if(!output_format) {
			m_state = START_FILE;
			menu_activated();
		} else {
			// get all formatable file systems
			std::vector<std::reference_wrapper<const floppy_image_device::fs_info>> fs;
			for (const auto &this_fs : fd.get_fs()) {
				if (can_format(this_fs))
					fs.emplace_back(std::ref(this_fs));
			}

			output_filename = util::zippath_combine(m_current_directory, m_current_file);
			if(fs.size() == 1) {
				create_fs = &(fs[0].get());
				do_load_create();
				stack_pop();
			} else {
				m_submenu_result.i = -1;
				menu::stack_push<menu_select_floppy_init>(ui(), container(), std::move(fs), &m_submenu_result.i);
				m_state = SELECT_INIT;
			}
		}
		break;

	case SELECT_INIT:
		// figure out which (if any) create file system was selected
		create_fs = nullptr;
		if(m_submenu_result.i >= 0) {
			int i = 0;
			for (const auto &this_fs : fd.get_fs()) {
				if (can_format(this_fs)) {
					if (i == m_submenu_result.i) {
						create_fs = &this_fs;
						break;
					}
					i++;
				}
			}
		}

		if(!create_fs) {
			m_state = START_FILE;
			menu_activated();
		} else {
			do_load_create();
			stack_pop();
		}
		break;

	default:
		menu_control_device_image::menu_activated();
	}
}

} // namespace ui