kfile.c

Go to the documentation of this file.
00001 
00043 #include "kfile.h"
00044 #include <appconfig.h>
00045 
00046 #include <cfg/debug.h>
00047 #include <mware/formatwr.h>
00048 #include <string.h>
00049 
00050 /*
00051  * Sanity check for config parameters required by this module.
00052  */
00053 #if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1)
00054     #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h
00055 #endif
00056 #if !defined(CONFIG_PRINTF)
00057     #error CONFIG_PRINTF missing in appconfig.h
00058 #endif
00059 
00060 
00064 int kfile_putc(int _c, struct KFile *fd)
00065 {
00066     unsigned char c = (unsigned char)_c;
00067 
00068     if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
00069         return (int)((unsigned char)_c);
00070     else
00071         return EOF;
00072 }
00073 
00077 int kfile_getc(struct KFile *fd)
00078 {
00079     unsigned char c;
00080 
00081     if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
00082         return (int)((unsigned char)c);
00083     else
00084         return EOF;
00085 }
00086 
00087 #if CONFIG_PRINTF
00088 
00091 int kfile_printf(struct KFile *fd, const char *format, ...)
00092 {
00093     va_list ap;
00094     int len;
00095 
00096     va_start(ap, format);
00097     len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
00098     va_end(ap);
00099 
00100     return len;
00101 }
00102 #endif /* CONFIG_PRINTF */
00103 
00108 int kfile_print(struct KFile *fd, const char *s)
00109 {
00110     while (*s)
00111     {
00112         if (kfile_putc(*s++, fd) == EOF)
00113             return EOF;
00114     }
00115     return 0;
00116 }
00117 
00118 #if CONFIG_KFILE_GETS
00119 
00125 int kfile_gets(struct KFile *fd, char *buf, int size)
00126 {
00127     return kfile_gets_echo(fd, buf, size, false);
00128 }
00129 
00130 
00138 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
00139 {
00140     int i = 0;
00141     int c;
00142 
00143     for (;;)
00144     {
00145         if ((c = kfile_getc(fd)) == EOF)
00146         {
00147             buf[i] = '\0';
00148             return -1;
00149         }
00150 
00151         /* FIXME */
00152         if (c == '\r' || c == '\n' || i >= size-1)
00153         {
00154             buf[i] = '\0';
00155             if (echo)
00156                 kfile_print(fd, "\r\n");
00157             break;
00158         }
00159         buf[i++] = c;
00160         if (echo)
00161             kfile_putc(c, fd);
00162     }
00163 
00164     return i;
00165 }
00166 #endif /* !CONFIG_KFILE_GETS */
00167 
00168 
00175 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
00176 {
00177     uint32_t seek_pos;
00178 
00179     switch (whence)
00180     {
00181 
00182     case KSM_SEEK_SET:
00183         seek_pos = 0;
00184         break;
00185     case KSM_SEEK_END:
00186         seek_pos = fd->size;
00187         break;
00188     case KSM_SEEK_CUR:
00189         seek_pos = fd->seek_pos;
00190         break;
00191     default:
00192         ASSERT(0);
00193         return EOF;
00194         break;
00195     }
00196 
00197     /* Bound check */
00198     if (seek_pos + offset > fd->size)
00199     {
00200         ASSERT(0);
00201         return EOF;
00202     }
00203 
00204     fd->seek_pos = seek_pos + offset;
00205 
00206     return fd->seek_pos;
00207 }
00208 
00214 struct KFile * kfile_genericReopen(struct KFile *fd)
00215 {
00216     kfile_flush(fd);
00217     kfile_seek(fd, 0, KSM_SEEK_SET);
00218     return fd;
00219 }
00220 
00221