aboutsummaryrefslogtreecommitdiff
path: root/jindroush/acvt/acvt.cpp
blob: 857da5b0dfef9e84cc2947ab3d237148fb5cff91 (plain)
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
//    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 2 of the License, or
//    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., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "pub.def"
#include "jintypes.h"
#include "autil.h"
#include "adsk.h"

BOOL Convert( char* szInFile, char* szOutFile );

DISK_TYPE g_dtypeIn = DISK_AUTO;
DISK_TYPE g_dtypeOut = DISK_ATR;

BOOL g_bVerbose = FALSE;
BOOL g_bBatchMode = FALSE;
BOOL g_bRepairAuto = FALSE;
BOOL g_bRepair = TRUE;
BOOL g_bFirstErrStop = FALSE;
BOOL g_bOverWrite = FALSE;
BOOL g_bTestOnly = FALSE;
BOOL g_bForceClassic = FALSE;

#define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"

#define HEADER SHEADER \
   PRG_DESC "\n" \
	"  Latest version can be found at " PRG_URL "\n" \
	"  Published under GPL. See GPL.TXT.\n\n"

#define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE

#include "switches.cpp"

int main(int argc,char *argv[])
{
	setbuf( stdout, NULL );
	setbuf( stderr, NULL );

	if ( !SWITCHES_Init( &argc, argv ) )
		return 1;

	if ( argc < 2 )
	{
		SWFN_HELP( USAGE );
		return 1;
	}

	fprintf( stderr, SHEADER );

	BOOL bRet = FALSE;

	char szOutFile[ 255 ];

	if ( g_bBatchMode )
	{
		argc--;
		argv++;

		while( argc-- )
		{
			char* szInFile = *(argv++);
			GuessBestFnameFromPC( szOutFile, szInFile, GetDiskTypeExt( g_dtypeOut ) );
			bRet = Convert( szInFile, szOutFile );
			printf( "\n" );

			if ( !bRet && g_bFirstErrStop )
				break;
		}

	}
	else
	{
		char* szInFile = argv[ 1 ];

		if ( argc >= 3 )
		{
			strcpy( szOutFile, argv[ 2 ] );
		}
		else
		{
			GuessBestFnameFromPC( szOutFile, szInFile, GetDiskTypeExt( g_dtypeOut ) );
		}

		bRet = Convert( szInFile, szOutFile );
	}

	if ( bRet && !g_bTestOnly )
		printf ( "Done.\n" );

	return bRet ? 0 : 1;
}

BOOL Convert( char* szInFile, char* szOutFile )
{
	ADisk* pDiskIn = NULL;
	ADisk* pDiskOut = NULL;

	DISKINIT_RETCODE ret;

	g_dtypeIn = DISK_ATR;
	if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_ATR, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
	{
		g_dtypeIn = DISK_DI;
		if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_DI, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
		{
			g_dtypeIn = DISK_DCM;
			if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_DCM, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
			{
				g_dtypeIn = DISK_SCP;
				if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_SCP, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
				{
					g_dtypeIn = DISK_XFD;
					if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_XFD, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
					{
						printf( "Unable to determine format of disk image '%s'!\n", szInFile );
						return FALSE;
					}
				}
			}
		}
	}

	if ( ret == DI_RET_CANT_CONTINUE )
		return FALSE;

	printf( "Input file '%s' (%s)\n", szInFile, GetDiskTypeName( g_dtypeIn ) );

	if ( g_bVerbose )
	{
		DISK_GEOMETRY* pgeo = pDiskIn->GetGeometry();
		printf( "Sides: %d\n", pgeo->iSides );
		printf( "Tracks: %d\n", pgeo->iTracks );
		printf( "SecPerTrack: %d\n", pgeo->iSectorsPerTrack );
		printf( "BytesPerSec: %d\n", pgeo->iBytesPerSector );
		printf( "Sectors: %d\n", pgeo->iSectors );
	}

	if ( g_bTestOnly )
	{
		printf( "File loaded OK.\n" );
		delete pDiskIn;
		return TRUE;
	}

	switch( g_dtypeOut )
	{
		case DISK_ATR:
			pDiskOut = new CAtr();
			break;

		case DISK_XFD:
			pDiskOut = new CXfd();
			break;

		case DISK_DI:
			pDiskOut = new CDi();
			break;

		case DISK_SCP:
			pDiskOut = new CScp();
			break;

		case DISK_DCM:
			pDiskOut = new CDcm();
			break;

		default:
			break;
	}

	if ( !pDiskOut )
	{
		fprintf( stderr, "Can't create such image!\n" );
		return FALSE;
	}

	DISK_GEOMETRY* pgeo = pDiskIn->GetGeometry();
	DISK_GEOMETRY forced;

	memcpy( &forced, pgeo, sizeof( DISK_GEOMETRY ) );

	if ( g_bForceClassic )
		ForceClassicSize( &forced );

	//duplicating the image
	pDiskOut->Duplicate( pDiskIn, &forced );

	printf( "Output file '%s' (%s)\n", szOutFile, GetDiskTypeName( g_dtypeOut ) );

	//and save it in new format
	if ( !pDiskOut->Save( szOutFile, g_bOverWrite ) )
	{
		printf( "Error! %s\n", pDiskOut->GetLastError() );
		delete pDiskIn;
		delete pDiskOut;
		return FALSE;
	}

	delete pDiskIn;
	delete pDiskOut;

	return TRUE;
}