11 Star 46 Fork 141

OpenHarmony / commonlibrary_c_utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
c_utils_guide_rust_file.md 2.54 KB
一键复制 编辑 原始数据 按行查看 历史

读写文件

概述

提供Rust侧对应c_utils中读写文件的接口。其在Rust侧构成utils_rust包(Crate)中的file_ex模块,包括对文件内容的读写以及对指定字符串的查找功能。

涉及子模块

utils_rust::file_ex::ffi

子模块file_ex::ffi借助CXX工具通过FFI实现与C++对应代码的互操作。其中各接口通过FFI调用C++侧对应接口,以实现读写文件相关功能。

由于使用CXX对Rust侧以及C++侧的接口进行绑定,该模块中的接口命名风格与C++一致,其参数类型为兼容C++的Rust类型。

using utils_rust::file_ex

全局函数

名称
i32 RustCountStrInFile(fileName: &String, subStr: &String, caseSensitive: bool)
查看指定文件中出现指定字符串的次数
bool RustFileExists(fileName: &String)
检查指定文件是否存在。
bool RustLoadBufferFromFile(filePath: &String, content:&mut Vec<c_char>)
从指定文件中读出数据,存入输入缓存区(Vec<c_char>)结构体中。
bool RustLoadStringFromFd(fd: i32, content: &mut String)
通过文件对应的文件描述符,从中读取全部字符串存入输入String对象中。
bool RustLoadStringFromFile(filePath: &String, content: &mut String)
从指定文件中读出全部字符串存入输入String对象中。
bool RustSaveBufferToFile(filePath: &String, content:&Vec<c_char>, truncated: bool)
向指定文件中写入缓存区(Vec<c_char>)对象中的数据。
bool RustSaveStringToFd(fd: i32, content: &String)
通过文件对应的文件描述符,向其写入字符串。
bool RustSaveStringToFile(filePath: &String, content: &String, truncated: bool)
将字符串写入指定文件中。
bool RustStringExistsInFile(fileName: &String, subStr: &String, caseSensitive: bool)
检查指定文件中是否包含指定字符串

使用示例

  1. 测试用例编译运行方法
  • 测试用例代码参见 base/test/unittest/rust/utils_rust_file_test.cpp

  • 使用开发者自测试框架,使用方法参见:开发自测试执行框架-测试用例执行

  • 使用以下具体命令以运行utils_rust::file_ex对应测试用例

run -t UT -tp utils -ts utils_rust_file_test
1
https://gitee.com/openharmony/commonlibrary_c_utils.git
git@gitee.com:openharmony/commonlibrary_c_utils.git
openharmony
commonlibrary_c_utils
commonlibrary_c_utils
master

搜索帮助