kfile.c
Go to the documentation of this file.00001
00041 #include "kfile.h"
00042
00043 #include "cfg/cfg_kfile.h"
00044 #include <cfg/debug.h>
00045 #include <cfg/log.h>
00046
00047 #include <mware/formatwr.h>
00048
00049 #include <string.h>
00050
00051
00052
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
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
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
00168
00169
00176 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
00177 {
00178 kfile_off_t seek_pos;
00179
00180 switch (whence)
00181 {
00182
00183 case KSM_SEEK_SET:
00184 seek_pos = 0;
00185 break;
00186 case KSM_SEEK_END:
00187 seek_pos = fd->size;
00188 break;
00189 case KSM_SEEK_CUR:
00190 seek_pos = fd->seek_pos;
00191 break;
00192 default:
00193 ASSERT(0);
00194 return EOF;
00195 break;
00196 }
00197
00198 #if LOG_LEVEL >= LOG_LVL_INFO
00199
00200 if (seek_pos + offset > fd->size)
00201 LOG_INFO("seek outside EOF\n");
00202 #endif
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
00225 int kfile_genericClose(UNUSED_ARG(struct KFile *, fd))
00226 {
00227 return 0;
00228 };
00229
00230