micropython/py/lexerunix.c
xbe efe3422394 py: Clean up includes.
Remove unnecessary includes. Add includes that improve portability.
2014-03-17 02:43:40 -07:00

36 lines
804 B
C

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "lexerunix.h"
#if MICROPY_ENABLE_LEXER_UNIX
#include <sys/stat.h>
#include <sys/types.h>
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
return NULL;
}
uint size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
char *data = m_new(char, size);
int read_size = read(fd, data, size);
close(fd);
if (read_size != size) {
printf("error reading file %s\n", filename);
m_del(char, data, size);
return NULL;
}
return mp_lexer_new_from_str_len(qstr_from_str(filename), data, size, size);
}
#endif // MICROPY_ENABLE_LEXER_UNIX