1 /*---------------------------------------------------------------------------*
2   Project: test for open and read (archiver version)
3   File:    arctest1.c
4 
5   Copyright (C)2001-2006 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   $Log: arctest1.c,v $
14   Revision 1.1  04/19/2006 10:48:08  hiratsu
15   Initial check-in.  Ported from Dolphin.
16 
17 
18     1     7/03/01 3:16a Hashida
19     Initial revision.
20 
21   $NoKeywords: $
22  *---------------------------------------------------------------------------*/
23 
24 /*
25 
26  */
27 #include <revolution/os.h>
28 #include <revolution/dvd.h>
29 #include <revolution/arc.h>
30 
31 // XXX this definition is not needed any more. However, we use this because
32 // constitution files under dvddata is still 8.3 file names. (Archiver can
33 // handle long file names!)
34 #define SHORTFILENAME
35 
36 static void MyOSInit( void );
37 void callback(s32 result, DVDFileInfo* finfo);
38 void chdirOpenAllocRead(char* dirName, char* pathName);
39 
40 BOOL UseArc = FALSE;
41 
42 ARCHandle       Arc;
43 
44 
45 /*---------------------------------------------------------------------------*
46   Name:         chdirOpenAllocRead
47 
48   Description:  change dir to the specified dir, open the specified file,
49                 allocate memory enough to read it, and read
50 
51   Arguments:    dirName      directory to change. If NULL, don't change dir.
52                 pathName     file to read
53 
54   Returns:      None.
55  *---------------------------------------------------------------------------*/
chdirOpenAllocRead(char * dirName,char * pathName)56 void chdirOpenAllocRead(char* dirName, char* pathName)
57 {
58     BOOL               result;
59     DVDFileInfo        finfo;
60     ARCFileInfo        afinfo;
61     u32                length;
62     char*              buf;
63     char*              ptr;
64     u32                i;
65 
66     if (dirName != (char*)NULL)
67     {
68         if (UseArc)
69         {
70             result = ARCChangeDir(&Arc, dirName);
71         }
72         else
73         {
74             result = DVDChangeDir(dirName);
75         }
76 
77         OSReport("Changing dir to %s ", dirName);
78 
79         if (result)
80         {
81             OSReport("-- succeeded\n");
82         }
83         else
84         {
85             OSHalt("-- failed\n");
86         }
87     }
88 
89     if (UseArc)
90     {
91         result = ARCOpen(&Arc, pathName, &afinfo);
92     }
93     else
94     {
95         result = DVDOpen(pathName, &finfo);
96     }
97 
98     OSReport("Opening %s \n", pathName);
99 
100     if (result)
101     {
102         OSReport("-- succeeded\n");
103     }
104     else
105     {
106         OSHalt("-- failed\n");
107     }
108 
109     if (UseArc)
110     {
111         length = ARCGetLength(&afinfo);
112         buf = (char*)ARCGetStartAddrInMem(&afinfo);
113     }
114     else
115     {
116         length = DVDGetLength(&finfo);
117         buf = (char*)OSAlloc(OSRoundUp32B(length));
118 
119         if (OSRoundUp32B(length) != DVDRead(&finfo, (void*)buf, (s32)OSRoundUp32B(length), 0))
120         {
121             OSReport("Error occurred when reading %s\n", pathName);
122             OSHalt("");
123         }
124     }
125 
126     OSReport("\n-- Contents of the file starts from the following line.\n");
127     ptr = buf;
128     for (i = 0; i < length; i++)
129     {
130         OSReport("%c", *ptr++);
131     }
132     OSReport("<eof>\n\n");
133 
134     if (UseArc)
135     {
136         ARCClose(&afinfo);
137     }
138     else
139     {
140         OSFree(buf);
141 
142         DVDClose(&finfo);
143     }
144 }
145 
146 
Read(s32 entrynum)147 static void* Read(s32 entrynum)
148 {
149     BOOL                result;
150     DVDFileInfo         finfo;
151     u32                 length;
152     void*               buf;
153 
154 
155     result = DVDFastOpen(entrynum, &finfo);
156 
157     if (!result)
158     {
159         OSHalt("Read -- failed\n");
160     }
161 
162     length = OSRoundUp32B(DVDGetLength(&finfo));
163 
164     buf = OSAlloc(OSRoundUp32B(length));
165 
166     if ( length != DVDRead(&finfo, buf, (s32)length, 0) )
167     {
168         OSReport("Error occurred when reading\n");
169         OSHalt("");
170     }
171     DVDClose(&finfo);
172 
173     return buf;
174 }
175 
main(void)176 void main(void)
177 {
178     s32             entrynum;
179     void*           arcStart;
180 
181 
182     MyOSInit();
183 
184     // Check the existence of the archive file.
185     // If not, we use the normal way.
186     entrynum = DVDConvertPathToEntrynum("constitu.arc");
187     if (entrynum >= 0)
188     {
189         OSReport("Found an archive. Use archive APIs instead.\n");
190 
191         UseArc = TRUE;
192         arcStart = Read(entrynum);
193 
194         if (ARCInitHandle(arcStart, &Arc) == FALSE)
195         {
196             OSHalt("Bad archive format");
197         }
198     }
199 
200     // absolute path
201 #ifndef SHORTFILENAME
202     chdirOpenAllocRead((char*)NULL, "/constitution/article II/section 2");
203 #else
204     chdirOpenAllocRead((char*)NULL, "/constitu/article2/section2");
205 #endif
206 
207     // relative path from root
208 #ifndef SHORTFILENAME
209     chdirOpenAllocRead((char*)NULL, "constitution/article II/section 2");
210 #else
211     chdirOpenAllocRead((char*)NULL, "constitu/article2/section2");
212 #endif
213 
214     // change directory
215 #ifndef SHORTFILENAME
216     chdirOpenAllocRead("constitution/article I/", "section 5");
217 #else
218     chdirOpenAllocRead("constitu/article1/", "section5");
219 #endif
220 
221     // goto parent
222 #ifndef SHORTFILENAME
223     chdirOpenAllocRead((char*)NULL, "../article III/../article II/section 1");
224 #else
225     chdirOpenAllocRead((char*)NULL, "../article3/../article2/section1");
226 #endif
227 
228     // from root
229 #ifndef SHORTFILENAME
230     chdirOpenAllocRead((char*)NULL, "/constitution/article III/section 2");
231 #else
232     chdirOpenAllocRead((char*)NULL, "/constitu/article3/section2");
233 #endif
234 
235     // parent of root
236 #ifndef SHORTFILENAME
237     chdirOpenAllocRead("/", "../constitution/article I/section 8");
238 #else
239     chdirOpenAllocRead("/", "../constitu/article1/section8");
240 #endif
241 
242     OSHalt("End of program");
243 
244     // NOT REACHED HERE
245 }
246 
247 
248  /*---------------------------------------------------------------------------*
249     Name:               MyOSInit
250 
251     Description:        Initialize the operating system.
252                         Create a heap so we can use OSAlloc().
253 
254     Arguments:          none
255 
256     Returns:            none
257  *---------------------------------------------------------------------------*/
MyOSInit(void)258 static void MyOSInit ( void )
259 {
260     void*               arenaLo;
261     void*               arenaHi;
262 
263     OSInit();
264 
265     DVDInit();
266 
267     arenaLo = OSGetArenaLo();
268     arenaHi = OSGetArenaHi();
269 
270     // OSInitAlloc should only ever be invoked once.
271     arenaLo = OSInitAlloc(arenaLo, arenaHi, 1); // 1 heap
272     OSSetArenaLo(arenaLo);
273 
274     // The boundaries given to OSCreateHeap should be 32B aligned
275     OSSetCurrentHeap(OSCreateHeap((void*)OSRoundUp32B(arenaLo),
276                                   (void*)OSRoundDown32B(arenaHi)));
277     // From here on out, OSAlloc and OSFree behave like malloc and free
278     // respectively
279 
280     OSSetArenaLo(arenaLo = arenaHi);
281 
282     return;
283 }
284