Skip to content

Commit

Permalink
Clearer types in code rather than void pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Jan 7, 2024
1 parent 4b4a463 commit 662046a
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 116 deletions.
17 changes: 7 additions & 10 deletions checksum/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,18 @@ string checksum_logic::get (const vector <string>& data)


// Returns a proper checksum for the USFM in the chapter.
string checksum_logic::get_chapter (void * webserver_request, const string & bible, int book, int chapter)
string checksum_logic::get_chapter (Webserver_Request& webserver_request, const string & bible, int book, int chapter)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string usfm = request->database_bibles()->get_chapter (bible, book, chapter);
string usfm = webserver_request.database_bibles()->get_chapter (bible, book, chapter);
string checksum = md5 (filter::strings::trim (usfm));
return checksum;
}


// Returns a proper checksum for the USFM in the book.
string checksum_logic::get_book (void * webserver_request, const string & bible, int book)
string checksum_logic::get_book (Webserver_Request& webserver_request, const string & bible, int book)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
vector <int> chapters = request->database_bibles()->get_chapters (bible, book);
vector <int> chapters = webserver_request.database_bibles()->get_chapters (bible, book);
vector <string> checksums;
for (auto chapter : chapters) {
checksums.push_back (get_chapter (webserver_request, bible, book, chapter));
Expand All @@ -87,10 +85,9 @@ string checksum_logic::get_book (void * webserver_request, const string & bible,


// Returns a proper checksum for the USFM in the $bible.
string checksum_logic::get_bible (void * webserver_request, const string & bible)
string checksum_logic::get_bible (Webserver_Request& webserver_request, const string & bible)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
vector <int> books = request->database_bibles()->get_books (bible);
vector <int> books = webserver_request.database_bibles()->get_books (bible);
vector <string> checksums;
for (auto book : books) {
checksums.push_back (get_book (webserver_request, bible, book));
Expand All @@ -102,7 +99,7 @@ string checksum_logic::get_bible (void * webserver_request, const string & bible


// Returns a proper checksum for the USFM in the array of $bibles.
string checksum_logic::get_bibles (void * webserver_request, const vector <string> & bibles)
string checksum_logic::get_bibles (Webserver_Request& webserver_request, const vector <string> & bibles)
{
vector <string> checksums;
for (const auto & bible : bibles) {
Expand Down
10 changes: 6 additions & 4 deletions checksum/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

#include <config/libraries.h>

class Webserver_Request;

namespace checksum_logic {

std::string send (const std::string & data, bool readwrite);
std::string get (const std::string & data);
std::string get (const std::vector <std::string>& data);
std::string get_chapter (void * webserver_request, const std::string & bible, int book, int chapter);
std::string get_book (void * webserver_request, const std::string & bible, int book);
std::string get_bible (void * webserver_request, const std::string & bible);
std::string get_bibles (void * webserver_request, const std::vector <std::string> & bibles);
std::string get_chapter (Webserver_Request& webserver_request, const std::string & bible, int book, int chapter);
std::string get_book (Webserver_Request& webserver_request, const std::string & bible, int book);
std::string get_bible (Webserver_Request& webserver_request, const std::string & bible);
std::string get_bibles (Webserver_Request& webserver_request, const std::vector <std::string> & bibles);

};
15 changes: 6 additions & 9 deletions confirm/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ using namespace pugi;
#ifdef HAVE_CLOUD


Confirm_Worker::Confirm_Worker (void * webserver_request_in)
Confirm_Worker::Confirm_Worker (Webserver_Request& webserver_request):
m_webserver_request (webserver_request)
{
webserver_request = webserver_request_in;
}


Expand Down Expand Up @@ -75,8 +75,7 @@ void Confirm_Worker::setup (string mailto, string username,
}
node.text ().set (information.c_str());
node = document.append_child ("p");
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string siteUrl = config::logic::site_url (*request);
string siteUrl = config::logic::site_url (m_webserver_request);
string confirmation_url = filter_url_build_http_query (siteUrl + session_confirm_url (), "id", to_string(confirmation_id));
node.text ().set (confirmation_url.c_str());
stringstream output;
Expand All @@ -100,8 +99,7 @@ bool Confirm_Worker::handleEmail ([[maybe_unused]]string from, string subject, s
}
// An active ID was found: Execute the associated database query.
string query = database_confirm.get_query (id);
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
request->database_users()->execute (query);
m_webserver_request.database_users()->execute (query);
// Send confirmation mail.
string mailto = database_confirm.get_mail_to (id);
subject = database_confirm.get_subject (id);
Expand All @@ -121,8 +119,7 @@ bool Confirm_Worker::handleEmail ([[maybe_unused]]string from, string subject, s
bool Confirm_Worker::handleLink (string & email)
{
// Get the confirmation identifier from the link that was clicked.
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string web_id = request->query["id"];
string web_id = m_webserver_request.query["id"];

// If the identifier was not given, the link was not handled successfully.
if (web_id.empty()) return false;
Expand All @@ -137,7 +134,7 @@ bool Confirm_Worker::handleLink (string & email)

// An active ID was found: Execute the associated database query.
string query = database_confirm.get_query (id);
request->database_users()->execute (query);
m_webserver_request.database_users()->execute (query);

// Send confirmation mail.
string mailto = database_confirm.get_mail_to (id);
Expand Down
6 changes: 4 additions & 2 deletions confirm/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <config/libraries.h>

class Webserver_Request;

#ifdef HAVE_CLOUD

class Confirm_Worker
{
public:
Confirm_Worker (void * webserver_request_in);
Confirm_Worker (Webserver_Request& webserver_request);
void setup (std::string mailto, std::string username,
std::string initial_subject, std::string initial_body,
std::string query,
std::string subsequent_subject, std::string subsequent_body);
bool handleEmail (std::string from, std::string subject, std::string body);
bool handleLink (std::string & email);
private:
void * webserver_request {nullptr};
Webserver_Request& m_webserver_request;
void informManagers (std::string email, std::string body);
};

Expand Down
2 changes: 1 addition & 1 deletion consistency/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ string consistency_input (Webserver_Request& webserver_request)
const string translations = webserver_request.post ["translations"];
Database_Volatile::setValue (id, "passages", passages);
Database_Volatile::setValue (id, "translations", translations);
Consistency_Logic consistency_logic (std::addressof(webserver_request), id);
Consistency_Logic consistency_logic (webserver_request, id);
const string response = consistency_logic.response ();
Database_Volatile::setValue (id, "response", response);
return response;
Expand Down
28 changes: 12 additions & 16 deletions consistency/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,26 @@
using namespace std;


Consistency_Logic::Consistency_Logic (void * webserver_request_in, int id_in)
Consistency_Logic::Consistency_Logic (Webserver_Request& webserver_request, int id) :
m_webserver_request (webserver_request), m_id (id)
{
webserver_request = webserver_request_in;
id = id_in;
}


string Consistency_Logic::response ()
{
// The request.
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

// The resources to display in the Consistency tool.
vector <string> resources = request->database_config_user()->getConsistencyResources ();
string bible = access_bible::clamp (*request, request->database_config_user()->getBible ());
vector <string> resources = m_webserver_request.database_config_user()->getConsistencyResources ();
string bible = access_bible::clamp (m_webserver_request, m_webserver_request.database_config_user()->getBible ());
resources.insert (resources.begin (), bible);

// The passages entered in the Consistency tool.
string s_passages = Database_Volatile::getValue (id, "passages");
string s_passages = Database_Volatile::getValue (m_id, "passages");
s_passages = filter::strings::trim (s_passages);
vector <string> passages = filter::strings::explode (s_passages, '\n');

// The translations entered in the Consistency tool.
string s_translations = Database_Volatile::getValue (id, "translations");
string s_translations = Database_Volatile::getValue (m_id, "translations");
s_translations = filter::strings::trim (s_translations);
vector <string> translations = filter::strings::explode (s_translations, '\n');

Expand Down Expand Up @@ -88,10 +84,10 @@ string Consistency_Logic::response ()
// If there was no change, then the data can be fetched from the volatile database.
bool redoPassage = false;
string passageKey = filter::strings::convert_to_string (book) + "." + filter::strings::convert_to_string (chapter) + "." + verse;
int currentChapterId = request->database_bibles()->get_chapter_id (resources [0], book, chapter);
int storedChapterId = filter::strings::convert_to_int (Database_Volatile::getValue (id, passageKey + ".id"));
int currentChapterId = m_webserver_request.database_bibles()->get_chapter_id (resources [0], book, chapter);
int storedChapterId = filter::strings::convert_to_int (Database_Volatile::getValue (m_id, passageKey + ".id"));
if (currentChapterId != storedChapterId) {
Database_Volatile::setValue (id, passageKey + ".id", filter::strings::convert_to_string (currentChapterId));
Database_Volatile::setValue (m_id, passageKey + ".id", filter::strings::convert_to_string (currentChapterId));
redoPassage = true;
}

Expand All @@ -111,9 +107,9 @@ string Consistency_Logic::response ()
text.insert (0, R"(<div style="background-color: yellow;">)");
text.append ("</div>");
}
Database_Volatile::setValue (id, passageKey + "." + resource, text);
Database_Volatile::setValue (m_id, passageKey + "." + resource, text);
} else {
text = Database_Volatile::getValue (id, passageKey + "." + resource);
text = Database_Volatile::getValue (m_id, passageKey + "." + resource);
}

// Formatting.
Expand All @@ -140,7 +136,7 @@ string Consistency_Logic::response ()

string Consistency_Logic::verseText (string resource, int book, int chapter, int verse)
{
return resource_logic_get_html (webserver_request, resource, book, chapter, verse, false);
return resource_logic_get_html (std::addressof(m_webserver_request), resource, book, chapter, verse, false);
}


Expand Down
8 changes: 5 additions & 3 deletions consistency/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

#include <config/libraries.h>

class Webserver_Request;

class Consistency_Logic
{
public:
Consistency_Logic (void * webserver_request_in, int id_in);
Consistency_Logic (Webserver_Request& webserver_request, int id);
std::string response ();
private:
void * webserver_request {nullptr};
int id {0};
Webserver_Request& m_webserver_request;
int m_id {0};
std::string verseText (std::string resource, int book, int chapter, int verse);
std::string omit_verse_text (std::string input);
};
2 changes: 1 addition & 1 deletion consistency/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool consistency_poll_acl (Webserver_Request& webserver_request)
string consistency_poll (Webserver_Request& webserver_request)
{
const int id = filter::strings::convert_to_int (webserver_request.query ["id"]);
Consistency_Logic consistency_logic = Consistency_Logic (std::addressof(webserver_request), id);
Consistency_Logic consistency_logic = Consistency_Logic (webserver_request, id);
const string response = consistency_logic.response ();
if (response != Database_Volatile::getValue (id, "response")) {
Database_Volatile::setValue (id, "response", response);
Expand Down
25 changes: 9 additions & 16 deletions database/config/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
using namespace std;


Database_Config_User::Database_Config_User (void * webserver_request_in)
Database_Config_User::Database_Config_User (Webserver_Request& webserver_request):
m_webserver_request (webserver_request)
{
webserver_request = webserver_request_in;
}


Expand Down Expand Up @@ -70,8 +70,7 @@ string Database_Config_User::mapkey (string user, const char * key)

string Database_Config_User::getValue (const char * key, const char * default_value)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic ()->currentUser ();
string user = m_webserver_request.session_logic ()->currentUser ();
return getValueForUser (user, key, default_value);
}

Expand Down Expand Up @@ -125,8 +124,7 @@ int Database_Config_User::getIValueForUser (string user, const char * key, int d

void Database_Config_User::setValue (const char * key, string value)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic ()->currentUser ();
string user = m_webserver_request.session_logic ()->currentUser ();
setValueForUser (user, key, value);
}

Expand Down Expand Up @@ -163,8 +161,7 @@ void Database_Config_User::setBValueForUser (string user, const char * key, bool

vector <string> Database_Config_User::getList (const char * key)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic ()->currentUser ();
string user = m_webserver_request.session_logic ()->currentUser ();
return getListForUser (user, key);
}

Expand Down Expand Up @@ -193,8 +190,7 @@ vector <string> Database_Config_User::getListForUser (string user, const char *

void Database_Config_User::setList (const char * key, vector <string> values)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic ()->currentUser ();
string user = m_webserver_request.session_logic ()->currentUser ();
setListForUser (user, key, values);
}

Expand Down Expand Up @@ -283,8 +279,7 @@ string Database_Config_User::getBible ()
{
string bible = getValue ("bible", "");
// If the Bible does not exist, take the first one available.
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
Database_Bibles * database_bibles = request->database_bibles ();
Database_Bibles * database_bibles = m_webserver_request.database_bibles ();
vector <string> bibles = database_bibles->get_bibles ();
if (find (bibles.begin (), bibles.end (), bible) == bibles.end ()) {
// There may not even be a first Bible: Create sample Bible.
Expand Down Expand Up @@ -550,8 +545,7 @@ bool Database_Config_User::defaultBibleChecksNotification ()
#ifdef HAVE_CLIENT
return false;
#else
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
int level = request->session_logic ()->currentLevel ();
int level = m_webserver_request.session_logic ()->currentLevel ();
return (level >= Filter_Roles::translator () && level <= Filter_Roles::manager ());
#endif
}
Expand Down Expand Up @@ -1176,8 +1170,7 @@ bool Database_Config_User::getBasicInterfaceModeDefault ()
return true;
#endif
// The app running on a workspace or laptop have default to basic mode for a lower role.
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
int level = request->session_logic ()->currentLevel ();
int level = m_webserver_request.session_logic ()->currentLevel ();
if (level <= Filter_Roles::manager ()) return true;
// Higher role: default to advanced mode.
return false;
Expand Down
6 changes: 4 additions & 2 deletions database/config/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <config/libraries.h>
#include <filter/passage.h>

class Webserver_Request;

class Database_Config_User
{
public:
Database_Config_User (void * webserver_request_in);
Database_Config_User (Webserver_Request& webserver_request);
void trim ();
void remove (std::string username);
void clear_cache ();
Expand Down Expand Up @@ -245,7 +247,7 @@ class Database_Config_User
std::vector <std::string> getChangeNotificationsBiblesForUser (const std::string & user);
void setChangeNotificationsBibles (const std::vector <std::string>& values);
private:
void * webserver_request {nullptr};
Webserver_Request& m_webserver_request;
std::string file (std::string user);
std::string file (std::string user, const char * key);
std::string mapkey (std::string user, const char * key);
Expand Down
2 changes: 1 addition & 1 deletion database/maintenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void database_maintenance ()
database_jobs.optimize ();


Database_Config_User database_config_user = Database_Config_User (&webserver_request);
Database_Config_User database_config_user (webserver_request);
database_config_user.trim ();


Expand Down
2 changes: 1 addition & 1 deletion email/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void email_receive ()
for (int i = 1; i <= emailcount; i++) {

Webserver_Request webserver_request;
Confirm_Worker confirm_worker = Confirm_Worker (&webserver_request);
Confirm_Worker confirm_worker = (webserver_request);
Notes_Logic notes_logic (webserver_request);

error.clear ();
Expand Down
4 changes: 2 additions & 2 deletions library/bibledit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ const char * bibledit_get_reference_for_accordance ()
// Get the active Bible and its versification system.
Webserver_Request webserver_request;
webserver_request.session_logic()->set_username(user);
Database_Config_User database_config_user (&webserver_request);
Database_Config_User database_config_user (webserver_request);
string bible = webserver_request.database_config_user ()->getBible ();
string versification = Database_Config_Bible::getVersificationSystem (bible);

Expand Down Expand Up @@ -474,7 +474,7 @@ void bibledit_put_reference_from_accordance (const char * reference)
int verse = filter::strings::convert_to_int(chapter_verse[1]);

// Get the active Bible and its versification system.
Database_Config_User database_config_user (&webserver_request);
Database_Config_User database_config_user (webserver_request);
string bible = webserver_request.database_config_user ()->getBible ();
string versification = Database_Config_Bible::getVersificationSystem (bible);

Expand Down
Loading

0 comments on commit 662046a

Please sign in to comment.