fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/utils/psi/save.c *
7 * Created: 2013-06-09 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2013-2017 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include "main.h"
24#include "save.h"
25
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29
30#include <drivers/psi/psi.h>
31
32
33static
34int psi_save_sectors_cb (psi_img_t *img, psi_sct_t *sct,
35 unsigned c, unsigned h, unsigned s, unsigned a, void *opaque)
36{
37 FILE *fp;
38
39 fp = opaque;
40
41 if (fwrite (sct->data, 1, sct->n, fp) != sct->n) {
42 return (1);
43 }
44
45 par_cnt += 1;
46
47 return (0);
48}
49
50int psi_save_sectors (psi_img_t *img, const char *fname)
51{
52 int r;
53 FILE *fp;
54
55 fp = fopen (fname, "wb");
56
57 if (fp == NULL) {
58 fprintf (stderr, "%s: can't create file (%s)\n", arg0, fname);
59 return (1);
60 }
61
62 par_cnt = 0;
63
64 r = psi_for_all_sectors (img, psi_save_sectors_cb, fp);
65
66 fclose (fp);
67
68 if (par_verbose) {
69 fprintf (stderr, "%s: save %lu sectors to %s\n",
70 arg0, par_cnt, fname
71 );
72 }
73
74 if (r) {
75 fprintf (stderr, "%s: saving sectors failed\n", arg0);
76 }
77
78 return (r);
79}
80
81
82static
83int psi_save_weak_cb (psi_img_t *img, psi_sct_t *sct,
84 unsigned c, unsigned h, unsigned s, unsigned a, void *opaque)
85{
86 FILE *fp;
87
88 fp = opaque;
89
90 if (sct->weak != NULL) {
91 if (fwrite (sct->weak, 1, sct->n, fp) != sct->n) {
92 return (1);
93 }
94
95 par_cnt += 1;
96 }
97
98 return (0);
99}
100
101int psi_save_weak (psi_img_t *img, const char *fname)
102{
103 int r;
104 FILE *fp;
105
106 if ((fp = fopen (fname, "wb")) == NULL) {
107 fprintf (stderr, "%s: can't create file (%s)\n", arg0, fname);
108 return (1);
109 }
110
111 par_cnt = 0;
112
113 r = psi_for_all_sectors (img, psi_save_weak_cb, fp);
114
115 fclose (fp);
116
117 if (par_verbose) {
118 fprintf (stderr, "%s: save %lu weak masks to %s\n",
119 arg0, par_cnt, fname
120 );
121 }
122
123 if (r) {
124 fprintf (stderr, "%s: saving weak masks failed\n", arg0);
125 }
126
127 return (r);
128}
129
130
131static
132int psi_save_tags_cb (psi_img_t *img, psi_sct_t *sct,
133 unsigned c, unsigned h, unsigned s, unsigned a, void *opaque)
134{
135 FILE *fp;
136 unsigned char buf[12];
137
138 fp = opaque;
139
140 psi_sct_get_tags (sct, buf, 12);
141
142 if (fwrite (buf, 1, 12, fp) != 12) {
143 return (1);
144 }
145
146 par_cnt += 1;
147
148 return (0);
149}
150
151int psi_save_tags (psi_img_t *img, const char *fname)
152{
153 int r;
154 FILE *fp;
155
156 fp = fopen (fname, "wb");
157
158 if (fp == NULL) {
159 fprintf (stderr, "%s: can't create file (%s)\n", arg0, fname);
160 return (1);
161 }
162
163 par_cnt = 0;
164
165 r = psi_for_all_sectors (img, psi_save_tags_cb, fp);
166
167 fclose (fp);
168
169 if (par_verbose) {
170 fprintf (stderr, "%s: save %lu sector tags to %s\n",
171 arg0, par_cnt, fname
172 );
173 }
174
175 if (r) {
176 fprintf (stderr, "%s: saving tags failed\n", arg0);
177 }
178
179 return (r);
180}