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