File: tim_io_stdio.cpp

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (102 lines) | stat: -rw-r--r-- 3,123 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
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO 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 2 of the License, or
(at your option) any later version.

XMOTO 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 XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

/* tim_io_stdio.cpp
 * 
 * stdio driver.
 */
#include <stdio.h>
#include "tim.h"

/*==============================================================================
File I/O system - stdio
==============================================================================*/

void *tim_stdio_open(char *pcWhere,tim_io_mode_t IOMode) {
	const char *pcIOMode=NULL;

	/* Determine mode of I/O */
	switch(IOMode) {
		case TIM_IM_READ: pcIOMode="rb"; break;
		case TIM_IM_WRITE: pcIOMode="wb"; break;
	}
	
	/* No prober I/O mode? */
	if(pcIOMode==NULL) return NULL;
	
	/* Use fopen() to open the file */
	return (void *)fopen(pcWhere,pcIOMode);
}

void tim_stdio_close(void *pvHandle) {
	/* Use fclose() to close the file */
	fclose((FILE *)pvHandle);
}

int tim_stdio_seek(void *pvHandle,int nOffset,tim_seek_mode_t SeekMode) {
	int nSeekMode=-1;
	int nCurPos;

	/* Use fseek() to seek in the file, depending on the SeekMode */
	switch(SeekMode) {
		case TIM_SM_ABS: nSeekMode=SEEK_SET; break; /* Absolute seeking */
		case TIM_SM_REL: nSeekMode=SEEK_CUR; break; /* Seek relatively to current pos */
		case TIM_SM_END: nSeekMode=SEEK_END; break; /* Seek relatively to end */
	}
	
	/* No prober SeekMode? */
	if(nSeekMode<0) return TIM_RV_ERR_INVALID_PARAM;
	
	/* Perform the actual seek using fseek() */
	if(fseek((FILE *)pvHandle,nOffset,nSeekMode))
		return TIM_RV_ERR_COULD_NOT_PERFORM;
	
	/* Get current position using ftell() */
	nCurPos=ftell((FILE *)pvHandle);
	if(nCurPos<0) return TIM_RV_ERR_COULD_NOT_PERFORM;
	
	/* Return current position */
	return nCurPos;
}

int tim_stdio_read(void *pvHandle,void *pvBuf,int nSize) {
	/* Fill buffer if posible (using fread()) */
	return (int)fread(pvBuf,1,nSize,(FILE *)pvHandle);
}

int tim_stdio_write(void *pvHandle,void *pvBuf,int nSize) {
	/* Write buffer contents if posible (using fwrite()) */
	return (int)fwrite(pvBuf,1,nSize,(FILE *)pvHandle);
}

int tim_stdio_eof(void *pvHandle) {
	/* feof() */
	return feof((FILE *)pvHandle);
}

/*==============================================================================
Globals
==============================================================================*/

tim_io_t g_IO_Stdio={
	tim_stdio_open,tim_stdio_close,tim_stdio_seek,tim_stdio_read,tim_stdio_write,
	tim_stdio_eof
};