-
Notifications
You must be signed in to change notification settings - Fork 5
/
tc_io.c
executable file
·76 lines (66 loc) · 1.4 KB
/
tc_io.c
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
/**
* \file tc_io.c
* \brief Header for tc_io
* \author Kevin Fabricio Ortega
*
* Copyright 2012, by the California Institute of Technology. ALL RIGHTS
* RESERVED. United States Government Sponsorship acknowledged. Any
* commercial use must be negotiated with the Office of Technology
* Transfer at the California Institute of Technology.
*/
#include <stdio.h>
#include "tc_io.h"
#include "tc_image.h"
void tc_close_io(void *tc_io)
{
fclose((FILE*)tc_io);
}
int tc_getline_io(void *tc_io, char *buffer, int buf_size, char delim)
{
int i = 0;
char endofline = '\n';
for(i = 0; i < buf_size; i++)
{
fread(buffer+i, 1, 1, (FILE*)tc_io);
if(buffer[i] == delim || buffer[i] == endofline)
break;
}
if(buffer[i] != delim && buffer[i] != endofline)
{
return ERR;
}
else
{
buffer[i] = '\0';
return OK;
}
}
int tc_write_io(void *tc_io, char *buffer, int buf_size)
{
int ret_val = 0;
ret_val = fwrite(buffer, 1, buf_size, (FILE*)tc_io);
if( ret_val > 0 )
{
return OK;
}
else
{
return ERR;
}
}
void* tc_init_io(char *filename, const char *mode)
{
void *tc_io = 0;
if(filename != NULL)
{
if ((tc_io = (FILE*)fopen(filename, mode)) == NULL)
{
return NULL;
}
return tc_io;
}
else
{
return NULL;
}
}