/
ofUtils.cpp
764 lines (649 loc) · 18.8 KB
/
ofUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "ofUtils.h"
#include "ofImage.h"
#include "ofTypes.h"
#include "ofGraphics.h"
#include "ofAppRunner.h"
#include "Poco/String.h"
#include "Poco/LocalDateTime.h"
#include "Poco/DateTimeFormatter.h"
#include <cctype> // for toupper
#ifdef TARGET_WIN32
#ifndef _MSC_VER
#include <unistd.h> // this if for MINGW / _getcwd
#include <sys/param.h> // for MAXPATHLEN
#endif
#endif
#if defined(TARGET_OF_IPHONE) || defined(TARGET_OSX ) || defined(TARGET_LINUX)
#include <sys/time.h>
#endif
#ifdef TARGET_OSX
#ifndef TARGET_OF_IPHONE
#include <mach-o/dyld.h>
#include <sys/param.h> // for MAXPATHLEN
#endif
#endif
#ifdef TARGET_WIN32
#include <mmsystem.h>
#ifdef _MSC_VER
#include <direct.h>
#endif
#endif
#ifdef TARGET_OF_IPHONE
#include "ofxiPhoneExtras.h"
#endif
#ifdef TARGET_ANDROID
#include "ofxAndroidUtils.h"
#endif
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
static bool enableDataPath = true;
static unsigned long long startTime = ofGetSystemTime(); // better at the first frame ?? (currently, there is some delay from static init, to running.
static unsigned long long startTimeMicros = ofGetSystemTimeMicros();
//--------------------------------------
unsigned long long ofGetElapsedTimeMillis(){
return ofGetSystemTime() - startTime;
}
//--------------------------------------
unsigned long long ofGetElapsedTimeMicros(){
return ofGetSystemTimeMicros() - startTimeMicros;
}
//--------------------------------------
float ofGetElapsedTimef(){
return ofGetElapsedTimeMicros() / 1000000.0f;
}
//--------------------------------------
void ofResetElapsedTimeCounter(){
startTime = ofGetSystemTime();
startTimeMicros = ofGetSystemTimeMicros();
}
//=======================================
// this is from freeglut, and used internally:
/* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.
* This value wraps every 49.7 days, but integer overflows cancel
* when subtracting an initial start time, unless the total time exceeds
* 32-bit, where the GLUT API return value is also overflowed.
*/
unsigned long long ofGetSystemTime( ) {
#ifndef TARGET_WIN32
struct timeval now;
gettimeofday( &now, NULL );
return
(unsigned long long) now.tv_usec/1000 +
(unsigned long long) now.tv_sec*1000;
#else
#if defined(_WIN32_WCE)
return GetTickCount();
#else
return timeGetTime();
#endif
#endif
}
unsigned long long ofGetSystemTimeMicros( ) {
#ifndef TARGET_WIN32
struct timeval now;
gettimeofday( &now, NULL );
return
(unsigned long long) now.tv_usec +
(unsigned long long) now.tv_sec*1000000;
#else
#if defined(_WIN32_WCE)
return ((unsigned long long)GetTickCount()) * 1000;
#else
return ((unsigned long long)timeGetTime()) * 1000;
#endif
#endif
}
//--------------------------------------------------
unsigned int ofGetUnixTime(){
return (unsigned int)time(NULL);
}
//default ofGetTimestampString returns in this format: 2011-01-15-18-29-35-299
//--------------------------------------------------
string ofGetTimestampString(){
string timeFormat = "%Y-%m-%d-%H-%M-%S-%i";
Poco::LocalDateTime now;
return Poco::DateTimeFormatter::format(now, timeFormat);
}
//specify the string format - eg: %Y-%m-%d-%H-%M-%S-%i ( 2011-01-15-18-29-35-299 )
//--------------------------------------------------
string ofGetTimestampString(string timestampFormat){
Poco::LocalDateTime now;
return Poco::DateTimeFormatter::format(now, timestampFormat);
}
//--------------------------------------------------
int ofGetSeconds(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
return local.tm_sec;
}
//--------------------------------------------------
int ofGetMinutes(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
return local.tm_min;
}
//--------------------------------------------------
int ofGetHours(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
return local.tm_hour;
}
//--------------------------------------------------
int ofGetYear(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
int year = local.tm_year + 1900;
return year;
}
//--------------------------------------------------
int ofGetMonth(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
int month = local.tm_mon + 1;
return month;
}
//--------------------------------------------------
int ofGetDay(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
return local.tm_mday;
}
//--------------------------------------------------
int ofGetWeekday(){
time_t curr;
tm local;
time(&curr);
local =*(localtime(&curr));
return local.tm_wday;
}
//--------------------------------------------------
void ofEnableDataPath(){
enableDataPath = true;
}
//--------------------------------------------------
void ofDisableDataPath(){
enableDataPath = false;
}
//--------------------------------------------------
//use ofSetDataPathRoot() to override this
static string & dataPathRoot(){
#if defined TARGET_OSX
static string * dataPathRoot = new string("../../../data/");
#elif defined TARGET_ANDROID
static string * dataPathRoot = new string("sdcard/");
#elif defined(TARGET_LINUX)
static string * dataPathRoot = new string(ofFilePath::join(ofFilePath::getCurrentExeDir(), "data/"));
#else
static string * dataPathRoot = new string("data/");
#endif
return *dataPathRoot;
}
static bool & isDataPathSet(){
static bool * dataPathSet = new bool(false);
return * dataPathSet;
}
//--------------------------------------------------
void ofSetDataPathRoot(string newRoot){
string newPath = "";
#ifdef TARGET_OSX
#ifndef TARGET_OF_IPHONE
char path[MAXPATHLEN];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0){
//printf("executable path is %s\n", path);
string pathStr = string(path);
//theo: check this with having '/' as a character in a folder name - OSX treats the '/' as a ':'
//checked with spaces too!
vector < string> pathBrokenUp = ofSplitString( pathStr, "/");
newPath = "";
for(int i = 0; i < pathBrokenUp.size()-1; i++){
newPath += pathBrokenUp[i];
newPath += "/";
}
//cout << newPath << endl; // some sanity checks here
//system( "pwd" );
chdir ( newPath.c_str() );
//system("pwd");
}else{
ofLog(OF_LOG_FATAL_ERROR, "buffer too small; need size %u\n", size);
}
#endif
#endif
dataPathRoot() = newRoot;
isDataPathSet() = true;
}
//--------------------------------------------------
string ofToDataPath(string path, bool makeAbsolute){
if (!isDataPathSet())
ofSetDataPathRoot(dataPathRoot());
if( enableDataPath ){
//we create dataPath as a string for the check, on windows we modify it to check both types of slashes
//however we use the original value from dataPathRoot() to prepend the string if needed.
string dataPath = dataPathRoot();
string enclosingFolder = path.substr(0,dataPath.length());
#ifdef TARGET_WIN32
//this is so we can check both "data\" and "data/" on windows
std::replace( enclosingFolder.begin(), enclosingFolder.end(), '\\', '/' );
std::replace( dataPath.begin(), dataPath.end(), '\\', '/' );
#endif // TARGET_WIN32
//check if absolute path has been passed or if data path has already been applied
//do we want to check for C: D: etc ?? like substr(1, 2) == ':' ??
if( path.length()==0 || (path.substr(0,1) != "/" && path.substr(1,1) != ":" && enclosingFolder != dataPath)){
path = dataPathRoot()+path;
}
if(makeAbsolute && (path.length()==0 || path.substr(0,1) != "/")){
#if !defined( TARGET_OF_IPHONE) & !defined(TARGET_ANDROID)
#ifndef TARGET_WIN32
char currDir[1024];
path = "/"+path;
path = getcwd(currDir, 1024)+path;
#else
char currDir[1024];
path = "\\"+path;
path = _getcwd(currDir, 1024)+path;
std::replace( path.begin(), path.end(), '/', '\\' ); // fix any unixy paths...
#endif
#else
//do we need iphone specific code here?
#endif
}
}
return path;
}
//----------------------------------------
template <>
string ofToHex(const string& value) {
ostringstream out;
// how many bytes are in the string
int numBytes = value.size();
for(int i = 0; i < numBytes; i++) {
// print each byte as a 2-character wide hex value
out << setfill('0') << setw(2) << hex << (unsigned int) ((unsigned char)value[i]);
}
return out.str();
}
//----------------------------------------
string ofToHex(const char* value) {
// this function is necessary if you want to print a string
// using a syntax like ofToHex("test")
return ofToHex((string) value);
}
//----------------------------------------
int ofToInt(const string& intString) {
int x = 0;
istringstream cur(intString);
cur >> x;
return x;
}
//----------------------------------------
int ofHexToInt(const string& intHexString) {
int x = 0;
istringstream cur(intHexString);
cur >> hex >> x;
return x;
}
//----------------------------------------
char ofHexToChar(const string& charHexString) {
int x = 0;
istringstream cur(charHexString);
cur >> hex >> x;
return (char) x;
}
//----------------------------------------
float ofHexToFloat(const string& floatHexString) {
union intFloatUnion {
int x;
float f;
} myUnion;
myUnion.x = 0;
istringstream cur(floatHexString);
cur >> hex >> myUnion.x;
return myUnion.f;
}
//----------------------------------------
string ofHexToString(const string& stringHexString) {
stringstream out;
stringstream stream(stringHexString);
// a hex string has two characters per byte
int numBytes = stringHexString.size() / 2;
for(int i = 0; i < numBytes; i++) {
string curByte;
// grab two characters from the hex string
stream >> setw(2) >> curByte;
// prepare to parse the two characters
stringstream curByteStream(curByte);
int cur = 0;
// parse the two characters as a hex-encoded int
curByteStream >> hex >> cur;
// add the int as a char to our output stream
out << (char) cur;
}
return out.str();
}
//----------------------------------------
float ofToFloat(const string& floatString) {
float x = 0;
istringstream cur(floatString);
cur >> x;
return x;
}
//----------------------------------------
double ofToDouble(const string& doubleString) {
double x = 0;
istringstream cur(doubleString);
cur >> x;
return x;
}
//----------------------------------------
bool ofToBool(const string& boolString) {
static const string trueString = "true";
static const string falseString = "false";
string lower = Poco::toLower(boolString);
if(lower == trueString) {
return true;
}
if(lower == falseString) {
return false;
}
bool x = false;
istringstream cur(lower);
cur >> x;
return x;
}
//----------------------------------------
char ofToChar(const string& charString) {
char x = '\0';
istringstream cur(charString);
cur >> x;
return x;
}
//----------------------------------------
template <> string ofToBinary(const string& value) {
stringstream out;
int numBytes = value.size();
for(int i = 0; i < numBytes; i++) {
bitset<8> bitBuffer(value[i]);
out << bitBuffer;
}
return out.str();
}
//----------------------------------------
string ofToBinary(const char* value) {
// this function is necessary if you want to print a string
// using a syntax like ofToBinary("test")
return ofToBinary((string) value);
}
//----------------------------------------
int ofBinaryToInt(const string& value) {
const int intSize = sizeof(int) * 8;
bitset<intSize> binaryString(value);
return (int) binaryString.to_ulong();
}
//----------------------------------------
char ofBinaryToChar(const string& value) {
const int charSize = sizeof(char) * 8;
bitset<charSize> binaryString(value);
return (char) binaryString.to_ulong();
}
//----------------------------------------
float ofBinaryToFloat(const string& value) {
const int floatSize = sizeof(float) * 8;
bitset<floatSize> binaryString(value);
union ulongFloatUnion {
unsigned long result;
float f;
} myUFUnion;
myUFUnion.result = binaryString.to_ulong();
return myUFUnion.f;
}
//----------------------------------------
string ofBinaryToString(const string& value) {
ostringstream out;
stringstream stream(value);
bitset<8> byteString;
int numBytes = value.size() / 8;
for(int i = 0; i < numBytes; i++) {
stream >> byteString;
out << (char) byteString.to_ulong();
}
return out.str();
}
//--------------------------------------------------
vector <string> ofSplitString(const string & source, const string & delimiter, bool ignoreEmpty, bool trim) {
vector<string> result;
if (delimiter.empty()) {
result.push_back(source);
return result;
}
string::const_iterator substart = source.begin(), subend;
while (true) {
subend = search(substart, source.end(), delimiter.begin(), delimiter.end());
string sub(substart, subend);
if(trim) {
Poco::trimInPlace(sub);
}
if (!ignoreEmpty || !sub.empty()) {
result.push_back(sub);
}
if (subend == source.end()) {
break;
}
substart = subend + delimiter.size();
}
return result;
}
//--------------------------------------------------
string ofJoinString(vector <string> stringElements, const string & delimiter){
string resultString = "";
int numElements = stringElements.size();
for(int k = 0; k < numElements; k++){
if( k < numElements-1 ){
resultString += stringElements[k] + delimiter;
} else {
resultString += stringElements[k];
}
}
return resultString;
}
//--------------------------------------------------
void ofStringReplace(string& input, string searchStr, string replaceStr){
size_t uPos = 0;
size_t uFindLen = searchStr.length();
size_t uReplaceLen = replaceStr.length();
if( uFindLen == 0 ){
return;
}
for( ;(uPos = input.find( searchStr, uPos )) != std::string::npos; ){
input.replace( uPos, uFindLen, replaceStr );
uPos += uReplaceLen;
}
}
//--------------------------------------------------
bool ofIsStringInString(string haystack, string needle){
return ( strstr(haystack.c_str(), needle.c_str() ) != NULL );
}
//--------------------------------------------------
string ofToLower(const string & src){
string dst(src);
transform(src.begin(),src.end(),dst.begin(),::tolower);
return dst;
}
//--------------------------------------------------
string ofToUpper(const string & src){
string dst(src);
transform(src.begin(),src.end(),dst.begin(),::toupper);
return dst;
}
//--------------------------------------------------
string ofVAArgsToString(const char * format, ...){
// variadic args to string:
// http://www.codeproject.com/KB/string/string_format.aspx
static char aux_buffer[10000];
string retStr("");
if (NULL != format){
va_list marker;
// initialize variable arguments
va_start(marker, format);
// Get formatted string length adding one for NULL
size_t len = vsprintf(aux_buffer, format, marker) + 1;
// Reset variable arguments
va_end(marker);
if (len > 0)
{
va_list args;
// initialize variable arguments
va_start(args, format);
// Create a char vector to hold the formatted string.
vector<char> buffer(len, '\0');
vsprintf(&buffer[0], format, args);
retStr = &buffer[0];
va_end(args);
}
}
return retStr;
}
string ofVAArgsToString(const char * format, va_list args){
// variadic args to string:
// http://www.codeproject.com/KB/string/string_format.aspx
char aux_buffer[10000];
string retStr("");
if (NULL != format){
// Get formatted string length adding one for NULL
vsprintf(aux_buffer, format, args);
retStr = aux_buffer;
}
return retStr;
}
//--------------------------------------------------
void ofLaunchBrowser(string url){
// http://support.microsoft.com/kb/224816
// make sure it is a properly formatted url:
// some platforms, like Android, require urls to start with lower-case http/https
if(Poco::icompare(url.substr(0,8), "https://") == 0){
url.replace(0,5,"https");
}
else if(Poco::icompare(url.substr(0,7), "http://") == 0){
url.replace(0,4,"http");
}
else{
ofLog(OF_LOG_WARNING, "ofLaunchBrowser: url must begin with http:// or https://");
return;
}
#ifdef TARGET_WIN32
#if (_MSC_VER)
// microsoft visual studio yaks about strings, wide chars, unicode, etc
ShellExecuteA(NULL, "open", url.c_str(),
NULL, NULL, SW_SHOWNORMAL);
#else
ShellExecute(NULL, "open", url.c_str(),
NULL, NULL, SW_SHOWNORMAL);
#endif
#endif
#ifdef TARGET_OSX
// ok gotta be a better way then this,
// this is what I found...
string commandStr = "open "+url;
system(commandStr.c_str());
#endif
#ifdef TARGET_LINUX
string commandStr = "xdg-open "+url;
int ret = system(commandStr.c_str());
if(ret!=0) ofLog(OF_LOG_ERROR,"ofLaunchBrowser: couldn't open browser");
#endif
#ifdef TARGET_OF_IPHONE
ofxiPhoneLaunchBrowser(url);
#endif
#ifdef TARGET_ANDROID
ofxAndroidLaunchBrowser(url);
#endif
}
//--------------------------------------------------
string ofGetVersionInfo(){
stringstream sstr;
sstr << OF_VERSION_MAJOR << "." << OF_VERSION_MINOR << "." << OF_VERSION_PATCH << endl;
return sstr.str();
}
//---- new to 006
//from the forums http://www.openframeworks.cc/forum/viewtopic.php?t=1413
//--------------------------------------------------
void ofSaveScreen(string filename) {
ofImage screen;
screen.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR);
screen.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
screen.saveImage(filename);
}
//--------------------------------------------------
void ofSaveViewport(string filename) {
// because ofSaveScreen doesn't related to viewports
ofImage screen;
ofRectangle view = ofGetCurrentViewport();
screen.allocate(view.width, view.height, OF_IMAGE_COLOR);
screen.grabScreen(0, 0, view.width, view.height);
screen.saveImage(filename);
}
//--------------------------------------------------
int saveImageCounter = 0;
void ofSaveFrame(bool bUseViewport){
string fileName = ofToString(saveImageCounter) + ".png";
if (bUseViewport){
ofSaveViewport(fileName);
} else {
ofSaveScreen(fileName);
}
saveImageCounter++;
}
//--------------------------------------------------
string ofSystem(string command){
FILE * ret = NULL;
#ifdef TARGET_WIN32
ret = _popen(command.c_str(),"r");
#else
ret = popen(command.c_str(),"r");
#endif
string strret;
char c;
if (ret == NULL){
ofLogError() << "ofSystem: error opening return file";
}else{
do {
c = fgetc (ret);
strret += c;
} while (c != EOF);
fclose (ret);
}
return strret;
}
//--------------------------------------------------
ofTargetPlatform ofGetTargetPlatform(){
#ifdef TARGET_LINUX
if(ofSystem("uname -m").find("x86_64")==0)
return OF_TARGET_LINUX64;
else
return OF_TARGET_LINUX;
#elif defined(TARGET_OSX)
return OF_TARGET_OSX;
#elif defined(TARGET_WIN32)
#if (_MSC_VER)
return OF_TARGET_WINVS;
#else
return OF_TARGET_WINGCC;
#endif
#elif defined(TARGET_ANDROID)
return OF_TARGET_ANDROID;
#elif defined(TARGET_OF_IPHONE)
return OF_TARGET_IPHONE;
#endif
}