circuitpython/py/lexerunix.c

36 lines
804 B
C
Raw Normal View History

#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);
2013-12-29 13:11:05 -05:00
int read_size = read(fd, data, size);
close(fd);
2013-12-29 13:11:05 -05:00
if (read_size != size) {
printf("error reading file %s\n", filename);
m_del(char, data, size);
2013-12-29 13:11:05 -05:00
return NULL;
}
return mp_lexer_new_from_str_len(qstr_from_str(filename), data, size, size);
}
#endif // MICROPY_ENABLE_LEXER_UNIX