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 00f8e76 commit 1fc1b4d
Show file tree
Hide file tree
Showing 27 changed files with 105 additions and 108 deletions.
11 changes: 4 additions & 7 deletions database/mail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ using namespace std;
// The risk of corruption is low and acceptable.


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


Expand Down Expand Up @@ -114,8 +114,7 @@ void Database_Mail::send (string to, string subject, string body, int time)
// Get number of mails for the current user.
int Database_Mail::getMailCount ()
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic ()->currentUser();
string user = m_webserver_request.session_logic ()->currentUser();
SqliteSQL sql = SqliteSQL ();
sql.add ("SELECT count(*) FROM mail WHERE username =");
sql.add (user);
Expand All @@ -134,8 +133,7 @@ int Database_Mail::getMailCount ()
vector <Database_Mail_User> Database_Mail::getMails ()
{
vector <Database_Mail_User> mails;
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic ()->currentUser();
string user = m_webserver_request.session_logic ()->currentUser();
SqliteSQL sql = SqliteSQL ();
sql.add ("SELECT rowid, timestamp, subject FROM mail WHERE username =");
sql.add (user);
Expand Down Expand Up @@ -244,4 +242,3 @@ vector <int> Database_Mail::getAllMails ()
}
return rowids;
}

6 changes: 4 additions & 2 deletions database/mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <config/libraries.h>

class Webserver_Request;

