1 /*---------------------------------------------------------------------------*
2   Project:  CNT API DEMO
3   File:     datatitledemo.c
4   Programers: John Cho
5               Jumpei Wada
6 
7   Copyright (C) 2007 Nintendo.  All rights reserved.
8 
9   These coded instructions, statements, and computer programs contain
10   proprietary information of Nintendo of America Inc. and/or Nintendo
11   Company Ltd., and are protected by Federal copyright law.  They may
12   not be disclosed to third parties or copied or duplicated in any form,
13   in whole or in part, without the prior written consent of Nintendo.
14 
15   $Log: datatitledemo.c,v $
16   Revision 1.1.2.2  2008/07/23 00:54:57  wada_jumpei
17   Revision 1.1 on main tree.
18 
19   Revision 1.1  2008/07/23 00:29:59  wada_jumpei
20   Moved from build/demos/datatitledemo.
21   Removed unnecessary macros and comments.
22 
23   Revision 1.2  2008/02/15 07:25:02  wada_jumpei
24   Modified for new CNTRead.
25 
26   Revision 1.1  2007/10/11 02:23:55  wada_jumpei
27   Moved from /build/demos/cntdemo.
28 
29  *---------------------------------------------------------------------------*/
30 
31 #include <revolution.h>
32 #include <revolution/os.h>
33 #include <revolution/dvd.h>
34 #include <revolution/arc.h>
35 #include <revolution/mem.h>
36 #include <demo.h>
37 #include <revolution/cnt.h>
38 
39 
40 // The names of the files we are going to read.
41 char*   fileName1 = "test1.txt";
42 char*   fileName2 = "test2.txt";
43 
44 // This is the number of content index to be used in this program.
45 #define TARGET_CONTENT_DATA_TITLE   1    // Data titles can use content index starting from 1
46 #define DATA_TITLE_TITLEID          0x0001000561303030  // or gamecode a000
47 #define DATA_TITLE_GAMECODE         "a000"
48 
main(void)49 void main(void)
50 {
51     CNTHandle   Cnt;
52     CNTFileInfo fileInfo1;
53     CNTFileInfo fileInfo2;
54     CNTFileInfo fileInfo3;
55     u32         fileSize1;
56     u32         fileSize2;
57     u32         fileSize3;
58     u8*         buffer1;        // pointer to the buffer
59     u8*         buffer2;        // pointer to the buffer
60     u8*         buffer3;        // pointer to the buffer
61     u8*         ptr1;
62     u8*         ptr2;
63     u8*         ptr3;
64     u32         i;
65     s32         rv;             // for checking error
66 
67     DEMOInit(NULL);
68     CNTInit();
69 
70     OSReport("--- Open and read from %.4s ---\n\n", DATA_TITLE_GAMECODE);
71     rv = CNTInitHandleTitle(DATA_TITLE_TITLEID, TARGET_CONTENT_DATA_TITLE, &Cnt, &DemoAllocator1);
72 
73     if(rv != CNT_RESULT_OK)
74     {
75         OSReport("Init handle finished: %d\n", rv);
76     }
77 
78     // Open the filename1 to fileInfo1
79     rv = CNTOpen(&Cnt, fileName1, &fileInfo1);
80 
81     if (rv != CNT_RESULT_OK)
82     {
83         OSReport("Cannot open file \"%s\"", fileName1);
84         OSHalt("");
85     }
86 
87     // Open the filename2 to fileInfo2 and fileInfo3
88     // (Then reads half of the file)
89     rv = CNTOpen(&Cnt, fileName2, &fileInfo2);
90 
91     if (rv != CNT_RESULT_OK)
92     {
93         OSReport("Cannot open file \"%s\"", fileName1);
94         OSHalt("");
95     }
96 
97     rv = CNTOpen(&Cnt, fileName2, &fileInfo3);
98 
99     if (rv != CNT_RESULT_OK)
100     {
101         OSReport("Cannot open file \"%s\"", fileName2);
102         OSHalt("");
103     }
104 
105     // Get the size of the files
106     fileSize1 = CNTGetLength(&fileInfo1);
107     fileSize2 = CNTGetLength(&fileInfo2) >> 1; // filesize / 2
108     fileSize3 = fileSize2;
109 
110 
111     // Allocate buffers to read the files.
112     // Note that pointers returned by Allocator are all 32byte aligned.
113     buffer1 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize1));
114     buffer2 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize2));
115     buffer3 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize3));
116 
117     // reads filename1 at one time
118     rv = CNTRead(&fileInfo1, (void*)buffer1, (u32)OSRoundUp32B(fileSize1));
119 
120     // If CNTRead suceeds, it returns length (the number of bytes).
121     if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize1))
122     {
123         OSHalt("Error occurred when issuing read for the first file");
124     }
125 
126     // reads filename2 dividing into two phase.
127     // First, reads the half of filename2 nomally.
128     rv = CNTRead(&fileInfo2, (void*)buffer2, (u32)OSRoundUp32B(fileSize2));
129 
130     if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize2))
131     {
132         OSHalt("Error occurred when issuing read for the second file");
133     }
134 
135     // Next, seek to the half of filename2 and reads remaining part.
136     rv = CNTSeek(&fileInfo3, (s32)fileSize2, CNT_SEEK_SET);
137 
138     if (rv != CNT_RESULT_OK)
139     {
140         OSHalt("Error occurred when issuing seek for the second file");
141     }
142 
143     rv = CNTRead(&fileInfo3, (void*)buffer3, (u32)OSRoundUp32B(fileSize3));
144 
145     /*----------------------------    **** WARNING **** ----------------------------------
146 
147     Because the end of the archive file is not aligned to 32B,
148     For DVD application, CNTRead returns number of bytes "really" read (return 64).
149     For NAND application, CNTRead returns "real" number of bytes of the file (return 56).
150 
151     When adding some files in dvddata/content2/, check if CNTRead returns
152     "(u32)OSRoundUp32B(fileSize3)".
153 
154 
155     if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize3))
156     {
157         OSHalt("Error occurred when issuing read for the second file");
158     }
159 
160     ------------------------------------------------------------------------------------*/
161 
162 
163     OSReport("read end\n");
164     OSReport("\n");
165 
166     // Close the files
167     CNTClose(&fileInfo1);
168     CNTClose(&fileInfo2);
169     CNTClose(&fileInfo3);
170 
171     // Show first file (filename1)
172     OSReport("First file:\n");
173     ptr1 = buffer1;
174     for (i = 0; i < fileSize1; i++)
175     {
176         OSReport("%c", *ptr1++);
177     }
178 
179     // Show second file (filename2)
180     OSReport("Second file:\n");
181     ptr2 = buffer2;
182     for (i = 0; i < fileSize2; i++)
183     {
184         OSReport("%c", *ptr2++);
185     }
186 
187     ptr3 = buffer3;
188     for (i = 0; i < fileSize3; i++)
189     {
190         OSReport("%c", *ptr3++);
191     }
192     OSReport("\n\n");
193 
194 
195     MEMFreeToAllocator(&DemoAllocator1, buffer1);
196     MEMFreeToAllocator(&DemoAllocator1, buffer2);
197     MEMFreeToAllocator(&DemoAllocator1, buffer3);
198 
199     // make sure to release CNTHandle
200     CNTReleaseHandle(&Cnt);
201 
202     // for safety device shutdown
203     CNTShutdown();
204 
205     OSHalt("End of demo");
206 
207     // NOT REACHED HERE
208 }
209