Skip to content
Snippets Groups Projects
Select Git revision
  • 76f8cedb52613e27d4d5a031899154e31e00b045
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

lexerunix.c

Blame
  • user avatar
    Paul Sokolovsky authored
    91108960
    History
    lexerunix.c 778 B
    #include <stdint.h>
    #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
    
    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