File: binaryfile.cpp

package info (click to toggle)
concordance 0.24-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 7,184 kB
  • sloc: sh: 11,237; cpp: 4,651; ansic: 2,665; python: 600; perl: 253; makefile: 99
file content (109 lines) | stat: -rw-r--r-- 2,155 bytes parent folder | download | duplicates (2)
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
/*
 * vi: formatoptions+=tc textwidth=80 tabstop=8 shiftwidth=8 noexpandtab:
 *
 * $Id: binaryfile.cpp,v 1.9 2008/04/11 05:05:26 jaymzh Exp $
 *
 * 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 3 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (C) Copyright Kevin Timmerman 2007
 * (C) Copyright Phil Dibowitz 2007
 */

// C++ iostreams will eat EOF chars, even when ios::binary is used with
// read() and write()

#include "libconcord.h"
#include "lc_internal.h"

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

#ifdef WIN32
#include <io.h>
#else
#include <sys/stat.h>
#endif

#include "binaryfile.h"


binaryfile::binaryfile()
{
	m_f = NULL;
}

binaryfile::~binaryfile()
{
	close();
}

int binaryfile::close(void)
{
	if (m_f) {
		const int i = fclose(m_f);
		m_f = NULL;
		return i;
	} else
		return 0;
}

binaryoutfile::binaryoutfile()
{
	::binaryfile();
}

int binaryoutfile::open(const char *path)
{
	m_f = fopen(path, "wb");
	return m_f ? 0 : 1;
}

size_t binaryoutfile::write(const uint8_t *b, uint32_t len)
{
	return fwrite(b, len, 1, m_f);
}

size_t binaryoutfile::write(const char *c)
{
	return fwrite(c,strlen(c), 1, m_f);
}


binaryinfile::binaryinfile()
{
	::binaryfile();
}

int binaryinfile::open(const char *path)
{
	m_f = fopen(path, "rb");
	return m_f ? 0 : 1;
}

unsigned int binaryinfile::getlength(void)
{
#ifdef WIN32
	return filelength(fileno(m_f));
#else
	struct stat fs;
	fstat(fileno(m_f), &fs);
	return fs.st_size;
#endif
}

size_t binaryinfile::read(uint8_t *b, uint32_t len)
{
	return fread(b, len, 1, m_f);
}