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