reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofConstants.h"
4
5#if !defined(TARGET_MINGW)
6 #include "utf8.h"
7#else
8 #include "utf8cpp/utf8.h" // MSYS2 : use of system-installed include
9#endif
10#include <bitset> // For ofToBinary.
11#include <chrono>
12#include <iomanip> //for setprecision
13#include <algorithm>
14#include <sstream>
15#include <type_traits>
16#include <random>
17
18#include "ofRandomEngine.h"
20
29
37float ofGetElapsedTimef();
38
44uint64_t ofGetUnixTimeMillis();
45
53uint64_t ofGetElapsedTimeMillis();
54
62uint64_t ofGetElapsedTimeMicros();
63
66uint64_t ofGetFrameNum();
67
71int ofGetSeconds();
72
75int ofGetMinutes();
76
79int ofGetHours();
80
86uint64_t ofGetUnixTime();
87
90OF_DEPRECATED_MSG("Use ofGetSystemTimeMillis() instead", uint64_t ofGetSystemTime());
91
94uint64_t ofGetSystemTimeMillis();
95
98uint64_t ofGetSystemTimeMicros();
99
100
101struct ofTime{
102 uint64_t seconds = 0;
103 uint64_t nanoseconds = 0;
104
109
110 uint64_t getAsMilliseconds() const;
111 uint64_t getAsMicroseconds() const;
112 uint64_t getAsNanoseconds() const;
113 double getAsSeconds() const;
114#ifndef TARGET_WIN32
115 timespec getAsTimespec() const;
116#endif
117
118 std::chrono::time_point<std::chrono::nanoseconds> getAsTimePoint() const;
119 std::chrono::nanoseconds operator-(const ofTime&) const;
120 bool operator<(const ofTime&) const;
121 bool operator>(const ofTime&) const;
122 bool operator<=(const ofTime&) const;
123 bool operator>=(const ofTime&) const;
124
125 template<typename rep, typename ratio>
126 ofTime operator+(const std::chrono::duration<rep,ratio> & duration) const{
127 constexpr uint64_t NANOS_PER_SEC = 1000000000ll;
128 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
129 ofTime t = *this;
130 t.nanoseconds += ns.count();
132 uint64_t secs = t.nanoseconds / NANOS_PER_SEC;
133 t.nanoseconds -= NANOS_PER_SEC*secs;
134 t.seconds+=secs;
135 }
136 return t;
137 }
138
139 template<typename rep, typename ratio>
140 ofTime &operator+=(const std::chrono::duration<rep,ratio> & duration){
141 constexpr uint64_t NANOS_PER_SEC = 1000000000ll;
142 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
143 this->nanoseconds += ns.count();
144 if(this->nanoseconds>=NANOS_PER_SEC){
145 uint64_t secs = this->nanoseconds / NANOS_PER_SEC;
146 this->nanoseconds -= NANOS_PER_SEC*secs;
147 this->seconds+=secs;
148 }
149 return *this;
150 }
151};
152
156
159void ofSleepMillis(int millis);
160
167std::string ofGetTimestampString();
168
204std::string ofGetTimestampString(const std::string& timestampFormat);
205
208int ofGetYear();
209
212int ofGetMonth();
213
216int ofGetDay();
217
223int ofGetWeekday();
224
229
230template<typename ... Args>
231void ofShuffle(Args&&... args) {
232 of::random::shuffle(std::forward<Args>(args)...);
233}
234
239
240template<class T>
241void ofRandomize(std::vector<T>& values) {
242 of::random::shuffle(values);
243}
244
283template<class T, class BoolFunction>
284void ofRemove(std::vector<T>& values, BoolFunction shouldErase) {
285 values.erase(remove_if(values.begin(), values.end(), shouldErase), values.end());
286}
287
297template<class T>
298void ofSort(std::vector<T>& values) {
299 sort(values.begin(), values.end());
300}
301
342template<class T, class BoolFunction>
343void ofSort(std::vector<T>& values, BoolFunction compare) {
344 std::sort(values.begin(), values.end(), compare);
345}
346
353template <class T>
354std::size_t ofFind(const std::vector<T>& values, const T& target) {
355 return std::distance(values.begin(), find(values.begin(), values.end(), target));
356}
357
363template <class T>
364bool ofContains(const std::vector<T>& values, const T& target) {
365 return ofFind(values, target) != values.size();
366}
367
368
369
396std::vector<std::string> ofSplitString(const std::string& source, const std::string& delimiter, bool ignoreEmpty = false, bool trim = false);
397
401std::string ofJoinString(const std::vector<std::string>& stringElements, const std::string& delimiter);
402
408void ofStringReplace(std::string& input, const std::string& searchStr, const std::string& replaceStr);
409
421bool ofIsStringInString(const std::string& haystack, const std::string& needle);
422
426std::size_t ofStringTimesInString(const std::string& haystack, const std::string& needle);
427
441std::string ofToLower(const std::string& src, const std::string & locale="");
442
456std::string ofToUpper(const std::string& src, const std::string & locale="");
457
480std::string ofTrimFront(const std::string & src, const std::string & locale = "");
481
504std::string ofTrimBack(const std::string & src, const std::string & locale = "");
505
528std::string ofTrim(const std::string & src, const std::string & locale = "");
529
530OF_DEPRECATED_MSG("Use ofUTF8Append instead", void ofAppendUTF8(std::string & str, uint32_t utf8));
531
543void ofUTF8Append(std::string & utf8, uint32_t codepoint);
544
557void ofUTF8Insert(std::string & utf8, size_t pos, uint32_t codepoint);
558
571void ofUTF8Erase(std::string & utf8, size_t pos, size_t len);
572
584std::string ofUTF8Substring(const std::string & utf8, size_t pos, size_t len);
585
596std::string ofUTF8ToString(uint32_t codepoint);
597
609size_t ofUTF8Length(const std::string & utf8);
610
611
617template <typename ... Args>
618//__attribute__((__format__ (__printf__, 2, 0)))
619std::string ofVAArgsToString(const char * format, Args&& ... args){
620 char buf[256];
621 size_t n = std::snprintf(buf, sizeof(buf), format, std::forward<Args>(args)...);
622
623// std::string str = format;
624// size_t n = std::snprintf(buf, sizeof(buf), str, std::forward<Args>(args)...);
625
626 // Static buffer large enough?
627 if (n < sizeof(buf)) {
628 return{ buf, n };
629 }
630
631 // Static buffer too small
632 std::string s(n + 1, 0);
633 std::snprintf(const_cast<char*>(s.data()), s.size(), format, std::forward<Args>(args)...);
634
635 return s;
636
637}
638
639
656template <class T>
657std::string ofToString(const T& value){
658 std::ostringstream out;
659 out << value;
660 return out.str();
661}
662
671template <class T>
672std::string ofToString(const T& value, int precision){
673 std::ostringstream out;
674 out << std::fixed << std::setprecision(precision) << value;
675 return out.str();
676}
677
687template <class T>
688std::string ofToString(const T& value, int width, char fill ){
689 std::ostringstream out;
690 out << std::fixed << std::setfill(fill) << std::setw(width) << value;
691 return out.str();
692}
693
704template <class T>
705std::string ofToString(const T& value, int precision, int width, char fill ){
706 std::ostringstream out;
707 out << std::fixed << std::setfill(fill) << std::setw(width) << std::setprecision(precision) << value;
708 return out.str();
709}
710
719template<class T>
720std::string ofToString(const std::vector<T>& values) {
721 std::stringstream out;
722 int n = values.size();
723 out << "{";
724 if(n > 0) {
725 for(int i = 0; i < n - 1; i++) {
726 out << values[i] << ", ";
727 }
728 out << values[n - 1];
729 }
730 out << "}";
731 return out.str();
732}
733
741template<class T>
742T ofFromString(const std::string & value){
743 T data;
744 std::stringstream ss;
745 ss << value;
746 ss >> data;
747 return data;
748}
749
753template<>
754std::string ofFromString(const std::string & value);
755
762template<>
763const char * ofFromString(const std::string & value);
764
769template<typename T>
770T ofTo(const std::string & str){
771 T x;
772 std::istringstream cur(str);
773 cur >> x;
774 return x;
775}
776
785int ofToInt(const std::string& intString);
786
794int64_t ofToInt64(const std::string& intString);
795
803float ofToFloat(const std::string& floatString);
804
812double ofToDouble(const std::string& doubleString);
813
822bool ofToBool(const std::string& boolString);
823
832template <class T>
833std::string ofToHex(const T& value) {
834 std::ostringstream out;
835 // pretend that the value is a bunch of bytes
836 unsigned char* valuePtr = (unsigned char*) &value;
837 // the number of bytes is determined by the datatype
838 int numBytes = sizeof(T);
839 // the bytes are stored backwards (least significant first)
840 for(int i = numBytes - 1; i >= 0; i--) {
841 // print each byte out as a 2-character wide hex value
842 out << std::setfill('0') << std::setw(2) << std::hex << (int) valuePtr[i];
843 }
844 return out.str();
845}
846
854template <>
855std::string ofToHex(const std::string& value);
856
864std::string ofToHex(const char* value);
865
873int ofHexToInt(const std::string& intHexString);
874
882char ofHexToChar(const std::string& charHexString);
883
891float ofHexToFloat(const std::string& floatHexString);
892
900std::string ofHexToString(const std::string& stringHexString);
901
911char ofToChar(const std::string& charString);
912
921template <class T>
922std::string ofToBinary(const T& value) {
923 return std::bitset<8 * sizeof(T)>(*reinterpret_cast<const uint64_t*>(&value)).to_string();
924}
925
933template <>
934std::string ofToBinary(const std::string& value);
935
943std::string ofToBinary(const char* value);
944
952int ofBinaryToInt(const std::string& value);
953
961char ofBinaryToChar(const std::string& value);
962
970float ofBinaryToFloat(const std::string& value);
971
979std::string ofBinaryToString(const std::string& value);
980
988std::string ofGetVersionInfo();
989
996unsigned int ofGetVersionMajor();
997
1004unsigned int ofGetVersionMinor();
1005
1012unsigned int ofGetVersionPatch();
1013
1025std::string ofGetVersionPreRelease();
1026
1027
1039void ofSaveScreen(const std::string& filename);
1040
1047void ofSaveFrame(bool bUseViewport = false);
1048
1054void ofSaveViewport(const std::string& filename);
1055
1056
1058
1064#ifndef TARGET_EMSCRIPTEN
1065void ofLaunchBrowser(const std::string& url, bool uriEncodeQuery=false);
1066#endif
1067
1076std::string ofSystem(const std::string& command);
1077
1082
1089std::string ofGetEnv(const std::string & var, const std::string defaultValue = "");
1090
1115public:
1119 ofUTF8Iterator(const std::string & str);
1120
1122 utf8::iterator<std::string::const_iterator> begin() const;
1123
1125 utf8::iterator<std::string::const_iterator> end() const;
1126
1128 utf8::iterator<std::string::const_reverse_iterator> rbegin() const;
1129
1131 utf8::iterator<std::string::const_reverse_iterator> rend() const;
1132
1133private:
1138 std::string src_valid;
1139};
1140
1141
1143namespace of{
1144namespace priv{
1145 void initutils();
1146 void endutils();
1147}
1148}
Iterate through each Unicode codepoint in a UTF8-encoded std::string.
Definition ofUtils.h:1114
utf8::iterator< std::string::const_iterator > begin() const
Definition ofUtils.cpp:723
utf8::iterator< std::string::const_iterator > end() const
Definition ofUtils.cpp:732
utf8::iterator< std::string::const_reverse_iterator > rend() const
Definition ofUtils.cpp:750
utf8::iterator< std::string::const_reverse_iterator > rbegin() const
Definition ofUtils.cpp:741
void initutils()
Definition ofUtils.cpp:62
void endutils()
Definition ofUtils.cpp:67
void shuffle(T &values)
Shuffles the order of the elements within the passed container, using the centralized random engine.
Definition ofRandomEngine.h:68
Definition ofEvents.cpp:625
unsigned int width
Definition ofAppEGLWindow.cpp:124
ofTargetPlatform
This enumerates the targeted operating systems or platforms.
Definition ofConstants.h:31
#define OF_DEPRECATED_MSG(message,...)
Definition ofConstants.h:78
#define NANOS_PER_SEC
Definition ofTimer.cpp:3
uint64_t ofGetSystemTime()
Definition ofUtils.cpp:321
void ofAppendUTF8(string &str, uint32_t utf8)
Definition ofUtils.cpp:831
bool ofToBool(const std::string &boolString)
Convert a string to a boolean.
std::string ofGetVersionPreRelease()
Get the pre-release version of openFrameworks.
Definition ofUtils.cpp:998
std::string ofSystem(const std::string &command)
Executes a system command. Similar to run a command in terminal.
std::vector< std::string > ofSplitString(const std::string &source, const std::string &delimiter, bool ignoreEmpty=false, bool trim=false)
Splits a string using a delimiter.
size_t ofUTF8Length(const std::string &utf8)
Get the number of Unicode code points in a UTF8-encoded string.
Definition ofUtils.cpp:898
uint64_t ofGetSystemTimeMillis()
Get the system time in milliseconds (system uptime).
Definition ofUtils.cpp:326
std::string ofVAArgsToString(const char *format, Args &&... args)
Convert a variable length argument to a string.
Definition ofUtils.h:619
float ofHexToFloat(const std::string &floatHexString)
Convert a string representing an float in hexadecimal to a float.
void ofUTF8Append(std::string &utf8, uint32_t codepoint)
Append a Unicode codepoint to a UTF8-encoded std::string.
std::string ofJoinString(const std::vector< std::string > &stringElements, const std::string &delimiter)
Join a vector of strings together into one string.
std::string ofToHex(const T &value)
Converts any value to its equivalent hexadecimal representation.
Definition ofUtils.h:833
float ofBinaryToFloat(const std::string &value)
Convert a binary string to a float.
int ofGetMinutes()
Get minutes after the hour.
Definition ofUtils.cpp:403
uint64_t ofGetElapsedTimeMillis()
Get the elapsed time in milliseconds.
Definition ofUtils.cpp:301
std::string ofToBinary(const T &value)
Converts any datatype value to a string of only 1s and 0s.
Definition ofUtils.h:922
uint64_t ofGetElapsedTimeMicros()
Get the elapsed time in microseconds.
Definition ofUtils.cpp:306
uint64_t ofGetUnixTime()
Get the number of seconds since Midnight, January 1, 1970.
Definition ofUtils.cpp:336
void ofUTF8Insert(std::string &utf8, size_t pos, uint32_t codepoint)
Insert a Unicode codepoint into a UTF8-encoded string at a position.
int ofHexToInt(const std::string &intHexString)
Convert a string representing an integer in hexadecimal to a string.
void ofRandomize(std::vector< T > &values)
Randomly reorder the values in a vector.
Definition ofUtils.h:241
std::size_t ofStringTimesInString(const std::string &haystack, const std::string &needle)
Check how many times a string contains another string.
void ofSort(std::vector< T > &values)
Sort a vector of values into ascending order.
Definition ofUtils.h:298
std::string ofGetEnv(const std::string &var, const std::string defaultValue="")
Get the value of a given environment variable.
Definition ofUtils.cpp:1105
void ofSleepMillis(int millis)
Sleeps the current thread for the specified amount of milliseconds.
Definition ofUtils.cpp:346
bool ofContains(const std::vector< T > &values, const T &target)
Search for a target value in a vector of values.
Definition ofUtils.h:364
int ofGetMonth()
Get the current month.
Definition ofUtils.cpp:431
void ofRemove(std::vector< T > &values, BoolFunction shouldErase)
Conditionally remove values from a vector.
Definition ofUtils.h:284
char ofToChar(const std::string &charString)
Convert a string representation of a char to a actual char.
int ofGetYear()
Get the current year.
Definition ofUtils.cpp:421
int ofBinaryToInt(const std::string &value)
Convert a binary string to an int.
unsigned int ofGetVersionMajor()
Get the major version number of openFrameworks.
Definition ofUtils.cpp:986
int ofGetHours()
Get the hour of the day.
Definition ofUtils.cpp:412
std::string ofGetVersionInfo()
Get the current version of openFrameworks as a string.
Definition ofUtils.cpp:974
void ofSaveScreen(const std::string &filename)
Saves the current screen image to a file on disk.
void ofLaunchBrowser(const std::string &url, bool uriEncodeQuery=false)
Launch the given URL in the default browser.
void ofStringReplace(std::string &input, const std::string &searchStr, const std::string &replaceStr)
Replace all occurrences of a string with another string.
bool ofIsStringInString(const std::string &haystack, const std::string &needle)
Check if string contains another string.
void ofUTF8Erase(std::string &utf8, size_t pos, size_t len)
Erase a range of codepoints from a UTF8-encoded substring.
uint64_t ofGetFrameNum()
Get the number of frames rendered since the program started.
Definition ofEvents.cpp:50
std::string ofBinaryToString(const std::string &value)
Convert a binary string to ASCII characters.
ofTime ofGetCurrentTime()
Get the system time.
Definition ofUtils.cpp:295
uint64_t ofGetSystemTimeMicros()
Get the system time in microseconds (system uptime).
Definition ofUtils.cpp:331
std::string ofToUpper(const std::string &src, const std::string &locale="")
Converts all characters in the string to uppercase.
std::string ofUTF8ToString(uint32_t codepoint)
Convert a Unicode codepoint to a UTF8-encoded std::string.
Definition ofUtils.cpp:891
T ofFromString(const std::string &value)
Convert a string represetnation to another type.
Definition ofUtils.h:742
char ofBinaryToChar(const std::string &value)
Convert a binary string to an char.
std::string ofUTF8Substring(const std::string &utf8, size_t pos, size_t len)
Extract a range of codepoints from as a std::string.
int64_t ofToInt64(const std::string &intString)
Convert a string to a int64_t.
int ofToInt(const std::string &intString)
Convert a string to an integer.
int ofGetDay()
Get the current day within the month.
Definition ofUtils.cpp:441
int ofGetSeconds()
Get the seconds after the minute.
Definition ofUtils.cpp:394
void ofSaveFrame(bool bUseViewport=false)
Saves the current frame as a PNG image.
Definition ofUtils.cpp:1033
unsigned int ofGetVersionMinor()
Get the minor version number of openFrameworks.
Definition ofUtils.cpp:990
std::string ofTrimFront(const std::string &src, const std::string &locale="")
Remove locale-defined whitespace from the beginning of a string.
ofTargetPlatform ofGetTargetPlatform()
Get the target platform of the current system.
Definition ofUtils.cpp:1074
uint64_t ofGetUnixTimeMillis()
Get the Unix Time in milliseconds.
Definition ofUtils.cpp:340
float ofToFloat(const std::string &floatString)
Convert a string to a float.
std::size_t ofFind(const std::vector< T > &values, const T &target)
Search for a target value in a vector of values.
Definition ofUtils.h:354
double ofToDouble(const std::string &doubleString)
Convert a string to a double.
void ofSaveViewport(const std::string &filename)
Saves the current viewport as an image.
std::string ofToString(const T &value)
Convert a value to a string.
Definition ofUtils.h:657
std::string ofTrim(const std::string &src, const std::string &locale="")
Remove locale-defined whitespace from the beginning and end of a string.
void ofShuffle(Args &&... args)
Randomly reorder the values in a container.
Definition ofUtils.h:231
unsigned int ofGetVersionPatch()
Get the patch version number of openFrameworks.
Definition ofUtils.cpp:994
std::string ofTrimBack(const std::string &src, const std::string &locale="")
Remove locale-defined whitespace from the end of a string.
std::string ofGetTimestampString()
Formats the current system time according to the given format.
Definition ofUtils.cpp:360
void ofResetElapsedTimeCounter()
Reset the elapsed time counter.
Definition ofUtils.cpp:316
int ofGetWeekday()
Get the current weekday.
Definition ofUtils.cpp:450
char ofHexToChar(const std::string &charHexString)
Convert a string representing an char in hexadecimal to a char.
std::string ofToLower(const std::string &src, const std::string &locale="")
Converts all characters in a string to lowercase.
std::string ofHexToString(const std::string &stringHexString)
Convert a string representing an string in hexadecimal to a string.
float ofGetElapsedTimef()
Get the elapsed time in seconds.
Definition ofUtils.cpp:311
T ofTo(const std::string &str)
Convert a string to a given data type.
Definition ofUtils.h:770
Definition ofUtils.h:101
Mode
Definition ofUtils.h:105
@ System
Definition ofUtils.h:106
@ FixedRate
Definition ofUtils.h:107
timespec getAsTimespec() const
Definition ofUtils.cpp:198
bool operator<=(const ofTime &) const
Definition ofUtils.cpp:232
bool operator>(const ofTime &) const
Definition ofUtils.cpp:227
bool operator>=(const ofTime &) const
Definition ofUtils.cpp:237
bool operator<(const ofTime &) const
Definition ofUtils.cpp:222
uint64_t getAsMicroseconds() const
Definition ofUtils.cpp:178
std::chrono::time_point< std::chrono::nanoseconds > getAsTimePoint() const
Definition ofUtils.cpp:207
ofTime operator+(const std::chrono::duration< rep, ratio > &duration) const
Definition ofUtils.h:126
enum ofTime::Mode mode
std::chrono::nanoseconds operator-(const ofTime &) const
Definition ofUtils.cpp:215
uint64_t nanoseconds
Definition ofUtils.h:103
uint64_t seconds
Definition ofUtils.h:102
uint64_t getAsMilliseconds() const
Definition ofUtils.cpp:170
uint64_t getAsNanoseconds() const
Definition ofUtils.cpp:186
double getAsSeconds() const
Definition ofUtils.cpp:193
ofTime & operator+=(const std::chrono::duration< rep, ratio > &duration)
Definition ofUtils.h:140