git repos / PlatformFolders

commit 3e5ce8ff

sum01 · 2018-04-16 21:01
3e5ce8ff99fb8a40e1c322b33056d549aaa6a96b patch · browse files
parent be6a67eb1e3c0778e3ed24f48d823058b35635fe

Use nullptr (C++11) over NULL

NULL can technically vary on some systems, while C++11's nullptr guarntees safety.

Changed files

M CMakeLists.txt before
M sago/platform_folders.cpp before
diff --git a/CMakeLists.txt b/CMakeLists.txt index 28a8297..7657313 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -1,4 +1,5 @@
-cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
+# For target_compile_features
+cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(platform_folders VERSION 3.2.0 LANGUAGES CXX)
# Since it's off, the library will be static by default
@@ -20,6 +21,12 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
PUBLIC_HEADER "sago/platform_folders.h"
)
+# Minimum C++11 for "nullptr" and other stuff
+target_compile_features(${PROJECT_NAME} PRIVATE
+ cxx_std_11
+ cxx_nullptr
+)
+
# Apple requires linking to CoreServices
# Check sys name instead of "APPLE" for cross-compilation
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
diff --git a/sago/platform_folders.cpp b/sago/platform_folders.cpp index 34e6bae..2dfaa4c 100644 --- a/sago/platform_folders.cpp +++ b/sago/platform_folders.cpp
@@ -29,7 +29,6 @@ SOFTWARE.
#include "platform_folders.h"
#include <iostream>
#include <stdexcept>
-#include <string.h>
#include <cstdio>
#include <cstdlib>
@@ -37,14 +36,15 @@ SOFTWARE.
#include <windows.h>
#include <shlobj.h>
-static std::string win32_utf16_to_utf8(const wchar_t* wstr) {
+static std::string win32_utf16_to_utf8(const wchar_t* wstr)
+{
std::string res;
// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
- int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
+ int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
if (actualSize > 0) {
//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
std::vector<char> buffer(actualSize);
- actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), NULL, NULL);
+ actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), nullptr, nullptr);
res = buffer.data();
}
if (actualSize == 0) {
@@ -58,7 +58,8 @@ static std::string win32_utf16_to_utf8(const wchar_t* wstr) {
static std::string GetWindowsFolder(int folderId, const char* errorMsg) {
wchar_t szPath[MAX_PATH];
szPath[0] = 0;
- if ( !SUCCEEDED( SHGetFolderPathW( NULL, folderId, NULL, 0, szPath ) ) ) {
+ if ( !SUCCEEDED( SHGetFolderPathW( nullptr, folderId, nullptr, 0, szPath ) ) )
+ {
throw std::runtime_error(errorMsg);
}
return win32_utf16_to_utf8(szPath);
@@ -151,7 +152,7 @@ static std::string getLinuxFolderDefault(const char* envName, const char* defaul
static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
std::vector<char> buffer(envValue, envValue + std::strlen(envValue) + 1);
char* p = std::strtok ( &buffer[0], ":");
- while (p != NULL) {
+ while (p != nullptr) {
if (p[0] == '/') {
folders.push_back(p);
}
@@ -160,7 +161,7 @@ static void appendExtraFoldersTokenizer(const char* envName, const char* envValu
//The XDG documentation indicates that the folder should be ignored but that the program should continue.
std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
}
- p = std::strtok (NULL, ":");
+ p = std::strtok (nullptr, ":");
}
}