1 /*---------------------------------------------------------------------------*
2   Project: test for open and read
3   File:    open-read.c
4 
5   Copyright 1998, 1999 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: directory.c,v $
14   Revision 1.2  02/20/2006 04:13:09  mitu
15   changed include path from dolphin/ to revolution/.
16 
17   Revision 1.1  2006/01/17 02:43:50  hiratsu
18   Initial check in.
19 
20 
21     3     2000/03/28 3:40p Hashida
22     Added DVDSetRoot for MAC build
23 
24     2     2000/03/01 1:14p Hashida
25     Renamed dirent->filename to name.
26 
27     1     2000/01/13 12:17p Hashida
28     Initial revision
29 
30   $NoKeywords: $
31  *---------------------------------------------------------------------------*/
32 
33 /*
34 
35  */
36 #include <revolution/os.h>
37 #include <revolution/dvd.h>
38 
39 #include <string.h>
40 
41 static void MyOSInit( void );
42 static void printOneLevel(char* pathName);
43 
44 struct dirs_t
45 {
46     struct dirs_t*  next;       // must be first
47     DVDDirEntry     dirEntry;
48 };
49 
50 typedef struct dirs_t       dirs;
51 
52 
printOneLevel(char * pathName)53 static void printOneLevel(char* pathName)
54 {
55     DVDFileInfo     finfo;
56     DVDDir          dir;
57     DVDDirEntry     dirent;
58     dirs*           start = (dirs*)NULL;
59     dirs*           curr;
60     dirs            *p, *q;
61     char            path[256];
62 
63     curr = (dirs*)&start;
64 
65     if (FALSE == DVDOpenDir(".", &dir))
66     {
67         OSReport("Can't open dir %s\n", path);
68         return;
69     }
70 
71     while(1)
72     {
73         if (FALSE == DVDReadDir(&dir, &dirent))
74         {
75             for(p = start; p != (dirs*)NULL; )
76             {
77                 strcpy(path, pathName);
78                 strcat(path, "/");
79                 strcat(path, p->dirEntry.name);
80                 OSReport("\n%s:\n", path);
81 
82                 if (FALSE == DVDChangeDir(p->dirEntry.name))
83                 {
84                     OSReport("Can't change dir to %s\n", path);
85                     return;
86                 }
87                 printOneLevel(path);
88                 if (FALSE == DVDChangeDir(".."))
89                 {
90                     OSReport("Can't change dir to %s/..\n", path);
91                     return;
92                 }
93 
94                 q = p;
95                 p = p->next;
96                 OSFree((void*)q);
97             }
98             return;
99         }
100 
101         if (dirent.isDir)
102         {
103             OSReport("D %9d %s\n", 0, dirent.name);
104 
105             if (NULL == ( curr->next = (dirs*)OSAlloc(sizeof(dirs)) ) )
106             {
107                 OSReport("Can't allocate memory\n");
108                 return;
109             }
110             curr = curr->next;
111             curr->next = (dirs*)NULL;
112             memcpy((void*)&(curr->dirEntry), (void*)&dirent,
113                    sizeof(DVDDirEntry));
114             // we open directories later
115         }
116         else
117         {
118             if (FALSE == DVDOpen(dirent.name, &finfo))
119             {
120                 OSReport("Can't open file %s/%s\n", path, dirent.name);
121                 return;
122             }
123 
124             OSReport("F %9d %s\n", DVDGetLength(&finfo), dirent.name);
125 
126             DVDClose(&finfo);
127         }
128 
129     } // while(1)
130 
131 } // void printOneLevel(char*)
132 
133 
main(void)134 void main(void)
135 {
136     MyOSInit();
137 
138 #ifdef MAC
139     OSReport("\n-----------------------------------");
140     OSReport("\n  Hit Command+Q to quit this demo");
141     OSReport("\n-----------------------------------\n\n");
142 
143     DVDSetRoot("DOLPHIN/dvddata");
144 #endif
145 
146     printOneLevel(".");
147 
148     OSHalt("End of program");
149 
150     // NOT REACHED HERE
151 }
152 
153 
154  /*---------------------------------------------------------------------------*
155     Name:               MyOSInit
156 
157     Description:        Initialize the operating system.
158                         Create a heap so we can use OSAlloc().
159 
160     Arguments:          none
161 
162     Returns:            none
163  *---------------------------------------------------------------------------*/
MyOSInit(void)164 static void MyOSInit ( void )
165 {
166     void*               arenaLo;
167     void*               arenaHi;
168 
169     OSInit();
170 
171     DVDInit();
172 
173     arenaLo = OSGetArenaLo();
174     arenaHi = OSGetArenaHi();
175 
176     // OSInitAlloc should only ever be invoked once.
177     arenaLo = OSInitAlloc(arenaLo, arenaHi, 1); // 1 heap
178     OSSetArenaLo(arenaLo);
179 
180     // The boundaries given to OSCreateHeap should be 32B aligned
181     OSSetCurrentHeap(OSCreateHeap((void*)OSRoundUp32B(arenaLo),
182                                   (void*)OSRoundDown32B(arenaHi)));
183     // From here on out, OSAlloc and OSFree behave like malloc and free
184     // respectively
185 
186     OSSetArenaLo(arenaLo = arenaHi);
187 
188     return;
189 }
190