-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
682 lines (602 loc) · 16.5 KB
/
app.cpp
File metadata and controls
682 lines (602 loc) · 16.5 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
///////////////////////////////////////////////////////////////////////////////
//
// app.cpp
//
// see RIO.CPP for version history
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include "std.h"
#include "rio.h"
// output
#define INFOSTR printf
#define ERRORSTR INFOSTR
// platform dependencies
#if defined(_WINNT)
// MS VC++ v5.0 for WinNT v4
#include <stdlib.h>
#define SIZE_MAXPATH _MAX_PATH
#define DELETEARRAY delete[]
#elif defined(_WIN32)
// MS VC++ v5.0 for Win9x
#include <stdlib.h>
#define SIZE_MAXPATH _MAX_PATH
#define DELETEARRAY delete[]
#elif defined(__linux__)
// linux g++
#include <unistd.h>
#include <values.h>
#if defined(PATH_MAX)
#define SIZE_MAXPATH PATH_MAX
#else
#define SIZE_MAXPATH 256
#endif
#define DELETEARRAY delete[]
#elif defined(__TURBOC__)
// turboc v1.01
#include <dir.h>
#define SIZE_MAXPATH MAXPATH
#define DELETEARRAY delete
#else
// not supported
#error ! ! compiler/platform not supported ! !
#endif
// default port base
#if defined(__alpha)
#define PORT_BASE_DEFAULT 0x3bc
#else
#define PORT_BASE_DEFAULT 0x378
#endif
// return code
#define CLEANUP_RETURN( ret ) \
{ \
if ( pRio ) \
{ \
delete pRio; \
pRio = NULL; \
} \
if ( pszFilePlaylistTemp ) \
{ \
unlink( pszFilePlaylistTemp ); \
pszFilePlaylistTemp = NULL; \
} \
return ret; \
}
///////////////////////////////////////////////////////////////////////////////
// return pointer to static string containing datetime
static char* TimeStr( long lValue )
{
static char szBuf[ 64 ];
struct tm* psDateTime;
psDateTime = localtime( &lValue );
if ( !psDateTime )
strcpy( szBuf, "INVALID DATE/TIME" );
else
{
sprintf(
szBuf,
"%02u/%02u/%02u %02u:%02u:%02u",
(UINT)psDateTime->tm_mday % 100,
(UINT)psDateTime->tm_mon+1 % 100,
(UINT)psDateTime->tm_year % 100,
(UINT)psDateTime->tm_hour % 100,
(UINT)psDateTime->tm_min % 100,
(UINT)psDateTime->tm_sec % 100
);
}
return szBuf;
}
///////////////////////////////////////////////////////////////////////////////
// return file size
static long GetFileSize( char* pszPathFile )
{
long lReturn = 0;
FILE* fpFile = fopen( pszPathFile, "rb" );
if ( fpFile )
{
struct stat sStat;
if ( !stat(pszPathFile, &sStat) )
lReturn = sStat.st_size;
fclose( fpFile );
}
return lReturn;
}
///////////////////////////////////////////////////////////////////////////////
// progress callback
static BOOL ProgressCallback( int iPos, int iCount )
{
INFOSTR( "blocks %-5hd\r", iCount-iPos );
fflush( stdout );
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// display directory
static void DisplayDirectory( CRio& cRio, BOOL bVerbose )
{
CDirBlock& cDirBlock = cRio.GetDirectoryBlock();
CDirHeader& cDirHeader = cDirBlock.m_cDirHeader;
INFOSTR( "\n" );
INFOSTR( " entry count: %hu\n", cDirHeader.m_usCountEntry );
INFOSTR( " total memory: %ld KB\n", ((long)cDirHeader.m_usCount32KBlockAvailable * CRIO_SIZE_32KBLOCK) / 1024 );
INFOSTR( " used memory: %ld KB\n", ((long)cDirHeader.m_usCount32KBlockUsed * CRIO_SIZE_32KBLOCK) / 1024 );
INFOSTR( " unused memory: %ld KB\n", ((long)cDirHeader.m_usCount32KBlockRemaining * CRIO_SIZE_32KBLOCK) / 1024 );
INFOSTR( "flash ram type: %s\n", cRio.GetUseExternalFlashStatus() ? "external" : "internal" );
if ( bVerbose )
{
INFOSTR( "bad 32K blocks: %hu\n", cDirHeader.m_usCount32KBlockBad );
INFOSTR( " last update: %s\n", TimeStr(cDirHeader.m_lTimeLastUpdate) );
INFOSTR( " checksum1: 0x%04hx\n", cDirHeader.m_usChecksum1 );
INFOSTR( " checksum2: 0x%04hx\n", cDirHeader.m_usChecksum2 );
INFOSTR( " host version: 0x%04hx\n", cDirHeader.m_usVersion );
}
UINT uiCountEntry = cDirHeader.m_usCountEntry;
if ( uiCountEntry )
{
CDirEntry* pDirEntry = cDirBlock.m_acDirEntry;
if ( uiCountEntry > CRIO_MAX_DIRENTRY )
uiCountEntry = CRIO_MAX_DIRENTRY;
// extended output
if ( bVerbose )
{
INFOSTR( "\n" );
INFOSTR( "No 32KPos 32KCount Mod32K Size Upload Date/Time Title\n" );
INFOSTR( "-----------------------------------------------------------------------------\n" );
for( UINT uiA=0; uiA<uiCountEntry; ++uiA, ++pDirEntry )
{
INFOSTR( "%02u 0x%04hx %-4hu 0x%04hx %-8ld %s %-24.24s\n",
uiA+1,
pDirEntry->m_usPos32KBlock,
pDirEntry->m_usCount32KBlock,
pDirEntry->m_usSize32KMod,
pDirEntry->m_lSize,
TimeStr(pDirEntry->m_lTimeUpload),
pDirEntry->m_szName
);
}
}
// normal output
else
{
INFOSTR( "\n" );
INFOSTR( "No Size Upload Date/Time Title\n" );
INFOSTR( "-----------------------------------------------------------------------------\n" );
for( UINT uiA=0; uiA<uiCountEntry; ++uiA, ++pDirEntry )
{
INFOSTR( "%02u %-8ld %s %-40.40s\n",
uiA+1,
pDirEntry->m_lSize,
TimeStr(pDirEntry->m_lTimeUpload),
pDirEntry->m_szName
);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// hex dump directory block
static void DumpDirectory( CRio& cRio, int iPos, int iSize )
{
int iA, iB;
CDirBlock& cDirBlock = cRio.GetDirectoryBlock();
UCHAR* pucS = ((UCHAR*)&cDirBlock) + iPos;
if ( iSize % 16 )
iSize = ((iSize / 16)+1) * 16;
for( iA=0; iA<(iSize/16); ++iA, pucS+=16 )
{
INFOSTR( "%08lx ", iA*16 );
for( iB=0; iB<16; ++iB )
{
int iC = *(pucS+iB);
INFOSTR( "%02x ", iC );
if ( (iB%8) == 7 )
INFOSTR( " " );
}
for( iB=0; iB<16; ++iB )
{
int iC = *(pucS+iB);
INFOSTR( "%c", iC > 32 ? iC : '.' );
if ( (iB%8) == 7 )
INFOSTR( " " );
}
INFOSTR( "\n" );
if ( (iA%8) == 7 )
INFOSTR( "\n" );
}
}
///////////////////////////////////////////////////////////////////////////////
// process playlist file
static BOOL ProcessPlaylist( CRio& cRio, char* pszFile, BOOL bVerbose )
{
char szBuf[ SIZE_MAXPATH ];
// open playlist for read
FILE* fpFile = fopen( pszFile, "r" );
if ( !fpFile )
{
ERRORSTR( "unable to open '%s' for read\n", pszFile );
return FALSE;
}
// check if device can accommodate all files in playlist
CDirBlock& cDirBlock = cRio.GetDirectoryBlock();
CDirHeader& cDirHeader = cDirBlock.m_cDirHeader;
long lSizeAvailable = (long)cDirHeader.m_usCount32KBlockAvailable * CRIO_SIZE_32KBLOCK;
long lSizeCurrent = (long)cDirHeader.m_usCount32KBlockUsed * CRIO_SIZE_32KBLOCK;
int iCountEntryCurrent = cDirHeader.m_usCountEntry;
while( fgets(szBuf, sizeof(szBuf), fpFile) )
{
// strip 'new line' char
szBuf[ strlen(szBuf)-1 ] = 0;
// get file size
long lSize = GetFileSize( szBuf );
if ( !lSize )
{
ERRORSTR( "unable to open '%s' for read\n", szBuf );
fclose( fpFile );
return FALSE;
}
// check space
lSizeCurrent += lSize;
if ( lSizeCurrent > lSizeAvailable )
{
ERRORSTR( "entry '%s' exceed's memory capacity of device\n", szBuf );
fclose( fpFile );
return FALSE;
}
// check enough entries
++iCountEntryCurrent;
if ( iCountEntryCurrent > CRIO_MAX_DIRENTRY )
{
ERRORSTR( "entry '%s' exceed's maximum directory entry count for device\n", szBuf );
fclose( fpFile );
return FALSE;
}
}
// rewind playlist
rewind( fpFile );
// process entries
while( fgets(szBuf, sizeof(szBuf), fpFile) )
{
// strip 'new line' char
szBuf[ strlen(szBuf)-1 ] = 0;
// upload
if ( bVerbose )
INFOSTR( "uploading %s\n", szBuf );
if ( !cRio.TxFile(szBuf, bVerbose ? ProgressCallback : NULL) )
{
ERRORSTR( "tx file failed, %s\n", cRio.GetErrorStr() );
fclose( fpFile );
return FALSE;
}
}
// close playlist file
fclose( fpFile );
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// change playlist order
static BOOL ChangePlaylistOrder( CRio& cRio, char* pszPlaylistOrder )
{
UINT auiPosNew[ CRIO_MAX_DIRENTRY ];
UINT uiPosNew = 0;
char* pszEntry;
do
{
pszEntry = strtok( uiPosNew ? NULL : pszPlaylistOrder, " " );
if ( pszEntry )
auiPosNew[ uiPosNew++ ] = atoi( pszEntry ) - 1;
} while( pszEntry && (uiPosNew < CRIO_MAX_DIRENTRY) );
if ( !cRio.SetFileOrder(auiPosNew, uiPosNew) )
{
ERRORSTR( "change in playlist order failed, %s\n", cRio.GetErrorStr() );
return FALSE;
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// display help
static void Help( void )
{
INFOSTR( "\nRio utility v%d.%02d - Ashpool Systems (c) 1999\n", CRIO_ID_VERSION/100, CRIO_ID_VERSION%100 );
INFOSTR( "--------------------------------------------\n" );
INFOSTR( "command line switches available :-\n" );
INFOSTR( " -d display directory\n" );
INFOSTR( " -iy initialize with check for bad blocks\n" );
INFOSTR( " -in initialize without check for bad blocks\n" );
INFOSTR( " -x perform operations on external flash ram\n" );
INFOSTR( " -u specify file(s) to upload\n" );
INFOSTR( " -g specify file to download\n" );
INFOSTR( " -f specify text based playlist file which contains files to be upload\n" );
INFOSTR( " -z specify file to delete\n" );
INFOSTR( " -za delete all files\n" );
INFOSTR( " -o specify new playlist order in quotes\n" );
INFOSTR( " -p specify parallel port base IO address, default=0x%x\n", PORT_BASE_DEFAULT );
INFOSTR( " -v enable verbose mode\n" );
INFOSTR( "\nexamples...\n" );
INFOSTR( " ; display directory using parallel port at 0x278\n" );
INFOSTR( " rio -p 0x278 -d\n" );
INFOSTR( " ; initialize (with bad block check) and upload files\n" );
INFOSTR( " rio -iy -u *.mp3\n" );
INFOSTR( " ; delete existing files and upload playlist onto external flash ram\n" );
INFOSTR( " rio -za -f playlist.txt -x\n" );
INFOSTR( " ; initialize, upload files in playlist and then display directory\n" );
INFOSTR( " rio -d -in -f playlist.txt\n" );
INFOSTR( " ; download file then delete it and finally display directory\n" );
INFOSTR( " rio -d -g mp3Files/song.mp3 -z song.mp3\n" );
INFOSTR( " ; reverse playlist order and then display directory\n" );
INFOSTR( " rio -o \"5 4 3 2 1\" -d\n" );
}
///////////////////////////////////////////////////////////////////////////////
int main( int iCountArg, char* paszArg[] )
{
// default settings
BOOL bDisplayDir = FALSE;
BOOL bInit = FALSE;
BOOL bInitMarkBadBlock = FALSE;
BOOL bUseExternalFlash = FALSE;
BOOL bDeleteAll = FALSE;
BOOL bVerbose = FALSE;
CRio* pRio = NULL;
char* pszFileDelete = NULL;
char* pszFileDownload = NULL;
char* pszFilePlaylist = NULL;
char* pszFilePlaylistTemp = NULL;
char* pszPlaylistOrder = NULL;
int iPortBase = PORT_BASE_DEFAULT;
int iPosDumpDirectory = 0;
int iSizeDumpDirectory = 0;
// process command line args
if ( iCountArg < 2 )
{
Help();
CLEANUP_RETURN( FALSE );
}
for( int iA=1; iA<iCountArg; ++iA )
{
// check for display directory request
if ( !strcmp(paszArg[iA], "-d") )
bDisplayDir = TRUE;
// check for init with mark bad block request
else if ( !strcmp(paszArg[iA], "-iy") )
{
bInit = TRUE;
bInitMarkBadBlock = TRUE;
}
// check for init without mark bad block request
else if ( !strcmp(paszArg[iA], "-in") )
bInit = TRUE;
// check for delete all file request
else if ( !strcmp(paszArg[iA], "-za") )
bDeleteAll = TRUE;
// check for request to use external flash memory
else if ( !strcmp(paszArg[iA], "-x") )
bUseExternalFlash = TRUE;
// check for verbose mode request
else if ( !strcmp(paszArg[iA], "-v") )
bVerbose = TRUE;
// check for dump directory request
else if ( !strcmp(paszArg[iA], "-h") )
{
if ( (iA+2) < iCountArg )
{
++iA;
iPosDumpDirectory = atoi( paszArg[iA] );
++iA;
iSizeDumpDirectory = atoi( paszArg[iA] );
}
}
// check for delete file request
else if ( !strcmp(paszArg[iA], "-z") )
{
if ( (iA+1) < iCountArg )
{
++iA;
pszFileDelete = paszArg[iA];
}
}
// check for upload file request
else if ( !strcmp(paszArg[iA], "-u") )
{
if ( (iA+1) < iCountArg )
{
++iA;
// append all command line upload requests to temp playlist
if ( !pszFilePlaylistTemp )
pszFilePlaylistTemp = tmpnam( NULL );
if ( pszFilePlaylistTemp )
{
FILE* fpFile = fopen( pszFilePlaylistTemp, "a" );
if ( !fpFile )
{
ERRORSTR( "unable to open '%s' for amend\n", pszFilePlaylistTemp );
CLEANUP_RETURN( FALSE );
}
while( iA < iCountArg )
{
fprintf( fpFile, "%s\n", paszArg[iA] );
if ( (iA+1) < iCountArg )
{
if ( paszArg[iA+1][0] == '-' )
break;
}
++iA;
}
fclose( fpFile );
}
}
}
// check for download file request
else if ( !strcmp(paszArg[iA], "-g") )
{
if ( (iA+1) < iCountArg )
{
++iA;
pszFileDownload = paszArg[iA];
}
}
// check for playlist file request
else if ( !strcmp(paszArg[iA], "-f") )
{
if ( (iA+1) < iCountArg )
{
++iA;
pszFilePlaylist = paszArg[iA];
}
}
// check for playlist order request
else if ( !strcmp(paszArg[iA], "-o") )
{
if ( (iA+1) < iCountArg )
{
++iA;
pszPlaylistOrder = paszArg[iA];
}
}
// check for port base request
else if ( !strcmp(paszArg[iA], "-p") )
{
if ( (iA+1) < iCountArg )
{
++iA;
sscanf( paszArg[iA], "%x", &iPortBase );
}
}
// else help
else
{
Help();
CLEANUP_RETURN( FALSE );
}
}
// setup
pRio = new CRio;
if ( !pRio )
{
ERRORSTR( "not enough memory\n" );
CLEANUP_RETURN( FALSE );
}
CRio& cRio = *pRio;
if ( !cRio.Set(iPortBase) )
{
ERRORSTR( "%s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
// select internal or external flash memory
if ( bVerbose )
INFOSTR( "performing operations on %s flash ram\n", bUseExternalFlash ? "external" : "internal" );
cRio.UseExternalFlash( bUseExternalFlash );
// check device present
if ( bVerbose )
INFOSTR( "check device present\n" );
if ( !cRio.CheckPresent() )
{
ERRORSTR( "%s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
// update directory flag
BOOL bUpdateDirectory = FALSE;
// get current directory
if ( bVerbose )
INFOSTR( "downloading directory\n" );
if ( !cRio.RxDirectory() )
{
if ( cRio.GetErrorID() == CRIO_ERROR_CORRUPT )
ERRORSTR( "%s\n", cRio.GetErrorStr() );
else
{
ERRORSTR( "rx directory failed, %s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
}
// if dump directory request
if ( iSizeDumpDirectory )
DumpDirectory( cRio, iPosDumpDirectory, iSizeDumpDirectory );
// if init request
if ( bInit )
{
if ( bVerbose )
INFOSTR( "initializing with bad block check %s\n", bInitMarkBadBlock ? "enabled" : "disabled" );
if ( !cRio.Initialize(bInitMarkBadBlock, bVerbose ? ProgressCallback : NULL) )
{
ERRORSTR( "Initialize failed, %s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
bUpdateDirectory = TRUE;
}
// if playlist request
if ( pszFilePlaylist )
{
if ( !ProcessPlaylist(cRio, pszFilePlaylist, bVerbose) )
CLEANUP_RETURN( FALSE );
bUpdateDirectory = TRUE;
}
// if temp playlist request
if ( pszFilePlaylistTemp )
{
if ( !ProcessPlaylist(cRio, pszFilePlaylistTemp, bVerbose) )
CLEANUP_RETURN( FALSE );
bUpdateDirectory = TRUE;
}
// if download request
if ( pszFileDownload )
{
if ( bVerbose )
INFOSTR( "downloading %s\n", pszFileDownload );
if ( !cRio.RxFile(pszFileDownload, bVerbose ? ProgressCallback : NULL) )
{
ERRORSTR( "rx file failed, %s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
}
// if delete request
if ( pszFileDelete )
{
if ( bVerbose )
INFOSTR( "deleting %s\n", pszFileDelete );
if ( !cRio.RemoveFile(pszFileDelete) )
{
ERRORSTR( "delete file failed, %s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
bUpdateDirectory = TRUE;
}
// if delete all request
if ( bDeleteAll )
{
if ( bVerbose )
INFOSTR( "deleting all files\n" );
if ( !cRio.RemoveAllFiles() )
{
ERRORSTR( "delete all files failed, %s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
bUpdateDirectory = TRUE;
}
// if playlist order request
if ( pszPlaylistOrder )
{
if ( bVerbose )
INFOSTR( "changing playlist order\n" );
if ( !ChangePlaylistOrder(cRio, pszPlaylistOrder) )
CLEANUP_RETURN( FALSE );
bUpdateDirectory = TRUE;
}
// if directory update required
if ( bUpdateDirectory )
{
if ( bVerbose )
INFOSTR( "updating directory\n" );
if ( !cRio.TxDirectory() )
{
ERRORSTR( "tx directory failed, %s\n", cRio.GetErrorStr() );
CLEANUP_RETURN( FALSE );
}
}
// if display directory request
if ( bDisplayDir )
DisplayDirectory( cRio, bVerbose );
// restore
CLEANUP_RETURN( TRUE );
}