Simple RAM file system.

Dependents:   modem_ref_helper

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ram_fs.cpp Source File

ram_fs.cpp

00001 #include "ram_fs.h"
00002 #include "WizziDebug.h"
00003 
00004 #if 0
00005     #define RAM_FS_PRINT(...)  PRINT(__VA_ARGS__)
00006 #else
00007     #define RAM_FS_PRINT(...);
00008 #endif
00009 
00010 static ram_fs_file_t* g_fs = NULL;
00011 
00012 static void ram_fs_update(ram_fs_file_t** file, ram_fs_file_t* file_new)
00013 {
00014     if (*file == NULL)
00015     {
00016         // Append new file
00017         *file = file_new;
00018         return;
00019     }
00020     
00021     if ((*file)->file_id == file_new->file_id)
00022     {
00023         // Update existing file
00024         (*file)->header = file_new->header;
00025         (*file)->data = file_new->data;
00026         FREE(file_new);
00027         return;
00028     }
00029     
00030     ram_fs_update((ram_fs_file_t**)&((*file)->next), file_new);
00031 }
00032 
00033 static ram_fs_file_t* ram_fs_get(ram_fs_file_t* file, uint8_t fid)
00034 {
00035     if (file == NULL)
00036     {
00037         return NULL;
00038     }
00039     
00040     if (file->file_id == fid)
00041     {
00042         return file;
00043     }
00044     
00045     return ram_fs_get((ram_fs_file_t*)file->next, fid);
00046 }
00047 
00048 static bool ram_fs_remove(ram_fs_file_t** file, uint8_t fid)
00049 {
00050     if (*file == NULL)
00051     {
00052         // File does nor exist
00053         return true;
00054     }
00055     
00056     if ((*file)->file_id == fid)
00057     {
00058         ram_fs_file_t* file_old = *file;
00059         *file = (ram_fs_file_t*)file_old->next;
00060         FREE(file_old);
00061         return false;
00062     }
00063     
00064     return ram_fs_remove((ram_fs_file_t**)&((*file)->next), fid);
00065 }
00066 
00067 void ram_fs_new(uint8_t file_id, uint8_t* header, uint8_t* data)
00068 {
00069     RAM_FS_PRINT("NF(%d)\r\n", file_id);
00070     
00071     ram_fs_file_t* file_new = (ram_fs_file_t*)MALLOC(sizeof(ram_fs_file_t));
00072     
00073     file_new->file_id = file_id;
00074     file_new->header = header;
00075     file_new->data = data;
00076     file_new->next = NULL;
00077     
00078     ram_fs_update(&g_fs, file_new);
00079 }
00080 
00081 bool ram_fs_delete(uint8_t file_id)
00082 {
00083     RAM_FS_PRINT("DF(%d)\r\n", file_id);
00084     return ram_fs_remove(&g_fs, file_id);
00085 }
00086 
00087 bool ram_fs_write(uint8_t file_id, uint16_t offset, uint16_t size, uint8_t* content)
00088 {
00089     ram_fs_file_t* file;
00090         
00091     RAM_FS_PRINT("WF(%d)\r\n", file_id);
00092     
00093     // Retrieve pointer to file
00094     file = ram_fs_get(g_fs, file_id);
00095         
00096     if (file == NULL)
00097     {
00098         return true;
00099     }
00100     
00101     // Write the new data
00102     memcpy((void*)(file->data+offset), (void*)content, size);
00103 
00104     return false;
00105 }
00106 
00107 bool ram_fs_read(uint8_t file_id, uint16_t offset, uint16_t size, uint8_t* buf)
00108 {
00109     ram_fs_file_t* file;
00110         
00111     RAM_FS_PRINT("RF(%d)\r\n", file_id);
00112     
00113     // Retrieve pointer to file
00114     file = ram_fs_get(g_fs, file_id);
00115         
00116     if (file == NULL)
00117     {
00118         return true;
00119     }
00120     
00121     // Read data
00122     memcpy((void*)buf, (void*)(file->data+offset), size);
00123 
00124     return false;
00125 }
00126 
00127 void* ram_fs_get_header(uint8_t file_id)
00128 {
00129     ram_fs_file_t* file;
00130     
00131     RAM_FS_PRINT("RH(%d)\r\n", file_id);
00132     
00133     // Retrieve pointer to file
00134     file = ram_fs_get(g_fs, file_id);
00135         
00136     //ASSERT(file != NULL, "File %d does not exist!\r\n", file_id);
00137     
00138     if (file == NULL)
00139     {
00140         return NULL;
00141     }
00142     else
00143     {
00144         // Return header pointer
00145         return file->header;
00146     }
00147 }
00148 
00149 void* ram_fs_get_data(uint8_t file_id)
00150 {
00151     ram_fs_file_t* file;
00152     
00153     RAM_FS_PRINT("RH(%d)\r\n", file_id);
00154     
00155     // Retrieve pointer to file
00156     file = ram_fs_get(g_fs, file_id);
00157         
00158     //ASSERT(file != NULL, "File %d does not exist!\r\n", file_id);
00159     
00160     if (file == NULL)
00161     {
00162         return NULL;
00163     }
00164     else
00165     {
00166         // Return data pointer
00167         return file->data;
00168     }
00169 }