1
1
// ////////////////////////////////////////////////////
2
2
// /// main entry point for mbox2eml.cc
3
3
// ///////////////////////////////////////////////////
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
+
4
34
#include < iostream>
5
35
#include < fstream>
6
36
#include < string>
9
39
#include < mutex>
10
40
#include < filesystem>
11
41
12
- // Compile with g++ -O3 -std=c++23 -pthread -lstdc++fs -o mbox2eml mbox2eml.cc
13
-
14
42
namespace fs = std::filesystem;
15
43
16
44
// Structure to hold email data
@@ -26,7 +54,7 @@ std::vector<Email> extractEmails(const std::string& mbox_file) {
26
54
Email current_email;
27
55
28
56
while (std::getline (file, line)) {
29
- if (line.starts_with (" From " )) {
57
+ if (line.starts_with (" From " )) { // use c++20 feature
30
58
// Start of a new email
31
59
if (!current_email.content .empty ()) {
32
60
emails.push_back (current_email);
@@ -67,6 +95,8 @@ void workerThread(const std::vector<Email>& emails, const std::string& output_di
67
95
int main (int argc, char * argv[]) {
68
96
// Check for correct number of arguments
69
97
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;
70
100
std::cerr << " Usage: " << argv[0 ] << " <mbox_file> <output_directory>" << std::endl;
71
101
return 1 ;
72
102
}
@@ -75,8 +105,12 @@ int main(int argc, char* argv[]) {
75
105
std::string output_dir = argv[2 ];
76
106
77
107
// Create the output directory if it doesn't exist
108
+ try {
78
109
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
+ }
80
114
// Extract emails from the mbox file
81
115
std::vector<Email> emails = extractEmails (mbox_file);
82
116
std::cout << " Extracted " << emails.size () << " emails." << std::endl;
0 commit comments