From 1b68f71348ecf3983b76b40d7940da8377f049b7 Mon Sep 17 00:00:00 2001 From: Andrey Lihatskiy Date: Mon, 29 Apr 2024 07:43:28 +0300 Subject: #824 Process source files in bulk: replace tabs with spaces, convert CRLF to LF, and trim trailing whitespaces as needed --- indra/newview/llautoreplace.cpp | 1228 +++++++++++++++++++-------------------- 1 file changed, 614 insertions(+), 614 deletions(-) (limited to 'indra/newview/llautoreplace.cpp') diff --git a/indra/newview/llautoreplace.cpp b/indra/newview/llautoreplace.cpp index 0516520c56..e1d494f7c7 100644 --- a/indra/newview/llautoreplace.cpp +++ b/indra/newview/llautoreplace.cpp @@ -34,98 +34,98 @@ const char* LLAutoReplace::SETTINGS_FILE_NAME = "autoreplace.xml"; void LLAutoReplace::autoreplaceCallback(S32& replacement_start, S32& replacement_length, LLWString& replacement_string, S32& cursor_pos, const LLWString& input_text) { - // make sure these returned values are cleared in case there is no replacement - replacement_start = 0; - replacement_length = 0; - replacement_string.clear(); - - static LLCachedControl perform_autoreplace(gSavedSettings, "AutoReplace", 0); - if (perform_autoreplace) - { - S32 word_end = cursor_pos - 1; - - bool at_space = (input_text[word_end] == ' '); - bool have_word = (LLWStringUtil::isPartOfWord(input_text[word_end])); - - if (at_space || have_word) - { - if (at_space && word_end > 0) - { - // find out if this space immediately follows a word - word_end--; - have_word = (LLWStringUtil::isPartOfWord(input_text[word_end])); - } - if (have_word) - { - // word_end points to the end of a word, now find the start of the word - std::string word; - S32 word_start = word_end; - for (S32 back_one = word_start - 1; - back_one >= 0 && LLWStringUtil::isPartOfWord(input_text[back_one]); - back_one-- - ) - { - word_start--; // walk word_start back to the beginning of the word - } - LL_DEBUGS("AutoReplace") << "word_start: " << word_start << " word_end: " << word_end << LL_ENDL; - LLWString old_string = input_text.substr(word_start, word_end - word_start + 1); - std::string last_word = wstring_to_utf8str(old_string); - std::string replacement_word(mSettings.replaceWord(last_word)); - - if (replacement_word != last_word) - { - // The last word is one for which we have a replacement - if (at_space) - { - // return the replacement string - replacement_start = word_start; - replacement_length = word_end - word_start + 1; - replacement_string = utf8str_to_wstring(replacement_word); - S32 size_change = replacement_string.size() - old_string.size(); - cursor_pos += size_change; - } - } - } - } - } + // make sure these returned values are cleared in case there is no replacement + replacement_start = 0; + replacement_length = 0; + replacement_string.clear(); + + static LLCachedControl perform_autoreplace(gSavedSettings, "AutoReplace", 0); + if (perform_autoreplace) + { + S32 word_end = cursor_pos - 1; + + bool at_space = (input_text[word_end] == ' '); + bool have_word = (LLWStringUtil::isPartOfWord(input_text[word_end])); + + if (at_space || have_word) + { + if (at_space && word_end > 0) + { + // find out if this space immediately follows a word + word_end--; + have_word = (LLWStringUtil::isPartOfWord(input_text[word_end])); + } + if (have_word) + { + // word_end points to the end of a word, now find the start of the word + std::string word; + S32 word_start = word_end; + for (S32 back_one = word_start - 1; + back_one >= 0 && LLWStringUtil::isPartOfWord(input_text[back_one]); + back_one-- + ) + { + word_start--; // walk word_start back to the beginning of the word + } + LL_DEBUGS("AutoReplace") << "word_start: " << word_start << " word_end: " << word_end << LL_ENDL; + LLWString old_string = input_text.substr(word_start, word_end - word_start + 1); + std::string last_word = wstring_to_utf8str(old_string); + std::string replacement_word(mSettings.replaceWord(last_word)); + + if (replacement_word != last_word) + { + // The last word is one for which we have a replacement + if (at_space) + { + // return the replacement string + replacement_start = word_start; + replacement_length = word_end - word_start + 1; + replacement_string = utf8str_to_wstring(replacement_word); + S32 size_change = replacement_string.size() - old_string.size(); + cursor_pos += size_change; + } + } + } + } + } } std::string LLAutoReplace::getUserSettingsFileName() { - std::string path=gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, ""); + std::string path=gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, ""); - if (!path.empty()) - { - path = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, SETTINGS_FILE_NAME); - } - return path; + if (!path.empty()) + { + path = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, SETTINGS_FILE_NAME); + } + return path; } std::string LLAutoReplace::getAppSettingsFileName() { - std::string path=gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, ""); + std::string path=gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, ""); - if (!path.empty()) - { - path = gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, SETTINGS_FILE_NAME); - } - else - { - LL_ERRS("AutoReplace") << "Failed to get app settings directory name" << LL_ENDL; - } - return path; + if (!path.empty()) + { + path = gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, SETTINGS_FILE_NAME); + } + else + { + LL_ERRS("AutoReplace") << "Failed to get app settings directory name" << LL_ENDL; + } + return path; } LLAutoReplaceSettings LLAutoReplace::getSettings() { - return mSettings; + return mSettings; } void LLAutoReplace::setSettings(const LLAutoReplaceSettings& newSettings) { - mSettings.set(newSettings); - /// Make the newSettings active and write them to user storage - saveToUserSettings(); + mSettings.set(newSettings); + /// Make the newSettings active and write them to user storage + saveToUserSettings(); } LLAutoReplace::LLAutoReplace() @@ -139,87 +139,87 @@ void LLAutoReplace::initSingleton() void LLAutoReplace::loadFromSettings() { - std::string filename=getUserSettingsFileName(); - if (filename.empty()) - { - LL_INFOS("AutoReplace") << "no valid user settings directory." << LL_ENDL; - } - if(gDirUtilp->fileExists(filename)) - { - LLSD userSettings; - llifstream file; - file.open(filename.c_str()); - if (file.is_open()) - { - LLSDSerialize::fromXML(userSettings, file); - } - file.close(); - if ( mSettings.setFromLLSD(userSettings) ) - { - LL_INFOS("AutoReplace") << "settings loaded from '" << filename << "'" << LL_ENDL; - } - else - { - LL_WARNS("AutoReplace") << "invalid settings found in '" << filename << "'" << LL_ENDL; - } - } - else // no user settings found, try application settings - { - std::string defaultName = getAppSettingsFileName(); - LL_INFOS("AutoReplace") << " user settings file '" << filename << "' not found"<< LL_ENDL; - - bool gotSettings = false; - if(gDirUtilp->fileExists(defaultName)) - { - LLSD appDefault; - llifstream file; - file.open(defaultName.c_str()); - if (file.is_open()) - { - LLSDSerialize::fromXMLDocument(appDefault, file); - } - file.close(); - - if ( mSettings.setFromLLSD(appDefault) ) - { - LL_INFOS("AutoReplace") << "settings loaded from '" << defaultName.c_str() << "'" << LL_ENDL; - gotSettings = true; - } - else - { - LL_WARNS("AutoReplace") << "invalid settings found in '" << defaultName.c_str() << "'" << LL_ENDL; - } - } - - if ( ! gotSettings ) - { - if (mSettings.setFromLLSD(mSettings.getExampleLLSD())) - { - LL_WARNS("AutoReplace") << "no settings found; loaded example." << LL_ENDL; - } - else - { - LL_WARNS("AutoReplace") << "no settings found and example invalid!" << LL_ENDL; - } - } - } + std::string filename=getUserSettingsFileName(); + if (filename.empty()) + { + LL_INFOS("AutoReplace") << "no valid user settings directory." << LL_ENDL; + } + if(gDirUtilp->fileExists(filename)) + { + LLSD userSettings; + llifstream file; + file.open(filename.c_str()); + if (file.is_open()) + { + LLSDSerialize::fromXML(userSettings, file); + } + file.close(); + if ( mSettings.setFromLLSD(userSettings) ) + { + LL_INFOS("AutoReplace") << "settings loaded from '" << filename << "'" << LL_ENDL; + } + else + { + LL_WARNS("AutoReplace") << "invalid settings found in '" << filename << "'" << LL_ENDL; + } + } + else // no user settings found, try application settings + { + std::string defaultName = getAppSettingsFileName(); + LL_INFOS("AutoReplace") << " user settings file '" << filename << "' not found"<< LL_ENDL; + + bool gotSettings = false; + if(gDirUtilp->fileExists(defaultName)) + { + LLSD appDefault; + llifstream file; + file.open(defaultName.c_str()); + if (file.is_open()) + { + LLSDSerialize::fromXMLDocument(appDefault, file); + } + file.close(); + + if ( mSettings.setFromLLSD(appDefault) ) + { + LL_INFOS("AutoReplace") << "settings loaded from '" << defaultName.c_str() << "'" << LL_ENDL; + gotSettings = true; + } + else + { + LL_WARNS("AutoReplace") << "invalid settings found in '" << defaultName.c_str() << "'" << LL_ENDL; + } + } + + if ( ! gotSettings ) + { + if (mSettings.setFromLLSD(mSettings.getExampleLLSD())) + { + LL_WARNS("AutoReplace") << "no settings found; loaded example." << LL_ENDL; + } + else + { + LL_WARNS("AutoReplace") << "no settings found and example invalid!" << LL_ENDL; + } + } + } } void LLAutoReplace::saveToUserSettings() { - std::string filename=getUserSettingsFileName(); - llofstream file; - file.open(filename.c_str()); - LLSDSerialize::toPrettyXML(mSettings.asLLSD(), file); - file.close(); - LL_INFOS("AutoReplace") << "settings saved to '" << filename << "'" << LL_ENDL; + std::string filename=getUserSettingsFileName(); + llofstream file; + file.open(filename.c_str()); + LLSDSerialize::toPrettyXML(mSettings.asLLSD(), file); + file.close(); + LL_INFOS("AutoReplace") << "settings saved to '" << filename << "'" << LL_ENDL; } // ================================================================ // LLAutoReplaceSettings // ================================================================ -const std::string LLAutoReplaceSettings::AUTOREPLACE_LIST_NAME = "name"; ///< key for looking up list names +const std::string LLAutoReplaceSettings::AUTOREPLACE_LIST_NAME = "name"; ///< key for looking up list names const std::string LLAutoReplaceSettings::AUTOREPLACE_LIST_REPLACEMENTS = "replacements"; ///< key for looking up replacement map LLAutoReplaceSettings::LLAutoReplaceSettings() @@ -228,572 +228,572 @@ LLAutoReplaceSettings::LLAutoReplaceSettings() LLAutoReplaceSettings::LLAutoReplaceSettings(const LLAutoReplaceSettings& settings) { - // copy all values through fundamental type intermediates for thread safety - mLists = LLSD::emptyArray(); - - for ( LLSD::array_const_iterator list = settings.mLists.beginArray(), listEnd = settings.mLists.endArray(); - list != listEnd; - list++ - ) - { - if ( (*list).isMap() ) // can fail due to LLSD-30: ignore it - { - LLSD listMap = LLSD::emptyMap(); - std::string listName = (*list)[AUTOREPLACE_LIST_NAME]; - listMap[AUTOREPLACE_LIST_NAME] = listName; - listMap[AUTOREPLACE_LIST_REPLACEMENTS] = LLSD::emptyMap(); - - for ( LLSD::map_const_iterator - entry = (*list)[AUTOREPLACE_LIST_REPLACEMENTS].beginMap(), - entriesEnd = (*list)[AUTOREPLACE_LIST_REPLACEMENTS].endMap(); - entry != entriesEnd; - entry++ - ) - { - std::string keyword = entry->first; - std::string replacement = entry->second.asString(); - listMap[AUTOREPLACE_LIST_REPLACEMENTS].insert(keyword, LLSD(replacement)); - } - - mLists.append(listMap); - } - } + // copy all values through fundamental type intermediates for thread safety + mLists = LLSD::emptyArray(); + + for ( LLSD::array_const_iterator list = settings.mLists.beginArray(), listEnd = settings.mLists.endArray(); + list != listEnd; + list++ + ) + { + if ( (*list).isMap() ) // can fail due to LLSD-30: ignore it + { + LLSD listMap = LLSD::emptyMap(); + std::string listName = (*list)[AUTOREPLACE_LIST_NAME]; + listMap[AUTOREPLACE_LIST_NAME] = listName; + listMap[AUTOREPLACE_LIST_REPLACEMENTS] = LLSD::emptyMap(); + + for ( LLSD::map_const_iterator + entry = (*list)[AUTOREPLACE_LIST_REPLACEMENTS].beginMap(), + entriesEnd = (*list)[AUTOREPLACE_LIST_REPLACEMENTS].endMap(); + entry != entriesEnd; + entry++ + ) + { + std::string keyword = entry->first; + std::string replacement = entry->second.asString(); + listMap[AUTOREPLACE_LIST_REPLACEMENTS].insert(keyword, LLSD(replacement)); + } + + mLists.append(listMap); + } + } } void LLAutoReplaceSettings::set(const LLAutoReplaceSettings& newSettings) { - mLists = newSettings.mLists; + mLists = newSettings.mLists; } bool LLAutoReplaceSettings::setFromLLSD(const LLSD& settingsFromLLSD) { - bool settingsValid = true; - - if ( settingsFromLLSD.isArray() ) - { - for ( LLSD::array_const_iterator - list = settingsFromLLSD.beginArray(), - listEnd = settingsFromLLSD.endArray(); - settingsValid && list != listEnd; - list++ - ) - { - if ( (*list).isDefined() ) // can be undef due to LLSD-30: ignore it - { - settingsValid = listIsValid(*list); - } - } - } - else - { - settingsValid = false; - LL_WARNS("AutoReplace") << "settings are not an array" << LL_ENDL; - } - - if ( settingsValid ) - { - mLists = settingsFromLLSD; - } - else - { - LL_WARNS("AutoReplace") << "invalid settings discarded; using hard coded example" << LL_ENDL; - } - - return settingsValid; + bool settingsValid = true; + + if ( settingsFromLLSD.isArray() ) + { + for ( LLSD::array_const_iterator + list = settingsFromLLSD.beginArray(), + listEnd = settingsFromLLSD.endArray(); + settingsValid && list != listEnd; + list++ + ) + { + if ( (*list).isDefined() ) // can be undef due to LLSD-30: ignore it + { + settingsValid = listIsValid(*list); + } + } + } + else + { + settingsValid = false; + LL_WARNS("AutoReplace") << "settings are not an array" << LL_ENDL; + } + + if ( settingsValid ) + { + mLists = settingsFromLLSD; + } + else + { + LL_WARNS("AutoReplace") << "invalid settings discarded; using hard coded example" << LL_ENDL; + } + + return settingsValid; } bool LLAutoReplaceSettings::listNameMatches( const LLSD& list, const std::string name ) { - return list.isMap() - && list.has(AUTOREPLACE_LIST_NAME) - && list[AUTOREPLACE_LIST_NAME].asString() == name; + return list.isMap() + && list.has(AUTOREPLACE_LIST_NAME) + && list[AUTOREPLACE_LIST_NAME].asString() == name; } const LLSD* LLAutoReplaceSettings::getListEntries(std::string listName) { - const LLSD* returnedEntries = NULL; - for( LLSD::array_const_iterator list = mLists.beginArray(), endList = mLists.endArray(); - returnedEntries == NULL && list != endList; - list++ - ) - { - const LLSD& thisList = *list; - if ( listNameMatches(thisList, listName) ) - { - returnedEntries = &thisList[AUTOREPLACE_LIST_REPLACEMENTS]; - } - } - return returnedEntries; + const LLSD* returnedEntries = NULL; + for( LLSD::array_const_iterator list = mLists.beginArray(), endList = mLists.endArray(); + returnedEntries == NULL && list != endList; + list++ + ) + { + const LLSD& thisList = *list; + if ( listNameMatches(thisList, listName) ) + { + returnedEntries = &thisList[AUTOREPLACE_LIST_REPLACEMENTS]; + } + } + return returnedEntries; } std::string LLAutoReplaceSettings::replacementFor(std::string keyword, std::string listName) { - std::string replacement; - bool foundList = false; - for( LLSD::array_const_iterator list = mLists.beginArray(), endList = mLists.endArray(); - ! foundList && list != endList; - list++ - ) - { - const LLSD& thisList = *list; - if ( listNameMatches(thisList, listName) ) - { - foundList = true; // whether there is a replacement or not, we're done - if ( thisList.isMap() - && thisList.has(AUTOREPLACE_LIST_REPLACEMENTS) - && thisList[AUTOREPLACE_LIST_REPLACEMENTS].has(keyword) - ) - { - replacement = thisList[AUTOREPLACE_LIST_REPLACEMENTS][keyword].asString(); - LL_DEBUGS("AutoReplace")<<"'"< '"< '"<second.isString() ) - { - listValid = false; - LL_WARNS("AutoReplace") - << "non-string replacement value found in list '" - << list[AUTOREPLACE_LIST_NAME].asString() << "'" - << LL_ENDL; - } - } - } - - return listValid; + bool listValid = true; + if ( ! list.isMap() ) + { + listValid = false; + LL_WARNS("AutoReplace") << "list is not a map" << LL_ENDL; + } + else if ( ! list.has(AUTOREPLACE_LIST_NAME) + || ! list[AUTOREPLACE_LIST_NAME].isString() + || list[AUTOREPLACE_LIST_NAME].asString().empty() + ) + { + listValid = false; + LL_WARNS("AutoReplace") + << "list found without " << AUTOREPLACE_LIST_NAME + << " (or it is empty)" + << LL_ENDL; + } + else if ( ! list.has(AUTOREPLACE_LIST_REPLACEMENTS) || ! list[AUTOREPLACE_LIST_REPLACEMENTS].isMap() ) + { + listValid = false; + LL_WARNS("AutoReplace") << "list '" << list[AUTOREPLACE_LIST_NAME].asString() << "' without " << AUTOREPLACE_LIST_REPLACEMENTS << LL_ENDL; + } + else + { + for ( LLSD::map_const_iterator + entry = list[AUTOREPLACE_LIST_REPLACEMENTS].beginMap(), + entriesEnd = list[AUTOREPLACE_LIST_REPLACEMENTS].endMap(); + listValid && entry != entriesEnd; + entry++ + ) + { + if ( ! entry->second.isString() ) + { + listValid = false; + LL_WARNS("AutoReplace") + << "non-string replacement value found in list '" + << list[AUTOREPLACE_LIST_NAME].asString() << "'" + << LL_ENDL; + } + } + } + + return listValid; } const LLSD* LLAutoReplaceSettings::exportList(std::string listName) { - const LLSD* exportedList = NULL; - for ( LLSD::array_const_iterator list = mLists.beginArray(), listEnd = mLists.endArray(); - exportedList == NULL && list != listEnd; - list++ - ) - { - if ( listNameMatches(*list, listName) ) - { - const LLSD& namedList = (*list); - exportedList = &namedList; - } - } - return exportedList; + const LLSD* exportedList = NULL; + for ( LLSD::array_const_iterator list = mLists.beginArray(), listEnd = mLists.endArray(); + exportedList == NULL && list != listEnd; + list++ + ) + { + if ( listNameMatches(*list, listName) ) + { + const LLSD& namedList = (*list); + exportedList = &namedList; + } + } + return exportedList; } bool LLAutoReplaceSettings::listNameIsUnique(const LLSD& newList) { - bool nameIsUnique = true; - // this must always be called with a valid list, so it is safe to assume it has a name - std::string newListName = newList[AUTOREPLACE_LIST_NAME].asString(); - for ( LLSD::array_const_iterator list = mLists.beginArray(), listEnd = mLists.endArray(); - nameIsUnique && list != listEnd; - list++ - ) - { - if ( listNameMatches(*list, newListName) ) - { - LL_WARNS("AutoReplace")<<"duplicate list name '"<= 0) - { - LL_DEBUGS("AutoReplace") << "erase "<= 0) + { + LL_DEBUGS("AutoReplace") << "erase "< autoreplace_enabled(gSavedSettings, "AutoReplace", false); - if ( autoreplace_enabled ) - { - LL_DEBUGS("AutoReplace")<<"checking '"< '" << replacements[currentWord].asString() << "'" - << LL_ENDL; - returnedWord = replacements[currentWord].asString(); - } - } - } - return returnedWord; + std::string returnedWord = currentWord; // in case no replacement is found + static LLCachedControl autoreplace_enabled(gSavedSettings, "AutoReplace", false); + if ( autoreplace_enabled ) + { + LL_DEBUGS("AutoReplace")<<"checking '"< '" << replacements[currentWord].asString() << "'" + << LL_ENDL; + returnedWord = replacements[currentWord].asString(); + } + } + } + return returnedWord; } bool LLAutoReplaceSettings::addEntryToList(LLWString keyword, LLWString replacement, std::string listName) { - bool added = false; - - if ( ! keyword.empty() && ! replacement.empty() ) - { - bool isOneWord = true; - for (S32 character = 0; isOneWord && character < keyword.size(); character++ ) - { - if ( ! LLWStringUtil::isPartOfWord(keyword[character]) ) - { - LL_WARNS("AutoReplace") << "keyword '" << wstring_to_utf8str(keyword) << "' not a single word (len "<