File: filereader.cpp

package info (click to toggle)
amoeba 1.1-17
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 732 kB
  • ctags: 971
  • sloc: cpp: 8,315; makefile: 178
file content (74 lines) | stat: -rw-r--r-- 1,455 bytes parent folder | download | duplicates (11)
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
/* simple wrapper class to port stdio-based code easier */

#include <stdio.h>
#include <string.h>

#include "demolib_prefs.h"
#include "filereader.h"
	
#if DEMOLIB_DATA_FILEREADER
FileReader::FileReader(File *file)
{
	this->curr_file = file;
	this->position = 0;
}

FileReader::~FileReader()
{
	delete this->curr_file;
}

size_t FileReader::fread(void *ptr, size_t size, size_t nmemb)
{
	size_t i;
	for (i = 0; i < nmemb; i++) {
		if ((int)this->position + (int)size > (int)this->curr_file->data_length()) {
			return i;
		}
		memcpy((char *)(ptr) + i * size, this->curr_file->get_data() + this->position, size);
		this->position += size;
	}
	return i;
}

size_t FileReader::read(void *buf, size_t count)
{
	if ((int)this->position + (int)count > (int)this->curr_file->data_length()) {
		count = this->curr_file->data_length() - this->position;
	}
	memcpy(buf, this->curr_file->get_data() + this->position, count);
	return count;
}

long FileReader::ftell()
{
	return this->position;
}

int FileReader::fseek(long offset, int whence)
{
	int real_offset;
	switch (whence) {
	case SEEK_SET:
		real_offset = 0;
		break;
	case SEEK_CUR:
		real_offset = this->position;
		break;
	case SEEK_END:
		real_offset = this->curr_file->data_length();
		break;
	default:
		return -1;
	}

	if (real_offset + offset < 0 ||
            real_offset + offset > this->curr_file->data_length()) {
		return -1;
	}

	this->position = real_offset + offset;
	return 0;
}

#endif