nlib
misc/readfile/readfile.cpp

nlibに実装されているPosix風の関数(大抵のプラットフォームでは対応するposix関数を呼び出しているだけ)を利用してテキストファイルを読み込んでコンソールに表示します。 利用方法は対応するPosix関数に準じますが、エラー値を戻り値として返します。-1を返したり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