Move headers inside header guard; fix pasto.
[bertos.git] / bertos / fs / fat.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief FatFS: kfile interface for FatFS module by ChaN.
34  *
35  * \version $Id$
36  *
37  * \author Luca Ottaviano <lottaviano@develer.com>
38  *
39  */
40
41 #include "fat.h"
42
43 static size_t fatfile_read(struct KFile *_fd, void *buf, size_t size)
44 {
45         FatFile *fd = FATFILE_CAST(_fd);
46         UINT count;
47         fd->error_code = f_read(&fd->fat_file, buf, size, &count);
48         return count;
49 }
50
51 static size_t fatfile_write(struct KFile *_fd, const void *buf, size_t size)
52 {
53         FatFile *fd = FATFILE_CAST(_fd);
54         UINT count;
55         fd->error_code = f_write(&fd->fat_file, buf, size, &count);
56         return count;
57 }
58
59 static int fatfile_close(struct KFile *_fd)
60 {
61         FatFile *fd = FATFILE_CAST(_fd);
62         fd->error_code = f_close(&fd->fat_file);
63         if (fd->error_code)
64                 return EOF;
65         else
66                 return 0;
67 }
68
69 static kfile_off_t fatfile_seek(struct KFile *_fd, kfile_off_t offset, KSeekMode whence)
70 {
71         /* clip at start-of-file
72          * don't clip at end-of-file when in write mode
73          */
74         FatFile *fd = FATFILE_CAST(_fd);
75         DWORD lseek_offset;
76         switch (whence)
77         {
78         case KSM_SEEK_SET:
79                 if (offset > 0)
80                         lseek_offset = (DWORD) offset;
81                 else
82                         lseek_offset = 0;
83                 break;
84         case KSM_SEEK_CUR:
85                 if (offset > 0)
86                         lseek_offset = fd->fat_file.fptr + (DWORD) offset;
87                 else
88                 {
89                         if (fd->fat_file.fptr > (DWORD) (-offset))
90                                 lseek_offset = fd->fat_file.fptr - (DWORD)(-offset);
91                         else
92                                 lseek_offset = 0;
93                 }
94                 break;
95         case KSM_SEEK_END:
96                 if (offset > 0)
97                         lseek_offset = fd->fat_file.fsize + (DWORD) offset;
98                 else
99                 {
100                         if (fd->fat_file.fsize > (DWORD) (-offset))
101                                 lseek_offset = fd->fat_file.fsize + (DWORD) offset;
102                         else
103                                 lseek_offset = 0;
104                 }
105                 break;
106         }
107         fd->error_code = f_lseek(&fd->fat_file, lseek_offset);
108         if ((fd->error_code) || (fd->fat_file.fptr != lseek_offset))
109                 return EOF;
110         else
111                 /* TODO: this conversion may overflow */
112                 return (kfile_off_t)fd->fat_file.fptr;
113 }
114
115 static int fatfile_flush(struct KFile *_fd)
116 {
117         FatFile *fd = FATFILE_CAST(_fd);
118         fd->error_code = f_sync(&fd->fat_file);
119         if (fd->error_code)
120                 return EOF;
121         else
122                 return 0;
123 }
124
125 static int fatfile_error(struct KFile *_fd)
126 {
127         FatFile *fd = FATFILE_CAST(_fd);
128         return (int)fd->error_code;
129 }
130
131 static void fatfile_clearerr(struct KFile *_fd)
132 {
133         FatFile *fd = FATFILE_CAST(_fd);
134         fd->error_code = FR_OK;
135 }
136
137 FRESULT fatfile_open(FatFile *file, const char *file_path, BYTE mode)
138 {
139         file->fd._type = KFT_FATFILE;
140         file->fd.read = fatfile_read;
141         file->fd.write = fatfile_write;
142         file->fd.reopen = 0;
143         file->fd.close = fatfile_close;
144         file->fd.seek = fatfile_seek;
145         file->fd.flush = fatfile_flush;
146         file->fd.error = fatfile_error;
147         file->fd.clearerr = fatfile_clearerr;
148         return f_open(&file->fat_file, file_path, mode);
149 }
150