ion_file.h File Reference
#include "../key_value/kv_system.h"
#include "stdio.h"
#include "unistd.h"

Description

A file API for the ionDB.

Author
Graeme Douglas
Todo:
Include support for other file systems (Arduino, Contiki).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1.Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3.Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file ion_file.h.

Include dependency graph for ion_file.h:
This graph shows which files directly or indirectly include this file:

Macros

#define ION_FILE_START   SEEK_SET
 
#define ION_FILE_END   SEEK_END
 
#define ION_NOFILE   ((ion_file_handle_t) (NULL))
 
#define ION_FILE_NULL   -1
 

Typedefs

typedef long ion_file_offset_t
 
typedef FILE * ion_file_handle_t
 

Functions

ion_boolean_t ion_fexists (char *name)
 
ion_file_handle_t ion_fopen (char *name)
 
ion_err_t ion_fclose (ion_file_handle_t file)
 
ion_err_t ion_fremove (char *name)
 
ion_err_t ion_fseek (ion_file_handle_t file, ion_file_offset_t seek_to, int origin)
 
ion_file_offset_t ion_ftell (ion_file_handle_t file)
 
ion_file_offset_t ion_fend (ion_file_handle_t file)
 
ion_err_t ion_fwrite (ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *to_write)
 
ion_err_t ion_fwrite_at (ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *to_write)
 
ion_err_t ion_fread (ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *write_to)
 
ion_err_t ion_fread_at (ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *write_to)
 

Macro Definition Documentation

#define ION_FILE_END   SEEK_END

Definition at line 50 of file ion_file.h.

#define ION_FILE_NULL   -1

Definition at line 75 of file ion_file.h.

#define ION_FILE_START   SEEK_SET

Definition at line 49 of file ion_file.h.

#define ION_NOFILE   ((ion_file_handle_t) (NULL))

Definition at line 71 of file ion_file.h.

Typedef Documentation

typedef FILE* ion_file_handle_t

Definition at line 69 of file ion_file.h.

typedef long ion_file_offset_t

Definition at line 47 of file ion_file.h.

Function Documentation

ion_err_t ion_fclose ( ion_file_handle_t  file)

Definition at line 80 of file ion_file.c.

82  {
83 #if defined(ARDUINO)
84  fclose(file.file);
85  return err_ok;
86 #else
87  fclose(file);
88  return err_ok;
89 #endif
90 }

Here is the caller graph for this function:

Definition at line 143 of file ion_file.c.

145  {
146  ion_file_offset_t previous;
147  ion_file_offset_t to_return;
148 
149  previous = ion_ftell(file);
150  ion_fseek(file, 0, ION_FILE_END);
151  to_return = ion_ftell(file);
152  ion_fseek(file, previous, ION_FILE_START);
153 
154  return to_return;
155 }
#define ION_FILE_END
Definition: ion_file.h:50
ion_err_t ion_fseek(ion_file_handle_t file, ion_file_offset_t seek_to, int origin)
Definition: ion_file.c:109
#define ION_FILE_START
Definition: ion_file.h:49
long ion_file_offset_t
Definition: ion_file.h:47
ion_file_offset_t ion_ftell(ion_file_handle_t file)
Definition: ion_file.c:132

Here is the call graph for this function:

Here is the caller graph for this function:

ion_boolean_t ion_fexists ( char *  name)

Definition at line 40 of file ion_file.c.

42  {
43 #if defined(ARDUINO)
44  return (ion_boolean_t) SD_File_Exists(name);
45 #else
46  return -1 != access(name, F_OK);
47 #endif
48 }
int SD_File_Exists(char *filepath)
char ion_boolean_t
A boolean type.
Definition: kv_system.h:269

Here is the call graph for this function:

Here is the caller graph for this function:

ion_file_handle_t ion_fopen ( char *  name)

Definition at line 51 of file ion_file.c.

53  {
54 #if defined(ARDUINO)
55 
56  ion_file_handle_t toret;
57 
58  toret.file = fopen(name, "r+");
59 
60  if (NULL == toret.file) {
61  toret.file = fopen(name, "w+");
62  }
63 
64  return toret;
65 #else
66 
67  ion_file_handle_t file;
68 
69  file = fopen(name, "r+b");
70 
71  if (NULL == file) {
72  file = fopen(name, "w+b");
73  }
74 
75  return file;
76 #endif
77 }
FILE * ion_file_handle_t
Definition: ion_file.h:69

