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, 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]
NextLine(iter: *mut c_void) -> *mut Str36 pub unsafe extern "C" fn NextLine(iter: *mut c_void) -> *mut Str {
37 match next_line(iter) {
38 Ok(s) => s,
39 Err(e) => {
40 error_control(e);
41 null_mut()
42 }
43 }
44 }
45
46 #[no_mangle]
Lseek(fd: i32, offset: i64, pos: SeekPos) -> c_longlong47 pub extern "C" fn Lseek(fd: i32, offset: i64, pos: SeekPos) -> c_longlong {
48 match seek(fd, offset, pos) {
49 Ok(pos) => pos as c_longlong,
50 Err(e) => unsafe {
51 error_control(e);
52 -1
53 },
54 }
55 }
56
57 #[no_mangle]
Mkdirs(path: *const c_char, mode: MakeDirectionMode) -> c_int58 pub extern "C" fn Mkdirs(path: *const c_char, mode: MakeDirectionMode) -> c_int {
59 match create_dir(path, mode) {
60 Ok(_) => 0,
61 Err(e) => unsafe {
62 error_control(e);
63 -1
64 },
65 }
66 }
67
68 #[no_mangle]
GetParent(fd: i32) -> *mut Str69 pub extern "C" fn GetParent(fd: i32) -> *mut Str {
70 match get_parent(fd) {
71 Ok(str) => str,
72 Err(e) => {
73 unsafe {
74 error_control(e);
75 }
76 null_mut()
77 }
78 }
79 }
80
81 #[no_mangle]
StrFree(str: *mut Str)82 pub unsafe extern "C" fn StrFree(str: *mut Str) {
83 if !str.is_null() {
84 let string = Box::from_raw(str);
85 let _ = CString::from_raw(string.str);
86 }
87 }
88
89 #[no_mangle]
CutFileName(path: *const c_char, size: usize) -> *mut Str90 pub unsafe extern "C" fn CutFileName(path: *const c_char, size: usize) -> *mut Str {
91 cut_file_name(path, size)
92 }
93