-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimg.c
More file actions
90 lines (72 loc) · 2.48 KB
/
img.c
File metadata and controls
90 lines (72 loc) · 2.48 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
#if 0
/*===========================================================================
I M G . C
IMAGE - Raw data. The first byte in the file is assumed to
occupy target system memory address 0. Each additional byte
occupies the next sequential target memory address. On the
VAX, the minimum file size is 512 (since image files are
stored as fixed length records), however, this is too
restrictive. I want the system to be able to read image
files of any aribitray size.
Copyright 1989 Atari Games. All rights reserved.
Author: Lyle Rains
---------------------------------------------------------------------------
Revision history:
---------------------------------------------------------------------------
Known bugs/features/limitations:
===========================================================================*/
#endif
#include "mixit.h"
#ifndef SEEK_SET
#define SEEK_SET 0 /* Set file pointer to "offset" */
#define SEEK_CUR 1 /* Set file pointer to current plus "offset" */
#define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#endif
/*==========================================================================*/
int GetRec_img(InRecord *rec)
{
size_t len;
rec->recData = rec->recBuf;
if ((len = fread(rec->recBuf, 1, rec->recBufLen, rec->recFile)) > 0)
{
rec->recSAddr += rec->recLen;
rec->recLen = len;
rec->recEAddr = rec->recSAddr+len-1;
rec->recType = REC_DATA;
}
else
{
rec->recType = feof(rec->recFile) ? REC_EOF : REC_ERR;
rec->recLen = 0;
}
return (rec->recType);
} /* end GetRec_img */
/*==========================================================================*
* Outputs a single record in IMG format.
*==========================================================================*/
int PutRec_img( FILE *file, uint8_t *data, int recsize, uint32_t recstart )
{
#if defined(VMS)
uint8_t buffer[512];
int amount;
while ( recsize > 0 )
{
amount = min( recsize, 512 );
memcpy( buffer, data, amount );
if (amount < 512)
memset( &buffer[amount], 0, 512-amount );
if ( fwrite( buffer, 512, 1, file ) != 1)
return perr_return( 0, "Error writing data" );
recsize -= amount;
data += amount;
}
#else
if ( debug )
printf("PutRec_img(): size=0x%X, pos=0x%08X\n", recsize, recstart);
if ( fseek(file, recstart, SEEK_SET) < 0 )
return perr_return( 0, "Error seeking to position in IMAGE file");
if (fwrite( data, recsize, 1, file) != 1)
return perr_return( 0, "Error writing data");
#endif
return 1;
} /* end PutRec_img */