root/trunk/bertos/kern/kfile.c @ 1886

Revision 1886, 4.9 KB (checked in by batt, 16 months ago)

Merged from external project:

**********
r22496 | batt | 2008-10-08 16:18:02 +0200 (Wed, 08 Oct 2008) | 1 line


Reformat.
**********

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 2007 Develer S.r.l. (http://www.develer.com/)
30 * -->
31 *
32 * \brief Virtual KFile I/O interface.
33 *
34 * This module implements some generic I/O interfaces for kfile.
35 *
36 * \version $Id$
37 * \author Francesco Sacchi <batt@develer.com>
38 * \author Daniele Basile <asterix@develer.com>
39 */
40
41#include "kfile.h"
42
43#include "cfg/cfg_kfile.h"
44#include <cfg/debug.h>
45#include <cfg/log.h>
46
47#include <mware/formatwr.h>
48
49#include <string.h>
50
51/*
52 * Sanity check for config parameters required by this module.
53 */
54#if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1)
55        #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h
56#endif
57#if !defined(CONFIG_PRINTF)
58        #error CONFIG_PRINTF missing in appconfig.h
59#endif
60
61
62/**
63 * Generic putc() implementation using \a fd->write.
64 */
65int kfile_putc(int _c, struct KFile *fd)
66{
67        unsigned char c = (unsigned char)_c;
68
69        if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
70                return (int)((unsigned char)_c);
71        else
72                return EOF;
73}
74
75/**
76 * Generic getc() implementation using \a fd->read.
77 */
78int kfile_getc(struct KFile *fd)
79{
80        unsigned char c;
81
82        if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
83                return (int)((unsigned char)c);
84        else
85                return EOF;
86}
87
88#if CONFIG_PRINTF
89/**
90 * Formatted write.
91 */
92int kfile_printf(struct KFile *fd, const char *format, ...)
93{
94        va_list ap;
95        int len;
96
97        va_start(ap, format);
98        len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
99        va_end(ap);
100
101        return len;
102}
103#endif /* CONFIG_PRINTF */
104
105/**
106 * Write a string to kfile \a fd.
107 * \return 0 if OK, EOF in case of error.
108 */
109int kfile_print(struct KFile *fd, const char *s)
110{
111        while (*s)
112        {
113                if (kfile_putc(*s++, fd) == EOF)
114                        return EOF;
115        }
116        return 0;
117}
118
119#if CONFIG_KFILE_GETS
120/**
121 * Read a line long at most as size and put it
122 * in buf.
123 * \return number of chars read or EOF in case
124 *         of error.
125 */
126int kfile_gets(struct KFile *fd, char *buf, int size)
127{
128        return kfile_gets_echo(fd, buf, size, false);
129}
130
131
132/**
133 * Read a line long at most as size and put it
134 * in buf, with optional echo.
135 *
136 * \return number of chars read, or EOF in case
137 *         of error.
138 */
139int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
140{
141        int i = 0;
142        int c;
143
144        for (;;)
145        {
146                if ((c = kfile_getc(fd)) == EOF)
147                {
148                        buf[i] = '\0';
149                        return -1;
150                }
151
152                /* FIXME */
153                if (c == '\r' || c == '\n' || i >= size-1)
154                {
155                        buf[i] = '\0';
156                        if (echo)
157                                kfile_print(fd, "\r\n");
158                        break;
159                }
160                buf[i++] = c;
161                if (echo)
162                        kfile_putc(c, fd);
163        }
164
165        return i;
166}
167#endif /* !CONFIG_KFILE_GETS */
168
169
170/**
171 * Move \a fd file seek position of \a offset bytes from \a whence.
172 *
173 * This is a generic implementation of seek function, you can redefine
174 * it in your local module if needed.
175 */
176kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
177{
178        kfile_off_t seek_pos;
179
180        switch (whence)
181        {
182
183        case KSM_SEEK_SET:
184                seek_pos = 0;
185                break;
186        case KSM_SEEK_END:
187                seek_pos = fd->size;
188                break;
189        case KSM_SEEK_CUR:
190                seek_pos = fd->seek_pos;
191                break;
192        default:
193                ASSERT(0);
194                return EOF;
195                break;
196        }
197
198        #if LOG_LEVEL >= LOG_LVL_INFO
199        /* Bound check */
200        if (seek_pos + offset > fd->size)
201                LOG_INFO("seek outside EOF\n");
202        #endif
203
204        fd->seek_pos = seek_pos + offset;
205
206        return fd->seek_pos;
207}
208
209/**
210 * Reopen file \a fd.
211 * This is a generic implementation that only flush file
212 * and reset seek_pos to 0.
213 */
214struct KFile * kfile_genericReopen(struct KFile *fd)
215{
216        kfile_flush(fd);
217        kfile_seek(fd, 0, KSM_SEEK_SET);
218        return fd;
219}
220
221/**
222 * Close file \a fd.
223 * This is a generic implementation that only return 0.
224 */
225int kfile_genericClose(UNUSED_ARG(struct KFile *, fd))
226{
227        return 0;
228};
229
230
Note: See TracBrowser for help on using the browser.