Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions source/api_c/include/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,13 @@ extern void DP_ConvertPbtxtToPb(const char* c_pbtxt, const char* c_pb);
*/
extern void DP_PrintSummary(const char* c_pre);

/**
* @brief Read a file to a char array.
* @param[in] c_model The name of the file.
* @return const char* The char array.
*/
const char* DP_ReadFileToChar(const char* c_model);

#ifdef __cplusplus
} /* end extern "C" */
#endif
11 changes: 11 additions & 0 deletions source/api_c/include/deepmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1575,5 +1575,16 @@ class DipoleChargeModifier {
DP_DipoleChargeModifier *dcm;
int nsel_types;
};

/**
* @brief Read model file to a string.
* @param[in] model Path to the model.
* @param[out] file_content Content of the model file.
**/
void inline read_file_to_string(std::string model, std::string &file_content) {
const char *c_file_content = DP_ReadFileToChar(model.c_str());
file_content = std::string(c_file_content);
};

} // namespace hpp
} // namespace deepmd
7 changes: 7 additions & 0 deletions source/api_c/src/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,11 @@ void DP_PrintSummary(const char* c_pre) {
deepmd::print_summary(pre);
}

const char* DP_ReadFileToChar(const char* c_model) {
std::string model(c_model);
std::string file_content;
deepmd::read_file_to_string(model, file_content);
return string_to_char(file_content);
}

} // extern "C"
25 changes: 25 additions & 0 deletions source/api_c/tests/test_read_file_to_string.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <fcntl.h>
#include <gtest/gtest.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <algorithm>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include "deepmd.hpp"
TEST(TestReadFileToString, readfiletostring) {
std::string file_content;
deepmd::hpp::read_file_to_string("../../tests/infer/deeppot.txt",
file_content);

std::string file_name_2 = "../../tests/infer/deeppot.txt";
std::stringstream buffer;
std::ifstream file_txt(file_name_2);
buffer << file_txt.rdbuf();
std::string expected_out_string = buffer.str();
EXPECT_STREQ(expected_out_string.c_str(), file_content.c_str());
}