1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - FS - libraries
3   File:     util.h
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-25#$
14   $Rev: 8648 $
15   $Author: yosizaki $
16 
17  *---------------------------------------------------------------------------*/
18 
19 
20 #if	!defined(NITRO_FS_UTIL_H_)
21 #define NITRO_FS_UTIL_H_
22 
23 #include <nitro/misc.h>
24 #include <nitro/types.h>
25 
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 
32 /*---------------------------------------------------------------------------*/
33 /* Constants */
34 
35 // Enable this definition when you want to use the file system from the ARM7.
36 // #define SDK_ARM7FS
37 
38 // This is only defined for environments that should include the full set of file system functionality
39 #if	!defined(SDK_ARM7) || defined(SDK_ARM7FS)
40 #define	FS_IMPLEMENT
41 #endif
42 
43 
44 /*---------------------------------------------------------------------------*/
45 /* functions */
46 
47 /*---------------------------------------------------------------------------*
48   Name:         FSi_IsSlash
49 
50   Description:  Internal function.
51                 Determine if a character is a directory delimiter.
52 
53   Arguments:    c:                Character to determine.
54 
55   Returns:      TRUE if a directory delimiting character.
56  *---------------------------------------------------------------------------*/
FSi_IsSlash(u32 c)57 SDK_INLINE BOOL FSi_IsSlash(u32 c)
58 {
59     return (c == '/') || (c == '\\');
60 }
61 
62 /*---------------------------------------------------------------------------*
63   Name:         FSi_IncrementSjisPosition
64 
65   Description:  Advances the access position of a Shift JIS string by one character.
66 
67   Arguments:    str: Pointer to the start of a Shift JIS string.
68                 pos: The current access position in the string. (in bytes)
69 
70   Returns:      The access position that results from advancing pos by one character.
71  *---------------------------------------------------------------------------*/
FSi_IncrementSjisPosition(const char * str,int pos)72 SDK_INLINE int FSi_IncrementSjisPosition(const char *str, int pos)
73 {
74     return pos + 1 + STD_IsSjisLeadByte(str[pos]);
75 }
76 
77 /*---------------------------------------------------------------------------*
78   Name:         FSi_DecrementSjisPosition
79 
80   Description:  Moves the access position of a Shift JIS string backward by one character.
81 
82   Arguments:    str: Pointer to the start of a Shift JIS string.
83                 pos: The current access position in the string. (in bytes)
84 
85   Returns:      Either -1 or the access position that results from moving pos backward by one character.
86  *---------------------------------------------------------------------------*/
87 int FSi_DecrementSjisPosition(const char *str, int pos);
88 
89 /*---------------------------------------------------------------------------*
90   Name:         FSi_IncrementSjisPositionToSlash
91 
92   Description:  Advances the access position in a Shift JIS string to either a directory-delimiting character or the end of the string.
93 
94 
95   Arguments:    str: Pointer to the start of a Shift JIS string.
96                 pos: The current access position in the string. (in bytes)
97 
98   Returns:      Either the first directory delimiter starting from pos or the end of the string.
99  *---------------------------------------------------------------------------*/
100 int FSi_IncrementSjisPositionToSlash(const char *str, int pos);
101 
102 /*---------------------------------------------------------------------------*
103   Name:         FSi_DecrementSjisPositionToSlash
104 
105   Description:  Moves the access position in a Shift JIS string backward to either a directory-delimiting character or the start of the string.
106 
107 
108   Arguments:    str: Pointer to the start of a Shift JIS string.
109                 pos: The current access position in the string. (in bytes)
110 
111   Returns:      Either the first directory delimiter before pos or -1.
112  *---------------------------------------------------------------------------*/
113 int FSi_DecrementSjisPositionToSlash(const char *str, int pos);
114 
115 /*---------------------------------------------------------------------------*
116   Name:         FSi_TrimSjisTrailingSlash
117 
118   Description:  Deletes a directory-delimiting character if it exists at the end of a Shift JIS string.
119 
120   Arguments:    str: Shift JIS string.
121 
122   Returns:      Length of string
123  *---------------------------------------------------------------------------*/
124 int FSi_TrimSjisTrailingSlash(char *str);
125 
126 /*---------------------------------------------------------------------------*
127   Name:         FSi_StrNICmp
128 
129   Description:  Performs a case-insensitive string comparison for only the specified number of bytes.
130                 Note that this does not consider the terminating NULL character.
131 
132   Arguments:    str1: Source string to compare
133                 str2: Target string to compare
134                 len: Number of bytes to compare
135 
136   Returns:      The result of comparing (str1 - str2)
137  *---------------------------------------------------------------------------*/
FSi_StrNICmp(const char * str1,const char * str2,u32 len)138 SDK_INLINE int FSi_StrNICmp(const char *str1, const char *str2, u32 len)
139 {
140     int     retval = 0;
141     int     i;
142     for (i = 0; i < len; ++i)
143     {
144         u32     c = (u8)(str1[i] - 'A');
145         u32     d = (u8)(str2[i] - 'A');
146         if (c <= 'Z' - 'A')
147         {
148             c += 'a' - 'A';
149         }
150         if (d <= 'Z' - 'A')
151         {
152             d += 'a' - 'A';
153         }
154         retval = (int)(c - d);
155         if (retval != 0)
156         {
157             break;
158         }
159     }
160     return retval;
161 }
162 
163 /*---------------------------------------------------------------------------*
164   Name:         FSi_IsUnicodeSlash
165 
166   Description:  Determines whether a Unicode character is a path delimiter.
167 
168   Arguments:    c: Unicode1 character
169 
170   Returns:      TRUE if it is L'/' (0x2F) or L'\\' (0x5C).
171  *---------------------------------------------------------------------------*/
FSi_IsUnicodeSlash(u16 c)172 SDK_INLINE BOOL FSi_IsUnicodeSlash(u16 c)
173 {
174     return (c == L'/') || (c == L'\\');
175 }
176 
177 /*---------------------------------------------------------------------------*
178   Name:         FSi_DecrementUnicodePosition
179 
180   Description:  Moves the access position of a Unicode string backward by one character.
181 
182   Arguments:    str: Pointer to the start of a Unicode string.
183                 pos: The current access position in the string. (in bytes)
184 
185   Returns:      Either -1 or the access position that results from moving pos backward by one character.
186  *---------------------------------------------------------------------------*/
187 int FSi_DecrementUnicodePosition(const u16 *str, int pos);
188 
189 /*---------------------------------------------------------------------------*
190   Name:         FSi_DecrementUnicodePositionToSlash
191 
192   Description:  Moves the access position in a Unicode string backward to either a directory-delimiting character or the start of the string.
193 
194 
195   Arguments:    str: Pointer to the start of a Unicode string.
196                 pos: The current access position in the string. (in bytes)
197 
198   Returns:      Either the first directory delimiter before pos or -1.
199  *---------------------------------------------------------------------------*/
200 int FSi_DecrementUnicodePositionToSlash(const u16 *str, int pos);
201 
202 /*---------------------------------------------------------------------------*
203   Name:         FSi_WaitConditionOn
204 
205   Description:  Sleeps until the specified bit is 1.
206 
207   Arguments:    flags: Bit set to monitor
208                 bits: Bit that should change to 1
209                 queue: A sleep queue or NULL
210 
211   Returns:      None.
212  *---------------------------------------------------------------------------*/
FSi_WaitConditionOn(u32 * flags,u32 bits,OSThreadQueue * queue)213 SDK_INLINE void FSi_WaitConditionOn(u32 *flags, u32 bits, OSThreadQueue *queue)
214 {
215     OSIntrMode bak = OS_DisableInterrupts();
216     while ((*flags & bits) == 0)
217     {
218         OS_SleepThread(queue);
219     }
220     (void)OS_RestoreInterrupts(bak);
221 }
222 
223 /*---------------------------------------------------------------------------*
224   Name:         FSi_WaitConditionOff
225 
226   Description:  Sleeps until the specified bit is 0.
227 
228   Arguments:    flags: Bit set to monitor
229                 bits: Bit that should change to 0
230                 queue: A sleep queue or NULL
231 
232   Returns:      None.
233  *---------------------------------------------------------------------------*/
FSi_WaitConditionOff(u32 * flags,u32 bits,OSThreadQueue * queue)234 SDK_INLINE void FSi_WaitConditionOff(u32 *flags, u32 bits, OSThreadQueue *queue)
235 {
236     OSIntrMode bak = OS_DisableInterrupts();
237     while ((*flags & bits) != 0)
238     {
239         OS_SleepThread(queue);
240     }
241     (void)OS_RestoreInterrupts(bak);
242 }
243 
244 /*---------------------------------------------------------------------------*
245   Name:         FSi_GetFileLengthIfProc
246 
247   Description:  Gets the size of the specified file if it is an archive procedure.
248 
249   Arguments:    file: File handle
250                 length: Location to save the size
251 
252   Returns:      The size of the specified file if it is an archive procedure
253  *---------------------------------------------------------------------------*/
254 BOOL FSi_GetFileLengthIfProc(FSFile *file, u32 *length);
255 
256 /*---------------------------------------------------------------------------*
257   Name:         FSi_GetFilePositionIfProc
258 
259   Description:  Gets the current position of the specified file if it is an archive procedure.
260 
261   Arguments:    file: File handle
262                 length: Location to save the size
263 
264   Returns:      The current position of the specified file if it is an archive procedure
265  *---------------------------------------------------------------------------*/
266 BOOL FSi_GetFilePositionIfProc(FSFile *file, u32 *length);
267 
268 
269 /*---------------------------------------------------------------------------*/
270 
271 
272 #ifdef __cplusplus
273 } /* extern "C" */
274 #endif
275 
276 
277 #endif /* NITRO_FS_UTIL_H_ */
278