Skip to content

Commit f24d3be

Browse files
committed
code clean up
1 parent a9762bb commit f24d3be

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

mbox2eml.cc

100644100755
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
//////////////////////////////////////////////////////
22
///// main entry point for mbox2eml.cc
33
/////////////////////////////////////////////////////
4+
// Copyright (c) Bishoy H.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
//
24+
// Description:
25+
// This tool, mbox2eml, is designed to extract individual email messages from an
26+
// mbox file and save them as separate .eml files in a given folder. It utilizes multithreading to
27+
// speed up the processing of large mbox files by distributing the workload across
28+
// multiple CPU cores, but it requires enough memory to load the mbox file. The tool takes two command-line arguments: the path to the
29+
// mbox file and the output directory where the .eml files will be saved.
30+
31+
// Compile with g++ -O3 -std=c++23 -pthread -lstdc++fs -o mbox2eml mbox2eml.cc
32+
33+
434
#include <iostream>
535
#include <fstream>
636
#include <string>
@@ -9,8 +39,6 @@
939
#include <mutex>
1040
#include <filesystem>
1141

12-
// Compile with g++ -O3 -std=c++23 -pthread -lstdc++fs -o mbox2eml mbox2eml.cc
13-
1442
namespace fs = std::filesystem;
1543

1644
// Structure to hold email data
@@ -26,7 +54,7 @@ std::vector<Email> extractEmails(const std::string& mbox_file) {
2654
Email current_email;
2755

2856
while (std::getline(file, line)) {
29-
if (line.starts_with("From ")) {
57+
if (line.starts_with("From ")) { // use c++20 feature
3058
// Start of a new email
3159
if (!current_email.content.empty()) {
3260
emails.push_back(current_email);
@@ -67,6 +95,8 @@ void workerThread(const std::vector<Email>& emails, const std::string& output_di
6795
int main(int argc, char* argv[]) {
6896
// Check for correct number of arguments
6997
if (argc != 3) {
98+
std::cerr << "mbox2eml: Extract individual email messages from an mbox file and save them as separate .eml files." << std::endl;
99+
std::cerr << "Error: Incorrect number of arguments." << std::endl;
70100
std::cerr << "Usage: " << argv[0] << " <mbox_file> <output_directory>" << std::endl;
71101
return 1;
72102
}
@@ -75,8 +105,12 @@ int main(int argc, char* argv[]) {
75105
std::string output_dir = argv[2];
76106

77107
// Create the output directory if it doesn't exist
108+
try {
78109
fs::create_directory(output_dir);
79-
110+
} catch (const std::exception& e) {
111+
std::cerr << "Error creating output directory: " << e.what() << std::endl;
112+
return 1;
113+
}
80114
// Extract emails from the mbox file
81115
std::vector<Email> emails = extractEmails(mbox_file);
82116
std::cout << "Extracted " << emails.size() << " emails." << std::endl;

0 commit comments

Comments
 (0)