File: memory_mod.c

package info (click to toggle)
m68k-vme-tftplilo 1.0.0-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 316 kB
  • ctags: 637
  • sloc: ansic: 5,475; makefile: 78
file content (117 lines) | stat: -rw-r--r-- 2,165 bytes parent folder | download | duplicates (4)
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
/*
 * memory_mod.c -- Trivial module for reading simple, memory files
 *
 * Copyright (c) 1997 by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
 * Modified by Nick Holgate to support memory file I/O.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive
 * for more details.
 */

#include "defs.h"

/***************************** Prototypes *****************************/

static int mod_memory_open( void *data, long count );
static long mod_memory_fillbuf( void *buf );
static int mod_memory_skip( long cnt );
static int mod_memory_close( void );

/************************* End of Prototypes **************************/

#define MAXBUF	(32*1024)

/* definition of the module structure */
MODULE memory_mod = {
	"memory",					/* name					*/
	MAXBUF,						/* maxbuf (arbitrary)	*/
	mod_memory_open,
	mod_memory_fillbuf,
	mod_memory_skip,
	mod_memory_close,
	MOD_REST_INIT
};

/*--------------------------------------------------------------------------*/

static	long	f_size;
static	long	f_pos;
static	char	*f_data;

/*--------------------------------------------------------------------------*/

static
int
mod_memory_open
(	void	*data,
	long	count
)
{
	f_pos  = 0;
	f_size = count;
	f_data = data;

	return SUCCESS;
}

/*--------------------------------------------------------------------------*/

static
long
mod_memory_fillbuf
(	void	*buf
)
{	long	count = MAXBUF;
	long	left;

	if (f_pos == f_size)
	{
		/* EOF */
		return -1;
	}

	if (count > (left = f_size - f_pos))
	{
		count = left;
	}

	mem_move(buf, f_data + f_pos, count);

	f_pos += count;
	return count;
}

/*--------------------------------------------------------------------------*/

static
int
mod_memory_skip
(	long	cnt
)
{
	cnt += f_pos;

	/* only allow seek within extents of file */
    if (cnt < 0 || cnt > f_size)
    {
		return -1;
	}

	f_pos = cnt;

    return cnt;
}

/*--------------------------------------------------------------------------*/

static
int
mod_memory_close
(	void
)
{
	return 0;
}

/*-----------------------------< end of file >------------------------------*/