0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claudeにファイルの読み込みについて聞いてみた

0
Last updated at Posted at 2026-07-06

ClaudeにTOMLファイル読み込みについて聞いた

$ git clone https://github.com/ToruNiina/toml11
$ g++ -std=c++11 -O2 -Wall -I toml11/include -o toml_test toml_main.cpp toml_config.cpp
// toml_config.h : テストパターン設定TOMLを読み込むクラス(toml11バックエンド)
// toml11: https://github.com/ToruNiina/toml11 (MITライセンス、C++11以上、ヘッダオンリー)
#ifndef TOML_CONFIG_H
#define TOML_CONFIG_H

#include <string>
#include <toml.hpp>

class TomlConfig {
public:
  TomlConfig( void );
  TomlConfig( const char *fname );
  ~TomlConfig( void );

  bool load( const char *fname );
  void release( void );
  bool isLoaded( void ) const;

  int         getSectionNum( void ) const;
  std::string getSectionName( int idx ) const;
  bool        hasSection( const char *section ) const;
  bool        hasKey( const char *section, const char *key ) const;
  int         getKeyNum( const char *section ) const;
  std::string getKeyName( const char *section, int idx ) const;

  int         getInt( const char *section, const char *key, int defval ) const;
  std::string getStr( const char *section, const char *key, const char *defval ) const;

private:
  // ordered_type_config: セクションとキーのファイル記載順を保持する
  typedef toml::basic_value<toml::ordered_type_config> Value;

  bool  loaded_;
  Value root_;

  const Value *findSec( const char *section ) const;
  const Value *findVal( const char *section, const char *key ) const;
};

#endif
// toml_config.cpp
#include <cstdio>
#include "toml_config.h"

TomlConfig::TomlConfig( void )
{
  loaded_ = false;
}

TomlConfig::TomlConfig( const char *fname )
{
  loaded_ = false;
  load(fname);
}

TomlConfig::~TomlConfig( void )
{
  release();
}

bool TomlConfig::load( const char *fname )
{
  release();

  try {
    root_ = toml::parse<toml::ordered_type_config>(fname);
  } catch( const std::exception &e ) {
    // 構文エラー時はtoml11が行番号と位置指示付きのメッセージを生成する
    fprintf(stderr, "TomlConfig::load: %s\n", e.what());
    return false;
  }

  loaded_ = true;
  return true;
}

void TomlConfig::release( void )
{
  root_   = Value();
  loaded_ = false;
}

bool TomlConfig::isLoaded( void ) const
{
  return loaded_;
}

int TomlConfig::getSectionNum( void ) const
{
  if( !loaded_||!root_.is_table() ) {
    return 0;
  }
  const auto &tbl = root_.as_table();
  int n = 0;
  for( auto it=tbl.begin(); it!=tbl.end(); ++it ) {
    if( it->second.is_table() ) {
      n++;
    }
  }
  return n;
}

std::string TomlConfig::getSectionName( int idx ) const
{
  if( loaded_&&root_.is_table() ) {
    const auto &tbl = root_.as_table();
    int n = 0;
    for( auto it=tbl.begin(); it!=tbl.end(); ++it ) {
      if( it->second.is_table() ) {
        if( n==idx ) {
          return it->first;
        }
        n++;
      }
    }
  }
  fprintf(stderr, "TomlConfig::getSectionName: out of range idx=%d (num=%d)\n",
          idx, getSectionNum());
  return "";
}

bool TomlConfig::hasSection( const char *section ) const
{
  return findSec(section)!=NULL;
}

bool TomlConfig::hasKey( const char *section, const char *key ) const
{
  return findVal(section, key)!=NULL;
}

int TomlConfig::getKeyNum( const char *section ) const
{
  const Value *sec = findSec(section);
  if( sec==NULL ) {
    return 0;
  }
  return (int)sec->as_table().size();
}

std::string TomlConfig::getKeyName( const char *section, int idx ) const
{
  const Value *sec = findSec(section);
  if( sec!=NULL ) {
    const auto &tbl = sec->as_table();
    int n = 0;
    for( auto it=tbl.begin(); it!=tbl.end(); ++it ) {
      if( n==idx ) {
        return it->first;
      }
      n++;
    }
  }
  fprintf(stderr, "TomlConfig::getKeyName: out of range [%s] idx=%d\n", section, idx);
  return "";
}

int TomlConfig::getInt( const char *section, const char *key, int defval ) const
{
  const Value *v = findVal(section, key);
  if( v==NULL ) {
    return defval;
  }
  if( v->is_integer() ) {
    return (int)v->as_integer();
  }
  if( v->is_boolean() ) {
    return v->as_boolean() ? 1 : 0;
  }
  fprintf(stderr, "TomlConfig::getInt: [%s] %s is not an integer, use default %d\n",
          section, key, defval);
  return defval;
}

std::string TomlConfig::getStr( const char *section, const char *key, const char *defval ) const
{
  const Value *v = findVal(section, key);
  if( v==NULL ) {
    return defval;
  }
  if( v->is_string() ) {
    return v->as_string();
  }
  if( v->is_integer() ) {
    return std::to_string(v->as_integer());
  }
  if( v->is_floating() ) {
    return std::to_string(v->as_floating());
  }
  if( v->is_boolean() ) {
    return v->as_boolean() ? "true" : "false";
  }
  fprintf(stderr, "TomlConfig::getStr: [%s] %s is not a string, use default \"%s\"\n",
          section, key, defval);
  return defval;
}

const TomlConfig::Value *TomlConfig::findSec( const char *section ) const
{
  if( !loaded_||!root_.is_table() ) {
    return NULL;
  }
  const auto &tbl = root_.as_table();
  for( auto it=tbl.begin(); it!=tbl.end(); ++it ) {
    if( it->first==section&&it->second.is_table() ) {
      return &it->second;
    }
  }
  return NULL;
}

const TomlConfig::Value *TomlConfig::findVal( const char *section, const char *key ) const
{
  const Value *sec = findSec(section);
  if( sec==NULL ) {
    return NULL;
  }
  const auto &tbl = sec->as_table();
  for( auto it=tbl.begin(); it!=tbl.end(); ++it ) {
    if( it->first==key ) {
      return &it->second;
    }
  }
  return NULL;
}
// toml_main.cpp : TomlConfigの使用例(xlsx2toml.py生成ファイルの読み出し)
#include <cstdio>
#include "toml_config.h"

int main( int argc, char *argv[] )
{
  const char *fname = "hoge.toml";
  if( argc>=2 ) {
    fname = argv[1];
  }

  TomlConfig cfg(fname);
  if( !cfg.isLoaded() ) {
    return 1;
  }

  // 先頭セクション名=パターン名
  std::string pat   = cfg.getSectionName(0);
  int         rnd   = cfg.getInt(pat.c_str(), "RAND", 0);
  int         frame = cfg.getInt(pat.c_str(), "frame", 0);
  printf("pattern=%s  RAND=%d  frame=%d\n", pat.c_str(), rnd, frame);

  for( int i=0; i<frame; i++ ) {
    char sec[64];
    snprintf(sec, sizeof(sec), "FRAME_%d", i);
    if( !cfg.hasSection(sec) ) {
      fprintf(stderr, "missing section [%s]\n", sec);
      return 1;
    }
    printf("[%s] frame_name=%s\n", sec, cfg.getStr(sec, "frame_name", "(none)").c_str());
    for( int k=0; k<cfg.getKeyNum(sec); k++ ) {
      std::string key = cfg.getKeyName(sec, k);
      if( key=="frame_name" ) {
        continue;
      }
      printf("  %-20s = %d\n", key.c_str(), cfg.getInt(sec, key.c_str(), 0));
    }
  }

  return 0;
}

Claudeにjsonファイル読み込みについて聞いてみた

Claudeにyamlファイル読み込みについて聞いてみた

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?