1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - FS - demos - file-2
3   File:     main.c
4 
5   Copyright 2003-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-10-02 #$
14   $Rev: 8827 $
15   $Author: yosizaki $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro.h>
19 
20 
21 // Read one line from a file
GetLine(char * buf,int len,FSFile * file)22 static char* GetLine(char *buf, int len, FSFile *file)
23 {
24     char   *retval = NULL;
25     int     ret = FS_ReadFile(file, buf, len - 1);
26     if (ret >= 0)
27     {
28         int     i;
29         for (i = 0; i < ret; ++i)
30         {
31             if (buf[i] == '\n')
32             {
33                 ++i;
34                 break;
35             }
36         }
37         buf[i] = '\0';
38         (void)FS_SeekFile(file, i - ret, FS_SEEK_CUR);
39         retval = buf;
40     }
41     return retval;
42 }
43 
44 // List the directories in a ROM archive
DumpRomDirectorySub(int tab,FSDirEntry * pe)45 static void DumpRomDirectorySub(int tab, FSDirEntry *pe)
46 {
47     FSFile  d;
48     FS_InitFile(&d);
49     OS_TPrintf("%*s%s/\n", tab, "", pe->name);
50     if (FS_SeekDir(&d, &pe->dir_id))
51     {
52         tab += 4;
53         while (FS_ReadDir(&d, pe))
54         {
55             if (pe->is_directory)
56             {
57                 DumpRomDirectorySub(tab, pe);
58             }
59             else
60             {
61                 OS_Printf("%*s%s\n", tab, "", pe->name);
62             }
63         }
64     }
65 }
DumpRomDir(const char * path)66 static void DumpRomDir(const char *path)
67 {
68     FSDirEntry  entry;
69     FSFile      dir;
70     FS_InitFile(&dir);
71     (void)FS_ChangeDir(path);
72     (void)FS_FindDir(&dir, "");
73     entry.name[0] = '\0';
74     (void)FS_TellDir(&dir, &entry.dir_id);
75     DumpRomDirectorySub(0, &entry);
76 }
77 
78 
NitroMain(void)79 void NitroMain(void)
80 {
81 
82     // Initialize the OS and memory allocator
83     OS_Init();
84     {
85         OSHeapHandle hh;
86         void   *tmp;
87         tmp = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
88         OS_SetArenaLo(OS_ARENA_MAIN, tmp);
89         hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
90         if (hh < 0)
91         {
92             OS_TPanic("ARM9: Fail to create heap...\n");
93         }
94         (void)OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
95     }
96     (void)OS_EnableIrq();
97     // Initialize the FS
98     FS_Init(3);
99     {
100         u32     need_size = FS_GetTableSize();
101         void   *p_table = OS_Alloc(need_size);
102         SDK_ASSERT(p_table != NULL);
103         (void)FS_LoadTable(p_table, need_size);
104     }
105 
106     OS_TPrintf("\n"
107               "++++++++++++++++++++++++++++++++++++++++\n"
108               "test 1 : open specified files ... \n\n");
109     {
110         static const char *(paths[]) =
111         {
112         "\\hello.txt",
113                 "test1.txt", "/./../.\\.\\tesT2.tXT",};
114 
115         int     i;
116         for (i = 0; i < sizeof(paths) / sizeof(*paths); ++i)
117         {
118             FSFile  file;
119             const char *path = paths[i];
120             BOOL    open_is_ok;
121             FS_InitFile(&file);
122             open_is_ok = FS_OpenFileEx(&file, path, FS_FILEMODE_R);
123             OS_TPrintf("FS_OpenFileEx(\"%s\", FS_FILEMODE_R) ... %s!\n", path, open_is_ok ? "OK" : "ERROR");
124             if (open_is_ok)
125             {
126                 char    buf[256];
127                 OS_TPrintf("{\n");
128                 while (GetLine(buf, sizeof(buf), &file) && (*buf != '\0'))
129                 {
130                     OS_TPrintf("    %s", buf);
131                 }
132                 OS_TPrintf("\n}\n");
133             }
134         }
135 
136     }
137 
138     OS_TPrintf("\n"
139                "++++++++++++++++++++++++++++++++++++++++\n" "test 2 : query directories ... \n\n");
140 
141     DumpRomDir("rom:/");
142 
143 
144     OS_TPrintf("\n"
145                "++++++++++++++++++++++++++++++++++++++++\n"
146                "test 3 : FS_TellDir() and FS_SeekDir() ... \n\n");
147     {
148         FSFile  d;
149         FSDirEntry e;
150         FSDirPos pos_list[10];
151         int     file_count = 0;
152 
153         BOOL    ret;
154 
155         FS_InitFile(&d);
156         ret = FS_FindDir(&d, "rom:/");
157         SDK_ASSERT(ret);
158 
159         for (;;)
160         {
161             FSDirPos pos;
162             (void)FS_TellDir(&d, &pos);
163             if (!FS_ReadDir(&d, &e))
164             {
165                 break;
166             }
167             if (!e.is_directory)
168             {
169                 if (file_count < sizeof(pos_list) / sizeof(*pos_list))
170                 {
171                     pos_list[file_count] = pos;
172                     OS_TPrintf("TellDir[%d] : %s\n", file_count, e.name);
173                     ++file_count;
174                 }
175             }
176         }
177         ret = FS_RewindDir(&d);
178         SDK_ASSERT(ret);
179         ret = FS_ReadDir(&d, &e);
180         SDK_ASSERT(ret);
181         OS_TPrintf("RewindDir : %s\n", e.name);
182 
183         while (--file_count >= 0)
184         {
185             ret = FS_SeekDir(&d, &pos_list[file_count]);
186             SDK_ASSERT(ret);
187             ret = FS_ReadDir(&d, &e);
188             SDK_ASSERT(ret);
189             OS_TPrintf("SeekDir[%d] : %s\n", file_count, e.name);
190         }
191 
192     }
193 
194     OS_TPrintf("\n" "++++++++++++++++++++++++++++++++++++++++\n" "end\n\n");
195     OS_Terminate();
196 }
197