Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/file/copy.c2
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

module file_utils;

import string local;

// NOTE: depends on reader + writer
public fn i32 copy_file(const char* source, const char* dest) {
// Note: not most optimal yet
Expand Down
13 changes: 0 additions & 13 deletions common/file/file_utils.c2
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ public fn const char* get_extension(const char* s) {
return ext ? ext : s;
}

// Copy a string with truncation
// return the length of the string if shorter than size
// otherwise truncate the string and return size
// compare the return value with size to detect truncation
fn usize pstrcpy(char* dest, usize size, const char* src) {
usize i;
for (i = 0; i < size; i++) {
if ((dest[i] = src[i]) == '\0') return i;
}
if (size) dest[size - 1] = '\0';
return size;
}

// Copy a directory name with truncation and append a / if not empty
fn usize copy_dirname(char* buf, usize size, const char* dir) {
usize pos = pstrcpy(buf, size, dir);
Expand Down
4 changes: 4 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ warnings:

clean:
rm -rf output

test:
@echo "---- running tests ----"
@./test.sh
8 changes: 4 additions & 4 deletions examples/base64/base64.c2
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const u8 FIRST = '+';
const u8 LAST = 'z';

public fn void encode(const u8* in, u32 len, i8* out) {
u32 i = 0;
u32 j = 0;
u32 i = 0;
u32 j = 0;

for (; i< len; ++i) {
for (; i< len; ++i) {
switch (i % 3) {
case 0:
out[j++] = Lut_enc[(in[i] >> 2) & 0x3F];
Expand All @@ -58,7 +58,7 @@ public fn void encode(const u8* in, u32 len, i8* out) {
out[j++] = Lut_enc[in[i] & 0x3F];
break;
}
}
}
i -= 1; // move back
// check the last and add padding
switch (i % 3) {
Expand Down
112 changes: 0 additions & 112 deletions examples/comment_strip/main.c2

This file was deleted.

34 changes: 17 additions & 17 deletions examples/common/color.c2
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

module color;

public const char[] Black = "\033[0;30m";
public const char[] Red = "\033[0;31m";
public const char[] Green = "\033[0;32m";
public const char[] Yellow = "\033[0;33m";
public const char[] Blue = "\033[0;34m";
public const char[] Magenta = "\033[0;35m";
public const char[] Cyan = "\033[0;36m";
public const char[] Grey = "\033[0;37m";
public const char[] Darkgrey = "\033[01;30m";
public const char[] Bred = "\033[01;31m";
public const char[] Bgreen = "\033[01;32m";
public const char[] Byellow = "\033[01;33m";
public const char[] Bblue = "\033[01;34m";
public const char[] Bmagenta = "\033[01;35m";
public const char[] Bcyan = "\033[01;36m";
public const char[] White = "\033[01;37m";
public const char[] Normal = "\033[0m";
public const char[] Black @(unused) = "\033[0;30m";
public const char[] Red @(unused) = "\033[0;31m";
public const char[] Green @(unused) = "\033[0;32m";
public const char[] Yellow @(unused) = "\033[0;33m";
public const char[] Blue @(unused) = "\033[0;34m";
public const char[] Magenta @(unused) = "\033[0;35m";
public const char[] Cyan @(unused) = "\033[0;36m";
public const char[] Grey @(unused) = "\033[0;37m";
public const char[] Darkgrey @(unused) = "\033[01;30m";
public const char[] Bred @(unused) = "\033[01;31m";
public const char[] Bgreen @(unused) = "\033[01;32m";
public const char[] Byellow @(unused) = "\033[01;33m";
public const char[] Bblue @(unused) = "\033[01;34m";
public const char[] Bmagenta @(unused) = "\033[01;35m";
public const char[] Bcyan @(unused) = "\033[01;36m";
public const char[] White @(unused) = "\033[01;37m";
public const char[] Normal @(unused) = "\033[0m";

123 changes: 87 additions & 36 deletions examples/common/file/reader.c2
Original file line number Diff line number Diff line change
Expand Up @@ -17,76 +17,127 @@ module file_utils;

import libc_fcntl local;
import c_errno local;
import sys_stat local;
import unistd local;
import stdlib;
import stdio local;
import stdlib local;
import string local;

u8 empty;

public const i32 Err_not_a_file = 2001;
u8[1] empty;

public type Reader struct {
void* region;
u32 size;
i32 errno;
}

public fn bool Reader.open(Reader* file, const char* filename) {
public fn bool Reader.open(Reader* file, const char* filename) @(unused) {
file.region = nil;
file.size = 0;
file.errno = 0;

i32 fd = open(filename, O_RDONLY);
if (fd == -1) {
file.errno = *errno2();
file.errno = errno;
return false;
}
bool res = file.openHandle(fd);
close(fd);
return res;
}

Stat statbuf;
i32 err = fstat(fd, &statbuf);
if (err) {
file.errno = *errno2();
return false;
}
public fn bool Reader.openStream(Reader* file, FILE* fp) @(unused) {
char *data = nil;
usize size = 0;

if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
close(fd);
file.errno = Err_not_a_file;
return false;
file.region = empty;
file.size = 0;
file.errno = 0;

if (fseek(fp, 0, SEEK_END) < 0) { // stream is not seekable
char[4096] buf;
usize nread;
while ((nread = fread(buf, 1, elemsof(buf), fp)) > 0) {
data = realloc(data, size + nread + 1);
memcpy(data + size, buf, nread);
size += nread;
}
} else {
i64 file_length = ftell(fp);
usize length = (usize)file_length;
data = malloc(length + 1);
fseek(fp, 0, SEEK_SET);
size = fread(data, 1, length, fp);
}
if (data) {
data[size] = '\0';
file.region = data;
file.size = (u32)size;
}
return true;
}

file.size = (u32)statbuf.st_size;
public fn bool Reader.openHandle(Reader* file, i32 fd) @(unused) {
char *data = nil;
usize size = 0;

if (file.size == 0) {
file.region = &empty;
} else {
file.region = stdlib.malloc(file.size+1);
isize numread = read(fd, file.region, file.size);
if (numread != file.size) return false;
// 0-terminate
u8* ptr = file.region;
ptr[file.size] = 0;
file.region = empty;
file.size = 0;
file.errno = 0;

i64 file_length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
if (file_length < 0) { // file descriptor is not seekable
char[4096] buf;
isize nread;
while ((nread = read(fd, buf, elemsof(buf))) != 0) {
if (nread < 0) {
if (errno == EINTR)
continue;
file.errno = errno;
break;
}
data = realloc(data, size + (usize)nread + 1);
memcpy(data + size, buf, (usize)nread);
size += nread;
}
} else
if (file_length > 0) {
usize length = (usize)file_length;
data = malloc(length + 1);
isize nread;
while (size < length && (nread = read(fd, data + size, length - size)) != 0) {
if (nread < 0) {
if (errno == EINTR)
continue;
file.errno = errno;
break;
}
size += nread;
}
}
if (data) {
data[size] = '\0';
file.region = data;
file.size = (u32)size;
}
close(fd);
return true;
}

public fn void Reader.close(Reader* file) {
if (file.region) {
stdlib.free(file.region);
public fn void Reader.close(Reader* file) @(unused) {
if (file.region && file.region != empty) {
free(file.region);
file.region = nil;
}
}

public fn bool Reader.isOpen(const Reader* file) {
public fn bool Reader.isOpen(const Reader* file) @(unused) {
return file.region != nil;
}

public fn const void* Reader.data(Reader* file) {
public fn const void* Reader.data(Reader* file) @(unused) {
return file.region;
}

public fn bool Reader.isEmpty(const Reader* file) {
public fn bool Reader.isEmpty(const Reader* file) @(unused) {
return file.size == 0;
}


Loading