1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - FS - demos - file-3
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 
NitroMain(void)45 void NitroMain(void)
46 {
47     // Initialize the OS and memory allocator
48     OS_Init();
49     {
50         OSHeapHandle hh;
51         void   *tmp;
52         tmp = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
53         OS_SetArenaLo(OS_ARENA_MAIN, tmp);
54         hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
55         if (hh < 0)
56         {
57             OS_TPanic("ARM9: Fail to create heap...\n");
58         }
59         (void)OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
60     }
61     (void)OS_EnableIrq();
62     // Initialize the FS
63     FS_Init(3);
64     {
65         u32     need_size = FS_GetTableSize();
66         void   *p_table = OS_Alloc(need_size);
67         SDK_ASSERT(p_table != NULL);
68         (void)FS_LoadTable(p_table, need_size);
69     }
70 
71     OS_TPrintf("\n"
72               "++++++++++++++++++++++++++++++++++++++++\n"
73               "test 1 : create temporary file from memory... \n\n");
74     {
75         FSFile  file;
76         BOOL    ret;
77         char    buf[256];
78 
79         static char map_mem[] =
80             "here is memory buffer read as file.\n"
81             "***********************************\n" "***********************************\n";
82 
83         /* Open memory file */
84         FS_InitFile(&file);
85         ret = FS_CreateFileFromMemory(&file, map_mem, sizeof(map_mem));
86         SDK_ASSERT(ret);
87         OS_TPrintf("{\n");
88         while (GetLine(buf, sizeof(buf), &file) && (*buf != '\0'))
89         {
90             OS_TPrintf("    %s", buf);
91         }
92         OS_TPrintf("\n}\n");
93 
94         /* Change content with write-access */
95         (void)FS_SeekFileToBegin(&file);
96         while (FS_WriteFile(&file, "#", 1) > 0)
97         {
98         }
99         (void)FS_SeekFileToBegin(&file);
100         OS_TPrintf("{\n");
101         while (GetLine(buf, sizeof(buf), &file) && (*buf != '\0'))
102         {
103             OS_TPrintf("    %s", buf);
104         }
105         OS_TPrintf("\n}\n");
106 
107         ret = FS_CloseFile(&file);
108         SDK_ASSERT(ret);
109     }
110 
111     OS_TPrintf("\n"
112               "++++++++++++++++++++++++++++++++++++++++\n"
113               "test 2 : create temporary file from ROM... \n\n");
114     {
115         FSFile  file, src;
116         BOOL    ret;
117         char    buf[256];
118 
119 
120         FS_InitFile(&src);
121         ret = FS_OpenFileEx(&src, "rom:/main.c", FS_FILEMODE_R);
122         SDK_ASSERT(ret);
123 
124         FS_InitFile(&file);
125         ret = FS_CreateFileFromRom(&file, FS_GetFileImageTop(&src), FS_GetFileLength(&src));
126         SDK_ASSERT(ret);
127 
128         ret = FS_CloseFile(&src);
129         SDK_ASSERT(ret);
130 
131         OS_TPrintf("{\n");
132         while (GetLine(buf, sizeof(buf), &file) && (*buf != '\0'))
133             OS_TPrintf("    %s", buf);
134         OS_TPrintf("\n}\n");
135 
136         ret = FS_CloseFile(&file);
137         SDK_ASSERT(ret);
138 
139     }
140 
141     OS_TPrintf("\n" "++++++++++++++++++++++++++++++++++++++++\n" "end\n\n");
142     OS_Terminate();
143 }
144