1 /*---------------------------------------------------------------------------*
2   Project: test for archiver
3   File:    arctest3.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: arctest3.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 #include <string.h>
31 
32 static void MyOSInit( void );
33 static void printOneLevel(char* pathName);
34 
35 ARCHandle       Arc;
36 
37 struct dirs_t
38 {
39     struct dirs_t*  next;       // must be first
40     ARCDirEntry     dirEntry;
41 };
42 
43 typedef struct dirs_t       dirs;
44 
45 
printOneLevel(char * pathName)46 static void printOneLevel(char* pathName)
47 {
48     ARCFileInfo     finfo;
49     ARCDir          dir;
50     ARCDirEntry     dirent;
51     dirs*           start = (dirs*)NULL;
52     dirs*           curr;
53     dirs            *p, *q;
54     char            path[256];
55 
56     curr = (dirs*)&start;
57 
58     if (FALSE == ARCOpenDir(&Arc, ".", &dir))
59     {
60         OSReport("Can't open dir %s\n", path);
61         return;
62     }
63 
64     while(1)
65     {
66         if (FALSE == ARCReadDir(&dir, &dirent))
67         {
68             for(p = start; p != (dirs*)NULL; )
69             {
70                 strcpy(path, pathName);
71                 strcat(path, "/");
72                 strcat(path, p->dirEntry.name);
73                 OSReport("\n%s:\n", path);
74 
75                 if (FALSE == ARCChangeDir(&Arc, p->dirEntry.name))
76                 {
77                     OSReport("Can't change dir to %s\n", path);
78                     return;
79                 }
80                 printOneLevel(path);
81                 if (FALSE == ARCChangeDir(&Arc, ".."))
82                 {
83                     OSReport("Can't change dir to %s/..\n", path);
84                     return;
85                 }
86 
87                 q = p;
88                 p = p->next;
89                 OSFree((void*)q);
90             }
91             return;
92         }
93 
94         if (dirent.isDir)
95         {
96             OSReport("D %9d %s\n", 0, dirent.name);
97 
98             if (NULL == ( curr->next = (dirs*)OSAlloc(sizeof(dirs)) ) )
99             {
100                 OSReport("Can't allocate memory\n");
101                 return;
102             }
103             curr = curr->next;
104             curr->next = (dirs*)NULL;
105             memcpy((void*)&(curr->dirEntry), (void*)&dirent,
106                    sizeof(ARCDirEntry));
107             // we open directories later
108         }
109         else
110         {
111             if (FALSE == ARCOpen(&Arc, dirent.name, &finfo))
112             {
113                 OSReport("Can't open file %s/%s\n", path, dirent.name);
114                 return;
115             }
116 
117             OSReport("F %9d %s\n", ARCGetLength(&finfo), dirent.name);
118 
119             ARCClose(&finfo);
120         }
121 
122     } // while(1)
123 
124 } // void printOneLevel(char*)
125 
126 
Read(s32 entrynum)127 static void* Read(s32 entrynum)
128 {
129     BOOL                result;
130     DVDFileInfo         finfo;
131     u32                 length;
132     void*               buf;
133 
134 
135     result = DVDFastOpen(entrynum, &finfo);
136 
137     if (!result)
138     {
139         OSHalt("Read -- failed\n");
140     }
141 
142     length = OSRoundUp32B(DVDGetLength(&finfo));
143 
144     buf = OSAlloc(OSRoundUp32B(length));
145 
146     if ( length != DVDRead(&finfo, buf, (s32)length, 0) )
147     {
148         OSReport("Error occurred when reading\n");
149         OSHalt("");
150     }
151     DVDClose(&finfo);
152 
153     return buf;
154 }
155 
main(void)156 void main(void)
157 {
158     s32             entrynum;
159     void*           arcStart;
160 
161     MyOSInit();
162 
163     entrynum = DVDConvertPathToEntrynum("constitu.arc");
164     if (entrynum >= 0)
165     {
166         arcStart = Read(entrynum);
167 
168         if (ARCInitHandle(arcStart, &Arc) == FALSE)
169         {
170             OSHalt("Bad archive format");
171         }
172     }
173     else
174     {
175         OSHalt("Can't find archive \"constitu.arc\". Abort.");
176     }
177 
178     printOneLevel(".");
179 
180     OSHalt("End of program");
181 
182     // NOT REACHED HERE
183 }
184 
185 
186  /*---------------------------------------------------------------------------*
187     Name:               MyOSInit
188 
189     Description:        Initialize the operating system.
190                         Create a heap so we can use OSAlloc().
191 
192     Arguments:          none
193 
194     Returns:            none
195  *---------------------------------------------------------------------------*/
MyOSInit(void)196 static void MyOSInit ( void )
197 {
198     void*               arenaLo;
199     void*               arenaHi;
200 
201     OSInit();
202 
203     DVDInit();
204 
205     arenaLo = OSGetArenaLo();
206     arenaHi = OSGetArenaHi();
207 
208     // OSInitAlloc should only ever be invoked once.
209     arenaLo = OSInitAlloc(arenaLo, arenaHi, 1); // 1 heap
210     OSSetArenaLo(arenaLo);
211 
212     // The boundaries given to OSCreateHeap should be 32B aligned
213     OSSetCurrentHeap(OSCreateHeap((void*)OSRoundUp32B(arenaLo),
214                                   (void*)OSRoundDown32B(arenaHi)));
215     // From here on out, OSAlloc and OSFree behave like malloc and free
216     // respectively
217 
218     OSSetArenaLo(arenaLo = arenaHi);
219 
220     return;
221 }
222