nlib
misc/readfile/readfile.cpp

This function uses a POSIX-style function, which only calls its corresponding posix function on most platforms, implemented in nlib to read a text file and displays it on the console. Although the function can be used in the same way as its corresponding posix function, it returns an error value as the return value. Note that it will not return -1 or set an errno.

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. 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.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
NLIB_PATHMAPPER_FORSAMPLE
bool SampleMain(int, char**) {
// Initializes g_pathmapper which NLIB_PATHMAPPER_FORSAMPLE defines
InitPathMapperForSample();
// converts an URI to the corresponding native path
char filename[1024];
g_pathmapper.ResolvePath(NULL, filename, "nlibpath:///readonly/readfile.cpp");
nlib_fd fd;
nlib_offset filesize_;
e = nlib_fd_open(&fd, filename, NLIB_FD_O_RDONLY);
if (e != 0) return false;
e = nlib_fd_getsize(&filesize_, fd);
if (e != 0) {
(void)nlib_fd_close(fd);
return false;
}
size_t filesize = static_cast<size_t>(filesize_);
nlib_ns::UniquePtr<char[]> text(new (std::nothrow) char[filesize + 1]);
if (!text) {
(void)nlib_fd_close(fd);
return false;
}
text[filesize] = '\0';
while (filesize > 0) {
size_t readsize;
e = nlib_fd_read(&readsize, fd, text.get(), filesize);
if (e != 0) {
(void)nlib_fd_close(fd);
return false;
}
filesize -= readsize;
}
nlib_printf("%s", text.get());
e = nlib_fd_close(fd);
if (e != 0) return false;
return true;
}
NLIB_MAINFUNC