crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
locale.h
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * locale.h
24  *
25  * List available locales.
26  *
27  * Created on: Apr 17, 2019
28  * Author: ans
29  */
30 
31 #ifndef HELPER_PORTABILITY_LOCALE_H_
32 #define HELPER_PORTABILITY_LOCALE_H_
33 
34 #include <string>
35 #include <vector>
36 
37 #ifdef _WIN32
38 #include "stdafx.h"
39 #include "windows.h"
40 #include <wstring>
41 #else
42 #include "../Strings.hpp"
43 #include "../System.hpp"
44 #endif
45 
47 
48 #ifdef _WIN32
49  inline constexpr auto localeBufferSize{1024};
51 
53 
62  BOOL CALLBACK addLocale(LPWSTR pStr, DWORD dwFlags, LPARAM lparam) {
63  WCHAR wcBuffer[localeBufferSize];
64  int iResult{};
65 
66  if(GetLocaleInfoEx(pStr, LOCALE_SENGLANGUAGE, wcBuffer, localeBufferSize)) {
67  GetLocaleInfoEx(pStr, LOCALE_SENGLANGUAGE, wcBuffer, localeBufferSize);
68 
69  std::wstring wstr(wcBuffer);
70 
71  reinterpret_cast<std::vector<std::string> *>(
72  lparam)->emplace_back(wstr.cbegin(), wstr.cend()
73  );
74  }
75 
76  return TRUE;
77  }
78 #endif
79 
81 
85  inline std::vector<std::string> enumLocales() {
86  std::vector<std::string> result;
87 
88 #ifdef _WIN32
89  EnumSystemLocalesEx(addLocale, LOCALE_ALL, reinterpret_cast<LPARAM>(&result), NULL);
90 #else
91  const std::string locales = Helper::System::exec("locale -a");
92 #endif
93 
94  return Helper::Strings::split(locales, "\n");
95  }
96 
97 } /* namespace crawlservpp::Helper::Portability */
98 
99 #endif /* HELPER_PORTABILITY_LOCALE_H_ */
Namespace for global portability functions.
Definition: getch.h:44
std::string exec(const char *cmd)
Executes a system command and returns the stdout of the program.
Definition: System.hpp:64
std::vector< std::string > enumLocales()
Enumerates all available locale on the system.
Definition: locale.h:85
std::vector< std::string > split(const std::string &str, char delimiter)
Splits a string into a vector of strings using the given delimiter.
Definition: Strings.hpp:739