2 * FreeRTOS+FAT SL V1.0.1 (C) 2014 HCC Embedded
\r
4 * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license
\r
7 * FreeRTOS+FAT SL uses a dual license model that allows the software to be used
\r
8 * under a standard GPL open source license, or a commercial license. The
\r
9 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
10 * itself is distributed) requires that all software statically linked with
\r
11 * FreeRTOS+FAT SL is also distributed under the same GPL V2 license terms.
\r
12 * Details of both license options follow:
\r
14 * - Open source licensing -
\r
15 * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and
\r
16 * distributed without charge provided the user adheres to version two of the
\r
17 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
18 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
19 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
21 * - Commercial licensing -
\r
22 * Businesses and individuals who for commercial or other reasons cannot comply
\r
23 * with the terms of the GPL V2 license must obtain a commercial license before
\r
24 * incorporating FreeRTOS+FAT SL into proprietary software for distribution in
\r
25 * any form. Commercial licenses can be purchased from
\r
26 * http://shop.freertos.org/fat_sl and do not require any source files to be
\r
29 * FreeRTOS+FAT SL is distributed in the hope that it will be useful. You
\r
30 * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as
\r
31 * is'. FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the
\r
32 * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A
\r
33 * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all
\r
34 * conditions and terms, be they implied, expressed, or statutory.
\r
36 * http://www.FreeRTOS.org
\r
37 * http://www.FreeRTOS.org/FreeRTOS-Plus
\r
41 #include "../../api/fat_sl.h"
\r
45 #include "../../version/ver_fat_sl.h"
\r
46 #if VER_FAT_SL_MAJOR != 5 || VER_FAT_SL_MINOR != 2
\r
47 #error Incompatible FAT_SL version number!
\r
52 /****************************************************************************
\r
56 * checking a string if could be valid
\r
60 * ptr - pointer to name or extension
\r
61 * len - number max char of name or extension
\r
65 ***************************************************************************/
\r
66 static unsigned char _f_checknameprim ( char * ptr, unsigned char len )
\r
68 unsigned char inspace = 0;
\r
80 if ( ( ch == '|' ) || ( ch == '[' ) || ( ch == ']' ) || ( ch == '<' ) || ( ch == '>' ) || ( ch == '/' ) || ( ch == '\\' ) || ( ch == ':' ) )
\r
85 else if ( ch != ' ' )
\r
87 return 1; /*no inspace allowed*/
\r
92 } /* _f_checknameprim */
\r
95 /****************************************************************************
\r
99 * checking filename and extension for special characters
\r
103 * name - filename (e.g.: filename)
\r
104 * ext - extension of file (e.g.: txt)
\r
108 * 0 - if no contains invalid character
\r
109 * other - if contains any invalid character
\r
111 ***************************************************************************/
\r
112 unsigned char _f_checkname ( char * name, char * ext )
\r
114 if ( _f_checknameprim( name, F_MAXNAME ) )
\r
119 if ( _f_checknameprim( ext, F_MAXEXT ) )
\r
128 /****************************************************************************
\r
132 * checking filename and extension for wildcard character
\r
136 * name - filename (e.g.: filename)
\r
137 * ext - extension of file (e.g.: txt)
\r
141 * 0 - if no contains wildcard character (? or *)
\r
142 * other - if contains any wildcard character
\r
144 ***************************************************************************/
\r
145 unsigned char _f_checknamewc ( const char * name, const char * ext )
\r
147 unsigned char a = 0;
\r
149 for ( a = 0 ; a < F_MAXNAME ; a++ )
\r
152 if ( ( ch == '?' ) || ( ch == '*' ) )
\r
158 for ( a = 0 ; a < F_MAXEXT ; a++ )
\r
161 if ( ( ch == '?' ) || ( ch == '*' ) )
\r
167 return _f_checkname( (char *)name, (char *)ext );
\r
168 } /* _f_checknamewc */
\r
173 /****************************************************************************
\r
177 * convert a string into filename and extension separatelly, the terminator
\r
178 * character could be zero char, '/' or '\'
\r
182 * s - source string (e.g.: hello.txt)
\r
183 * name - where to store name (this array size has to be F_MAXNAME (8))
\r
184 * ext - where to store extension (this array size has to be F_MAXEXT (3))
\r
188 * length of the used bytes from source string array
\r
190 ***************************************************************************/
\r
191 unsigned char _f_setnameext ( char * s, char * name, char * ext )
\r
193 unsigned char len, extlen = 0;
\r
195 unsigned char setext = 1;
\r
197 for ( len = 0 ; ; )
\r
199 unsigned char ch = s[len];
\r
200 if ( ( ch == 0 ) || ( ch == '\\' ) || ( ch == '/' ) )
\r
205 len++; /*calculate len*/
\r
208 if ( len && ( s[0] == '.' ) )
\r
210 /* if (len==1 || (s[1]=='.' && len==2)) goto dots; */
\r
211 if ( ( len == 1 ) || ( s[1] == '.' ) )
\r
217 for ( a = len ; a ; a-- )
\r
219 if ( s[a - 1] == '.' )
\r
223 extlen = (unsigned char)( len - a + 1 );
\r
224 len = (unsigned char)( a - 1 );
\r
226 for ( b = 0 ; b < F_MAXEXT ; b++ )
\r
228 if ( b < extlen - 1 )
\r
230 ext[b] = _f_toupper( s[a++] );
\r
246 for ( a = 0 ; a < F_MAXEXT ; a++ )
\r
252 for ( a = 0 ; a < F_MAXNAME ; a++ )
\r
256 name[a] = _f_toupper( s[a] );
\r
264 return (unsigned char)( len + extlen );
\r
265 } /* _f_setnameext */
\r
269 /****************************************************************************
\r
273 * convert a single string into F_NAME structure
\r
277 * name - combined name with drive,path,filename,extension used for source
\r
278 * fsname - where to fill this structure with separated drive,path,name,ext
\r
282 * 0 - if successfully
\r
283 * other - if name contains invalid path or name
\r
285 ***************************************************************************/
\r
286 unsigned char _f_setfsname ( const char * name, F_NAME * fsname )
\r
289 unsigned char namepos = 0;
\r
291 unsigned char pathpos = 0;
\r
298 return 1; /*no name*/
\r
301 if ( name[1] == ':' )
\r
306 if ( ( name[0] != '/' ) && ( name[0] != '\\' ) )
\r
308 if ( fn_getcwd( fsname->path, F_MAXPATH, 0 ) )
\r
310 return 1; /*error*/
\r
313 for ( pathpos = 0 ; fsname->path[pathpos] ; )
\r
322 char ch = _f_toupper( *name++ );
\r
331 return 1; /*not allowed*/
\r
334 if ( ( ch == '/' ) || ( ch == '\\' ) )
\r
338 if ( fsname->path[pathpos - 1] == '/' )
\r
340 return 1; /*not allowed double */
\r
343 if ( pathpos >= F_MAXPATH - 2 )
\r
345 return 1; /*path too long*/
\r
348 fsname->path[pathpos++] = '/';
\r
351 for ( ; namepos ; )
\r
353 if ( s[namepos - 1] != ' ' )
\r
358 namepos--; /*remove end spaces*/
\r
361 for ( a = 0 ; a < namepos ; a++ )
\r
363 if ( pathpos >= F_MAXPATH - 2 )
\r
365 return 1; /*path too long*/
\r
368 fsname->path[pathpos++] = s[a];
\r
375 if ( ( ch == ' ' ) && ( !namepos ) )
\r
377 continue; /*remove start spaces*/
\r
380 if ( namepos >= ( sizeof( s ) - 2 ) )
\r
382 return 1; /*name too long*/
\r
388 s[namepos] = 0; /*terminates it*/
\r
389 fsname->path[pathpos] = 0; /*terminates it*/
\r
391 for ( ; namepos ; )
\r
393 if ( s[namepos - 1] != ' ' )
\r
398 s[namepos - 1] = 0; /*remove end spaces*/
\r
402 if ( !_f_setnameext( s, fsname->filename, fsname->fileext ) )
\r
404 return 2; /*no name*/
\r
407 if ( fsname->filename[0] == ' ' )
\r
409 return 1; /*cannot be*/
\r
413 } /* _f_setfsname */
\r
416 /****************************************************************************
\r
418 * _f_createfullname
\r
424 * buffer - where to create
\r
425 * buffersize - size of the buffer
\r
426 * drivenum - drive number
\r
427 * path - path of the file
\r
428 * filename - file name
\r
429 * fileext - file extension
\r
433 * 1 - if found and osize is filled
\r
436 ***************************************************************************/
\r
437 int _f_createfullname ( char * buffer, int buffersize, char * path, char * filename, char * fileext )
\r
439 char * fullname = buffer;
\r
442 /* adding drive letter */
\r
443 if ( buffersize < 1 )
\r
463 if ( buffersize <= 0 )
\r
472 /* adding separator */
\r
473 if ( buffersize <= 0 )
\r
482 for ( a = 0 ; a < F_MAXNAME ; a++ )
\r
484 char ch = *filename++;
\r
486 if ( ( !ch ) || ( ch == 32 ) )
\r
491 if ( buffersize <= 0 )
\r
501 if ( fileext[0] && ( fileext[0] != 32 ) )
\r
511 for ( a = 0 ; a < F_MAXEXT ; a++ )
\r
513 char ch = *fileext++;
\r
515 if ( ( !ch ) || ( ch == 32 ) )
\r
520 if ( buffersize <= 0 )
\r
530 /* adding terminator */
\r
531 if ( buffersize <= 0 )
\r
539 } /* _f_createfullname */
\r