Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active March 15, 2023 08:27
Show Gist options
  • Save duruyao/c4b44e1ce0d98e52677ecedefea4a7c3 to your computer and use it in GitHub Desktop.
Save duruyao/c4b44e1ce0d98e52677ecedefea4a7c3 to your computer and use it in GitHub Desktop.
Save files to and load files from Protocol Buffers Messages (implemented in C++).
/*
* @author duruyao
* @date 2023-03-14
* @desc save files to and load files from protobuf messages
* @usage EXECUTABLE <SRC_FILENAME> <DST_FILENAME>
*/
#include <cstdio>
#include <string>
#include "protobuf_rw_file.pb.h"
const int BUFF_SIZE = (UINT16_MAX << 8) + 2; // 16 M + 1
int main(int argc, char **argv) {
// handle arguments
if (3 != argc || 0 == strcmp(argv[1], argv[2])) {
fprintf(stdout,
"Usage:\n"
" %s <SRC_FILENAME> <DST_FILENAME>\n"
"Description:\n"
" Read a file into protobuf messages and write protobuf messages to another file.\n", argv[0]);
return 0;
}
// read data from a binary file to a buffer
FILE *fp;
if (nullptr == (fp = fopen(argv[1], "rb"))) {
fprintf(stderr, "Error: cannot open file: '%s'\n", argv[1]);
return 1;
}
char *dataBuff1 = new char[BUFF_SIZE];
auto dataSize1 = fread(dataBuff1, sizeof(char), BUFF_SIZE, fp);
fclose(fp);
if (dataSize1 >= BUFF_SIZE) {
fprintf(stderr, "Error: file size greater than 16 MB: '%s'\n", argv[1]);
return 1;
}
// assign values to the object pbFile1
PBFile pbFile1;
pbFile1.set_data_size(dataSize1);
pbFile1.set_data_buff(dataBuff1, dataSize1);
// serialize the object pbFile1 to a string
auto serializedStr = pbFile1.SerializeAsString();
// deserialize a string to the object pbFile2
PBFile pbFile2;
pbFile2.ParseFromString(serializedStr);
// read data from pbFile2 to a buffer
char *dataBuff2 = new char[BUFF_SIZE];
auto dataSize2 = pbFile2.data_size();
memcpy(dataBuff2, pbFile2.data_buff().c_str(), pbFile2.data_size());
// compare dataBuff1 with dataBuff2
if (0 != memcmp(dataBuff1, dataBuff2, dataSize2)) {
fprintf(stderr, "Error: dataBuff1 != dataBuff2\n");
return 1;
}
// write data from a buffer to a binary file
if (nullptr == (fp = fopen(argv[2], "wb"))) {
fprintf(stderr, "Error: cannot open file: '%s'\n", argv[2]);
return 1;
}
fwrite(dataBuff2, sizeof(char), dataSize2, fp);
fclose(fp);
return 0;
}
syntax = "proto3";
message PBFile {
uint64 data_size = 1;
bytes data_buff = 2;
}
@duruyao
Copy link
Author

duruyao commented Mar 15, 2023

1. Prerequisites

2. Unix Builds

Create a file CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(pb_rw_file)
set(CMAKE_CXX_STANDARD 11)
find_package(Protobuf REQUIRED)
link_libraries(${Protobuf_LIBRARIES})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS pb_rw_file.proto)
add_executable(pb_rw_file)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} ALL_SRC)
target_sources(pb_rw_file PRIVATE ${ALL_SRC} ${PROTO_SRCS} ${PROTO_HDRS})
install(TARGETS pb_rw_file RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

Use CMake to build targets.

cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="${PWD}"
cmake --build build --target all -- --jobs=4
cmake --build build --target install

3. Usage

$ ./protobuf_rw_file --help
Usage:
    ./protobuf_rw_file <SRC_FILENAME> <DST_FILENAME>
Description:
    Read a file into protobuf messages and write protobuf messages to another file.

4. More

Access https://github.com/duruyao/protobuf-rw-file to get the complete example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment