mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 06:35:05 +02:00
Also added a couple shared utility functions for reading command line options (since that's how you can load a local file at this point).
106 lines
2.8 KiB
C++
106 lines
2.8 KiB
C++
//
|
|
// SharedUtil.cpp
|
|
// hifi
|
|
//
|
|
// Created by Stephen Birarda on 2/22/13.
|
|
//
|
|
//
|
|
|
|
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include "SharedUtil.h"
|
|
|
|
#ifdef __APPLE__
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
#endif
|
|
|
|
double usecTimestamp(timeval *time) {
|
|
return (time->tv_sec * 1000000.0 + time->tv_usec);
|
|
}
|
|
|
|
double usecTimestampNow() {
|
|
timeval now;
|
|
gettimeofday(&now, NULL);
|
|
return (now.tv_sec * 1000000.0 + now.tv_usec);
|
|
}
|
|
|
|
float randFloat () {
|
|
return (rand() % 10000)/10000.f;
|
|
}
|
|
|
|
unsigned char randomColorValue(int miniumum) {
|
|
return miniumum + (rand() % (255 - miniumum));
|
|
}
|
|
|
|
bool randomBoolean() {
|
|
return rand() % 2;
|
|
}
|
|
|
|
void outputBits(unsigned char byte) {
|
|
printf("%d: ", byte);
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
printf("%d", byte >> (7 - i) & 1);
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
|
|
int numberOfOnes(unsigned char byte) {
|
|
return (byte >> 7)
|
|
+ ((byte >> 6) & 1)
|
|
+ ((byte >> 5) & 1)
|
|
+ ((byte >> 4) & 1)
|
|
+ ((byte >> 3) & 1)
|
|
+ ((byte >> 2) & 1)
|
|
+ ((byte >> 1) & 1)
|
|
+ (byte & 1);
|
|
}
|
|
|
|
bool oneAtBit(unsigned char byte, int bitIndex) {
|
|
return (byte >> (7 - bitIndex) & 1);
|
|
}
|
|
|
|
void switchToResourcesIfRequired() {
|
|
#ifdef __APPLE__
|
|
CFBundleRef mainBundle = CFBundleGetMainBundle();
|
|
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
|
|
char path[PATH_MAX];
|
|
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)) {
|
|
// error!
|
|
}
|
|
CFRelease(resourcesURL);
|
|
|
|
chdir(path);
|
|
#endif
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
// Function: getCmdOption()
|
|
// Description: Handy little function to tell you if a command line flag and option was
|
|
// included while launching the application, and to get the option value
|
|
// immediately following the flag. For example if you ran:
|
|
// ./app -i filename.txt
|
|
// then you're using the "-i" flag to set the input file name.
|
|
// Usage: char * inputFilename = getCmdOption(argv, argv + argc, "-i");
|
|
// Complaints: Brad :)
|
|
char* getCmdOption(char ** begin, char ** end, const std::string & option)
|
|
{
|
|
char ** itr = std::find(begin, end, option);
|
|
if (itr != end && ++itr != end)
|
|
{
|
|
return *itr;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
// Function: getCmdOption()
|
|
// Description: Handy little function to tell you if a command line option flag was
|
|
// included while launching the application. Returns bool true/false
|
|
// Usage: bool wantDump = cmdOptionExists(argv, argv+argc, "-d");
|
|
// Complaints: Brad :)
|
|
bool cmdOptionExists(char** begin, char** end, const std::string& option)
|
|
{
|
|
return std::find(begin, end, option) != end;
|
|
}
|