見出し画像

Notes C API探訪: データベース情報バッファのラップ

以前紹介した以下の記事で、データベースのタイトルを含む情報を出し入れする関数をご紹介しました。

今回は、これをnxpp::Databaseと新しいクラスにラップしてみたいと思います。なお、APIの働きについては、先の記事をご確認ください。

まず、nxpp::Databaseを拡張して、データベース情報バッファを取得したり、設定したりします。

// nxpp/include/nxpp/nxpp_database.hpp

#ifndef NXPP_DATABASE_HPP
#define NXPP_DATABASE_HPP

#include "./nxpp_pathnet.hpp"
#include "./nxpp_dbinfo.hpp"
#include <iostream>

#ifdef NT
#pragma pack(push, 1)
#endif

#include <global.h>
#include <nsfdb.h>

#ifdef NT
#pragma pack(pop)
#endif

namespace nxpp {

class Database;
using DatabasePtr = std::shared_ptr<Database>;
using Db = Database;
using DbPtr = DatabasePtr;

/**
* @brief データベース/ディレクトリクラス
*/
class Database
{
// ...

 DbInfo getDbInfo() const {
   return Database::getDbInfo(handle_);
 }

 void setDbInfo(DbInfo &dbInfo) {
   return Database::setDbInfo(handle_, dbInfo);
 }

 static DbInfo getDbInfo(DBHANDLE hDB) {
   DbInfo dbInfo;
   Status status = NSFDbInfoGet(hDB, dbInfo.data());
   if (!status) { throw status; }
   return dbInfo;
 }

 static void setDbInfo(DBHANDLE hDB, const DbInfo &dbInfo) {
   Status status = NSFDbInfoSet(hDB, const_cast<char*>(dbInfo.data()));
   if (!status) { throw status; }
 }
};

} // namespace nxpp

#endif // NXPP_DATABASE_HPP

そして、これとは別にnxpp::DbInfoクラスを定義します。

// nxpp/include/nxpp/nxpp_dbinfo.hpp

#ifndef NXPP_DBINFO_HPP
#define NXPP_DBINFO_HPP

#include "./lmbcs/nxpp_lmbcs.hpp"

#ifdef NT
#pragma pack(push, 1)
#endif

#include <global.h>
#include <nsfdb.h>

#ifdef NT
#pragma pack(pop)
#endif

namespace nxpp {

/**
* @brief データベース情報バッファ
*/
class DbInfo
{
 char data_[NSF_INFO_SIZE];

public:
 /**
  * @brief デフォルトコンストラクタ
  */
 DbInfo() : data_("") {}

 /**
  * @brief バッファへのアクセス(const)
  * @return バッファポインタ
  */
 const char *data() const { return data_; }
 
 /**
  * @brief バッファへのアクセス(constなし)
  * @return バッファポインタ
  */
 char *data() { return data_; }

 /**
  * @brief 情報の部分取り出し
  * @param what 情報タイプ
  * @return 情報バッファの部分データ
  */
 Lmbcs extract(WORD what) const {
   char part[NSF_INFO_SIZE];
   NSFDbInfoParse(const_cast<char*>(data_), what, part, NSF_INFO_SIZE - 1);
   part[NSF_INFO_SIZE - 1] = '\0';
   return Lmbcs(part);
 }

 /**
  * @brief 情報の部分修正
  * @param what 情報タイプ
  * @param part 修正データ
  */
 void modify(WORD what, const Lmbcs &part) {
   NSFDbInfoModify(data_, what, part.data());
 }
};

} // namespace nxpp

#endif // NXPP_DBINFO_HPP

このクラスでは、バッファを保持して、タイトルなどの部分データを抽出したり、書き換えたりすることができます。

これらの使い方は、次のようになります。

nxpp::DbPtr dbPtr = nxpp::Database::open("hoge.nsf", "server");
nxpp::DbInfo dbInfo = dbPtr->getDbInfo();
std::wcout << fromLmbcs(dbInfo.extract(INFOPARSE_TITLE)) << std::endl;
dbInfo.modify(type, "New Db Title");
dbPtr->setDbInfo(dbInfo);

まとめ

実はこのラップコードでタイトルを変更してみると、Notesクライアントのワークスペースに貼り付けられているDBアイコンのタイトルは変わりませんでした。しかし、一度アイコンをワークスペースから外し、再度ワークスペースに貼り付けると、APIからの変更が反映されていました(カテゴリーやクラス、設計クラスは問題ありません)。
現時点では憶測ですが、Notesクライアントのワークスペースはdesktop8.NDKやCache.ndkなどで管理されていると聞いたことがあります。おそらくその影響を受けているのではないかと思われます。

いいなと思ったら応援しよう!