SAM4S-EK_FreeRTOS+FAT-SL  1.0
An example project to test the functionality of FreeRTOS+FAT-SL using SD card as data storage medium
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
util_sfn.c
Go to the documentation of this file.
1 /*
2  * FreeRTOS+FAT SL V1.0.1 (C) 2014 HCC Embedded
3  *
4  * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license
5  * terms.
6  *
7  * FreeRTOS+FAT SL uses a dual license model that allows the software to be used
8  * under a standard GPL open source license, or a commercial license. The
9  * standard GPL license (unlike the modified GPL license under which FreeRTOS
10  * itself is distributed) requires that all software statically linked with
11  * FreeRTOS+FAT SL is also distributed under the same GPL V2 license terms.
12  * Details of both license options follow:
13  *
14  * - Open source licensing -
15  * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and
16  * distributed without charge provided the user adheres to version two of the
17  * GNU General Public License (GPL) and does not remove the copyright notice or
18  * this text. The GPL V2 text is available on the gnu.org web site, and on the
19  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
20  *
21  * - Commercial licensing -
22  * Businesses and individuals who for commercial or other reasons cannot comply
23  * with the terms of the GPL V2 license must obtain a commercial license before
24  * incorporating FreeRTOS+FAT SL into proprietary software for distribution in
25  * any form. Commercial licenses can be purchased from
26  * http://shop.freertos.org/fat_sl and do not require any source files to be
27  * changed.
28  *
29  * FreeRTOS+FAT SL is distributed in the hope that it will be useful. You
30  * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as
31  * is'. FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the
32  * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A
33  * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all
34  * conditions and terms, be they implied, expressed, or statutory.
35  *
36  * http://www.FreeRTOS.org
37  * http://www.FreeRTOS.org/FreeRTOS-Plus
38  *
39  */
40 
41 #include "../../api/fat_sl.h"
42 
43 #include "util.h"
44 
45 #include "../../version/ver_fat_sl.h"
46 #if VER_FAT_SL_MAJOR != 5 || VER_FAT_SL_MINOR != 2
47  #error Incompatible FAT_SL version number!
48 #endif
49 
50 
51 
52 /****************************************************************************
53  *
54  * _f_checknameprim
55  *
56  * checking a string if could be valid
57  *
58  * INPUTS
59  *
60  * ptr - pointer to name or extension
61  * len - number max char of name or extension
62  *
63  * RETURNS
64  *
65  ***************************************************************************/
66 static unsigned char _f_checknameprim ( char * ptr, unsigned char len )
67 {
68  unsigned char inspace = 0;
69 
70  while ( len-- )
71  {
72  char ch = *ptr++;
73  if ( !inspace )
74  {
75  if ( ch == ' ' )
76  {
77  inspace = 1;
78  }
79 
80  if ( ( ch == '|' ) || ( ch == '[' ) || ( ch == ']' ) || ( ch == '<' ) || ( ch == '>' ) || ( ch == '/' ) || ( ch == '\\' ) || ( ch == ':' ) )
81  {
82  return 1;
83  }
84  }
85  else if ( ch != ' ' )
86  {
87  return 1; /*no inspace allowed*/
88  }
89  }
90 
91  return 0;
92 } /* _f_checknameprim */
93 
94 
95 /****************************************************************************
96  *
97  * _f_checkname
98  *
99  * checking filename and extension for special characters
100  *
101  * INPUTS
102  *
103  * name - filename (e.g.: filename)
104  * ext - extension of file (e.g.: txt)
105  *
106  * RETURNS
107  *
108  * 0 - if no contains invalid character
109  * other - if contains any invalid character
110  *
111  ***************************************************************************/
112 unsigned char _f_checkname ( char * name, char * ext )
113 {
114  if ( _f_checknameprim( name, F_MAXNAME ) )
115  {
116  return 1;
117  }
118 
119  if ( _f_checknameprim( ext, F_MAXEXT ) )
120  {
121  return 1;
122  }
123 
124  return 0;
125 }
126 
127 
128 /****************************************************************************
129  *
130  * _f_checknamewc
131  *
132  * checking filename and extension for wildcard character
133  *
134  * INPUTS
135  *
136  * name - filename (e.g.: filename)
137  * ext - extension of file (e.g.: txt)
138  *
139  * RETURNS
140  *
141  * 0 - if no contains wildcard character (? or *)
142  * other - if contains any wildcard character
143  *
144  ***************************************************************************/
145 unsigned char _f_checknamewc ( const char * name, const char * ext )
146 {
147  unsigned char a = 0;
148 
149  for ( a = 0 ; a < F_MAXNAME ; a++ )
150  {
151  char ch = name[a];
152  if ( ( ch == '?' ) || ( ch == '*' ) )
153  {
154  return 1;
155  }
156  }
157 
158  for ( a = 0 ; a < F_MAXEXT ; a++ )
159  {
160  char ch = ext[a];
161  if ( ( ch == '?' ) || ( ch == '*' ) )
162  {
163  return 1;
164  }
165  }
166 
167  return _f_checkname( (char *)name, (char *)ext );
168 } /* _f_checknamewc */
169 
170 
171 
172 
173 /****************************************************************************
174  *
175  * _f_setnameext
176  *
177  * convert a string into filename and extension separatelly, the terminator
178  * character could be zero char, '/' or '\'
179  *
180  * INPUTS
181  *
182  * s - source string (e.g.: hello.txt)
183  * name - where to store name (this array size has to be F_MAXNAME (8))
184  * ext - where to store extension (this array size has to be F_MAXEXT (3))
185  *
186  * RETURNS
187  *
188  * length of the used bytes from source string array
189  *
190  ***************************************************************************/
191 unsigned char _f_setnameext ( char * s, char * name, char * ext )
192 {
193  unsigned char len, extlen = 0;
194  unsigned char a;
195  unsigned char setext = 1;
196 
197  for ( len = 0 ; ; )
198  {
199  unsigned char ch = s[len];
200  if ( ( ch == 0 ) || ( ch == '\\' ) || ( ch == '/' ) )
201  {
202  break;
203  }
204 
205  len++; /*calculate len*/
206  }
207 
208  if ( len && ( s[0] == '.' ) )
209  {
210 /* if (len==1 || (s[1]=='.' && len==2)) goto dots; */
211  if ( ( len == 1 ) || ( s[1] == '.' ) )
212  {
213  goto dots;
214  }
215  }
216 
217  for ( a = len ; a ; a-- )
218  {
219  if ( s[a - 1] == '.' )
220  {
221  unsigned char b;
222 
223  extlen = (unsigned char)( len - a + 1 );
224  len = (unsigned char)( a - 1 );
225 
226  for ( b = 0 ; b < F_MAXEXT ; b++ )
227  {
228  if ( b < extlen - 1 )
229  {
230  ext[b] = _f_toupper( s[a++] );
231  }
232  else
233  {
234  ext[b] = ' ';
235  }
236  }
237 
238  setext = 0;
239  break;
240  }
241  }
242 
243 dots:
244  if ( setext )
245  {
246  for ( a = 0 ; a < F_MAXEXT ; a++ )
247  {
248  ext[a] = ' ';
249  }
250  }
251 
252  for ( a = 0 ; a < F_MAXNAME ; a++ )
253  {
254  if ( a < len )
255  {
256  name[a] = _f_toupper( s[a] );
257  }
258  else
259  {
260  name[a] = ' ';
261  }
262  }
263 
264  return (unsigned char)( len + extlen );
265 } /* _f_setnameext */
266 
267 
268 
269 /****************************************************************************
270  *
271  * _f_setfsname
272  *
273  * convert a single string into F_NAME structure
274  *
275  * INPUTS
276  *
277  * name - combined name with drive,path,filename,extension used for source
278  * fsname - where to fill this structure with separated drive,path,name,ext
279  *
280  * RETURNS
281  *
282  * 0 - if successfully
283  * other - if name contains invalid path or name
284  *
285  ***************************************************************************/
286 unsigned char _f_setfsname ( const char * name, F_NAME * fsname )
287 {
288  char s[F_MAXPATH];
289  unsigned char namepos = 0;
290 
291  unsigned char pathpos = 0;
292  unsigned char a;
293 
294  s[0] = 0;
295 
296  if ( !name[0] )
297  {
298  return 1; /*no name*/
299  }
300 
301  if ( name[1] == ':' )
302  {
303  name += 2;
304  }
305 
306  if ( ( name[0] != '/' ) && ( name[0] != '\\' ) )
307  {
308  if ( fn_getcwd( fsname->path, F_MAXPATH, 0 ) )
309  {
310  return 1; /*error*/
311  }
312 
313  for ( pathpos = 0 ; fsname->path[pathpos] ; )
314  {
315  pathpos++;
316  }
317  }
318 
319 
320  for ( ; ; )
321  {
322  char ch = _f_toupper( *name++ );
323 
324  if ( !ch )
325  {
326  break;
327  }
328 
329  if ( ch == ':' )
330  {
331  return 1; /*not allowed*/
332  }
333 
334  if ( ( ch == '/' ) || ( ch == '\\' ) )
335  {
336  if ( pathpos )
337  {
338  if ( fsname->path[pathpos - 1] == '/' )
339  {
340  return 1; /*not allowed double */
341  }
342 
343  if ( pathpos >= F_MAXPATH - 2 )
344  {
345  return 1; /*path too long*/
346  }
347 
348  fsname->path[pathpos++] = '/';
349  }
350 
351  for ( ; namepos ; )
352  {
353  if ( s[namepos - 1] != ' ' )
354  {
355  break;
356  }
357 
358  namepos--; /*remove end spaces*/
359  }
360 
361  for ( a = 0 ; a < namepos ; a++ )
362  {
363  if ( pathpos >= F_MAXPATH - 2 )
364  {
365  return 1; /*path too long*/
366  }
367 
368  fsname->path[pathpos++] = s[a];
369  }
370 
371  namepos = 0;
372  continue;
373  }
374 
375  if ( ( ch == ' ' ) && ( !namepos ) )
376  {
377  continue; /*remove start spaces*/
378  }
379 
380  if ( namepos >= ( sizeof( s ) - 2 ) )
381  {
382  return 1; /*name too long*/
383  }
384 
385  s[namepos++] = ch;
386  }
387 
388  s[namepos] = 0; /*terminates it*/
389  fsname->path[pathpos] = 0; /*terminates it*/
390 
391  for ( ; namepos ; )
392  {
393  if ( s[namepos - 1] != ' ' )
394  {
395  break;
396  }
397 
398  s[namepos - 1] = 0; /*remove end spaces*/
399  namepos--;
400  }
401 
402  if ( !_f_setnameext( s, fsname->filename, fsname->fileext ) )
403  {
404  return 2; /*no name*/
405  }
406 
407  if ( fsname->filename[0] == ' ' )
408  {
409  return 1; /*cannot be*/
410  }
411 
412  return 0;
413 } /* _f_setfsname */
414 
415 
416 /****************************************************************************
417  *
418  * _f_createfullname
419  *
420  * create full name
421  *
422  * INPUTS
423  *
424  * buffer - where to create
425  * buffersize - size of the buffer
426  * drivenum - drive number
427  * path - path of the file
428  * filename - file name
429  * fileext - file extension
430  *
431  * RETURNS
432  *
433  * 1 - if found and osize is filled
434  * 0 - not found
435  *
436  ***************************************************************************/
437 int _f_createfullname ( char * buffer, int buffersize, char * path, char * filename, char * fileext )
438 {
439  char * fullname = buffer;
440  int a;
441 
442  /* adding drive letter */
443  if ( buffersize < 1 )
444  {
445  return 1;
446  }
447 
448  *fullname++ = '/';
449  buffersize -= 1;
450 
451  /* adding path */
452  if ( path[0] )
453  {
454  for ( ; ; )
455  {
456  char ch = *path++;
457 
458  if ( !ch )
459  {
460  break;
461  }
462 
463  if ( buffersize <= 0 )
464  {
465  return 1;
466  }
467 
468  *fullname++ = ch;
469  buffersize--;
470  }
471 
472  /* adding separator */
473  if ( buffersize <= 0 )
474  {
475  return 1;
476  }
477 
478  *fullname++ = '/';
479  }
480 
481  /* adding name */
482  for ( a = 0 ; a < F_MAXNAME ; a++ )
483  {
484  char ch = *filename++;
485 
486  if ( ( !ch ) || ( ch == 32 ) )
487  {
488  break;
489  }
490 
491  if ( buffersize <= 0 )
492  {
493  return 1;
494  }
495 
496  *fullname++ = ch;
497  buffersize--;
498  }
499 
500  /* adding ext*/
501  if ( fileext[0] && ( fileext[0] != 32 ) )
502  {
503  /* adding dot */
504  if ( !buffersize )
505  {
506  return 1;
507  }
508 
509  *fullname++ = '.';
510 
511  for ( a = 0 ; a < F_MAXEXT ; a++ )
512  {
513  char ch = *fileext++;
514 
515  if ( ( !ch ) || ( ch == 32 ) )
516  {
517  break;
518  }
519 
520  if ( buffersize <= 0 )
521  {
522  return 1;
523  }
524 
525  *fullname++ = ch;
526  buffersize--;
527  }
528  }
529 
530  /* adding terminator */
531  if ( buffersize <= 0 )
532  {
533  return 1;
534  }
535 
536  *fullname++ = 0;
537 
538  return 0;
539 } /* _f_createfullname */
540 
541 
542 
unsigned char fn_getcwd(char *buffer, unsigned char maxlen, char root)
Definition: dir.c:513
Definition: fat_sl.h:58
unsigned char _f_setfsname(const char *name, F_NAME *fsname)
Definition: util_sfn.c:286
#define F_MAXNAME
Definition: fat_sl.h:55
#define F_MAXPATH
Definition: config_fat_sl.h:59
char _f_toupper(char)
Definition: util.c:272
unsigned char _f_setnameext(char *s, char *name, char *ext)
Definition: util_sfn.c:191
unsigned char _f_checknamewc(const char *name, const char *ext)
Definition: util_sfn.c:145
char path[F_MAXPATH]
Definition: fat_sl.h:60
char filename[F_MAXNAME]
Definition: fat_sl.h:61
int _f_createfullname(char *buffer, int buffersize, char *path, char *filename, char *fileext)
Definition: util_sfn.c:437
#define F_MAXEXT
Definition: fat_sl.h:56
char fileext[F_MAXEXT]
Definition: fat_sl.h:62
unsigned char _f_checkname(char *name, char *ext)
Definition: util_sfn.c:112