43 lines
1.0 KiB
C++
43 lines
1.0 KiB
C++
#include "cat-file.h"
|
|
|
|
#include <iostream>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "zstr.hpp"
|
|
|
|
int catFile(const int argc, const char** argv)
|
|
{
|
|
if (argc < 4)
|
|
{
|
|
std::cerr << "please provide proper arguments. example '-p <blob_sha>'.\n";
|
|
return 1;
|
|
}
|
|
|
|
const std::string flag = argv[2];
|
|
if (flag != "-p")
|
|
{
|
|
std::cerr << flag << "is an invalid flag, please use \"-p\"\n";
|
|
return 1;
|
|
}
|
|
|
|
const std::string input = argv[3];
|
|
const std::string dir = input.substr(0, 2);
|
|
const std::string file = input.substr(2);
|
|
|
|
zstr::ifstream inputFile("./.git/objects/" + dir + "/" + file, std::ofstream::binary);
|
|
if (!inputFile.is_open())
|
|
{
|
|
std::cerr << "Could not open file\n";
|
|
return 1;
|
|
}
|
|
|
|
const std::string output{std::istreambuf_iterator<char>(inputFile), std::istreambuf_iterator<char>()};
|
|
std::cout << std::string_view(output).substr(output.find('\0') + 1);
|
|
inputFile.close();
|
|
|
|
return 0;
|
|
}
|