File: sidcon.cpp

package info (click to toggle)
sidplay-base 1.0.9-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 416 kB
  • ctags: 226
  • sloc: sh: 3,011; cpp: 2,377; makefile: 57
file content (337 lines) | stat: -rw-r--r-- 8,299 bytes parent folder | download | duplicates (5)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// A dirty sidtune format converter hack with almost no security code.
// Copyright (C) Michael Schwendt.
//
//  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
//  (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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#if defined(HAVE_FREEBSD)
#include <sys/types.h>
#endif
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <iomanip>

#include <sidplay/sidtune.h>
#include <sidplay/fformat.h>

using namespace std;

static bool toPSID = true,
    toSIDPLAY = false,
    checkOnly = false,
    processDir = false,
    recursive = false,
    assumeAll = false;

static bool isDir(const char* file);
static void recurseDir(DIR* directory, const char* path);
static void handleFile(const char* file);
static void printUsage();

int main(int argc, char* argv[])
{
    DIR *directory;
    if ((directory = opendir(".")) == 0)
    {
        cerr << "ERROR: Cannot read current directory." << endl;
        exit(-1);
    }
    
    if (argc < 2)
    {
        printUsage();
        exit(-1);
    }
    
    int a = 0;
    
    while (a++ < argc-1)
    {
        if (myStrNcaseCmp(argv[a],"--psid") == 0)
        {
            toPSID = true;
            toSIDPLAY = false;
        }
        else if (myStrNcaseCmp(argv[a],"--sidplay") == 0)
        {
            toPSID = false;
            toSIDPLAY = true;
        }
        else if (myStrNcaseCmp(argv[a],"--") == 0)
        {
            printUsage();
            exit(-1);
        }
        else if (myStrNcaseCmp(argv[a],"-a") == 0)
        {
            assumeAll = true;
        }
        else if (myStrNcaseCmp(argv[a],"-c") == 0)
        {
            checkOnly = true;
        }
        else if (myStrNcaseCmp(argv[a],"-d") == 0)
        {
            processDir = true;
        }
        else if (myStrNcaseCmp(argv[a],"-r") == 0)
        {
            recursive = true;
        }
        else if (myStrNcaseCmp(argv[a],"-") == 0)
        {
            printUsage();
            exit(-1);
        }
    }

    if (recursive)
    {
		recurseDir(directory,".");
        exit(0);
    }
	
    if (processDir)
    {
        dirent *entry;
        while ((entry = readdir(directory)) != 0)
        {
            if ((strcmp(entry->d_name,".")!=0) &&
                (strcmp(entry->d_name,"..")!=0) &&
				!isDir(entry->d_name))
            {
                handleFile(entry->d_name);
            }
        }
        closedir(directory);
        exit(0);
    }

    // Check for any filenames given.
    bool success = true;
    a = 0;
    while (a++ < argc-1)
    {
        success = true;  // reset flag for each new file
        
        if (myStrNcaseCmp(argv[a],"-") != 0)
        {
            handleFile(argv[a]);
        }
    }
}

static void printUsage()
{
    cerr
        << "Usage: sidcon <options> [--psid|--sidplay] <files>" << endl
        << "options: -a    assume yes (y) on all questions" << endl
        << "         -c    only check each file's loadaddress" << endl
        << "         -d    process current directory" << endl
        << "         -r    recurse into current directory tree" << endl;
}