Here is the caller graph for this function:

ion_err_t ion_fread ( ion_file_handle_t  file,
unsigned int  num_bytes,
ion_byte_t write_to 
)

Definition at line 214 of file ion_file.c.

218  {
219 #if defined(ARDUINO)
220 
221  if (num_bytes != (fread(write_to, num_bytes, 1, file.file) * num_bytes)) {
222  return err_file_read_error;
223  }
224 
225  return err_ok;
226 #else
227 
228  if (1 != fread(write_to, num_bytes, 1, file)) {
229  return err_file_read_error;
230  }
231 
232  return err_ok;
233 #endif
234 }

Here is the caller graph for this function:

ion_err_t ion_fread_at ( ion_file_handle_t  file,
ion_file_offset_t  offset,
unsigned int  num_bytes,
ion_byte_t write_to 
)

Definition at line 237 of file ion_file.c.

242  {
244 
245  error = ion_fseek(file, offset, ION_FILE_START);
246 
247  if (err_ok != error) {
248  return error;
249  }
250 
251  error = ion_fread(file, num_bytes, write_to);
252  return error;
253 }
char ion_err_t
The error type used to store error codes.
Definition: kv_system.h:226
ion_err_t ion_fseek(ion_file_handle_t file, ion_file_offset_t seek_to, int origin)
Definition: ion_file.c:109
#define ION_FILE_START
Definition: ion_file.h:49
#define error(rc)
Definition: bpp_tree.c:151
ion_err_t ion_fread(ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *write_to)
Definition: ion_file.c:214

Here is the call graph for this function:

Here is the caller graph for this function:

ion_err_t ion_fremove ( char *  name)

Definition at line 93 of file ion_file.c.

95  {
96  int status;
97 
98  status = fremove(name);
99 
100  if (0 == status) {
101  return err_ok;
102  }
103  else {
104  return err_file_delete_error;
105  }
106 }
#define fremove(x)
Definition: kv_system.h:56

Here is the caller graph for this function:

ion_err_t ion_fseek ( ion_file_handle_t  file,
ion_file_offset_t  seek_to,
int  origin 
)

Definition at line 109 of file ion_file.c.

113  {
114 #if defined(ARDUINO)
115 
116  if (0 != fseek(file.file, seek_to, origin)) {
117  return err_file_bad_seek;
118  }
119 
120  return err_ok;
121 #else
122 
123  if (0 != fseek(file, seek_to, origin)) {
124  return err_file_bad_seek;
125  }
126 
127  return err_ok;
128 #endif
129 }

Here is the caller graph for this function:

ion_file_offset_t ion_ftell ( ion_file_handle_t  file)

Definition at line 132 of file ion_file.c.

134  {
135 #if defined(ARDUINO)
136  return ftell(file.file);
137 #else
138  return ftell(file);
139 #endif
140 }

Here is the caller graph for this function:

ion_err_t ion_fwrite ( ion_file_handle_t  file,
unsigned int  num_bytes,
ion_byte_t to_write 
)

Definition at line 158 of file ion_file.c.

162  {
163 #if defined(ARDUINO)
164 
165  if (num_bytes != (fwrite(to_write, num_bytes, 1, file.file) * num_bytes)) {
166  return err_file_write_error;
167  }
168 
169  return err_ok;
170 #else
171  fwrite(to_write, num_bytes, 1, file);
172  return err_ok;
173 #endif
174 }

Here is the caller graph for this function:

ion_err_t ion_fwrite_at ( ion_file_handle_t  file,
ion_file_offset_t  offset,
unsigned int  num_bytes,
ion_byte_t to_write 
)

Definition at line 177 of file ion_file.c.

182  {
184 
185  error = ion_fseek(file, offset, ION_FILE_START);
186 
187  if (err_ok != error) {
188  return error;
189  }
190 
191  error = ion_fwrite(file, num_bytes, to_write);
192  return error;
193 }
char ion_err_t
The error type used to store error codes.
Definition: kv_system.h:226
ion_err_t ion_fseek(ion_file_handle_t file, ion_file_offset_t seek_to, int origin)
Definition: ion_file.c:109
ion_err_t ion_fwrite(ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *to_write)
Definition: ion_file.c:158
#define ION_FILE_START
Definition: ion_file.h:49
#define error(rc)
Definition: bpp_tree.c:151

Here is the call graph for this function:

Here is the caller graph for this function: