本文主要是介绍Nebula2探秘02-使用FileServer2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Nebula2探秘01-创建KernelServer&使用log
happykevins文
/**/ /****************************************************************************/
/**/ /* Nebula2 - Tutorial 02 */
/**/ /* 使用FileServer2 */
/**/ /* author: happykevins */
/**/ /****************************************************************************/
/**/ ///----------------------------------------------------------------------------
/// +必要头文件
#include " kernel/nkernelserver.h "
#include " kernel/nfileserver2.h "
#include " kernel/nfile.h "
#include " kernel/nautoref.h "
/**/ /// -必要头文件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
/**/ /// -链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +Application
int main( int argc, const char ** argv)
... {
/**//// 创建KernelServer
nKernelServer* ks = n_new(nKernelServer);
/**////----------------------------------------------------------------------------
/// +获得FileServer2的实例
/// @note:有以下四种方式。在一般情况下他们是等价的
nFileServer2* pFileServer = NULL;
// 1.通过Singleton获得
pFileServer = nFileServer2::Instance();
// 2.通过KernelServer直接获得
pFileServer = ks->GetFileServer();
// 3.通过NOH体系获得
pFileServer = (nFileServer2*)ks->Lookup("sys/servers/file2");
// 4.通过nAutoRef获得(同NOH体系获得法)
nAutoRef<nFileServer2> refFileServer("sys/servers/file2");
if ( refFileServer.isvalid() )
...{
pFileServer = refFileServer.get();
}
/**//// -获得FileServer2的实例
///----------------------------------------------------------------------------
ks->Print("*****Assigns Operation***** ");
/**////----------------------------------------------------------------------------
/// +Assigns
/// @note:
/// 1. Assigns是一种以标识替代绝对路径的技术,它对于应用程序的资源部署非常有用
/// 2. Assigns的变量都以nEnv类型存储在NOH体系的/sys/share/assigns/位置下
/// 3. /sys/share/assigns/home和/sys/share/assigns/bin是两个默认存在的变量,
/// 分别是应用程序根和执行文件所在路径。
/// 4. 在设置完Assign路径后就可以通过"assign:filename"的形式来索引文件了。
///
// 两个默认的assigns
nString home = pFileServer->GetAssign("home");
nString bin = pFileServer->GetAssign("bin");
nString user = pFileServer->GetAssign("user");
nString temp = pFileServer->GetAssign("temp");
ks->Print("home: %s ", home.Get());
ks->Print("bin : %s ", bin.Get() );
ks->Print("user: %s ", user.Get());
ks->Print("temp: %s ", temp.Get());
// 设置proj索引
pFileServer->SetAssign("proj", "d:/");
nString proj = pFileServer->GetAssign("proj");
ks->Print("proj: %s ", proj.Get());
/**////
/// -Assigns
///----------------------------------------------------------------------------
ks->Print("*****File W/R Operation***** ");
/**////----------------------------------------------------------------------------
/// +File Operation
///
// get nFile object
nFile* file = pFileServer->NewFileObject();
// open file for write
file->Open("bin:T02.txt", "w");
// write file
nString output("T02 FileWriteTest!");
file->Write(output.Get(), output.Length());
// close file for write
file->Close();
// open file for read
file->Open("bin:T02.txt", "r");
// read file content
char buf[512];
int readCount = file->Read(buf, sizeof(buf));
buf[readCount] = 0;
// log content
nString absPath = pFileServer->ManglePath("bin:T02.txt");
ks->Print("Read File from: %s ", absPath.Get());
ks->Print("File Content: %s", buf);
// close file for read
file->Close();
// release nFile object
file->Release();
/**////
/// -File Operation
///----------------------------------------------------------------------------
/// 销毁KernelServer
n_delete(ks);
getchar();
return 0;
}
/**/ /// -Application
///----------------------------------------------------------------------------
/**/ /* Nebula2 - Tutorial 02 */
/**/ /* 使用FileServer2 */
/**/ /* author: happykevins */
/**/ /****************************************************************************/
/**/ ///----------------------------------------------------------------------------
/// +必要头文件
#include " kernel/nkernelserver.h "
#include " kernel/nfileserver2.h "
#include " kernel/nfile.h "
#include " kernel/nautoref.h "
/**/ /// -必要头文件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
/**/ /// -链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +Application
int main( int argc, const char ** argv)
... {
/**//// 创建KernelServer
nKernelServer* ks = n_new(nKernelServer);
/**////----------------------------------------------------------------------------
/// +获得FileServer2的实例
/// @note:有以下四种方式。在一般情况下他们是等价的
nFileServer2* pFileServer = NULL;
// 1.通过Singleton获得
pFileServer = nFileServer2::Instance();
// 2.通过KernelServer直接获得
pFileServer = ks->GetFileServer();
// 3.通过NOH体系获得
pFileServer = (nFileServer2*)ks->Lookup("sys/servers/file2");
// 4.通过nAutoRef获得(同NOH体系获得法)
nAutoRef<nFileServer2> refFileServer("sys/servers/file2");
if ( refFileServer.isvalid() )
...{
pFileServer = refFileServer.get();
}
/**//// -获得FileServer2的实例
///----------------------------------------------------------------------------
ks->Print("*****Assigns Operation***** ");
/**////----------------------------------------------------------------------------
/// +Assigns
/// @note:
/// 1. Assigns是一种以标识替代绝对路径的技术,它对于应用程序的资源部署非常有用
/// 2. Assigns的变量都以nEnv类型存储在NOH体系的/sys/share/assigns/位置下
/// 3. /sys/share/assigns/home和/sys/share/assigns/bin是两个默认存在的变量,
/// 分别是应用程序根和执行文件所在路径。
/// 4. 在设置完Assign路径后就可以通过"assign:filename"的形式来索引文件了。
///
// 两个默认的assigns
nString home = pFileServer->GetAssign("home");
nString bin = pFileServer->GetAssign("bin");
nString user = pFileServer->GetAssign("user");
nString temp = pFileServer->GetAssign("temp");
ks->Print("home: %s ", home.Get());
ks->Print("bin : %s ", bin.Get() );
ks->Print("user: %s ", user.Get());
ks->Print("temp: %s ", temp.Get());
// 设置proj索引
pFileServer->SetAssign("proj", "d:/");
nString proj = pFileServer->GetAssign("proj");
ks->Print("proj: %s ", proj.Get());
/**////
/// -Assigns
///----------------------------------------------------------------------------
ks->Print("*****File W/R Operation***** ");
/**////----------------------------------------------------------------------------
/// +File Operation
///
// get nFile object
nFile* file = pFileServer->NewFileObject();
// open file for write
file->Open("bin:T02.txt", "w");
// write file
nString output("T02 FileWriteTest!");
file->Write(output.Get(), output.Length());
// close file for write
file->Close();
// open file for read
file->Open("bin:T02.txt", "r");
// read file content
char buf[512];
int readCount = file->Read(buf, sizeof(buf));
buf[readCount] = 0;
// log content
nString absPath = pFileServer->ManglePath("bin:T02.txt");
ks->Print("Read File from: %s ", absPath.Get());
ks->Print("File Content: %s", buf);
// close file for read
file->Close();
// release nFile object
file->Release();
/**////
/// -File Operation
///----------------------------------------------------------------------------
/// 销毁KernelServer
n_delete(ks);
getchar();
return 0;
}
/**/ /// -Application
///----------------------------------------------------------------------------
这篇关于Nebula2探秘02-使用FileServer2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!