Simple RAM file system.

Dependents:   modem_ref_helper

ram_fs.cpp

Committer:
Jeej
Date:
21 months ago
Revision:
5:d2b8d685f86f
Parent:
4:58ee8b15988b

File content as of revision 5:d2b8d685f86f:

#include "ram_fs.h"
#include "WizziDebug.h"

#if 0
    #define RAM_FS_PRINT(...)  PRINT(__VA_ARGS__)
#else
    #define RAM_FS_PRINT(...);
#endif

static ram_fs_file_t* g_fs = NULL;

static void ram_fs_update(ram_fs_file_t** file, ram_fs_file_t* file_new)
{
    if (*file == NULL)
    {
        // Append new file
        *file = file_new;
        return;
    }
    
    if ((*file)->file_id == file_new->file_id)
    {
        // Update existing file
        (*file)->header = file_new->header;
        (*file)->data = file_new->data;
        FREE(file_new);
        return;
    }
    
    ram_fs_update((ram_fs_file_t**)&((*file)->next), file_new);
}

static ram_fs_file_t* ram_fs_get(ram_fs_file_t* file, uint8_t fid)
{
    if (file == NULL)
    {
        return NULL;
    }
    
    if (file->file_id == fid)
    {
        return file;
    }
    
    return ram_fs_get((ram_fs_file_t*)file->next, fid);
}

static bool ram_fs_remove(ram_fs_file_t** file, uint8_t fid)
{
    if (*file == NULL)
    {
        // File does nor exist
        return true;
    }
    
    if ((*file)->file_id == fid)
    {
        ram_fs_file_t* file_old = *file;
        *file = (ram_fs_file_t*)file_old->next;
        FREE(file_old);
        return false;
    }
    
    return ram_fs_remove((ram_fs_file_t**)&((*file)->next), fid);
}

void ram_fs_new(uint8_t file_id, uint8_t* header, uint8_t* data)
{
    RAM_FS_PRINT("NF(%d)\r\n", file_id);
    
    ram_fs_file_t* file_new = (ram_fs_file_t*)MALLOC(sizeof(ram_fs_file_t));
    
    file_new->file_id = file_id;
    file_new->header = header;
    file_new->data = data;
    file_new->next = NULL;
    
    ram_fs_update(&g_fs, file_new);
}

bool ram_fs_delete(uint8_t file_id)
{
    RAM_FS_PRINT("DF(%d)\r\n", file_id);
    return ram_fs_remove(&g_fs, file_id);
}

bool ram_fs_write(uint8_t file_id, uint16_t offset, uint16_t size, uint8_t* content)
{
    ram_fs_file_t* file;
        
    RAM_FS_PRINT("WF(%d)\r\n", file_id);
    
    // Retrieve pointer to file
    file = ram_fs_get(g_fs, file_id);
        
    if (file == NULL)
    {
        return true;
    }
    
    // Write the new data
    memcpy((void*)(file->data+offset), (void*)content, size);

    return false;
}

bool ram_fs_read(uint8_t file_id, uint16_t offset, uint16_t size, uint8_t* buf)
{
    ram_fs_file_t* file;
        
    RAM_FS_PRINT("RF(%d)\r\n", file_id);
    
    // Retrieve pointer to file
    file = ram_fs_get(g_fs, file_id);
        
    if (file == NULL)
    {
        return true;
    }
    
    // Read data
    memcpy((void*)buf, (void*)(file->data+offset), size);

    return false;
}

void* ram_fs_get_header(uint8_t file_id)
{
    ram_fs_file_t* file;
    
    RAM_FS_PRINT("RH(%d)\r\n", file_id);
    
    // Retrieve pointer to file
    file = ram_fs_get(g_fs, file_id);
        
    //ASSERT(file != NULL, "File %d does not exist!\r\n", file_id);
    
    if (file == NULL)
    {
        return NULL;
    }
    else
    {
        // Return header pointer
        return file->header;
    }
}

void* ram_fs_get_data(uint8_t file_id)
{
    ram_fs_file_t* file;
    
    RAM_FS_PRINT("RH(%d)\r\n", file_id);
    
    // Retrieve pointer to file
    file = ram_fs_get(g_fs, file_id);
        
    //ASSERT(file != NULL, "File %d does not exist!\r\n", file_id);
    
    if (file == NULL)
    {
        return NULL;
    }
    else
    {
        // Return data pointer
        return file->data;
    }
}