added os module

This commit is contained in:
Sven Vogel 2024-06-03 18:35:23 +02:00
parent af4e99b6da
commit a7bd9c2cc6
9 changed files with 105 additions and 6 deletions

View File

@ -20,3 +20,6 @@ add_library(io ${STDLIB_IO_SOURCE_FILES})
file(GLOB_RECURSE STDLIB_MEM_SOURCE_FILES src/mem/*.c)
add_library(mem ${STDLIB_MEM_SOURCE_FILES})
file(GLOB_RECURSE STDLIB_OS_SOURCE_FILES src/os/*.c)
add_library(os ${STDLIB_OS_SOURCE_FILES})

14
lib/src/capi.h Normal file
View File

@ -0,0 +1,14 @@
//
// Created by servostar on 6/3/24.
//
#ifndef GEMSTONE_STD_LIB_CAPI_H
#define GEMSTONE_STD_LIB_CAPI_H
#if defined(_WIN32) || defined (_WIN64)
#define PLATFORM_WINDOWS
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__)
#define PLATFORM_POSIX
#endif
#endif // GEMSTONE_STD_LIB_CAPI_H

View File

@ -31,7 +31,7 @@ type signed double double float: f128
# String constant
type ref u8: str
type ref u8: cstr
# C style void pointer replacement

View File

@ -6,6 +6,7 @@
#define GEMSTONE_STD_LIB_DEF_H_
#include <stdint.h>
#include <stddef.h>
typedef uint8_t u8;
typedef uint16_t u16;
@ -20,7 +21,7 @@ typedef int64_t i64;
typedef float f32;
typedef double f64;
typedef u8* str;
typedef u8* cstr;
typedef u8* ptr;

View File

@ -1,7 +1,8 @@
#include <io/api.h>
#include <capi.h>
#if defined(_WIN32) || defined (_WIN64)
#if defined(PLATFORM_WINDOWS)
// Compile for Windows
@ -34,7 +35,7 @@ void flush(handle dev) {
FlushFileBuffers((HANDLE) dev);
}
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__)
#elif defined(PLATFORM_POSIX)
// Compile for Linux and BSD

View File

@ -1,7 +1,8 @@
#include <mem/api.h>
#include <capi.h>
#if defined(_WIN32) || defined (_WIN64)
#if defined(PLATFORM_WINDOWS)
#include <Windows.h>
@ -22,7 +23,7 @@ void heap_free(u8* ptr) {
HeapFree(heap, ptr);
}
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__)
#elif defined(PLATFORM_POSIX)
#include <malloc.h>

25
lib/src/os.gem Normal file
View File

@ -0,0 +1,25 @@
# Author: Sven Vogel
# Edited: 03.06.2024
# License: GPL-2.0
# ,----------------------------------------.
# | Operating System |
# `----------------------------------------`
import "def.gem"
# Return a hard coded C string identifying the underlying operating system
# Will return one of the following:
# - "windows" (for Windows 7, Windows 10 and Windows 11)
# - "unix" (for GNU/Linux and BSD)
fun getPlatformName(out cstr: name)
# Return a C string to the value of an environment varible named
# after the C string name.
fun getEnvVar(in cstr: name)(out cstr: value)
# Set the value of an environment variable with name to value.
fun setEnvVar(in cstr: name, in cstr: value)
# Unset a specific environment variable
fun unsetEnvVar(in cstr: name)

18
lib/src/os/api.h Normal file
View File

@ -0,0 +1,18 @@
//
// Created by servostar on 6/3/24.
//
#ifndef GEMSTONE_STD_LIB_OS_H
#define GEMSTONE_STD_LIB_OS_H
#include <def/api.h>
void getPlatformName(cstr* name);
void getEnvVar(cstr name, cstr* value);
void setEnvVar(cstr name, cstr value);
void unsetEnvVar(cstr name);
#endif // GEMSTONE_STD_LIB_OS_H

36
lib/src/os/impl.c Normal file
View File

@ -0,0 +1,36 @@
//
// Created by servostar on 6/3/24.
//
#include <capi.h>
#include <os/api.h>
#if defined(PLATFORM_WINDOWS)
void getPlatformName(cstr* name) {
*name = (u8*) "windows";
}
#elif defined(PLATFORM_POSIX)
void getPlatformName(cstr * name) {
*name = (u8*) "posix";
}
#endif
// Implementation based on libc
#include <stdlib.h>
void getEnvVar(cstr name, cstr* value) {
*value = (cstr) getenv((char*) name);
}
void setEnvVar(cstr name, cstr value) {
setenv((char*) name, (char*) value, true);
}
void unsetEnvVar(cstr name) {
unsetenv((char*) name);
}