• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, read_lines, seek, MakeDirectionMode, SeekPos,
18     Str, StringVector,
19 };
20 use libc::c_char;
21 use std::ffi::CString;
22 use std::ptr::null_mut;
23 
24 #[no_mangle]
ReadLines(path: *const c_char) -> *mut StringVector25 pub unsafe extern "C" fn ReadLines(path: *const c_char) -> *mut StringVector {
26     match read_lines(path) {
27         Ok(sv) => sv,
28         Err(e) => {
29             error_control(e);
30             null_mut()
31         }
32     }
33 }
34 
35 #[no_mangle]
NextLine(lines: *mut StringVector) -> *mut Str36 pub unsafe extern "C" fn NextLine(lines: *mut StringVector) -> *mut Str {
37     match next_line(lines) {
38         Ok(s) => s,
39         Err(e) => {
40             error_control(e);
41             null_mut()
42         }
43     }
44 }
45 
46 #[no_mangle]
StringVectorFree(lines: *mut StringVector)47 pub unsafe extern "C" fn StringVectorFree(lines: *mut StringVector) {
48     if !lines.is_null() {
49         let _ = Box::from_raw(lines);
50     }
51 }
52 
53 #[no_mangle]
Lseek(fd: i32, offset: i64, pos: SeekPos)54 pub extern "C" fn Lseek(fd: i32, offset: i64, pos: SeekPos) {
55     match seek(fd, offset, pos) {
56         Ok(_) => {}
57         Err(e) => unsafe {
58             error_control(e);
59         },
60     }
61 }
62 
63 #[no_mangle]
Mkdirs(path: *const c_char, mode: MakeDirectionMode)64 pub extern "C" fn Mkdirs(path: *const c_char, mode: MakeDirectionMode) {
65     match create_dir(path, mode) {
66         Ok(_) => {}
67         Err(e) => unsafe { error_control(e) },
68     }
69 }
70 
71 #[no_mangle]
GetParent(fd: i32) -> *mut Str72 pub extern "C" fn GetParent(fd: i32) -> *mut Str {
73     match get_parent(fd) {
74         Ok(str) => str,
75         Err(e) => {
76             unsafe {
77                 error_control(e);
78             }
79             null_mut()
80         }
81     }
82 }
83 
84 #[no_mangle]
ParentFree(str: *mut Str)85 pub unsafe extern "C" fn ParentFree(str: *mut Str) {
86     if !str.is_null() {
87         let string = Box::from_raw(str);
88         let _ = CString::from_raw(string.str as *mut c_char);
89     }
90 }
91