Skip to content

Commit 9aed34f

Browse files
committed
add functions to handle pmdb paths
1 parent 2bb9ca1 commit 9aed34f

File tree

10 files changed

+229
-40
lines changed

10 files changed

+229
-40
lines changed

code/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(pmdb_sources
2727
bbcode/TableBBCode.cpp
2828
bbcode/quotes.cpp
2929
filters/FilterUser.cpp
30+
paths.cpp
3031
../libstriezel/common/DirectoryFileList.cpp
3132
../libstriezel/common/StringUtils.cpp
3233
../libstriezel/filesystem/directory.cpp

code/FolderMap.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
-------------------------------------------------------------------------------
33
This file is part of the Private Message Database.
4-
Copyright (C) 2014, 2016 Dirk Stolle
4+
Copyright (C) 2014, 2016, 2025 Dirk Stolle
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -36,40 +36,39 @@ void FolderMap::add(const SHA256::MessageDigest& pm_digest, const std::string& f
3636

3737
bool FolderMap::hasEntry(const SHA256::MessageDigest& pm_digest) const
3838
{
39-
return (m_FolderMap.find(pm_digest)!=m_FolderMap.end());
39+
return m_FolderMap.find(pm_digest) != m_FolderMap.end();
4040
}
4141

4242
const std::string& FolderMap::getFolderName(const SHA256::MessageDigest& pm_digest) const
4343
{
4444
const std::map<SHA256::MessageDigest, std::string>::const_iterator iter = m_FolderMap.find(pm_digest);
45-
if (iter!=m_FolderMap.end()) return iter->second;
46-
throw std::runtime_error("The message database's folder map has no message entry for the given hash "+pm_digest.toHexString()+"!");
45+
if (iter != m_FolderMap.end())
46+
return iter->second;
47+
throw std::runtime_error("The message database's folder map has no message entry for the given hash " + pm_digest.toHexString() + "!");
4748
}
4849

4950
bool FolderMap::save(const std::string& directory) const
5051
{
5152
std::ofstream output;
52-
output.open((directory+"foldermap").c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
53+
output.open(directory + "foldermap", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
5354
if (!output)
5455
{
5556
return false;
5657
}
5758
const char space = ' ';
5859
const char end = '\n';
59-
std::map<SHA256::MessageDigest, std::string>::const_iterator iter = m_FolderMap.begin();
60-
while (iter!=m_FolderMap.end())
60+
for (const auto& [digest, folder]: m_FolderMap)
6161
{
62-
const std::string hexRepresentation = iter->first.toHexString();
63-
//write hash
62+
const std::string hexRepresentation = digest.toHexString();
63+
// write hash
6464
output.write(hexRepresentation.c_str(), hexRepresentation.length());
65-
//write space
65+
// write space
6666
output.write(&space, 1);
67-
//write folder name
68-
output.write(iter->second.c_str(), iter->second.length());
69-
//write end of line character
67+
// write folder name
68+
output.write(folder.c_str(), folder.length());
69+
// write end of line character
7070
output.write(&end, 1);
71-
++iter;
72-
}//while
71+
}
7372
const bool well = output.good();
7473
output.close();
7574
return well;
@@ -88,25 +87,25 @@ bool FolderMap::load(const std::string& directory)
8887
const unsigned int cMaxLine = 1024;
8988
char buffer[cMaxLine];
9089
std::string line = "";
91-
while (inFile.getline(buffer, cMaxLine-1))
90+
while (inFile.getline(buffer, cMaxLine - 1))
9291
{
9392
buffer[cMaxLine-1] = '\0';
9493
line = std::string(buffer);
9594
if (!md.fromHexString(line.substr(0, 64)))
9695
{
9796
inFile.close();
98-
std::cout << "Error: string \""<<line.substr(0,64)<<"\" is no valid hash!\n";
97+
std::cout << "Error: String \"" << line.substr(0,64) << "\" is no valid hash!\n";
9998
return false;
10099
}
101-
//remove hash
100+
// remove hash
102101
line.erase(0, 64);
103-
//remove leading spaces
102+
// remove leading spaces
104103
trimLeft(line);
105104
if (!line.empty())
106105
{
107106
m_FolderMap[md] = line;
108-
}//if
109-
}//while
107+
}
108+
}
110109
inFile.close();
111110
return true;
112111
}
@@ -115,10 +114,10 @@ std::set<std::string> FolderMap::getPresentFolders() const
115114
{
116115
std::set<std::string> allFolders;
117116
std::map<SHA256::MessageDigest, std::string>::const_iterator iter = m_FolderMap.begin();
118-
while (iter!=m_FolderMap.end())
117+
while (iter != m_FolderMap.end())
119118
{
120119
allFolders.insert(iter->second);
121120
++iter;
122-
}//while
121+
}
123122
return allFolders;
124123
}

