mirror of
https://gitea.home.endeavr.de/Marcel/Tarstats-pp.git
synced 2025-12-10 00:39:01 +01:00
this added output to text file and proper parsing of command line arguments. this also cleaned up some more stuff.
This commit is contained in:
83
main.cpp
83
main.cpp
@@ -3,7 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -12,8 +11,51 @@
|
|||||||
#include "tarfunc.h"
|
#include "tarfunc.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
// Trivial check for arguments.
|
||||||
|
if (argc < 2) {
|
||||||
|
tar::printhelp();
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
// GENERAL VARIABLES
|
// GENERAL VARIABLES
|
||||||
|
|
||||||
|
// Load command line parameters into vector, read them and set variables
|
||||||
|
std::vector<std::string> cmdparam{};
|
||||||
|
for (int i = 1; i < argc; i++){
|
||||||
|
cmdparam.emplace_back(argv[i]);
|
||||||
|
}
|
||||||
|
bool toFile = false;
|
||||||
|
bool toJSON = false;
|
||||||
|
|
||||||
|
std::vector<std::string> archiveFilename {};
|
||||||
|
for (auto i : cmdparam) {
|
||||||
|
if (i[0] != '-' && i.size() > 1) {
|
||||||
|
archiveFilename.push_back(i);
|
||||||
|
}
|
||||||
|
if (i[0] == '-') {
|
||||||
|
for (auto j : i) {
|
||||||
|
switch (j) {
|
||||||
|
case 'j':
|
||||||
|
toJSON = true;
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
toFile = true;
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
tar::printhelp();
|
||||||
|
return 9;
|
||||||
|
case '-':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "Invalid optional argument '" << j << "'!" << '\n' << '\n';
|
||||||
|
tar::printhelp();
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &archiveName : archiveFilename) {
|
||||||
//count the types of all items
|
//count the types of all items
|
||||||
std::map<std::string, uintmax_t> typecount{
|
std::map<std::string, uintmax_t> typecount{
|
||||||
{tarconstant::typeFile, 0}, {tarconstant::typeDir, 0}, {tarconstant::typeSym, 0},
|
{tarconstant::typeFile, 0}, {tarconstant::typeDir, 0}, {tarconstant::typeSym, 0},
|
||||||
@@ -22,31 +64,20 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
uint64_t sizeof_allfiles{}; // total size of all files in the archive
|
uint64_t sizeof_allfiles{}; // total size of all files in the archive
|
||||||
|
|
||||||
// Trivial check for arguments. Errorprone and has to be changed.
|
|
||||||
if (argc < 2) {
|
|
||||||
std::cout << tarconstant::helptext << '\n';
|
|
||||||
return 9;
|
|
||||||
}
|
|
||||||
// Getting name from argument lists on startup. Trivial and errorprone. Placeholder for now.
|
|
||||||
std::string archiveFilename(argv[1]);
|
|
||||||
|
|
||||||
for (int i = 0; i < argc; i++){
|
|
||||||
std::cout << argv[i] << '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
//Open tar File.
|
//Open tar File.
|
||||||
std::ifstream file(archiveFilename, std::ios::binary);
|
std::ifstream file(archiveName, std::ios::binary);
|
||||||
if(!file) {
|
if (!file) {
|
||||||
std::cout << "Error opening file!" << '\n' << '\n';
|
std::cout << "Error opening file " << archiveName << "!" << '\n' << '\n';
|
||||||
std::cout << tarconstant::helptext << '\n';
|
tar::printhelp();
|
||||||
return 9;
|
return 9;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tar::validTar(file)) {
|
if (!tar::validTar(file)) {
|
||||||
std::cout << archiveFilename << " is not a valid tar file for tarstats-pp" << '\n';
|
std::cout << "!!!!!!!" << '\n';
|
||||||
return 9;
|
std::cout << archiveName << " is not a valid tar file for tarstats-pp" << '\n';
|
||||||
|
std::cout << "!!!!!!!" << '\n' << '\n';
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
while (file) {
|
while (file) {
|
||||||
//Read header of next item in tar archive
|
//Read header of next item in tar archive
|
||||||
char *headbuffer = new char[tarconstant::blocksize];
|
char *headbuffer = new char[tarconstant::blocksize];
|
||||||
@@ -65,7 +96,7 @@ int main(int argc, char** argv) {
|
|||||||
sizeof_allfiles += tar::getitemsize(headbuffer);
|
sizeof_allfiles += tar::getitemsize(headbuffer);
|
||||||
|
|
||||||
// ignore file content. we want to get to the next header. item types != FILE have no content blocks (0 byte)
|
// ignore file content. we want to get to the next header. item types != FILE have no content blocks (0 byte)
|
||||||
if (tar::getitemsize(headbuffer)!=0) {
|
if (tar::getitemsize(headbuffer) != 0) {
|
||||||
file.ignore((tar::getitemsize(headbuffer) / tarconstant::blocksize)
|
file.ignore((tar::getitemsize(headbuffer) / tarconstant::blocksize)
|
||||||
* tarconstant::blocksize + tarconstant::blocksize);
|
* tarconstant::blocksize + tarconstant::blocksize);
|
||||||
}
|
}
|
||||||
@@ -75,8 +106,14 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
tar::consolestats(typecount, std::filesystem::file_size(archiveFilename), sizeof_allfiles);
|
std::cout << toJSON << '\n';
|
||||||
|
tar::consolestats(typecount, std::filesystem::file_size(archiveName), sizeof_allfiles);
|
||||||
|
if (toFile) {
|
||||||
|
tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), sizeof_allfiles,
|
||||||
|
archiveName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,10 @@ namespace tarconstant {
|
|||||||
"optional arguments\n"
|
"optional arguments\n"
|
||||||
"-h this helptext\n"
|
"-h this helptext\n"
|
||||||
"-j stats in JSON format printed to stdout\n"
|
"-j stats in JSON format printed to stdout\n"
|
||||||
"-f print stats to file <tarfilename>.txt or .json if -j invoked"};
|
"-f print stats to file <tarfilename>.txt or .json if -j invoked\n\n"
|
||||||
|
"Find more information as well as full source at github.com/blindi0815/Tarstats-pp."
|
||||||
|
"This software is as is under a GPL 3.0 license.\n\n"
|
||||||
|
"Written by Marcel Nowicki - github.com/blindi0815"};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //TARSTATS___TARCONST_H
|
#endif //TARSTATS___TARCONST_H
|
||||||
|
|||||||
20
tarfunc.cpp
20
tarfunc.cpp
@@ -61,6 +61,7 @@ std::string tar::getitemtype(char &n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writes stats to console in default style
|
||||||
void tar::consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall) {
|
void tar::consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall) {
|
||||||
std::cout << "Archive size: " << tarfilesize << " Bytes"<< '\n';
|
std::cout << "Archive size: " << tarfilesize << " Bytes"<< '\n';
|
||||||
std::cout << "Size of all items: " << sizeofall << " Bytes" << '\n' << '\n';
|
std::cout << "Size of all items: " << sizeofall << " Bytes" << '\n' << '\n';
|
||||||
@@ -68,3 +69,22 @@ void tar::consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t t
|
|||||||
std::cout << i.first <<": " << i.second << '\n';
|
std::cout << i.first <<": " << i.second << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writes default console output to txt file
|
||||||
|
void tar::txtfilestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
|
||||||
|
std::string archiveName) {
|
||||||
|
std::string txtname = archiveName + ".txt";
|
||||||
|
std::ofstream txtfile(txtname);
|
||||||
|
txtfile << "Archive size: " << tarfilesize << " Bytes"<< std::endl;
|
||||||
|
txtfile << "Size of all items: " << sizeofall << " Bytes" << std::endl << std::endl;
|
||||||
|
for (auto &i : typecount) {
|
||||||
|
txtfile << i.first <<": " << i.second << std::endl;
|
||||||
|
}
|
||||||
|
txtfile.close();
|
||||||
|
std::cout << "Stats written to " << txtname << '\n' << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
// print out helpertext
|
||||||
|
void tar::printhelp(){
|
||||||
|
std::cout << tarconstant::helptext << '\n' << '\n';
|
||||||
|
}
|
||||||
10
tarfunc.h
10
tarfunc.h
@@ -6,6 +6,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "tarconst.h"
|
||||||
|
|
||||||
|
|
||||||
#ifndef TARSTATS___TARFUNC_H
|
#ifndef TARSTATS___TARFUNC_H
|
||||||
@@ -22,8 +23,15 @@ namespace tar {
|
|||||||
|
|
||||||
// gets type of an item
|
// gets type of an item
|
||||||
std::string getitemtype(char &n);
|
std::string getitemtype(char &n);
|
||||||
//
|
|
||||||
|
// to write stats to console in default style
|
||||||
void consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall);
|
void consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall);
|
||||||
|
|
||||||
|
// to write default console output to txt file
|
||||||
|
void txtfilestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
|
||||||
|
std::string archiveName);
|
||||||
|
// print out helpertext
|
||||||
|
void printhelp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user