2013-10-20 13:41:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
2014-01-07 15:20:33 +00:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2013-10-20 13:41:27 +00:00
|
|
|
#include "lexer.h"
|
2014-01-08 15:28:26 +00:00
|
|
|
#include "lexerunix.h"
|
2013-10-20 13:41:27 +00:00
|
|
|
|
2014-01-07 15:20:33 +00:00
|
|
|
#if MICROPY_ENABLE_LEXER_UNIX
|
|
|
|
|
2014-03-16 07:14:26 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
|
2013-10-20 13:41:27 +00:00
|
|
|
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 18:11:05 +00:00
|
|
|
int read_size = read(fd, data, size);
|
2013-10-20 13:41:27 +00:00
|
|
|
close(fd);
|
2013-12-29 18:11:05 +00:00
|
|
|
if (read_size != size) {
|
|
|
|
printf("error reading file %s\n", filename);
|
2013-12-29 19:33:23 +00:00
|
|
|
m_del(char, data, size);
|
2013-12-29 18:11:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-20 13:41:27 +00:00
|
|
|
|
2014-01-25 13:51:19 +00:00
|
|
|
return mp_lexer_new_from_str_len(qstr_from_str(filename), data, size, size);
|
2013-10-20 13:41:27 +00:00
|
|
|
}
|
2014-01-03 14:03:48 +00:00
|
|
|
|
2014-01-07 15:20:33 +00:00
|
|
|
#endif // MICROPY_ENABLE_LEXER_UNIX
|