source: launchers/macosx/include/strutil.hpp @ e046418f

Last change on this file since e046418f was e046418f, checked in by meeh <meeh@…>, 23 months ago

Cleaning up the code base, remove dead code and failed attempts.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#ifndef __STRUTIL_HPP__
2#define __STRUTIL_HPP__
3
4#include <algorithm>
5#include <functional>
6#include <string>
7#include <memory>
8#include <iostream>
9#include <cctype>
10#include <locale>
11#include "optional.h"
12
13#include <CoreFoundation/CoreFoundation.h>
14#include <CoreFoundation/CFArray.h>
15#include <CoreFoundation/CFString.h>
16
17inline std::string strprintf(const char *fromat, ...)
18{
19        std::string s;
20        s.resize(128); // best guess
21        char *buff = const_cast<char *>(s.data());
22
23        va_list arglist;
24        va_start(arglist, fromat);
25        auto len = vsnprintf(buff, 128, fromat, arglist);
26        va_end(arglist);
27
28        if (len > 127)
29        {
30                va_start(arglist, fromat);
31                s.resize(len + 1); // leave room for null terminator
32                buff = const_cast<char *>(s.data());
33                len = vsnprintf(buff, len+1, fromat, arglist);
34                va_end(arglist);
35        }
36        s.resize(len);
37        return s; // move semantics FTW
38}
39
40inline std::string extractString(CFStringRef value)
41{
42  const char * data = CFStringGetCStringPtr(value, kCFStringEncodingUTF8);
43  if (data != NULL)
44  {
45    return std::string(data, strlen(data));
46  } else {
47    CFIndex strSize = CFStringGetLength(value)+1;
48    char * retry = (char *)malloc((int)strSize);
49    if (CFStringGetCString(value, retry, strSize, kCFStringEncodingUTF8)) {
50      return std::string(retry, strlen(retry));
51    }
52    return std::string("[null]");
53  }
54}
55
56inline bool replace(std::string& str, const std::string& from, const std::string& to) {
57  size_t start_pos = str.find(from);
58  if(start_pos == std::string::npos)
59    return false;
60  str.replace(start_pos, from.length(), to);
61  return true;
62}
63
64
65// trim from start (in place)
66static inline void ltrim(std::string &s) {
67    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
68            std::not1(std::ptr_fun<int, int>(std::isspace))));
69}
70
71// trim from end (in place)
72static inline void rtrim(std::string &s) {
73    s.erase(std::find_if(s.rbegin(), s.rend(),
74            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
75}
76
77// trim from both ends (in place)
78static inline void trim(std::string &s) {
79    ltrim(s);
80    rtrim(s);
81}
82
83// trim from start (copying)
84static inline std::string ltrim_copy(std::string s) {
85    ltrim(s);
86    return s;
87}
88
89// trim from end (copying)
90static inline std::string rtrim_copy(std::string s) {
91    rtrim(s);
92    return s;
93}
94
95// trim from both ends (copying)
96static inline std::string trim_copy(std::string s) {
97    trim(s);
98    return s;
99}
100
101#ifdef CPP17
102
103
104using std::experimental::optional;
105
106// Use CFStringRef instead of NSString*, otherwise disable ARC
107inline optional<CFStringRef> optionalString(bool val) {
108  optional<CFStringRef> myOptString;
109  if(val) {
110    // Cast to corresponding CoreFoundation object
111    myOptString = (CFStringRef)@"String";
112  }
113  return myOptString;
114}
115
116#endif
117
118
119#endif
Note: See TracBrowser for help on using the repository browser.