/*---------------------------------------------------------------------------* Project: CNT API DEMO File: datatitledemo.c Programers: John Cho Jumpei Wada Copyright (C) 2007 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: datatitledemo.c,v $ Revision 1.1.2.2 2008/07/23 00:54:57 wada_jumpei Revision 1.1 on main tree. Revision 1.1 2008/07/23 00:29:59 wada_jumpei Moved from build/demos/datatitledemo. Removed unnecessary macros and comments. Revision 1.2 2008/02/15 07:25:02 wada_jumpei Modified for new CNTRead. Revision 1.1 2007/10/11 02:23:55 wada_jumpei Moved from /build/demos/cntdemo. *---------------------------------------------------------------------------*/ #include #include #include #include #include #include #include // The names of the files we are going to read. char* fileName1 = "test1.txt"; char* fileName2 = "test2.txt"; // This is the number of content index to be used in this program. #define TARGET_CONTENT_DATA_TITLE 1 // Data titles can use content index starting from 1 #define DATA_TITLE_TITLEID 0x0001000561303030 // or gamecode a000 #define DATA_TITLE_GAMECODE "a000" void main(void) { CNTHandle Cnt; CNTFileInfo fileInfo1; CNTFileInfo fileInfo2; CNTFileInfo fileInfo3; u32 fileSize1; u32 fileSize2; u32 fileSize3; u8* buffer1; // pointer to the buffer u8* buffer2; // pointer to the buffer u8* buffer3; // pointer to the buffer u8* ptr1; u8* ptr2; u8* ptr3; u32 i; s32 rv; // for checking error DEMOInit(NULL); CNTInit(); OSReport("--- Open and read from %.4s ---\n\n", DATA_TITLE_GAMECODE); rv = CNTInitHandleTitle(DATA_TITLE_TITLEID, TARGET_CONTENT_DATA_TITLE, &Cnt, &DemoAllocator1); if(rv != CNT_RESULT_OK) { OSReport("Init handle finished: %d\n", rv); } // Open the filename1 to fileInfo1 rv = CNTOpen(&Cnt, fileName1, &fileInfo1); if (rv != CNT_RESULT_OK) { OSReport("Cannot open file \"%s\"", fileName1); OSHalt(""); } // Open the filename2 to fileInfo2 and fileInfo3 // (Then reads half of the file) rv = CNTOpen(&Cnt, fileName2, &fileInfo2); if (rv != CNT_RESULT_OK) { OSReport("Cannot open file \"%s\"", fileName1); OSHalt(""); } rv = CNTOpen(&Cnt, fileName2, &fileInfo3); if (rv != CNT_RESULT_OK) { OSReport("Cannot open file \"%s\"", fileName2); OSHalt(""); } // Get the size of the files fileSize1 = CNTGetLength(&fileInfo1); fileSize2 = CNTGetLength(&fileInfo2) >> 1; // filesize / 2 fileSize3 = fileSize2; // Allocate buffers to read the files. // Note that pointers returned by Allocator are all 32byte aligned. buffer1 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize1)); buffer2 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize2)); buffer3 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize3)); // reads filename1 at one time rv = CNTRead(&fileInfo1, (void*)buffer1, (u32)OSRoundUp32B(fileSize1)); // If CNTRead suceeds, it returns length (the number of bytes). if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize1)) { OSHalt("Error occurred when issuing read for the first file"); } // reads filename2 dividing into two phase. // First, reads the half of filename2 nomally. rv = CNTRead(&fileInfo2, (void*)buffer2, (u32)OSRoundUp32B(fileSize2)); if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize2)) { OSHalt("Error occurred when issuing read for the second file"); } // Next, seek to the half of filename2 and reads remaining part. rv = CNTSeek(&fileInfo3, (s32)fileSize2, CNT_SEEK_SET); if (rv != CNT_RESULT_OK) { OSHalt("Error occurred when issuing seek for the second file"); } rv = CNTRead(&fileInfo3, (void*)buffer3, (u32)OSRoundUp32B(fileSize3)); /*---------------------------- **** WARNING **** ---------------------------------- Because the end of the archive file is not aligned to 32B, For DVD application, CNTRead returns number of bytes "really" read (return 64). For NAND application, CNTRead returns "real" number of bytes of the file (return 56). When adding some files in dvddata/content2/, check if CNTRead returns "(u32)OSRoundUp32B(fileSize3)". if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize3)) { OSHalt("Error occurred when issuing read for the second file"); } ------------------------------------------------------------------------------------*/ OSReport("read end\n"); OSReport("\n"); // Close the files CNTClose(&fileInfo1); CNTClose(&fileInfo2); CNTClose(&fileInfo3); // Show first file (filename1) OSReport("First file:\n"); ptr1 = buffer1; for (i = 0; i < fileSize1; i++) { OSReport("%c", *ptr1++); } // Show second file (filename2) OSReport("Second file:\n"); ptr2 = buffer2; for (i = 0; i < fileSize2; i++) { OSReport("%c", *ptr2++); } ptr3 = buffer3; for (i = 0; i < fileSize3; i++) { OSReport("%c", *ptr3++); } OSReport("\n\n"); MEMFreeToAllocator(&DemoAllocator1, buffer1); MEMFreeToAllocator(&DemoAllocator1, buffer2); MEMFreeToAllocator(&DemoAllocator1, buffer3); // make sure to release CNTHandle CNTReleaseHandle(&Cnt); // for safety device shutdown CNTShutdown(); OSHalt("End of demo"); // NOT REACHED HERE }