static void handleFile(const char* file)
{
    cout << file << ' ';
            
    char* outFileName = 0;
    sidTune mySidTune(file,true,0);  // true => compile and run on MS Windows
    sidTuneInfo mySTI;
            
    bool success = mySidTune.getStatus();
    bool saveIt = !checkOnly;

	if (!success)
	{
		mySidTune.getInfo(mySTI);
		cerr << mySTI.statusString << endl;
	}

	char* origDataName = 0;
	char* infoFileName = 0;

    if (success)
    {
        cout << '.';
        success &= mySidTune.getInfo(mySTI);
        if (mySTI.fixLoad)
        {
            cout << endl << "Duplicate C64 load address detected. Fix it? (y/n) " << flush;
            bool fixIt = false;
            if (assumeAll)
            {
                cout << endl;
                fixIt = true;
            }
            else
            {
                char c;
                cin >> c;
                fixIt = (tolower(c)=='y');
            }
            if (fixIt)
            {
                mySidTune.fixLoadAddress();
                saveIt = true;
            }
        }
        success &= (mySTI.dataFileName!=0);
        if (success)
        {
            success &= (0!=(outFileName = new char[strlen(mySTI.path)+strlen(mySTI.dataFileName)+4+1]));
            if (success)
            {
                strcpy(outFileName,mySTI.path);
                strcat(outFileName,mySTI.dataFileName);
				origDataName = myStrDup(outFileName);  // save it for error recovery
            }
        }
    }

 	char* tmp = fileNameWithoutPath(tmpnam(NULL));
    char* tmpDataName = new char[strlen(mySTI.path)+strlen(tmp)+1];
    strcpy(tmpDataName,mySTI.path);
    strcat(tmpDataName,tmp);
     
 	tmp = fileNameWithoutPath(tmpnam(NULL));
    char* tmpInfoName = new char[strlen(mySTI.path)+strlen(tmp)+1];
    strcpy(tmpInfoName,mySTI.path);
    strcat(tmpInfoName,tmp);
	
    if (success && saveIt)
    {
        cout << '.';
		success &= (0==rename(outFileName,tmpDataName));  // outFileName = path+dataFileName
        if (mySTI.infoFileName != 0)
        {
            strcpy(outFileName,mySTI.path);
            strcat(outFileName,mySTI.infoFileName);
			infoFileName = myStrDup(outFileName);  // save it for error recovery
			success &= (0==rename(outFileName,tmpInfoName));
        }
		else
		{
			tmpInfoName = 0;
		}
    }

	
    if (success && saveIt)
    {
        cout << '.';
        if (toPSID)
        {
            strcpy(fileExtOfPath(outFileName),".sid");
            success &= (mySidTune.savePSIDfile(outFileName));
        }
        else if (toSIDPLAY)
        {
            strcpy(fileExtOfPath(outFileName),".c64");
            success &= (mySidTune.saveC64dataFile(outFileName));
            strcpy(fileExtOfPath(outFileName),".sid");
            success &= (mySidTune.saveSIDfile(outFileName));
        }
		
		if (success)
		{
			remove(tmpDataName);
			if (tmpInfoName != 0)
				remove(tmpInfoName);
		}
		else  // recover tmp files
		{
			rename(tmpDataName,origDataName);
			if (tmpInfoName != 0)
				rename(tmpInfoName,infoFileName);
		}
    }

    if (success && saveIt)
    {
        cout << " SAVED" << endl;
    }
    else if (success)
    {
        cout << " OK" << endl;
    }

    if (tmpDataName != 0)
        delete[] tmpDataName;
    if (tmpInfoName != 0)
        delete[] tmpInfoName;
    if (outFileName != 0)
        delete[] outFileName;
	if (origDataName != 0)
		delete[] origDataName;
	if (infoFileName != 0)
		delete[] infoFileName;
}

bool isDir(const char* file)
{
	DIR *directory;
	if ((directory = opendir(file)) == 0)
		return false;
	else
	{
		closedir(directory);
		return true;
	}
}

void recurseDir(DIR* directory, const char* path)
{
	dirent *entry;
	while ((entry = readdir(directory)) != 0)
	{
		char* file = new char[strlen(path)+strlen(entry->d_name)+1+1];
		strcpy(file,path);
		strcat(file,"/");
		strcat(file,entry->d_name);
		if ((strcmp(entry->d_name,".")!=0) &&
			(strcmp(entry->d_name,"..")!=0) &&
			!isDir(file))
		{
			handleFile(file);
		}
		else if ((strcmp(entry->d_name,".")!=0) &&
				 (strcmp(entry->d_name,"..")!=0) &&
				 isDir(file))
		{
			char* newPath = new char[strlen(path)+strlen(entry->d_name)+1+1];
			strcpy(newPath,path);
			strcat(newPath,"/");
			strcat(newPath,entry->d_name);
			DIR* nextDir;
			if ((nextDir = opendir(newPath)) == 0)
			{
				cerr << "ERROR: Cannot read directory ``" << newPath << "''." << endl;
			}
			else
			{
				recurseDir(nextDir,newPath);
			}
			delete[] newPath;
		}
		delete[] file;
	}
	closedir(directory);
}