struct Database_Mail_User
{
int rowid {0};
Expand All @@ -38,7 +40,7 @@ struct Database_Mail_Item
class Database_Mail
{
public:
Database_Mail (void * webserver_request_in);
Database_Mail (Webserver_Request& webserver_request);
~Database_Mail ();
Database_Mail(const Database_Mail&) = delete;
Database_Mail operator=(const Database_Mail&) = delete;
Expand All @@ -55,5 +57,5 @@ class Database_Mail
std::vector <int> getAllMails ();
private:
sqlite3 * connect ();
void * webserver_request {nullptr};
Webserver_Request& m_webserver_request;
};
2 changes: 1 addition & 1 deletion database/maintenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void database_maintenance ()
database_users.optimize ();


Database_Mail database_mail = Database_Mail (&webserver_request);
Database_Mail database_mail (webserver_request);
database_mail.trim ();
database_mail.optimize ();

Expand Down
2 changes: 1 addition & 1 deletion database/notes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ vector <int> Database_Notes::get_identifiers ()
string Database_Notes::assemble_contents (int identifier, string contents)
{
string new_contents = get_contents (identifier);
string datetime = filter::date::localized_date_format (std::addressof(m_webserver_request));
string datetime = filter::date::localized_date_format (m_webserver_request);
string user = m_webserver_request.session_logic ()->currentUser ();
// To make the notes more readable, add whitespace between the comments.
bool is_initial_comment = new_contents.empty ();
Expand Down
29 changes: 13 additions & 16 deletions demo/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ void demo_clean_data ()

// Create sample notes.
if (config::logic::default_bibledit_configuration ()) {
demo_create_sample_notes (&webserver_request);
demo_create_sample_notes (webserver_request);
}


// Create samples for the workspaces.
if (config::logic::default_bibledit_configuration ()) {
demo_create_sample_workspaces (&webserver_request);
demo_create_sample_workspaces (webserver_request);
}


Expand Down Expand Up @@ -332,10 +332,9 @@ void demo_prepare_sample_bible ()


// Create sample notes.
void demo_create_sample_notes (void * webserver_request)
void demo_create_sample_notes (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
Database_Notes database_notes (*request);
Database_Notes database_notes (webserver_request);
vector <int> identifiers = database_notes.get_identifiers ();
if (identifiers.size () < 10) {
for (int i = 1; i <= 10; i++) {
Expand All @@ -351,10 +350,8 @@ string demo_workspace ()
}


void demo_create_sample_workspaces (void * webserver_request)
void demo_create_sample_workspaces (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

map <int, string> urls {};
map <int, string> widths {};
for (int i = 0; i < 15; i++) {
Expand All @@ -377,18 +374,18 @@ void demo_create_sample_workspaces (void * webserver_request)
pair (2, "")
};

request->database_config_user()->setActiveWorkspace ("USFM");
workspace_set_urls (request, urls);
workspace_set_widths (request, widths);
workspace_set_heights (request, row_heights);
webserver_request.database_config_user()->setActiveWorkspace ("USFM");
workspace_set_urls (std::addressof(webserver_request), urls);
workspace_set_widths (std::addressof(webserver_request), widths);
workspace_set_heights (std::addressof(webserver_request), row_heights);

urls[0] = editone2_index_url ();
urls[1] = resource_index_url ();

request->database_config_user()->setActiveWorkspace (demo_workspace ());
workspace_set_urls (request, urls);
workspace_set_widths (request, widths);
workspace_set_heights (request, row_heights);
webserver_request.database_config_user()->setActiveWorkspace (demo_workspace ());
workspace_set_urls (std::addressof(webserver_request), urls);
workspace_set_widths (std::addressof(webserver_request), widths);
workspace_set_heights (std::addressof(webserver_request), row_heights);
}


Expand Down
6 changes: 4 additions & 2 deletions demo/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <config/libraries.h>

class Webserver_Request;

bool demo_acl (std::string user, std::string pass);

// Returns the address of the current demo server.
Expand All @@ -36,7 +38,7 @@ void demo_clean_data ();
std::string demo_sample_bible_name ();
void demo_create_sample_bible ();
void demo_prepare_sample_bible ();
void demo_create_sample_notes (void * webserver_request);
void demo_create_sample_notes (Webserver_Request& webserver_request);
std::string demo_workspace ();
void demo_create_sample_workspaces (void * webserver_request);
void demo_create_sample_workspaces (Webserver_Request& webserver_request);
std::vector <std::string> demo_logic_default_resources ();
2 changes: 1 addition & 1 deletion edit/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ string edit_load (Webserver_Request& webserver_request)
string unique_id = webserver_request.query ["id"];

// Store a copy of the USFM loaded in the editor for later reference.
storeLoadedUsfm2 (std::addressof(webserver_request), bible, book, chapter, unique_id);
storeLoadedUsfm2 (webserver_request, bible, book, chapter, unique_id);

string stylesheet = Database_Config_Bible::getEditorStylesheet (bible);

Expand Down
12 changes: 5 additions & 7 deletions edit/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,21 @@ string edit2_logic_volatile_key (string bible, int book, int chapter, string edi
}


void storeLoadedUsfm2 (void * webserver_request, string bible, int book, int chapter, string editor, [[maybe_unused]] const char * message)
void storeLoadedUsfm2 (Webserver_Request& webserver_request, string bible, int book, int chapter, string editor, [[maybe_unused]] const char * message)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

int userid = filter::strings::user_identifier (webserver_request);
int userid = filter::strings::user_identifier (std::addressof(webserver_request));

string key = edit2_logic_volatile_key (bible, book, chapter, editor);

string usfm = request->database_bibles()->get_chapter (bible, book, chapter);
string usfm = webserver_request.database_bibles()->get_chapter (bible, book, chapter);

Database_Volatile::setValue (userid, key, usfm);
}


string getLoadedUsfm2 (void * webserver_request, string bible, int book, int chapter, string editor)
string getLoadedUsfm2 (Webserver_Request& webserver_request, string bible, int book, int chapter, string editor)
{
int userid = filter::strings::user_identifier (webserver_request);
int userid = filter::strings::user_identifier (std::addressof(webserver_request));

string key = edit2_logic_volatile_key (bible, book, chapter, editor);

Expand Down
6 changes: 4 additions & 2 deletions edit/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <config/libraries.h>

void storeLoadedUsfm2 (void * webserver_request, std::string bible, int book, int chapter, std::string editor, const char * message = "");
std::string getLoadedUsfm2 (void * webserver_request, std::string bible, int book, int chapter, std::string editor);
class Webserver_Request;

void storeLoadedUsfm2 (Webserver_Request& webserver_request, std::string bible, int book, int chapter, std::string editor, const char * message = "");
std::string getLoadedUsfm2 (Webserver_Request& webserver_request, std::string bible, int book, int chapter, std::string editor);
4 changes: 2 additions & 2 deletions edit/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ string edit_save (Webserver_Request& webserver_request)
editor_export.run ();
string user_usfm = editor_export.get ();

string ancestor_usfm = getLoadedUsfm2 (std::addressof(webserver_request), bible, book, chapter, unique_id);
string ancestor_usfm = getLoadedUsfm2 (webserver_request, bible, book, chapter, unique_id);

vector <filter::usfm::BookChapterData> book_chapter_text = filter::usfm::usfm_import (user_usfm, stylesheet);
if (book_chapter_text.size () != 1) {
Expand Down Expand Up @@ -175,7 +175,7 @@ string edit_save (Webserver_Request& webserver_request)
#endif

// Store a copy of the USFM loaded in the editor for later reference.
storeLoadedUsfm2 (std::addressof(webserver_request), bible, book, chapter, unique_id);
storeLoadedUsfm2 (webserver_request, bible, book, chapter, unique_id);

// Convert the stored USFM to html.
// This converted html should be the same as the saved html.
Expand Down
8 changes: 4 additions & 4 deletions edit/styles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ string edit_styles (Webserver_Request& webserver_request)
{
if (webserver_request.query.count ("style")) {
string style = webserver_request.query["style"];
Editor_Styles::recordUsage (std::addressof(webserver_request), style);
string action = Editor_Styles::getAction (std::addressof(webserver_request), style);
Editor_Styles::recordUsage (webserver_request, style);
string action = Editor_Styles::getAction (webserver_request, style);
return style + "\n" + action;
}


if (webserver_request.query.count ("all")) {
return Editor_Styles::getAll (std::addressof(webserver_request));
return Editor_Styles::getAll (webserver_request);
}


return Editor_Styles::getRecentlyUsed (std::addressof(webserver_request));
return Editor_Styles::getRecentlyUsed (webserver_request);
}

2 changes: 1 addition & 1 deletion editone2/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ string editone2_load (Webserver_Request& webserver_request)
// 2. It updates the chapter snapshot.
// 3. It loads the other verse.
// 4. It updates the chapter snapshot.
storeLoadedUsfm2 (std::addressof(webserver_request), bible, book, chapter, unique_id);
storeLoadedUsfm2 (webserver_request, bible, book, chapter, unique_id);

string prefix_html;
string not_used;
Expand Down
4 changes: 2 additions & 2 deletions editone2/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ string editone2_save (Webserver_Request& webserver_request)
// it's worth to check on this.
// Because the user's editor may not yet have loaded this updated Bible text.
// https://github.com/bibledit/cloud/issues/340
string loaded_usfm = getLoadedUsfm2 (std::addressof(webserver_request), bible, book, chapter, unique_id);
string loaded_usfm = getLoadedUsfm2 (webserver_request, bible, book, chapter, unique_id);
if (loaded_usfm != old_chapter_usfm) {
bible_logic::recent_save_email (bible, book, chapter, username, loaded_usfm, old_chapter_usfm);
}
Expand Down Expand Up @@ -162,7 +162,7 @@ string editone2_save (Webserver_Request& webserver_request)


// Store a copy of the USFM now saved as identical to what's loaded in the editor for later reference.
storeLoadedUsfm2 (std::addressof(webserver_request), bible, book, chapter, unique_id);
storeLoadedUsfm2 (webserver_request, bible, book, chapter, unique_id);

return locale_logic_text_saved ();
}
Expand Down
8 changes: 4 additions & 4 deletions editor/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ string editor_style (Webserver_Request& webserver_request)
{
if (webserver_request.query.count ("style")) {
string style = webserver_request.query["style"];
Editor_Styles::recordUsage (std::addressof(webserver_request), style);
string action = Editor_Styles::getAction (std::addressof(webserver_request), style);
Editor_Styles::recordUsage (webserver_request, style);
string action = Editor_Styles::getAction (webserver_request, style);
return style + "\n" + action;
}


if (webserver_request.query.count ("all")) {
return Editor_Styles::getAll (std::addressof(webserver_request));
return Editor_Styles::getAll (webserver_request);
}


return Editor_Styles::getRecentlyUsed (std::addressof(webserver_request));
return Editor_Styles::getRecentlyUsed (webserver_request);
}

33 changes: 14 additions & 19 deletions editor/styles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ using namespace std;
using namespace pugi;


string Editor_Styles::getRecentlyUsed (void * webserver_request)
string Editor_Styles::getRecentlyUsed (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

string bible = request->database_config_user()->getBible ();
string bible = webserver_request.database_config_user()->getBible ();
string stylesheet = Database_Config_Bible::getEditorStylesheet (bible);

// The recent styles.
string s_styles = request->database_config_user()->getRecentlyAppliedStyles ();
string s_styles = webserver_request.database_config_user()->getRecentlyAppliedStyles ();
vector <string> styles = filter::strings::explode (s_styles, ' ');
string fragment = translate("Select style") + ": ";
for (unsigned int i = 0; i < styles.size(); i++) {
if (i) fragment += " | ";
string marker = styles [i];
Database_Styles_Item data = request->database_styles()->getMarkerData (stylesheet, marker);
Database_Styles_Item data = webserver_request.database_styles()->getMarkerData (stylesheet, marker);
if (data.marker.empty ()) continue;
string name = data.name + " (" + marker + ")";
string info = data.info;
Expand All @@ -80,14 +78,13 @@ string Editor_Styles::getRecentlyUsed (void * webserver_request)
}


string Editor_Styles::getAll (void * webserver_request)
string Editor_Styles::getAll (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string bible = request->database_config_user()->getBible ();
string bible = webserver_request.database_config_user()->getBible ();
string stylesheet = Database_Config_Bible::getEditorStylesheet (bible);

// The styles.
map <string, string> data = request->database_styles()->getMarkersAndNames (stylesheet);
map <string, string> data = webserver_request.database_styles()->getMarkersAndNames (stylesheet);

vector <string> lines;

Expand All @@ -100,7 +97,7 @@ string Editor_Styles::getAll (void * webserver_request)
string marker = item.first;
string name = item.second;
name = translate (name);
Database_Styles_Item marker_data = request->database_styles()->getMarkerData (stylesheet, marker);
Database_Styles_Item marker_data = webserver_request.database_styles()->getMarkerData (stylesheet, marker);
string category = marker_data.category;
category = styles_logic_category_text (category);
string line2 = marker + " " + name + " (" + category + ")";
Expand All @@ -119,11 +116,10 @@ string Editor_Styles::getAll (void * webserver_request)
}


void Editor_Styles::recordUsage (void * webserver_request, string style)
void Editor_Styles::recordUsage (Webserver_Request& webserver_request, string style)
{
if (style == "") return;
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string s_styles = request->database_config_user()->getRecentlyAppliedStyles ();
string s_styles = webserver_request.database_config_user()->getRecentlyAppliedStyles ();
vector <string> styles = filter::strings::explode (s_styles, ' ');
// Erase the style.
styles.erase (remove (styles.begin(), styles.end(), style), styles.end());
Expand All @@ -134,16 +130,15 @@ void Editor_Styles::recordUsage (void * webserver_request, string style)
styles.pop_back ();
}
s_styles = filter::strings::implode (styles, " ");
request->database_config_user()->setRecentlyAppliedStyles (s_styles);
webserver_request.database_config_user()->setRecentlyAppliedStyles (s_styles);
}


string Editor_Styles::getAction (void * webserver_request, string style)
string Editor_Styles::getAction (Webserver_Request& webserver_request, string style)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string bible = request->database_config_user()->getBible ();
string bible = webserver_request.database_config_user()->getBible ();
string stylesheet = Database_Config_Bible::getEditorStylesheet (bible);
Database_Styles_Item data = request->database_styles()->getMarkerData (stylesheet, style);
Database_Styles_Item data = webserver_request.database_styles()->getMarkerData (stylesheet, style);
int type = data.type;
int subtype = data.subtype;

Expand Down
Loading

0 comments on commit 1fc1b4d

Please sign in to comment.