Lib curl
Console program123 lines of .st198 lines of generated C
fetched: Hello from a local file, fetched by libcurl.
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "C"
standard = "c11"
}
}
lib_provider {
C_lang = {
source = "StructOp/c"
version = "@latest"
}
Libc = {
source = "C/libc"
version = "@latest"
}
Curl = {
source = "C/curl"
version = "@latest"
}
}
}main.st
# libcurl through the provider structop bind writes: read a local file through a
# file:// address (no network), collecting the text in the write callback.
ability "value" "curl" {
type = Curl
}
ability "value" "dir" {
type = String
size = 512
}
data "value" "dir" {
of = ability.value.dir
}
ability "value" "body" {
type = String
size = 256
}
data "value" "body" {
of = ability.value.body
}
ability "value" "code" {
type = CURLcode
}
data "value" "code" {
of = ability.value.code
}
ability "value" "reason" {
type = String
size = 128
}
data "value" "reason" {
of = ability.value.reason
}
ability "getenv" "Dir" {
name = "PWD"
result = ability.value.dir
}
ability "curl_easy_init" "Init" {
result = ability.value.curl
}
ability "curl_setopt_text" "Address" {
curl = ability.value.curl
option = CURLoption.CURLOPT_URL
value = "file://${data.value.dir.current}/hello.txt"
}
ability "curl_setopt_write" "Collect" {
curl = ability.value.curl
option = CURLoption.CURLOPT_WRITEFUNCTION
value = [ability.append.Chunk]
value_result = data.curl_setopt_write_value.Chunk.size * data.curl_setopt_write_value.Chunk.nmemb
}
data "curl_setopt_write_value" "Chunk" {
of = ability.curl_setopt_write.Collect
}
ability "append" "Chunk" {
target = ability.value.body
value = data.curl_setopt_write_value.Chunk.ptr
}
ability "curl_easy_perform" "Fetch" {
curl = ability.value.curl
result = ability.value.code
}
ability "if" "Fetched" {
condition = data.value.code.current == CURLcode.CURLE_OK
then = [ability.print.Body]
else = [ability.curl_easy_strerror.Why, ability.print.Failed]
}
ability "print" "Body" {
message = "fetched:\n${data.value.body.current}"
}
ability "curl_easy_strerror" "Why" {
arg1 = data.value.code.current
result = ability.value.reason
}
ability "print" "Failed" {
message = "failed: ${data.value.reason.current}"
}
ability "curl_easy_cleanup" "Done" {
curl = ability.value.curl
}Input, plan and generated C#
bind/libc.bind.st · hints for structop bind
bind/libc.bind.st · hints for structop bind
# The C standard library: macOS and Linux spell these parameters differently (or not at all),
# so names gives every kept function the same attribute names on both.
bind "libc" {
header = "stdlib.h"
version = "1.0.0"
prefix = ["qsort", "abs", "atoi", "getenv"]
skip = ["qsort_r", "qsort_b", "getenv_r"]
}
function "qsort" {
names = ["base", "count", "width", "compar"]
array = [{ data = "base", count = "count", size = "width", of = Int32 }]
points_to = Int32
}
function "abs" {
names = ["value"]
}
function "atoi" {
names = ["text"]
}
function "getenv" {
names = ["name"]
returns_text = "library"
}bind/curl.bind.st · hints for structop bind
bind/curl.bind.st · hints for structop bind
# libcurl: curl_easy_setopt takes its value through "...", so each kind of value is a variant
# with its own ability kind; the write callback gets the program state through CURLOPT_WRITEDATA.
bind "curl" {
header = "curl/curl.h"
package = "libcurl"
prefix = ["curl_easy_"]
}
function "curl_easy_setopt" {
variant "curl_setopt_text" {
value = String
}
variant "curl_setopt_long" {
value = Int64
}
variant "curl_setopt_write" {
value = callback("size_t (*)(char *, size_t, size_t, void *)")
user_data = "CURLOPT_WRITEDATA"
arg_names = { value = ["ptr", "size", "nmemb", ""] }
text = [{ data = "ptr", length = ["size", "nmemb"] }]
}
}structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.curl
+ ability.value.dir
+ ability.value.body
+ ability.value.code
+ ability.value.reason
+ ability.getenv.Dir
+ ability.curl_easy_init.Init
+ ability.curl_setopt_text.Address
+ ability.curl_setopt_write.Collect
+ ability.append.Chunk
+ ability.curl_easy_perform.Fetch
+ ability.if.Fetched
+ ability.print.Body
+ ability.curl_easy_strerror.Why
+ ability.print.Failed
+ ability.curl_easy_cleanup.Done
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.c
To inspect this generated file, select C in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
structop bind bind/libc.bind.st # from the repository root
structop bind bind/curl.bind.st # from the repository root
cd lib-curl # a folder holding the .st files above
structop init
structop plan
structop apply