C 標準ライブラリ
NagiScript から利用可能な C 標準ライブラリを解説します。
現行仕様(v0.2.0)
C 標準関数は extern "C" fn で宣言して利用します。
概要
NagiScript は C ABI と互換のため、C 標準ライブラリの関数を extern "C" fn で宣言してそのまま利用できます。 ただし、以下の点に注意してください:
- 文字列: C の文字列は null 終端、NagiScript は長さ付き
- ポインタ: ポインタは
*T記法で宣言し、unsafe ブロック内で操作が必要 - メモリ管理: C で確保したメモリは C で解放する
stdio.h
基本的な入出力
ngs
extern "C" fn printf(format: *u8) i32
extern "C" fn scanf(format: *u8) i32
extern "C" fn fprintf(stream: *i32, format: *u8) i32
extern "C" fn fscanf(stream: *i32, format: *u8) i32
使用例
ngs
fn main() {
unsafe {
printf("Hello, World!\n")
}
}
stdlib.h
メモリ管理
ngs
extern "C" fn malloc(size: i32) *u8
extern "C" fn calloc(num: i32, size: i32) *u8
extern "C" fn realloc(ptr: *u8, size: i32) *u8
extern "C" fn free(ptr: *u8)
使用例
ngs
fn main() {
unsafe {
val ptr = malloc(100)
// メモリを使用
*ptr = 42
// メモリを解放
free(ptr)
}
}
文字列変換
ngs
extern "C" fn atoi(s: *u8) i32
extern "C" fn atof(s: *u8) f64
extern "C" fn atol(s: *u8) i32
string.h
文字列操作
ngs
extern "C" fn strlen(s: *u8) i32
extern "C" fn strcpy(dest: *u8, src: *u8) *u8
extern "C" fn strncpy(dest: *u8, src: *u8, n: i32) *u8
extern "C" fn strcat(dest: *u8, src: *u8) *u8
extern "C" fn strcmp(s1: *u8, s2: *u8) i32
extern "C" fn strncmp(s1: *u8, s2: *u8, n: i32) i32
extern "C" fn strchr(s: *u8, c: i32) *u8
extern "C" fn strrchr(s: *u8, c: i32) *u8
extern "C" fn strstr(haystack: *u8, needle: *u8) *u8
使用例
ngs
extern "C" fn strlen(s: *u8) i32
fn main() {
unsafe {
val len = strlen("Hello, World!")
println(len)
}
}
math.h
三角関数
ngs
extern "C" fn sin(x: f64) f64
extern "C" fn cos(x: f64) f64
extern "C" fn tan(x: f64) f64
extern "C" fn asin(x: f64) f64
extern "C" fn acos(x: f64) f64
extern "C" fn atan(x: f64) f64
extern "C" fn atan2(y: f64, x: f64) f64
べき乗・対数
ngs
extern "C" fn pow(base: f64, exp: f64) f64
extern "C" fn sqrt(x: f64) f64
extern "C" fn cbrt(x: f64) f64
extern "C" fn log(x: f64) f64
extern "C" fn log10(x: f64) f64
extern "C" fn log2(x: f64) f64
extern "C" fn exp(x: f64) f64
丸め関数
ngs
extern "C" fn ceil(x: f64) f64
extern "C" fn floor(x: f64) f64
extern "C" fn round(x: f64) f64
extern "C" fn trunc(x: f64) f64
使用例
ngs
extern "C" fn pow(base: f64, exp: f64) f64
extern "C" fn sqrt(x: f64) f64
fn main() {
val y = pow(2.0, 3.0) // 8.0
val z = sqrt(y) // 2.8284271247461903
println(z)
}
time.h
時間操作
ngs
extern "C" fn time(timer: *i32) i32
extern "C" fn difftime(time1: f64, time2: f64) f64
extern "C" fn clock() i32
使用例
ngs
extern "C" fn time(timer: *i32) i32
fn main() {
unsafe {
val now = time(0 as *i32)
println(now)
}
}
ctype.h
文字判定
ngs
extern "C" fn isalpha(c: i32) i32
extern "C" fn isdigit(c: i32) i32
extern "C" fn isalnum(c: i32) i32
extern "C" fn isspace(c: i32) i32
extern "C" fn isupper(c: i32) i32
extern "C" fn islower(c: i32) i32
文字変換
ngs
extern "C" fn toupper(c: i32) i32
extern "C" fn tolower(c: i32) i32
stdint.h
固定幅整数型
ngs
// int8_t, int16_t, int32_t, int64_t
// uint8_t, uint16_t, uint32_t, uint64_t
// intmax_t, uintmax_t
// intmin_t, uintmin_t
使用例
ngs
fn main() {
val a: i32 = 42
val b: u8 = 255
val c: i64 = 999999999999
}
実践的な例: C ライブラリのラッパー
ngs
// libcurl のラッパー
extern "C" fn curl_easy_init() *u8
extern "C" fn curl_easy_setopt(curl: *u8, option: i32) i32
extern "C" fn curl_easy_perform(curl: *u8) i32
extern "C" fn curl_easy_cleanup(curl: *u8)
struct Curl {
handle: *u8
}
fn curl_new(handle: *u8) Curl {
Curl { handle: handle }
}
fn curl_get(curl: Curl) {
unsafe {
curl_easy_setopt(curl.handle, 10002) // CURLOPT_URL
curl_easy_perform(curl.handle)
}
}
fn main() {
unsafe {
val h = curl_easy_init()
val c = curl_new(h)
curl_get(c)
curl_easy_cleanup(h)
}
}
注意事項
- 文字列の変換: C の文字列は null 終端、NagiScript は長さ付き
- メモリ管理: C で確保したメモリは C で解放する
- スレッド安全性: C 関数がスレッド安全かどうかを確認
- 型の整合性: C と NagiScript の型サイズが一致することを確認
