Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP gunzip #82

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ cc_library(
name = "decompress",
hdrs = ["decompress.hpp"],
)

cc_library(
name = "gunzip",
srcs = ["gunzip.cpp"],
hdrs = ["gunzip.hpp"],
deps = ["//huffman"],
)

cc_binary(
name = "gunzip_main",
srcs = ["gunzip_main.cpp"],
deps = [
":decompress",
":gunzip",
],
)
9 changes: 9 additions & 0 deletions src/gunzip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "gunzip.hpp"

namespace starflate {
auto gunzip(std::istream& in, std::ostream& out) -> GunzipError
{
// TODO: implement
return GunzipError::NoError;
}
} // namespace starflate
12 changes: 12 additions & 0 deletions src/gunzip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <cstdint>
#include <istream>
#include <ostream>

namespace starflate {
enum class GunzipError : std::uint8_t
{
NoError,
Error,
};
auto gunzip(std::istream& in, std::ostream& out) -> GunzipError;
} // namespace starflate
76 changes: 76 additions & 0 deletions src/gunzip_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "gunzip.hpp"

#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <ostream>

// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)

auto usage(const char* program) -> int
{
std::cerr << "Usage: " << program << "[OPTIONS] <filename>" << std::endl;
std::cerr << "OPTIONS:" << std::endl;
std::cerr
<< "\n\t-c --stdout --to-stdout\n\t\tWrite to standard output"
<< std::endl;
return EXIT_FAILURE;
}

auto decompressed_path(const std::filesystem::path& in_path)
-> std::filesystem::path
{
std::filesystem::path res{in_path};
if (in_path.extension() == ".gz") {
res.replace_extension();
} else {
res += ".decompressed";
}
return res;
}

auto main(const int argc, const char* argv[]) -> int
{
if (argc < 2 || argc > 3) {
return usage(argv[0]);
}

std::filesystem::path in_path;
bool to_stdout{};
std::ofstream out_file;
if (argc == 3) {
const auto option = argv[1];
// NOLINTBEGIN(readability-magic-numbers)
if (strncmp(option, "-c", 2) != 0 && strncmp(option, "--stdout", 8) != 0 &&
strncmp(option, "--to-stdout", 11) != 0) {
// NOLINTEND(readability-magic-numbers)
return usage(argv[0]);
}
in_path = argv[2];
to_stdout = true;
} else {
in_path = argv[1];
std::filesystem::path out_path{decompressed_path(in_path)};
out_file.open(out_path, std::ios::binary);
if (!out_file.is_open()) {
std::cerr << "Failed to open " << out_path << " for writing" << std::endl;
return EXIT_FAILURE;
}
}
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
auto& out = to_stdout ? std::cout : out_file;

std::ifstream in{in_path, std::ios::binary};
if (!in.is_open()) {
std::cerr << "Failed to open " << in_path << " for reading" << std::endl;
return EXIT_FAILURE;
}

const auto err = starflate::gunzip(in, out);
if (err != starflate::GunzipError::NoError) {
std::cerr << "Error: " << static_cast<int>(err) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Loading