c99의 TOML; v1.0 호환.
C++ 라이브러리를 찾고 있다면 이 래퍼(https://github.com/cktan/tomlcpp)를 시도해 볼 수 있습니다.
자세한 내용은 toml.h
파일을 참조하세요. 다음은 이 구성 파일을 구문 분석하는 간단한 예입니다.
[ server ]
host = " www.example.com "
port = [ 8080 , 8181 , 8282 ]
다음은 파일에서 값을 가져오는 일반적인 단계입니다.
다음은 예제 테이블의 값을 구문 분석하는 예입니다.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "toml.h"
static void error ( const char * msg , const char * msg1 )
{
fprintf ( stderr , "ERROR: %s%sn" , msg , msg1 ? msg1 : "" );
exit ( 1 );
}
int main ()
{
FILE * fp ;
char errbuf [ 200 ];
// 1. Read and parse toml file
fp = fopen ( "sample.toml" , "r" );
if (! fp ) {
error ( "cannot open sample.toml - " , strerror ( errno ));
}
toml_table_t * conf = toml_parse_file ( fp , errbuf , sizeof ( errbuf ));
fclose ( fp );
if (! conf ) {
error ( "cannot parse - " , errbuf );
}
// 2. Traverse to a table.
toml_table_t * server = toml_table_in ( conf , "server" );
if (! server ) {
error ( "missing [server]" , "" );
}
// 3. Extract values
toml_datum_t host = toml_string_in ( server , "host" );
if (! host . ok ) {
error ( "cannot read server.host" , "" );
}
toml_array_t * portarray = toml_array_in ( server , "port" );
if (! portarray ) {
error ( "cannot read server.port" , "" );
}
printf ( "host: %sn" , host . u . s );
printf ( "port: " );
for ( int i = 0 ; ; i ++ ) {
toml_datum_t port = toml_int_at ( portarray , i );
if (! port . ok ) break ;
printf ( "%d " , ( int ) port . u . i );
}
printf ( "n" );
// 4. Free memory
free ( host . u . s );
toml_free ( conf );
return 0 ;
}
TOML 테이블은 문자열 키를 사용하여 조회가 수행되는 사전입니다. 일반적으로 테이블의 모든 액세스 함수 이름은 toml_*_in(...)
입니다.
일반적인 경우에는 키와 해당 콘텐츠 유형을 알고 있으며 다음 기능 중 하나를 사용하여 검색을 수행할 수 있습니다.
toml_string_in ( tab , key );
toml_bool_in ( tab , key );
toml_int_in ( tab , key );
toml_double_in ( tab , key );
toml_timestamp_in ( tab , key );
toml_table_in ( tab , key );
toml_array_in ( tab , key );
정수 인덱스를 사용하여 테이블의 키를 조사할 수도 있습니다.
toml_table_t * tab = toml_parse_file (...);
for ( int i = 0 ; ; i ++ ) {
const char * key = toml_key_in ( tab , i );
if (! key ) break ;
printf ( "key %d: %sn" , i , key );
}
TOML 배열은 정수 인덱스를 사용하여 역참조될 수 있습니다. 일반적으로 배열의 모든 액세스 방법 이름은 toml_*_at()
입니다.
배열의 크기를 얻으려면:
int size = toml_array_nelem ( arr );
배열의 내용을 얻으려면 유효한 인덱스를 사용하고 다음 함수 중 하나를 호출하십시오.
toml_string_at ( arr , idx );
toml_bool_at ( arr , idx );
toml_int_at ( arr , idx );
toml_double_at ( arr , idx );
toml_timestamp_at ( arr , idx );
toml_table_at ( arr , idx );
toml_array_at ( arr , idx );
일부 toml_*_at
및 toml_*_in
함수는 toml_datum_t 구조를 반환합니다. 구조의 ok
플래그는 함수 호출이 성공했는지 여부를 나타냅니다. 그렇다면 컨텐츠의 종류에 해당하는 값을 읽어오시면 됩니다.
예를 들어:
toml_datum_t host = toml_string_in(tab, "host");
if (host.ok) {
printf("host: %sn", host.u.s);
free(host.u.s); /* FREE applies to string and timestamp types only */
}
** 중요: 액세스된 값이 문자열이거나 타임스탬프인 경우 사용 후 각각 free(datum.us)
또는 free(datum.u.ts)
호출해야 합니다. **
일반 메이크 이면 충분합니다. 프로젝트에 toml.c
및 toml.h
파일을 간단히 포함할 수도 있습니다.
make install
호출하면 헤더와 라이브러리 파일이 /usr/local/{include,lib}에 설치됩니다.
또는 make install prefix=/a/file/path
지정하여 /a/file/path/{include,lib}에 설치합니다.
toml-lang/toml-test에서 제공하는 표준 테스트 세트에 대해 테스트하려면 다음을 수행하세요.
% make
% cd test1
% bash build.sh # do this once
% bash run.sh # this will run the test suite
iarna/toml에서 제공하는 표준 테스트 세트에 대해 테스트하려면 다음을 수행하세요.
% make
% cd test2
% bash build.sh # do this once
% bash run.sh # this will run the test suite