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

Last change on this file since f76874a was f76874a, checked in by meeh <meeh@…>, 2 years ago

Adding XCode workspace & Xcode project, and some files missing from last commit.

Please note that Xcode project embedds the packing script (meaning it runs "ant" - zip files, and moves it to bundle)

  • Property mode set to 100644
File size: 2.5 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
56using std::experimental::optional;
57
58// Use CFStringRef instead of NSString*, otherwise disable ARC
59inline optional<CFStringRef> optionalString(bool val) {
60    optional<CFStringRef> myOptString;
61    if(val) {
62        // Cast to corresponding CoreFoundation object
63        myOptString = (CFStringRef)@"String";
64    }
65    return myOptString;
66}
67
68
69// trim from start (in place)
70static inline void ltrim(std::string &s) {
71    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
72            std::not1(std::ptr_fun<int, int>(std::isspace))));
73}
74
75// trim from end (in place)
76static inline void rtrim(std::string &s) {
77    s.erase(std::find_if(s.rbegin(), s.rend(),
78            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
79}
80
81// trim from both ends (in place)
82static inline void trim(std::string &s) {
83    ltrim(s);
84    rtrim(s);
85}
86
87// trim from start (copying)
88static inline std::string ltrim_copy(std::string s) {
89    ltrim(s);
90    return s;
91}
92
93// trim from end (copying)
94static inline std::string rtrim_copy(std::string s) {
95    rtrim(s);
96    return s;
97}
98
99// trim from both ends (copying)
100static inline std::string trim_copy(std::string s) {
101    trim(s);
102    return s;
103}
104
105#endif
Note: See TracBrowser for help on using the repository browser.