diff --git a/src/cpphelp.cpp b/src/cpphelp.cpp index 8d38feb..122703e 100644 --- a/src/cpphelp.cpp +++ b/src/cpphelp.cpp @@ -1,226 +1,242 @@ /* @file cpphelp.h * @brief Common cpp helper stuff * * Copyright (C) 2018 Intevation GmbH * * This file is part of GpgOL. * * GpgOL is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * GpgOL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . */ #include "config.h" #include "cpphelp.h" #include #include #include #include #include "common.h" #include #include #include #include void release_cArray (char **carray) { if (carray) { for (int idx = 0; carray[idx]; idx++) xfree (carray[idx]); xfree (carray); } } void -rtrim(std::string &s) { - s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { - return !std::isspace(ch); - }).base(), s.end()); +rtrim(std::string &s) +{ + s.erase (std::find_if (s.rbegin(), s.rend(), [] (int ch) { + return !std::isspace(ch); + }).base(), s.end()); +} + +void +ltrim(std::string &s) +{ + s.erase (s.begin(), std::find_if (s.begin(), s.end(), [] (int ch) { + return !std::isspace(ch); + })); +} + +void +trim(std::string &s) +{ + ltrim (s); + rtrim (s); } char ** vector_to_cArray(const std::vector &vec) { char ** ret = (char**) xmalloc (sizeof (char*) * (vec.size() + 1)); for (size_t i = 0; i < vec.size(); i++) { ret[i] = strdup (vec[i].c_str()); } ret[vec.size()] = NULL; return ret; } bool in_de_vs_mode() { /* We cache the values only once. A change requires restart. This is because checking this is very expensive as gpgconf spawns each process to query the settings. */ static bool checked; static bool vs_mode; if (checked) { return vs_mode; } GpgME::Error err; const auto components = GpgME::Configuration::Component::load (err); log_debug ("%s:%s: Checking for de-vs mode.", SRCNAME, __func__); if (err) { log_error ("%s:%s: Failed to get gpgconf components: %s", SRCNAME, __func__, err.asString ()); checked = true; vs_mode = false; return vs_mode; } for (const auto &component: components) { if (component.name () && !strcmp (component.name (), "gpg")) { for (const auto &option: component.options ()) { if (option.name () && !strcmp (option.name (), "compliance") && option.currentValue ().stringValue () && !stricmp (option.currentValue ().stringValue (), "de-vs")) { log_debug ("%s:%s: Detected de-vs mode", SRCNAME, __func__); checked = true; vs_mode = true; return vs_mode; } } checked = true; vs_mode = false; return vs_mode; } } checked = true; vs_mode = false; return false; } std::map get_registry_subkeys (const char *path) { HKEY theKey; std::map ret; std::string regPath = GPGOL_REGPATH; regPath += "\\"; regPath += path; if (RegOpenKeyEx (HKEY_CURRENT_USER, regPath.c_str (), 0, KEY_ENUMERATE_SUB_KEYS | KEY_READ, &theKey) != ERROR_SUCCESS) { TRACEPOINT; return ret; } DWORD values = 0, maxValueName = 0, maxValueLen = 0; DWORD err = RegQueryInfoKey (theKey, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &values, &maxValueName, &maxValueLen, nullptr, nullptr); if (err != ERROR_SUCCESS) { TRACEPOINT; RegCloseKey (theKey); return ret; } /* Add space for NULL */ maxValueName++; maxValueLen++; char name[maxValueName + 1]; char value[maxValueLen + 1]; for (int i = 0; i < values; i++) { DWORD nameLen = maxValueName; err = RegEnumValue (theKey, i, name, &nameLen, nullptr, nullptr, nullptr, nullptr); if (err != ERROR_SUCCESS) { TRACEPOINT; continue; } DWORD type; DWORD valueLen = maxValueLen; err = RegQueryValueEx (theKey, name, NULL, &type, (BYTE*)value, &valueLen); if (err != ERROR_SUCCESS) { TRACEPOINT; continue; } if (type != REG_SZ) { TRACEPOINT; continue; } ret.insert (std::make_pair (std::string (name, nameLen), std::string (value, valueLen))); } RegCloseKey (theKey); return ret; } template void internal_split (const std::string &s, char delim, Out result) { std::stringstream ss(s); std::string item; while (std::getline (ss, item, delim)) { *(result++) = item; } } std::vector gpgol_split (const std::string &s, char delim) { std::vector elems; internal_split (s, delim, std::back_inserter (elems)); return elems; } diff --git a/src/cpphelp.h b/src/cpphelp.h index 51ffe80..c2f2983 100644 --- a/src/cpphelp.h +++ b/src/cpphelp.h @@ -1,47 +1,49 @@ #ifndef CPPHELP_H #define CPPHELP_H /* @file cpphelp.h * @brief Common cpp helper stuff * * Copyright (C) 2018 Intevation GmbH * * This file is part of GpgOL. * * GpgOL is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * GpgOL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . */ #include #include #include /* Stuff that should be in common but is c++ so it does not fit in there. */ /* Release a null terminated char* array */ void release_cArray (char **carray); /* Trim whitespace from a string. */ -void rtrim(std::string &s); +void rtrim (std::string &s); +void ltrim (std::string &s); +void trim (std::string &s); /* Convert a string vector to a null terminated char array */ char **vector_to_cArray (const std::vector &vec); /* Check if we are in de_vs mode. */ bool in_de_vs_mode (); /* Get a map of all subkey value pairs in a registry key */ std::map get_registry_subkeys (const char *path); std::vector gpgol_split(const std::string &s, char delim); #endif // CPPHELP_H