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 segmentsfunc basename(path: String) -> String # File name from pathfunc dirname(path: String) -> String # Directory from pathfunc ext(path: String) -> String # File extensionfunc stem(path: String) -> String # File name without extensionfunc strip_trailing(path: String) -> String # Remove trailing separatorfunc strip_leading(path: String) -> String # Remove leading separatorExamples
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 0lib/http.tg — HTTP Client
A simple HTTP client for GET and POST requests.
API
func get(url: String) -> String # HTTP GETfunc post(url: String, body: String) -> String # HTTP POSTfunc request(method: String, url: String, body: String) -> String # Generic requestExamples
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 0lib/time.tg — Time Utilities
API
func now_ms() -> i32 # Current time in millisecondsfunc sleep_ms(ms: i32) -> i32 # Sleep for specified millisecondsExamples
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 0lib/fs.tg — File System
File I/O
func read_text(path: String) -> String # Read entire file as textfunc write_text(path: String, text: String) -> i32 # Write text to filefunc exists(path: String) -> i32 # Check if path exists (1=yes)func mkdir(path: String) -> i32 # Create directoryfunc ensure_dir(path: String) -> i32 # Create dir if missingfunc list_dir(path: String) -> String # List directory contentsfunc move(src: String, dst: String) -> i32 # Move/rename fileBinary File Operations
func open_binary(path: String, mode: String) -> ptr # Open file in binary modefunc open_write(path: String) -> ptr # Shorthand: open for writingfunc open_read(path: String) -> ptr # Shorthand: open for readingfunc write_bytes(handle: ptr, buffer: String) -> i32 # Write bytesfunc read_bytes(handle: ptr, size: i32) -> String # Read bytesfunc seek(handle: ptr, offset: i32, whence: i32) -> i32 # Seek in filefunc close(handle: ptr) -> i32 # Close file handleExamples
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 0import 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