Quantum Leaps / Mbed 2 deprecated qp_lwip

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fs.c Source File

fs.c

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00019  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00021  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00022  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00024  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  *
00029  * Author: Adam Dunkels <adam@sics.se>
00030  */
00031 
00032 #include <string.h>
00033 #include "fs.h"                                  /* file system abstraction */
00034 
00035 struct fsdata_file {
00036     struct fsdata_file const *next;
00037     unsigned char const *name;
00038     unsigned char const *data;
00039     int len;
00040 };
00041 
00042 #include "fsdata.h"        /* file-system data generated by qfsdata utility */
00043 
00044 /*--------------------------------------------------------------------------*/
00045 /* Define the number of open files that we can support. */
00046 #ifndef LWIP_MAX_OPEN_FILES
00047 #define LWIP_MAX_OPEN_FILES     10
00048 #endif
00049 
00050 /* Define the file system memory allocation structure. */
00051 struct fs_table {
00052     struct fs_file file;
00053     int inuse;
00054 };
00055 
00056 /* Allocate file system memory */
00057 struct fs_table fs_memory[LWIP_MAX_OPEN_FILES];
00058 
00059 /*--------------------------------------------------------------------------*/
00060 static struct fs_file *fs_malloc(void) {
00061     int i;
00062     for (i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
00063         if (fs_memory[i].inuse == 0) {
00064             fs_memory[i].inuse = 1;
00065             return (&fs_memory[i].file);
00066         }
00067     }
00068     return (NULL);
00069 }
00070 
00071 /*--------------------------------------------------------------------------*/
00072 static void fs_free(struct fs_file *file) {
00073     int i;
00074     for (i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
00075         if (&fs_memory[i].file == file) {
00076             fs_memory[i].inuse = 0;
00077             break;
00078         }
00079     }
00080     return;
00081 }
00082 
00083 /*--------------------------------------------------------------------------*/
00084 struct fs_file *fs_open(char const *name) {
00085     struct fs_file *file;
00086     const struct fsdata_file *f;
00087 
00088     file = fs_malloc();
00089     if (file == NULL) {
00090         return NULL;
00091     }
00092 
00093     for (f = FS_ROOT; f != NULL; f = f->next) {
00094         if (!strcmp(name, (char *)f->name)) {
00095             file->data = (char *)f->data;
00096             file->len = f->len;
00097             file->index = f->len;
00098             file->pextension = NULL;
00099             return file;
00100         }
00101     }
00102     fs_free(file);
00103     return NULL;
00104 }
00105 
00106 /*--------------------------------------------------------------------------*/
00107 void fs_close(struct fs_file *file) {
00108     fs_free(file);
00109 }
00110 /*--------------------------------------------------------------------------*/
00111 int fs_read(struct fs_file *file, char *buffer, int count) {
00112     int read;
00113 
00114     if (file->index == file->len) {
00115         return -1;
00116     }
00117 
00118     read = file->len - file->index;
00119     if (read > count) {
00120         read = count;
00121     }
00122 
00123     memcpy(buffer, (file->data + file->index), read);
00124     file->index += read;
00125 
00126     return (read);
00127 }