Skip to content

Instantly share code, notes, and snippets.

@genbtc
Created March 3, 2025 18:49
Show Gist options
  • Save genbtc/bc9e8107d48693b6f667704cd1e9f396 to your computer and use it in GitHub Desktop.
Save genbtc/bc9e8107d48693b6f667704cd1e9f396 to your computer and use it in GitHub Desktop.
GPT-contents-boost-parser.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <boost/parser/parser.hpp>
#include <boost/hana/tuple.hpp>
namespace bp = boost::parser;
struct DirEntry {
std::string path;
};
struct ObjEntry {
std::string path;
std::string digest;
long mtime;
};
struct SymEntry {
std::string path;
std::string target;
long mtime;
};
using ContentEntry = boost::hana::tuple<DirEntry, ObjEntry, SymEntry>;
auto dir_parser = bp::lit("dir") >> bp::lexeme[+~bp::char_(' ')];
auto obj_parser = bp::lit("obj") >> bp::lexeme[+~bp::char_(' ')] >> bp::lexeme[+~bp::char_(' ')] >> bp::long_;
auto sym_parser = bp::lit("sym") >> bp::lexeme[+~bp::char_(' ')] >> bp::lit("->") >> bp::lexeme[+~bp::char_(' ')] >> bp::long_;
void parse_line(const std::string& line) {
auto it = line.begin(), end = line.end();
DirEntry dir_entry;
if (bp::parse(it, end, dir_parser, dir_entry.path)) {
std::cout << "[DIR] Path: " << dir_entry.path << '\n';
return;
}
ObjEntry obj_entry;
if (bp::parse(it, end, obj_parser, obj_entry.path, obj_entry.digest, obj_entry.mtime)) {
std::cout << "[OBJ] Path: " << obj_entry.path << ", Digest: " << obj_entry.digest << ", Mtime: " << obj_entry.mtime << '\n';
return;
}
SymEntry sym_entry;
if (bp::parse(it, end, sym_parser, sym_entry.path, sym_entry.target, sym_entry.mtime)) {
std::cout << "[SYM] Path: " << sym_entry.path << ", Target: " << sym_entry.target << ", Mtime: " << sym_entry.mtime << '\n';
return;
}
std::cerr << "Failed to parse line: " << line << '\n';
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
std::ifstream file(argv[1]);
if (!file.is_open()) {
std::cerr << "Could not open file: " << argv[1] << std::endl;
return 1;
}
std::string line;
while (std::getline(file, line)) {
if (line.empty()) continue;
parse_line(line);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment