MHEG5  18.9.0
MHEG5 Documentation
mh5storage.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  * Copyright © 2000 Koninklijke Philips Electronics N.V
5  *
6  * This file is part of a DTVKit Software Component
7  * You are permitted to copy, modify or distribute this file subject to the terms
8  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
9  *
10  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
11  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * If you or your organisation is not a member of DTVKit then you have access
15  * to this source code outside of the terms of the licence agreement
16  * and you are expected to delete this and any associated files immediately.
17  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
18  *******************************************************************************/
46 /*---includes for this file--------------------------------------------------*/
47 #include "mh5storage.h"
48 #include "mh5memory.h"
49 #include "mh5debug.h"
50 #include "mh5profile.h"
51 
52 /*---constant definitions for this file--------------------------------------*/
53 #define MAXFILES 32
54 
55 
56 /*---local typedef structs for this file-------------------------------------*/
57 typedef struct sRamFile
58 {
62  struct sRamFile *next, *prev;
63 } MHEG5RamFile;
64 
65 /*---local (static) variable declarations for this file----------------------*/
66 static MHEG5RamFile file_list[MAXFILES];
67 static MHEG5RamFile *file_list_top;
68 
69 /*---local function definitions----------------------------------------------*/
70 
71 /*---global function definitions---------------------------------------------*/
72 
73 
78 static MHEG5Int memUsed(void)
79 {
80  MHEG5Int i, rc = 0;
81 
82  /* Add all data lengths. */
83  for (i = 0; i < MAXFILES; i++)
84  {
85  rc += file_list[i].data.len;
86  }
87 
88  /* Return total size in bytes. */
89  return rc;
90 }
91 
96 static MHEG5Int numUsed(void)
97 {
98  MHEG5Int i, rc = 0;
99 
100  /* Add all data lengths. */
101  for (i = 0; i < MAXFILES; i++)
102  {
103  rc += (file_list[i].free == MHEG5FALSE);
104  }
105 
106  /* Return total number of slots in use. */
107  return rc;
108 }
109 
115 void MHEG5InitStore(void)
116 {
117  MHEG5Int i;
118 
119  file_list_top = 0;
120  for (i = 0; i != MAXFILES; i++)
121  {
122  file_list[i].data.len = 0;
123  file_list[i].name.len = 0;
124  file_list[i].next = 0;
125  file_list[i].prev = 0;
126  file_list[i].free = MHEG5TRUE;
127  }
128 }
129 
135 void MHEG5ResetStore(void)
136 {
137  /* Local variable. */
138  MHEG5Int i;
139 
140  /* Check initialised. */
141  for (i = 0; i != MAXFILES; i++)
142  {
143  if (!file_list[i].free)
144  {
145  MHEG5stringDestruct( &file_list[i].name );
146  MHEG5stringDestruct( &file_list[i].data );
147  file_list[i].name.len = 0;
148  file_list[i].next = 0;
149  file_list[i].prev = 0;
150  file_list[i].free = MHEG5TRUE;
151  }
152  }
153  file_list_top = 0;
154 }
155 
165 {
166  /* Get top of file list. */
167  MHEG5RamFile *temp_file;
168  MHEG5Int i, count;
169 
170  INFO_PRINT(("BEGIN: MHEG5storageWrite - File [%.*s], Length [0x%x].\n", fn.len, fn.data, len));
171 
172  /* Check that input file not bigger than total persistent storage unit. */
173  if (len > MHEG5STORAGE_MEMSIZE)
174  {
175  DEBUG_PRINT(("ERROR: MHEG5storageWrite - File [%.*s] too big [0x%x].\n", fn.len, fn.data, len));
176  return MHEG5FALSE;
177  }
178 
179  /* Find duplicate file names, and remove. */
180  for (i = 0; i != MAXFILES; i++)
181  {
182  /* Check if file has same name. */
183  if (MHEG5stringEqual(&file_list[i].name, &fn))
184  {
185  INFO_PRINT(("WARNING: MHEG5storageWrite - Found duplicate file name [%.*s] in list.\n", fn.len, fn.data));
186  /* Remove file from list. */
187  MHEG5stringDestruct(&file_list[i].name);
188  MHEG5stringDestruct(&file_list[i].data);
189  file_list[i].free = MHEG5TRUE;
190 
191  /* Remove file from chain. */
192  if (file_list[i].prev != 0)
193  {
194  file_list[i].prev->next = file_list[i].next;
195  }
196 
197  if (file_list[i].next != 0)
198  {
199  file_list[i].next->prev = file_list[i].prev;
200  }
201 
202  if (file_list_top == &file_list[i])
203  {
204  file_list_top = file_list[i].next;
205  }
206 
207  file_list[i].prev = 0;
208  file_list[i].next = 0;
209  }
210  }
211 
212  count = numUsed();
213 
214  /* Check enough space in list for new file. */
215  while ((memUsed() + len > MHEG5STORAGE_MEMSIZE || count == MAXFILES) &&
216  (file_list_top != 0))
217  {
218  INFO_PRINT(("WARNING: MHEG5storageWrite - Not enough space in list, deleting files.\n"));
219  temp_file = file_list_top;
220  if (temp_file == 0)
221  {
222  DEBUG_PRINT(("ERROR: MHEG5storageWrite - Cannot remove from empty list.\n"));
223  return MHEG5FALSE;
224  }
225 
226  while (temp_file->next != 0)
227  {
228  temp_file = temp_file->next;
229  }
230 
231  /* Remove file from list. */
232  MHEG5stringDestruct(&temp_file->name);
233  MHEG5stringDestruct(&temp_file->data);
234  temp_file->free = MHEG5TRUE;
235  count--;
236 
237  if (temp_file->prev != 0)
238  {
239  temp_file->prev->next = temp_file->next;
240  }
241 
242  if (file_list_top == temp_file)
243  {
244  file_list_top = 0;
245  }
246 
247  temp_file->next = 0;
248  temp_file->prev = 0;
249  }
250 
251  /* Find free file list item. */
252  i = 0;
253  while (i < MAXFILES && file_list[i].free != MHEG5TRUE)
254  {
255  i++;
256  }
257 
258  if (i == MAXFILES)
259  {
260  DEBUG_PRINT(("ERROR: MHEG5storageWrite - No empty slot in list.\n"));
261  return MHEG5FALSE;
262  }
263 
264  /* Update file details. */
265  file_list[i].data.data = STR_DataAlloc( len );
266  if (!file_list[i].data.data)
267  {
268  DEBUG_PRINT(("ERROR: MHEG5storageWrite - Unable to allocate memory for file.\n"));
269  return MHEG5FALSE;
270  }
271  file_list[i].name = MHEG5stringCopy(fn);
272  file_list[i].data.len = len;
273  file_list[i].free = MHEG5FALSE;
274  memcpy(file_list[i].data.data, buf, len);
275 
276  /* Add file to list. */
277  file_list[i].next = file_list_top;
278  if (file_list[i].next != 0)
279  {
280  file_list[i].next->prev = &file_list[i];
281  }
282  file_list[i].prev = 0;
283  file_list_top = &file_list[i];
284 
285  INFO_PRINT(("SUCCESS: MHEG5storageWrite - File [%.*s] added to list.\n", fn.len, fn.data));
286  return MHEG5TRUE;
287 }
288 
298 {
299  MHEG5Int i;
300  INFO_PRINT(("BEGIN: MHEG5storageRead - File [%.*s].\n", fn.len, fn.data));
301 
302  if (buf == 0 || len == 0)
303  {
304  DEBUG_PRINT(("ERROR: MHEG5storageRead - Bad data variable pointer passed.\n"));
305  return MHEG5FALSE;
306  }
307  for (i = 0; i < MAXFILES; i++)
308  {
309  if (MHEG5stringEqual(&file_list[i].name, &fn))
310  {
311  *buf = file_list[i].data.data;
312  *len = file_list[i].data.len;
313 
314  INFO_PRINT(("SUCCESS: MHEG5storageRead - Found file [%.*s] in list.\n", fn.len, fn.data));
315  return MHEG5TRUE;
316  }
317  }
318  INFO_PRINT(("WARNING: MHEG5storageRead - Unable to find file [%.*s] in list.\n", fn.len, fn.data));
319  return MHEG5FALSE;
320 }
321 
MHEG5Bool MHEG5storageWrite(MHEG5String fn, void *buf, MHEG5Int len)
Write a file to the persistent store.
Definition: mh5storage.c:164
MHEG5Bool MHEG5stringEqual(MHEG5String *s1, MHEG5String *s2)
Compare two Strings (case sensitive!)
Definition: mh5base.c:710
#define MHEG5STORAGE_MEMSIZE
Definition: mh5profile.h:141
#define INFO_PRINT(x)
Definition: mh5debug.h:69
unsigned char * STR_DataAlloc(unsigned int size)
Definition: glue_memory.c:596
MHEG5String name
Definition: mh5storage.c:59
#define MAXFILES
Definition: mh5storage.c:53
MHEG5String MHEG5stringCopy(MHEG5String source)
<Function description>="">
Definition: mh5base.c:574
void MHEG5InitStore(void)
Initialise Persistent Storage.
Definition: mh5storage.c:115
struct sRamFile * prev
Definition: mh5storage.c:62
MHEG5Bool free
Definition: mh5storage.c:61
void MHEG5stringDestruct(MHEG5String *item)
Destruct a MHEG5String.
Definition: mh5base.c:686
long MHEG5Int
Definition: mh5base.h:73
void MHEG5ResetStore(void)
destroy Persistent Storage - only really useful on platform like WINDOWS
Definition: mh5storage.c:135
MHEG5Bool MHEG5storageRead(MHEG5String fn, void **buf, MHEG5Int *len)
Read a file from the persistent store.
Definition: mh5storage.c:297
This file defines the profile for the MHEG engine.
short MHEG5Bool
Definition: mh5base.h:71
MHEG5Byte * data
Definition: mh5base.h:85
int len
Definition: mh5gate.c:57
#define MHEG5TRUE
Definition: mh5base.h:49
struct sRamFile * next
Definition: mh5storage.c:62
Mheg5 logging and debug printing.
Persistent storage module. The engine provides a persistent storage for 1024 bytes of data...
redirection include
MHEG5Int len
Definition: mh5base.h:84
struct sRamFile MHEG5RamFile
MHEG5String data
Definition: mh5storage.c:60
#define MHEG5FALSE
Definition: mh5base.h:48
#define DEBUG_PRINT(x)
Definition: mh5debug.h:70