LoginSignup
1
3

C/C++ iniファイルライブラリ

Last updated at Posted at 2023-11-27

C/C++ iniファイルライブラリ

概要

ライブラリ

  • inih:Cのライブラリ。組込みシステムに相応しい
  • iniparser:Cのライブラリ。使いやすい、オープンソース。二つHeaderファイルと二つCファイル。但し、Linux限定使用
  • simpleini:C++のライブラリ。使いやすい、オープンソース。二つHeaderファイルと二つCファイル

ini

構成

  • section
  • key
  • value

iniファイルサンプル

[message]
name = 田中
age = 25
height = 173.2

;ここはコメントを書けます

[server]
ip = 1.0.0.1
port = 6666

inih

inihライブラリは、INIファイルの読み書きを簡単に行えるC++のシンプルで軽量なライブラリです。以下にinihライブラリの詳細とその使用方法を説明します。

概要

inih(INI Not Invented Here)は、C++でINIファイルを読み書きするための小さなヘッダオンリーのライブラリです。以下はinihライブラリの主な特徴です:

  • シンプル: ヘッダオンリーであり、単一のヘッダファイルで使用できる。
  • 軽量: 無駄のないコードで構成されており、非常に高速。
  • 依存関係なし: 標準C++ライブラリ以外の依存関係はない。
  • UTF-8対応: UTF-8エンコーディングのINIファイルをサポート。

ライブラリのインストール
inihライブラリはGitHubから取得できます。以下はインストール方法の例です。

# GitHubリポジトリからクローンする
git clone https://github.com/benhoyt/inih.git

使用

必要なファイルは、**ini.hINIReader.h**です。これらのファイルをプロジェクトに追加します。

#include <iostream>
#include "INIReader.h"

int main() {
    std::string settingsPath = "settings.ini";
    INIReader reader(settingsPath);

    if (reader.ParseError() < 0) {
        std::cerr << "Can't load " << settingsPath << "\n";
        return 1;
    }

    std::string background = reader.Get("Colors", "background", "ffffff");
    std::string text = reader.Get("Colors", "text", "000000");
    std::string highlight = reader.Get("Colors", "highlight", "ff0000");
    int width = reader.GetInteger("Window", "width", 800);
    int height = reader.GetInteger("Window", "height", 600);
    bool resizable = reader.GetBoolean("Window", "resizable", true);

    std::cout << "Colors.background = " << background << "\n";
    std::cout << "Colors.text = " << text << "\n";
    std::cout << "Colors.highlight = " << highlight << "\n";
    std::cout << "Window.width = " << width << "\n";
    std::cout << "Window.height = " << height << "\n";
    std::cout << "Window.resizable = " << (resizable ? "true" : "false") << "\n";

    return 0;
}

INIReader クラスの詳細

INIReader クラスは、INIファイルの読み込みに使用されます。以下は主なメソッドの説明です:

  • INIReader(const std::string& filename): ファイルを読み込み、解析します。
  • int ParseError() const: 解析エラーが発生した場合、負の値を返します。
  • std::string Get(const std::string& section, const std::string& name, const std::string&
  • default_value) const: 指定したセクションとキーの値を取得します。値が存在しない場合は、default_valueを返します。
  • int GetInteger(const std::string& section, const std::string& name, int default_value) const: 整数値を取得します。
  • bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const: ブール値を取得します。

simpleini

iniスタイルの設定ファイルを読み書きするためのシンプルなAPIを提供するクロスプラットフォーム・ライブラリです。 ASCII、MBCS、Unicode形式のデータファイルをサポートしています。 どのプラットフォームにも移植できるように設計されており、Windows、WinCE、Linuxでテスト済みです。 オープンソースとしてMITライセンスを使用し、無料で配布されています。

機能概要

  • MITライセンスにより、すべてのソフトウェア(GPLや商用ソフトウェアを含む)で自由に使用可能
  • マルチプラットフォーム(Windows 95からWindows 10、Windows CE、Linux、Unix)
  • iniスタイルの設定ファイルのロードと保存
  • 設定ファイルはすべてのプラットフォームで任意の改行形式を使用可能
  • ファイル形式を自由に選択
  • セクションのないキー/値、値のないキー
  • セクション、キー、値の周りの空白の除去
  • 複数行の値(改行文字を埋め込んだ値)のサポート
  • 同じ名前の複数のキーのサポート(オプション
  • セクションとキーの大文字と小文字を区別しないオプション(ASCII文字のみ)
  • ファイルを読み込んだときに、セクションとキーを同じ順序で保存します。
  • ファイル、セクション、キーのコメントを可能な限り保存
  • charまたはwchar_tプログラミング・インターフェースの両方をサポート
  • MBCS(システム・ロケール設定)とUTF-8ファイル・エンコーディングの両方をサポートする。
  • Linux/Unixでは、UTF-8ファイルを読み込むためにシステムロケールがUTF-8である必要はありません。
  • セクション、キー、値、コメントにおける非アスキー文字のサポート。
  • ユーザー定義のコンバータ・クラスによる、非標準文字型やファイル・エンコーディングのサポート。
  • プログラムによる値の追加/変更のサポート
  • ほとんどのコンパイラで警告なしにコンパイルできる。

DownLoad

https://github.com/brofield/simpleini

https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2445123/cc322384-f861-4743-e554-9e62cd52180b.png

使い方

header

#include "SimpleIni.h"

ini読込


CSimpleIniA ini;// ファイル

// iniファイル
SI_Error rc;
rc = ini.LoadFile(FILE_NAME);	// 別の方法:SI_Error LoadFile(FILE * a_fpFile);
if (rc < 0) {
	printf("失敗!\\n", FILE_NAME);
	return -1;
}


using SI_Error = int;

constexpr int SI_OK = 0;        //!< No error
constexpr int SI_UPDATED = 1;   //!< An existing value was updated
constexpr int SI_INSERTED = 2;  //!< A new value was inserted

// note: test for any error with (retval < 0)
constexpr int SI_FAIL = -1;     //!< Generic failure
constexpr int SI_NOMEM = -2;    //!< Out of memory error
constexpr int SI_FILE = -3;     //!< File error (see errno for detail error)

設定


// UTF-8
ini.SetUnicode(true);

ini.SetMultiKey(false);

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3