1/*
2 * Copyright (C) 2020-2022 The opuntiaOS Project Authors.
3 * + Contributed by Nikita Melekhin <nimelehin@gmail.com>
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include <libfoundation/json/Lexer.h>
10#include <unistd.h>
11
12namespace LFoundation::Json {
13
14int Lexer::set_file(const std::string& filepath)
15{
16 char tmpbuf[1024];
17 m_pointer = 0;
18
19 int fd = open(filepath.c_str(), O_RDONLY);
20 if (fd < 0) {
21 return -1;
22 }
23
24 ssize_t read_cnt;
25 while ((read_cnt = read(fd, tmpbuf, sizeof(tmpbuf)))) {
26 if (read_cnt <= 0) {
27 return -2;
28 }
29 size_t buf_size = m_text_data.size();
30 m_text_data.resize(buf_size + read_cnt);
31 memcpy((uint8_t*)&m_text_data.data()[buf_size], (uint8_t*)tmpbuf, read_cnt);
32 if (read_cnt < sizeof(tmpbuf)) {
33 break;
34 }
35 }
36
37 if (m_text_data.empty()) {
38 return -3;
39 }
40
41 m_text_data.push_back('\0');
42 close(fd);
43 return 0;
44}
45
46}