Skip to content

Path, HTTP & Time

Path, HTTP & Time

lib/path.tg — Path Manipulation

Functions for working with file system paths.

API

func join(left: String, right: String) -> String # Join two path segments
func basename(path: String) -> String # File name from path
func dirname(path: String) -> String # Directory from path
func ext(path: String) -> String # File extension
func stem(path: String) -> String # File name without extension
func strip_trailing(path: String) -> String # Remove trailing separator
func strip_leading(path: String) -> String # Remove leading separator

Examples

path_ops.tg
import path
func main() -> i32:
let full = path.join("src", "main.tg")
print(full) # Output: src/main.tg
print(path.basename("src/main.tg")) # Output: main.tg
print(path.dirname("src/main.tg")) # Output: src
print(path.ext("src/main.tg")) # Output: tg
print(path.stem("src/main.tg")) # Output: main
return 0

lib/http.tg — HTTP Client

A simple HTTP client for GET and POST requests.

API

func get(url: String) -> String # HTTP GET
func post(url: String, body: String) -> String # HTTP POST
func request(method: String, url: String, body: String) -> String # Generic request

Examples

http_example.tg
import http
func main() -> i32:
# GET request
let response = http.get("https://httpbin.org/get")
print(response)
# POST request
let result = http.post("https://httpbin.org/post", "key=value")
print(result)
return 0

lib/time.tg — Time Utilities

API

func now_ms() -> i32 # Current time in milliseconds
func sleep_ms(ms: i32) -> i32 # Sleep for specified milliseconds

Examples

time_example.tg
import time
func main() -> i32:
let start = time.now_ms()
# Do some work
let i = 0
while (i < 1000):
i = i + 1
let elapsed = time.now_ms() - start
print(elapsed) # Prints elapsed time in ms
# Pause execution
time.sleep_ms(500) # Sleep for 500ms
return 0

lib/fs.tg — File System

File I/O

func read_text(path: String) -> String # Read entire file as text
func write_text(path: String, text: String) -> i32 # Write text to file
func exists(path: String) -> i32 # Check if path exists (1=yes)
func mkdir(path: String) -> i32 # Create directory
func ensure_dir(path: String) -> i32 # Create dir if missing
func list_dir(path: String) -> String # List directory contents
func move(src: String, dst: String) -> i32 # Move/rename file

Binary File Operations

func open_binary(path: String, mode: String) -> ptr # Open file in binary mode
func open_write(path: String) -> ptr # Shorthand: open for writing
func open_read(path: String) -> ptr # Shorthand: open for reading
func write_bytes(handle: ptr, buffer: String) -> i32 # Write bytes
func read_bytes(handle: ptr, size: i32) -> String # Read bytes
func seek(handle: ptr, offset: i32, whence: i32) -> i32 # Seek in file
func close(handle: ptr) -> i32 # Close file handle

Examples

file_io.tg
import "std/fs.tg" as fs
func main() -> i32:
# Write a file
print("Writing to test.txt...")
fs.write_text("test.txt", "Hello Thagore System!")
# Read a file
let content = fs.read_text("test.txt")
print(content)
# Check existence
if (fs.exists("test.txt") == 1):
print("File exists!")
# Create directory
fs.ensure_dir("output")
return 0
binary_io.tg
import fs
func main() -> i32:
# Binary write
let f = fs.open_write("data.bin")
fs.write_bytes(f, "binary content")
fs.close(f)
# Binary read
let r = fs.open_read("data.bin")
let data = fs.read_bytes(r, 14)
print(data)
fs.close(r)
return 0