1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 use crate::adapter::{
17 create_dir, error_control, get_parent, next_line, reader_iterator, drop_reader_iterator, seek, cut_file_name,
18 MakeDirectionMode, SeekPos, Str,
19 };
20 use libc::{c_char, c_int, c_longlong, c_void};
21 use std::ffi::CString;
22 use std::ptr::null_mut;
23
24 #[no_mangle]
ReaderIterator(path: *const c_char) -> *mut c_void25 pub unsafe extern "C" fn ReaderIterator(path: *const c_char) -> *mut c_void {
26 match reader_iterator(path) {
27 Ok(sv) => sv,
28 Err(e) => {
29 error_control(e);
30 null_mut()
31 }
32 }
33 }
34
35 #[no_mangle]
DropReaderIterator(iter: *mut c_void)36 pub unsafe extern "C" fn DropReaderIterator(iter: *mut c_void) {
37 drop_reader_iterator(iter)
38 }
39
40 #[no_mangle]
NextLine(iter: *mut c_void) -> *mut Str41 pub unsafe extern "C" fn NextLine(iter: *mut c_void) -> *mut Str {
42 match next_line(iter) {
43 Ok(s) => s,
44 Err(e) => {
45 error_control(e);
46 null_mut()
47 }
48 }
49 }
50
51 #[no_mangle]
Lseek(fd: i32, offset: i64, pos: SeekPos) -> c_longlong52 pub extern "C" fn Lseek(fd: i32, offset: i64, pos: SeekPos) -> c_longlong {
53 match seek(fd, offset, pos) {
54 Ok(pos) => pos as c_longlong,
55 Err(e) => unsafe {
56 error_control(e);
57 -1
58 },
59 }
60 }
61
62 #[no_mangle]
Mkdirs(path: *const c_char, mode: MakeDirectionMode) -> c_int63 pub extern "C" fn Mkdirs(path: *const c_char, mode: MakeDirectionMode) -> c_int {
64 match create_dir(path, mode) {
65 Ok(_) => 0,
66 Err(e) => unsafe {
67 error_control(e);
68 -1
69 },
70 }
71 }
72
73 #[no_mangle]
GetParent(fd: i32) -> *mut Str74 pub extern "C" fn GetParent(fd: i32) -> *mut Str {
75 match get_parent(fd) {
76 Ok(str) => str,
77 Err(e) => {
78 unsafe {
79 error_control(e);
80 }
81 null_mut()
82 }
83 }
84 }
85
86 #[no_mangle]
StrFree(str: *mut Str)87 pub unsafe extern "C" fn StrFree(str: *mut Str) {
88 if !str.is_null() {
89 let string = Box::from_raw(str);
90 let _ = CString::from_raw(string.str);
91 }
92 }
93
94 #[no_mangle]
CutFileName(path: *const c_char, size: usize) -> *mut Str95 pub unsafe extern "C" fn CutFileName(path: *const c_char, size: usize) -> *mut Str {
96 cut_file_name(path, size)
97 }
98