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 1fc1b4d commit 3f19d24
Show file tree
Hide file tree
Showing 24 changed files with 243 additions and 246 deletions.
8 changes: 4 additions & 4 deletions collaboration/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ void collaboration_link ([[maybe_unused]] const std::string& object,
database_jobs.set_progress (jobid, translate ("Copying"));
if (take_repo && result) {
success.push_back (translate ("Copying the data from the repository and storing it in Bibledit."));
Webserver_Request request {};
filter_git_sync_git_to_bible (std::addressof(request), path, object);
Webserver_Request webserver_request {};
filter_git_sync_git_to_bible (webserver_request, path, object);
}
database_jobs.set_percentage (jobid, 88);

Expand All @@ -213,9 +213,9 @@ void collaboration_link ([[maybe_unused]] const std::string& object,
if (take_me && result) {

// Bibledit's data goes into the local repository.
Webserver_Request request {};
Webserver_Request webserver_request {};
success.push_back (translate("Storing the local Bible data to the staging area."));
filter_git_sync_bible_to_git (std::addressof(request), object, path);
filter_git_sync_bible_to_git (webserver_request, object, path);

// Stage the data: add and remove it as needed.
if (result) {
Expand Down
2 changes: 1 addition & 1 deletion developer/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void developer_logic_import_changes_save (std::string bible, int book, int chapt

Webserver_Request webserver_request {};
std::string explanation = "import changes";
const std::string message = filter::usfm::safely_store_verse (&webserver_request, bible, book, chapter, verse, text, explanation, false);
const std::string message = filter::usfm::safely_store_verse (webserver_request, bible, book, chapter, verse, text, explanation, false);
if (!message.empty()) Database_Logs::log (message);
text.clear ();
}
Expand Down
4 changes: 2 additions & 2 deletions edit/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ string edit2_logic_volatile_key (string bible, int book, int chapter, string edi

void storeLoadedUsfm2 (Webserver_Request& webserver_request, string bible, int book, int chapter, string editor, [[maybe_unused]] const char * message)
{
int userid = filter::strings::user_identifier (std::addressof(webserver_request));
int userid = filter::strings::user_identifier (webserver_request);

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

Expand All @@ -53,7 +53,7 @@ void storeLoadedUsfm2 (Webserver_Request& webserver_request, string bible, int b

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

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

Expand Down
2 changes: 1 addition & 1 deletion edit/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ string edit_save (Webserver_Request& webserver_request)

// Safely store the chapter.
string explanation;
string message = filter::usfm::safely_store_chapter (std::addressof(webserver_request), bible, book, chapter, user_usfm, explanation);
string message = filter::usfm::safely_store_chapter (webserver_request, bible, book, chapter, user_usfm, explanation);
bible_logic::unsafe_save_mail (message, explanation, username, user_usfm, book, chapter);

// If an error message was given, then return that message to the browser.
Expand Down
2 changes: 1 addition & 1 deletion edit/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ string edit_update (Webserver_Request& webserver_request)
string explanation;
string message;
if (good2go && bible_write_access && text_was_edited) {
message = filter::usfm::safely_store_chapter (std::addressof(webserver_request), bible, book, chapter, edited_chapter_usfm, explanation);
message = filter::usfm::safely_store_chapter (webserver_request, bible, book, chapter, edited_chapter_usfm, explanation);
bible_logic::unsafe_save_mail (message, explanation, username, edited_chapter_usfm, book, chapter);
if (!message.empty ()) messages.push_back (message);
}
Expand Down
2 changes: 1 addition & 1 deletion editone2/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ string editone2_save (Webserver_Request& webserver_request)

// Safely store the verse.
string explanation;
string message = filter::usfm::safely_store_verse (std::addressof(webserver_request), bible, book, chapter, verse, verse_usfm, explanation, true);
string message = filter::usfm::safely_store_verse (webserver_request, bible, book, chapter, verse, verse_usfm, explanation, true);
bible_logic::unsafe_save_mail (message, explanation, username, verse_usfm, book, chapter);
// If storing the verse worked out well, there's no message to display.
if (message.empty ()) {
Expand Down
2 changes: 1 addition & 1 deletion editone2/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ string editone2_update (Webserver_Request& webserver_request)
string explanation;
string message;
if (good2go && bible_write_access && text_was_edited) {
message = filter::usfm::safely_store_verse (std::addressof(webserver_request), bible, book, chapter, verse, edited_verse_usfm, explanation, true);
message = filter::usfm::safely_store_verse (webserver_request, bible, book, chapter, verse, edited_verse_usfm, explanation, true);
bible_logic::unsafe_save_mail (message, explanation, username, edited_verse_usfm, book, chapter);
}

Expand Down
2 changes: 1 addition & 1 deletion editusfm/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ string editusfm_save (Webserver_Request& webserver_request)
if (access_bible::book_write (webserver_request, string(), bible, book)) {
// Safely store the chapter.
string explanation;
string message = filter::usfm::safely_store_chapter (std::addressof(webserver_request), bible, book, chapter, chapter_data_to_save, explanation);
string message = filter::usfm::safely_store_chapter (webserver_request, bible, book, chapter, chapter_data_to_save, explanation);
bible_logic::unsafe_save_mail (message, explanation, username, chapter_data_to_save, book, chapter);
if (message.empty()) {
#ifndef HAVE_CLIENT
Expand Down
26 changes: 11 additions & 15 deletions filter/git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,13 @@ void filter_git_sync_modifications_to_git (string bible, string repository)
// The $git is a git repository, and may contain other data as well.
// The filter focuses on reading the data in the git repository, and only writes to it if necessary,
// This speeds up the filter.
void filter_git_sync_bible_to_git (void * webserver_request, string bible, string repository)
void filter_git_sync_bible_to_git (Webserver_Request& webserver_request, string bible, string repository)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

// First stage.
// Read the chapters in the git repository,
// and check if they occur in the database.
// If a chapter is not in the database, remove it from the repository.
vector <int> books = request->database_bibles()->get_books (bible);
vector <int> books = webserver_request.database_bibles()->get_books (bible);
vector <string> bookfiles = filter_url_scandir (repository);
for (auto & bookname : bookfiles) {
string path = filter_url_create_path ({repository, bookname});
Expand All @@ -169,7 +167,7 @@ void filter_git_sync_bible_to_git (void * webserver_request, string bible, strin
if (book) {
if (in_array (book, books)) {
// Book exists in the database: Check the chapters.
vector <int> chapters = request->database_bibles()->get_chapters (bible, book);
vector <int> chapters = webserver_request.database_bibles()->get_chapters (bible, book);
vector <string> chapterfiles = filter_url_scandir (filter_url_create_path ({repository, bookname}));
for (auto & chaptername : chapterfiles) {
string chapter_path = filter_url_create_path ({repository, bookname, chaptername});
Expand Down Expand Up @@ -198,18 +196,18 @@ void filter_git_sync_bible_to_git (void * webserver_request, string bible, strin
// Read the books / chapters from the database,
// and check if they occur in the repository, and the data matches.
// If necessary, save the chapter to the repository.
books = request->database_bibles()->get_books (bible);
books = webserver_request.database_bibles()->get_books (bible);
for (auto & book : books) {
string bookname = database::books::get_english_from_id (static_cast<book_id>(book));
string bookdir = filter_url_create_path ({repository, bookname});
if (!file_or_dir_exists (bookdir)) filter_url_mkdir (bookdir);
vector <int> chapters = request->database_bibles()->get_chapters (bible, book);
vector <int> chapters = webserver_request.database_bibles()->get_chapters (bible, book);
for (auto & chapter : chapters) {
string chapterdir = filter_url_create_path ({bookdir, filter::strings::convert_to_string (chapter)});
if (!file_or_dir_exists (chapterdir)) filter_url_mkdir (chapterdir);
string datafile = filter_url_create_path ({chapterdir, "data"});
string contents = filter_url_file_get_contents (datafile);
string usfm = request->database_bibles()->get_chapter (bible, book, chapter);
string usfm = webserver_request.database_bibles()->get_chapter (bible, book, chapter);
if (contents != usfm) filter_url_file_put_contents (datafile, usfm);
}
}
Expand All @@ -222,10 +220,8 @@ void filter_git_sync_bible_to_git (void * webserver_request, string bible, strin
// The filter focuses on reading the data in the git repository and the database,
// and only writes to the database if necessary,
// This speeds up the filter.
void filter_git_sync_git_to_bible (void * webserver_request, string repository, string bible)
void filter_git_sync_git_to_bible (Webserver_Request& webserver_request, string repository, string bible)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

// Stage one:
// Read the chapters in the git repository,
// and check that they occur in the database.
Expand All @@ -238,7 +234,7 @@ void filter_git_sync_git_to_bible (void * webserver_request, string repository,
int book = static_cast<int>(database::books::get_id_from_english (bookname));
if (book) {
// Check the chapters.
vector <int> chapters = request->database_bibles()->get_chapters (bible, book);
vector <int> chapters = webserver_request.database_bibles()->get_chapters (bible, book);
vector <string> chapterfiles = filter_url_scandir (bookpath);
for (auto & chapterfile : chapterfiles) {
string chapterpath = filter_url_create_path ({bookpath, chapterfile});
Expand Down Expand Up @@ -271,18 +267,18 @@ void filter_git_sync_git_to_bible (void * webserver_request, string repository,
// If a chapter matches, check that the contents of the data in the git
// folder and the contents in the database match.
// If necessary, update the data in the database.
vector <int> books = request->database_bibles()->get_books (bible);
vector <int> books = webserver_request.database_bibles()->get_books (bible);
for (auto & book : books) {
string bookname = database::books::get_english_from_id (static_cast<book_id>(book));
string bookdir = filter_url_create_path ({repository, bookname});
if (file_or_dir_exists (bookdir)) {
vector <int> chapters = request->database_bibles()->get_chapters (bible, book);
vector <int> chapters = webserver_request.database_bibles()->get_chapters (bible, book);
for (auto & chapter : chapters) {
string chapterdir = filter_url_create_path ({bookdir, filter::strings::convert_to_string (chapter)});
if (file_or_dir_exists (chapterdir)) {
string datafile = filter_url_create_path ({chapterdir, "data"});
string contents = filter_url_file_get_contents (datafile);
string usfm = request->database_bibles()->get_chapter (bible, book, chapter);
string usfm = webserver_request.database_bibles()->get_chapter (bible, book, chapter);
if (contents != usfm) {
bible_logic::store_chapter (bible, book, chapter, contents);
Database_Logs::log (translate("A translator updated chapter") + " " + bible + " " + bookname + " " + filter::strings::convert_to_string (chapter));
Expand Down
6 changes: 4 additions & 2 deletions filter/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include <config/libraries.h>
#include <filter/passage.h>

class Webserver_Request;

#ifdef HAVE_CLOUD
std::string filter_git_directory (std::string object);
bool filter_git_init (std::string directory, bool bare = false);
void filter_git_sync_modifications_to_git (std::string bible, std::string repository);
void filter_git_sync_bible_to_git (void * webserver_request, std::string bible, std::string repository);
void filter_git_sync_git_to_bible (void * webserver_request, std::string repository, std::string bible);
void filter_git_sync_bible_to_git (Webserver_Request& webserver_request, std::string bible, std::string repository);
void filter_git_sync_git_to_bible (Webserver_Request& webserver_request, std::string repository, std::string bible);
void filter_git_sync_git_chapter_to_bible (std::string repository, std::string bible, int book, int chapter);
bool filter_git_remote_read (std::string url, std::string & error);
bool filter_git_remote_clone (std::string url, std::string path, int jobid, std::string & error);
Expand Down
5 changes: 2 additions & 3 deletions filter/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1419,10 +1419,9 @@ std::vector <std::string> search_needles (const std::string& search, const std::


// Returns an integer identifier based on the name of the current user.
int user_identifier (void * webserver_request)
int user_identifier (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
const std::string username = request->session_logic()->currentUser ();
const std::string username = webserver_request.session_logic()->currentUser ();
const std::string hash = md5 (username).substr (0, 5);
const int identifier = config::logic::my_stoi (hash, nullptr, 36);
return identifier;
Expand Down
4 changes: 3 additions & 1 deletion filter/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <config/libraries.h>
#include <filter/string.hpp>

class Webserver_Request;

namespace filter::strings {

std::vector <std::string> explode (std::string value, char delimiter);
Expand Down Expand Up @@ -93,7 +95,7 @@ void quick_sort (std::vector <int> & one, std::vector <int> & two, unsigned int
std::string number_in_string (const std::string & str);
std::string markup_words (const std::vector <std::string>& words, std::string text);
std::vector <std::string> search_needles (const std::string& search, const std::string& text);
int user_identifier (void * webserver_request);
int user_identifier (Webserver_Request& webserver_request);
std::string bin2hex (const std::string& bin);
std::string hex2bin (const std::string& hex);
std::string html_tidy (std::string html);
Expand Down
31 changes: 13 additions & 18 deletions filter/usfm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,22 +636,21 @@ string get_closing_usfm (string text, bool embedded)
// It returns an empty string if the difference is below the limit set for the Bible.
// It returns a short message specifying the difference if it exceeds that limit.
// It fills $explanation with a longer message in case saving is not safe.
string save_is_safe (void * webserver_request, string oldtext, string newtext, bool chapter, string & explanation)
string save_is_safe (Webserver_Request& webserver_request,
string oldtext, string newtext, bool chapter, string & explanation)
{
// Two texts are equal: safe.
if (newtext == oldtext) return string();

Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

const char * explanation1 = "The text was not saved for safety reasons.";
const char * explanation2 = "Make fewer changes at a time and wait till the editor has saved the text. Or relax the restriction in the editing settings. See menu Settings - Preferences.";

// Allowed percentage difference.
int allowed_percentage = 0;
if (chapter)
allowed_percentage = request->database_config_user ()->getEditingAllowedDifferenceChapter ();
allowed_percentage = webserver_request.database_config_user ()->getEditingAllowedDifferenceChapter ();
else
allowed_percentage = request->database_config_user ()->getEditingAllowedDifferenceVerse ();
allowed_percentage = webserver_request.database_config_user ()->getEditingAllowedDifferenceVerse ();

// When the verse editor has an empty verse, it should allow for 100% change.
// Same for the chapter editor, if it has empty verses, allow for a 100% change.
Expand Down Expand Up @@ -718,13 +717,11 @@ string save_is_safe (void * webserver_request, string oldtext, string newtext, b
// It also is useful in cases where the session is deleted from the server,
// where the text in the editors would get corrupted.
// It also is useful in view of an unstable connection between browser and server, to prevent data corruption.
string safely_store_chapter (void * webserver_request,
string bible, int book, int chapter, string usfm, string & explanation)
string safely_store_chapter (Webserver_Request& webserver_request,
string bible, int book, int chapter, string usfm, string & explanation)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

// Existing chapter contents.
string existing = request->database_bibles()->get_chapter (bible, book, chapter);
string existing = webserver_request.database_bibles()->get_chapter (bible, book, chapter);

// Bail out if the existing chapter equals the USFM to be saved.
if (usfm == existing) return "";
Expand All @@ -734,7 +731,7 @@ string safely_store_chapter (void * webserver_request,
if (!message.empty ()) return message;

// Record the change in the journal.
string user = request->session_logic ()->currentUser ();
string user = webserver_request.session_logic ()->currentUser ();
bible_logic::log_change (bible, book, chapter, usfm, user, translate ("Saving chapter"), false);

// Safety checks have passed: Save chapter.
Expand All @@ -752,12 +749,10 @@ string safely_store_chapter (void * webserver_request,
// where the text in the editors would get corrupted.
// It also is useful in view of an unstable connection between browser and server, to prevent data corruption.
// It handles combined verses.
string safely_store_verse (void * webserver_request,
string bible, int book, int chapter, int verse, string usfm,
string & explanation, bool quill)
string safely_store_verse (Webserver_Request& webserver_request,
string bible, int book, int chapter, int verse, string usfm,
string & explanation, bool quill)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

usfm = filter::strings::trim (usfm);

// Check that the USFM to be saved is for the correct verse.
Expand All @@ -779,7 +774,7 @@ string safely_store_verse (void * webserver_request,
}

// Get the existing chapter USFM.
string chapter_usfm = request->database_bibles()->get_chapter (bible, book, chapter);
string chapter_usfm = webserver_request.database_bibles()->get_chapter (bible, book, chapter);

// Get the existing USFM fragment for the verse to save.
string existing_verse_usfm;
Expand Down Expand Up @@ -829,7 +824,7 @@ string safely_store_verse (void * webserver_request,
chapter_usfm.insert (pos, usfm);

// Record the change in the journal.
string user = request->session_logic ()->currentUser ();
string user = webserver_request.session_logic ()->currentUser ();
bible_logic::log_change (bible, book, chapter, chapter_usfm, user, translate ("Saving verse"), false);

// Safety checks have passed: Save chapter.
Expand Down
Loading

0 comments on commit 3f19d24

Please sign in to comment.