code/main.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Config.hpp"
3030
#include "ConsoleColours.hpp"
3131
#include "ColourMap.hpp"
32+
#include "paths.hpp"
3233
#include "Version.hpp"
3334
#include "bbcode/BBCodeParser.hpp"
3435
#include "bbcode/DefaultCodes.hpp"
@@ -120,20 +121,10 @@ int main(int argc, char **argv)
120121
std::set<std::string> loadDirs;
121122
bool doSave = true;
122123
bool saveModeSpecified = false;
123-
std::string homeDirectory;
124-
std::string defaultSaveDirectory;
125124
Compression compression = Compression::none;
126125

127-
if (libstriezel::filesystem::directory::getHome(homeDirectory))
128-
{
129-
homeDirectory = libstriezel::filesystem::slashify(homeDirectory);
130-
defaultSaveDirectory = homeDirectory + std::string(".pmdb") + libstriezel::filesystem::pathDelimiter;
131-
}
132-
else
133-
{
134-
homeDirectory = std::string(".") + libstriezel::filesystem::pathDelimiter;
135-
defaultSaveDirectory = std::string(".pmdb") + libstriezel::filesystem::pathDelimiter;
136-
}
126+
const std::string defaultSaveDirectory = pmdb::paths::main() + libstriezel::filesystem::pathDelimiter;
127+
137128
bool doHTML = false;
138129
HTMLStandard standard = HTMLStandard::HTML4_01;
139130
bool nl2br = true;
@@ -449,7 +440,7 @@ int main(int argc, char **argv)
449440
if (!libstriezel::filesystem::directory::createRecursive(realDir))
450441
{
451442
std::cout << "failed!\nAborting.\n";
452-
return 0;
443+
return rcFileError;
453444
}
454445
std::cout << "success!\n";
455446
}
@@ -475,7 +466,7 @@ int main(int argc, char **argv)
475466
if (msgIter != mdb.getEnd())
476467
{
477468
// directory creation
478-
std::string htmlDir = libstriezel::filesystem::slashify(defaultSaveDirectory) + "html";
469+
std::string htmlDir = pmdb::paths::html();
479470
if (!libstriezel::filesystem::directory::exists(htmlDir))
480471
{
481472
std::cout << "Trying to create HTML directory \"" << htmlDir << "\"...";
@@ -493,9 +484,10 @@ int main(int argc, char **argv)
493484
conf.setForumURL("http://www.example.com/forum/");
494485
conf.setTPLFile("message.tpl");
495486
// try to load configuration file
496-
if (libstriezel::filesystem::file::exists(defaultSaveDirectory + "pmdb.conf"))
487+
const std::string conf_path = pmdb::paths::conf();
488+
if (libstriezel::filesystem::file::exists(conf_path))
497489
{
498-
if (!conf.loadFromFile(defaultSaveDirectory + "pmdb.conf"))
490+
if (!conf.loadFromFile(conf_path))
499491
{
500492
std::cout << "Could not load pmdb.conf, using default/incomplete values instead.\n";
501493
conf.setTPLFile("message.tpl");
@@ -640,7 +632,7 @@ int main(int argc, char **argv)
640632
if (searchForSubsets)
641633
{
642634
ColourMap cMap;
643-
const std::string pathToColourMap = defaultSaveDirectory + "pmdb.colourmap";
635+
const std::string pathToColourMap = pmdb::paths::colourmap();
644636
if (libstriezel::filesystem::file::exists(pathToColourMap))
645637
{
646638
if (!cMap.loadFromFile(pathToColourMap))

code/paths.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#include "paths.hpp"
22+
#include "../libstriezel/filesystem/directory.hpp"
23+
24+
namespace pmdb
25+
{
26+
27+
std::string paths::main()
28+
{
29+
std::string home;
30+
if (!libstriezel::filesystem::directory::getHome(home))
31+
{
32+
home = std::string(".");
33+
}
34+
35+
return libstriezel::filesystem::slashify(home) + std::string(".pmdb");
36+
}
37+
38+
std::string paths::html()
39+
{
40+
return main() + libstriezel::filesystem::pathDelimiter + "html";
41+
}
42+
43+
std::string paths::conf()
44+
{
45+
return main() + libstriezel::filesystem::pathDelimiter + "pmdb.conf";
46+
}
47+
48+
std::string paths::colourmap()
49+
{
50+
return main() + libstriezel::filesystem::pathDelimiter + "pmdb.colourmap";
51+
}
52+
53+
} // namespace

code/paths.hpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#ifndef PMDB_PATHS_HPP
22+
#define PMDB_PATHS_HPP
23+
24+
#include <string>
25+
26+
namespace pmdb
27+
{
28+
29+
struct paths
30+
{
31+
public:
32+
/** \brief Gets the path to pmdb's main directory.
33+
*
34+
* \remarks This is usually ~/.pmdb or the equivalent.
35+
* \return Returns the path to pmdb's main directory.
36+
*/
37+
static std::string main();
38+
39+
/** \brief Gets the directory where HTML files are saved.
40+
*
41+
* \return Returns the directory where HTML files are saved.
42+
* \remarks This is usually ~/.pmdb/html or the equivalent.
43+
*/
44+
static std::string html();
45+
46+
47+
/** \brief Gets the default path for the configuration file.
48+
*
49+
* \return Returns the default path for the configuration file.
50+
* \remarks This is usually ~/.pmdb/pmdb.conf or the equivalent.
51+
*/
52+
static std::string conf();
53+
54+
/** \brief Gets the path for the colour map.
55+
*
56+
* \return Returns the path for the colour map.
57+
* \remarks This is usually ~/.pmdb/pmdb.colourmap or the equivalent.
58+
*/
59+
static std::string colourmap();
60+
};
61+
62+
} // namespace
63+
64+
#endif // PMDB_PATHS_HPP

code/pmdb.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
<Unit filename="filters/FilterUser.cpp" />
143143
<Unit filename="filters/FilterUser.hpp" />
144144
<Unit filename="main.cpp" />
145+
<Unit filename="paths.cpp" />
146+
<Unit filename="paths.hpp" />
145147
<Extensions>
146148
<lib_finder disable_auto="1" />
147149
</Extensions>

program-no-compression/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(pmdb_no_comp_sources
3333
../code/bbcode/TableBBCode.cpp
3434
../code/bbcode/quotes.cpp
3535
../code/filters/FilterUser.cpp
36+
../code/paths.cpp
3637
../libstriezel/common/DirectoryFileList.cpp
3738
../libstriezel/common/StringUtils.cpp
3839
../libstriezel/filesystem/directory.cpp

tests/components/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ set(component_tests_sources
1919
../../code/bbcode/Smilie.cpp
2020
../../code/bbcode/TableBBCode.cpp
2121
../../code/filters/FilterUser.cpp
22+
../../code/paths.cpp
2223
../../libstriezel/common/StringUtils.cpp
24+
../../libstriezel/filesystem/directory.cpp
2325
../../libstriezel/hash/sha256/MessageSource.cpp
2426
../../libstriezel/hash/sha256/sha256.cpp
2527
../../libstriezel/zlib/CompressionFunctions.cpp
@@ -37,6 +39,7 @@ set(component_tests_sources
3739
bbcode/TableBBCode.cpp
3840
filter/FilterUser.cpp
3941
names_to_controlsequences.cpp
42+
paths.cpp
4043
main.cpp)
4144

4245
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")

tests/components/component_tests.cbp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@
6565
<Unit filename="../../code/filters/Filter.hpp" />
6666
<Unit filename="../../code/filters/FilterUser.cpp" />
6767
<Unit filename="../../code/filters/FilterUser.hpp" />
68+
<Unit filename="../../code/paths.cpp" />
69+
<Unit filename="../../code/paths.hpp" />
6870
<Unit filename="../../libstriezel/common/StringUtils.cpp" />
6971
<Unit filename="../../libstriezel/common/StringUtils.hpp" />
72+
<Unit filename="../../libstriezel/filesystem/directory.cpp" />
73+
<Unit filename="../../libstriezel/filesystem/directory.hpp" />
7074
<Unit filename="../../libstriezel/hash/sha256/MessageSource.cpp" />
7175
<Unit filename="../../libstriezel/hash/sha256/MessageSource.hpp" />
7276
<Unit filename="../../libstriezel/hash/sha256/sha256.cpp" />
@@ -89,6 +93,7 @@
8993
<Unit filename="filter/FilterUser.cpp" />
9094
<Unit filename="main.cpp" />
9195
<Unit filename="names_to_controlsequences.cpp" />
96+
<Unit filename="paths.cpp" />
9297
<Extensions>
9398
<lib_finder disable_auto="1" />
9499
</Extensions>

0 commit comments

Comments
 (0)