configure_parser.cpp 1.5 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
#include "butil/logging.h"

#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

namespace baidu {
namespace paddle_serving {
namespace configure {

int read_proto_conf(const std::string &conf_path,
                    const std::string &conf_file,
                    google::protobuf::Message *conf)
{
W
wangguibao 已提交
18
    std::string file_str = conf_path + "/" + conf_file;
W
wangguibao 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    int fd = open(file_str.c_str(), O_RDONLY);
    if (fd == -1) {
        LOG(WARNING) << "File not found: " << file_str.c_str();
        return -1;
    }

    google::protobuf::io::FileInputStream input(fd);
    bool success = google::protobuf::TextFormat::Parse(&input, conf);
    close(fd);
    if (!success) {
        return -1;
    }

    return 0;
}

int write_proto_conf(google::protobuf::Message *message,
                    const std::string &output_path,
                    const std::string &output_file)
{
    std::string binary_str;
    google::protobuf::TextFormat::PrintToString(*message, &binary_str);

W
wangguibao 已提交
42
    std::string file_str = output_path + "/" + output_file;
W
wangguibao 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    std::ofstream fout_bin((file_str.c_str()));
    if (!fout_bin) {
        LOG(WARNING) << "Open file error: " << file_str.c_str();
        return -1;
    }

    fout_bin.write((char *)binary_str.c_str(), binary_str.size());
    fout_bin.close();

    return 0;
}

} // configure
} // paddle_serving
} // baidu
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */