-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_file_info.cc
67 lines (62 loc) · 2.31 KB
/
show_file_info.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <inttypes.h>
#include <stdint.h>
#include <fstream>
#include <iomanip>
#include <vector>
#include "FileSinkHeader.h"
#include "FileSinkTrailer.h"
int print_file_infos (std::vector<std::ifstream> &ifs, std::vector<std::string>& filenames, std::vector<uint64_t> &filesizes) {
for (int ifile = 0; ifile < ifs.size(); ifile++) {
std::ifstream::pos_type beg = ifs[ifile].tellg();
FileSinkTrailer::Trailer fileTrailer;
ifs[ifile].seekg(filesizes[ifile]-sizeof(fileTrailer), std::ios_base::beg);
ifs[ifile].read((char*)&fileTrailer,sizeof(fileTrailer));
struct tm *dt;
char buffer [40];
dt = localtime(&fileTrailer.startUnixtime);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", dt);
std::string startTime = buffer;
dt = localtime(&fileTrailer.stopUnixtime);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", dt);
std::string stopTime = buffer;
std::cout << "file name: " << filenames[ifile]
<< ", file size: " << filesizes[ifile]
<< ", start: " << startTime << " (" << fileTrailer.startUnixtime << ")"
<< ", stop: " << stopTime << " (" << fileTrailer.stopUnixtime << ")"
<< std::endl;
ifs[ifile].seekg(beg, std::ios_base::beg);
}
return 0;
}
int get_file_sizes ( std::vector<std::ifstream>& ifs, std::vector<uint64_t> &filesizes) {
if (ifs.size() != filesizes.size()){
filesizes.resize(ifs.size());
}
for (int ifile = 0; ifile < ifs.size(); ifile++) {
std::ifstream::pos_type beg = ifs[ifile].tellg();
ifs[ifile].seekg(0, std::ios::end);
std::ifstream::pos_type fsize = ifs[ifile].tellg();
filesizes[ifile] = fsize;
ifs[ifile].seekg(beg, std::ios_base::beg);
}
return 0;
}
int main(int argc, char* argv[]){
if (argc <= 1) {
std::cout << "Usage: ./file_info file1 [file2 file3 ...]" << std::endl;
return 1;
}
std::vector<std::string> filenames;
std::vector<std::ifstream> ifs;
std::vector<uint64_t> filesizes;
for (unsigned int i = 1; i < argc; i++) {
filenames.push_back(argv[i]);
}
for (unsigned int i = 0; i < filenames.size(); i++) {
ifs.emplace_back(filenames[i].c_str(), std::ios::binary); /* emplace_back for std::ifstream is valid in C++11 or higher version. */
}
get_file_sizes(ifs, filesizes);
print_file_infos(ifs, filenames, filesizes);
return 0;
}