source: libcf/trunk/src/cf_file.c@ 16

Last change on this file since 16 was 16, checked in by cheese, 11 years ago

#1 add function for non-context debugging message printing

File size: 1.6 KB
Line 
1/**
2 * cf_file.c
3 */
4#include "cf_file.h"
5
6#ifdef _WIN32
7# include <stdio.h>
8# include <io.h>
9# include <sys/stat.h>
10
11# define S_IWUSR _S_IWRITE
12# define S_IRUSR _S_IREAD
13# define S_IXUSR _S_IEXEC
14# define S_IRGRP 0x00000000
15# define S_IWGRP 0x00000000
16# define S_IXGRP 0x00000000
17# define S_IROTH 0x00000000
18# define S_IWOTH 0x00000000
19# define S_IXOTH 0x00000000
20#else // #ifdef _WIN32
21# include <unistd.h>
22#endif // #ifdef _WIN32
23
24#define FILE_MODE S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH
25
26int
27CF_File_Open (const char * path,
28 const CF_FILE_FLAG flag)
29{
30 int result = open (path, flag);
31
32 if (result < 0)
33 return CF_ERROR_FILE_OPEN;
34
35 return result;
36}
37
38int
39CF_File_Create (const char * path)
40{
41 int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
42
43 if (result < 0)
44 return CF_ERROR_FILE_CREATE;
45
46 return result;
47}
48
49int
50CF_File_Close (const int fd)
51{
52 int result = 0;
53
54 if (fd < 0)
55 return CF_ERROR_FILE_INVALID_ARGS;
56
57 result = close (fd);
58
59 if (result < 0)
60 return CF_ERROR_FILE_CLOSE;
61
62 return CF_OK;
63}
64
65int
66CF_File_Read (const int fd,
67 void * buf,
68 const size_t len)
69{
70 int result = (int) read (fd, buf, len);
71
72 if (result < 0)
73 return CF_ERROR_FILE_READ;
74
75 return CF_OK;
76}
77
78int
79CF_File_Write (const int fd,
80 const void * buf,
81 const size_t len)
82{
83 int result = (int) write (fd, buf, len);
84
85 if (result != len)
86 return CF_ERROR_FILE_WRITE;
87
88 return CF_OK;
89}
90
91int
92CF_File_GetSize (const int fd)
93{
94 int result = (int) lseek (fd, 0, SEEK_END);
95
96 if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
97 return CF_ERROR_FILE_GET_SIZE;
98
99 return result;
100}
Note: See TracBrowser for help on using the repository browser.