1 /* 2 * Copyright 2024, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #include "LanguageRepository.h" 6 7 #include <algorithm> 8 9 #include "HaikuDepotConstants.h" 10 #include "Logger.h" 11 #include "StringUtils.h" 12 13 14 LanguageRepository::LanguageRepository() 15 { 16 } 17 18 19 LanguageRepository::~LanguageRepository() 20 { 21 } 22 23 24 void 25 LanguageRepository::Clear() 26 { 27 fLanguages.clear(); 28 HDINFO("did clear the languages"); 29 } 30 31 32 bool 33 LanguageRepository::IsEmpty() const 34 { 35 return fLanguages.empty(); 36 } 37 38 39 int32 40 LanguageRepository::CountLanguages() const 41 { 42 return fLanguages.size(); 43 } 44 45 46 const LanguageRef 47 LanguageRepository::LanguageAtIndex(int32 index) const 48 { 49 return fLanguages[index]; 50 } 51 52 53 void 54 LanguageRepository::AddLanguage(const LanguageRef& value) 55 { 56 int32 index = IndexOfLanguage(value->Code(), value->CountryCode(), value->ScriptCode()); 57 58 if (-1 == index) { 59 std::vector<LanguageRef>::iterator itInsertionPt 60 = std::lower_bound(fLanguages.begin(), fLanguages.end(), value, &IsLanguageBefore); 61 fLanguages.insert(itInsertionPt, value); 62 HDTRACE("did add the language [%s]", value->ID()); 63 } else { 64 fLanguages[index] = value; 65 HDTRACE("did replace the language [%s]", value->ID()); 66 } 67 } 68 69 70 int32 71 LanguageRepository::IndexOfLanguage(const char* code, const char* countryCode, 72 const char* scriptCode) const 73 { 74 size_t size = fLanguages.size(); 75 for (uint32 i = 0; i < size; i++) { 76 const char* lCode = fLanguages[i]->Code(); 77 const char* lCountryCode = fLanguages[i]->CountryCode(); 78 const char* lScriptCode = fLanguages[i]->ScriptCode(); 79 80 if (0 == StringUtils::NullSafeCompare(code, lCode) 81 && 0 == StringUtils::NullSafeCompare(countryCode, lCountryCode) 82 && 0 == StringUtils::NullSafeCompare(scriptCode, lScriptCode)) { 83 return i; 84 } 85 } 86 87 return -1; 88 } 89