BeRTOS
kfile.c
Go to the documentation of this file.
00001 
00040 #include "kfile.h"
00041 
00042 #include "cfg/cfg_kfile.h"
00043 #include <cfg/debug.h>
00044 #include <cfg/log.h>
00045 
00046 #include <drv/timer.h>
00047 #include <mware/formatwr.h>
00048 
00049 #include <string.h>
00050 
00051 /*
00052  * Sanity check for config parameters required by this module.
00053  */
00054 #if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1)
00055     #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h
00056 #endif
00057 #if !defined(CONFIG_PRINTF)
00058     #error CONFIG_PRINTF missing in appconfig.h
00059 #endif
00060 
00061 
00065 int kfile_putc(int _c, struct KFile *fd)
00066 {
00067     unsigned char c = (unsigned char)_c;
00068 
00069     if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
00070         return (int)((unsigned char)_c);
00071     else
00072         return EOF;
00073 }
00074 
00078 int kfile_getc(struct KFile *fd)
00079 {
00080     unsigned char c;
00081 
00082     if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
00083         return (int)((unsigned char)c);
00084     else
00085         return EOF;
00086 }
00087 
00088 #if CONFIG_PRINTF
00089 
00092 int kfile_printf(struct KFile *fd, const char *format, ...)
00093 {
00094     va_list ap;
00095     int len;
00096 
00097     va_start(ap, format);
00098     len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
00099     va_end(ap);
00100 
00101     return len;
00102 }
00103 #endif /* CONFIG_PRINTF */
00104 
00109 int kfile_print(struct KFile *fd, const char *s)
00110 {
00111     while (*s)
00112     {
00113         if (kfile_putc(*s++, fd) == EOF)
00114             return EOF;
00115     }
00116     return 0;
00117 }
00118 
00119 #if CONFIG_KFILE_GETS
00120 
00126 int kfile_gets(struct KFile *fd, char *buf, int size)
00127 {
00128     return kfile_gets_echo(fd, buf, size, false);
00129 }
00130 
00131 
00139 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
00140 {
00141     int i = 0;
00142     int c;
00143 
00144     for (;;)
00145     {
00146         if ((c = kfile_getc(fd)) == EOF)
00147         {
00148             buf[i] = '\0';
00149             return -1;
00150         }
00151 
00152         /* FIXME */
00153         if (c == '\r' || c == '\n' || i >= size-1)
00154         {
00155             buf[i] = '\0';
00156             if (echo)
00157                 kfile_print(fd, "\r\n");
00158             break;
00159         }
00160         buf[i++] = c;
00161         if (echo)
00162             kfile_putc(c, fd);
00163     }
00164 
00165     return i;
00166 }
00167 #endif /* !CONFIG_KFILE_GETS */
00168 
00169 
00170 kfile_off_t kfile_copy(KFile *src, KFile *dst, kfile_off_t size)
00171 {
00172     char buf[32];
00173     kfile_off_t cp_len = 0;
00174 
00175     while (size)
00176     {
00177         size_t len = MIN(sizeof(buf), (size_t)size);
00178         if (kfile_read(src, buf, len) != len)
00179             break;
00180 
00181         size_t wr_len = kfile_write(dst, buf, len);
00182         cp_len += wr_len;
00183         size -= len;
00184 
00185         if (wr_len != len)
00186             break;
00187     }
00188 
00189     return cp_len;
00190 }
00191 
00192 
00199 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
00200 {
00201     kfile_off_t seek_pos;
00202 
00203     switch (whence)
00204     {
00205 
00206     case KSM_SEEK_SET:
00207         seek_pos = 0;
00208         break;
00209     case KSM_SEEK_END:
00210         seek_pos = fd->size;
00211         break;
00212     case KSM_SEEK_CUR:
00213         seek_pos = fd->seek_pos;
00214         break;
00215     default:
00216         ASSERT(0);
00217         return EOF;
00218         break;
00219     }
00220 
00221     /* Bound check */
00222     if (seek_pos + offset > fd->size)
00223         LOG_INFO("seek outside EOF\n");
00224 
00225     fd->seek_pos = seek_pos + offset;
00226 
00227     return fd->seek_pos;
00228 }
00229 
00235 struct KFile * kfile_genericReopen(struct KFile *fd)
00236 {
00237     kfile_flush(fd);
00238     kfile_seek(fd, 0, KSM_SEEK_SET);
00239     return fd;
00240 }
00241 
00246 int kfile_genericClose(struct KFile *fd)
00247 {
00248     return kfile_flush(fd);
00249 }
00250 
00251 
00261 void kfile_resync(KFile *fd, mtime_t delay)
00262 {
00263     ticks_t start_time = timer_clock();
00264     for(;;)
00265     {
00266         if(kfile_getc(fd) != EOF)
00267             start_time = timer_clock();
00268 
00269         if ((timer_clock() - start_time) > ms_to_ticks(delay))
00270         {
00271             kfile_clearerr(fd);
00272             break;
00273         }
00274 
00275     }
00276 }
00277 
00282 static int kfile_generic(UNUSED_ARG(struct KFile *, fd))
00283 {
00284     return 0;
00285 };
00286 
00287 
00291 void kfile_init(struct KFile *fd)
00292 {
00293     ASSERT(fd);
00294     memset(fd, 0, sizeof(*fd));
00295     fd->clearerr = (ClearErrFunc_t)kfile_generic;
00296     fd->close =  kfile_genericClose;
00297     fd->error = kfile_generic;
00298     fd->flush = kfile_generic;
00299     fd->read = (ReadFunc_t)kfile_generic;
00300     fd->reopen = kfile_genericReopen;
00301     fd->seek = kfile_genericSeek;
00302     fd->write = (WriteFunc_t)kfile_generic;
00303 }
00304