Skip to content

Commit

Permalink
Merge pull request lcompilers#277 from Thirumalai-Shaktivel/os
Browse files Browse the repository at this point in the history
Initial implementation of file accessing using OS Module
  • Loading branch information
certik committed Mar 29, 2022
2 parents c133bf6 + c5d5f67 commit 7d250c7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test_numpy_01.py",
"test_numpy_02.py",
"test_random.py",
"test_os.py",
"test_builtin.py",
"test_builtin_abs.py",
"test_builtin_bool.py",
Expand Down
14 changes: 14 additions & 0 deletions integration_tests/test_os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ltypes import i64
from os import (open, read, close)

def test():
path: str
path = "integration_tests/test_os.py"
fd: i64
n: i64
fd = open(path, "r")
n = 100
print(read(fd, n))
close(fd)

test()
35 changes: 35 additions & 0 deletions src/runtime/impure/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,38 @@ LFORTRAN_API void _lfortran_dp_rand_num(double *x) {
srand(time(0));
*x = rand() / (double) RAND_MAX;
}

LFORTRAN_API int64_t _lpython_open(char *path, char *flags)
{
FILE *fd;
fd = fopen(path, flags);
if (!fd)
{
printf("Error in opening the file!\n");
perror(path);
exit(1);
}
return (int64_t)fd;
}

LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n)
{
char *c = (char *) calloc(n, sizeof(char));
if (fd < 0)
{
printf("Error in reading the file!\n");
exit(1);
}
int x = fread(c, 1, n, (FILE*)fd);
c[x] = '\0';
return c;
}

LFORTRAN_API void _lpython_close(int64_t fd)
{
if (fclose((FILE*)fd) != 0)
{
printf("Error in closing the file!\n");
exit(1);
}
}
3 changes: 3 additions & 0 deletions src/runtime/impure/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ LFORTRAN_API void _lfortran_i64sys_clock(
uint64_t *count, int64_t *rate, int64_t *max);
LFORTRAN_API void _lfortran_sp_rand_num(float *x);
LFORTRAN_API void _lfortran_dp_rand_num(double *x);
LFORTRAN_API int64_t _lpython_open(char *path, char *flags);
LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n);
LFORTRAN_API void _lpython_close(int64_t fd);

#ifdef __cplusplus
}
Expand Down
32 changes: 32 additions & 0 deletions src/runtime/os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from ltypes import i32, i64, ccall

def open(path: str, flag: str) -> i64:
"""
Returns the file descriptor for the newly opened file
"""
return _lpython_open(path, flag)

@ccall
def _lpython_open(path: str, flag: str) -> i64:
pass

def read(fd: i64, n: i64) -> str:
"""
Reads at most `n` bytes from file descriptor
"""
return _lpython_read(fd, n)

@ccall
def _lpython_read(fd: i64, n: i64) -> str:
pass

def close(fd: i64):
"""
Closes the file descriptor
"""
_lpython_close(fd)
return

@ccall
def _lpython_close(fd: i64):
pass

0 comments on commit 7d250c7

Please sign in to comment.