1 module graphite.utils; 2 3 4 import std.datetime, 5 std.file, 6 std.path, 7 std.string; 8 9 10 public import /*graphite.utils.constants,*/ 11 graphite.utils.log, 12 graphite.utils.json 13 //graphite.utils.noise, 14 //graphite.utils.thread, 15 //graphite.utils.matrixstack, 16 //graphite.graphics.image 17 ; 18 19 shared 20 21 22 23 struct Chrono 24 { 25 static: 26 SysTime startTime; 27 28 29 void resetElapsedTimeCounter() @property 30 { 31 startTime = Clock.currTime; 32 } 33 34 35 Duration elapsedTime() @property 36 { 37 return Clock.currTime - startTime; 38 } 39 40 41 auto elapsedTimeAsTotal(string units)() @property 42 { 43 return elapsedTime.total!units(); 44 } 45 } 46 47 48 /* 49 immutable shared string defaultDataPath; 50 51 shared static this() 52 { 53 static if(TargetPlatform.isOSX) 54 defaultDataPath = "../../../data"; 55 else static if(TargetPlatform.isAndroid) 56 defaultDataPath = "sdcard/"; 57 else static if(TargetPlatform.isLinux || TargetPlatform.isWindows) 58 defaultDataPath = buildPath([thisExePath(), "data/"]); 59 else 60 defaultDataPath = "data/"; 61 } 62 63 64 alias defaultWorkingDir = std.file.getcwd; 65 */ 66 67 68 69 70 71 //void ofResetElapsedTimeCounter(); // this happens on the first frame 72 //float ofGetElapsedTimef(); 73 //unsigned long long ofGetElapsedTimeMillis(); 74 //unsigned long long ofGetElapsedTimeMicros(); 75 //int ofGetFrameNum(); 76 77 //int ofGetSeconds(); 78 //int ofGetMinutes(); 79 //int ofGetHours(); 80 81 ////number of seconds since 1970 82 //uint ofGetUnixTime(); 83 84 //unsigned long long ofGetSystemTime( ); // system time in milliseconds; 85 //unsigned long long ofGetSystemTimeMicros( ); // system time in microseconds; 86 87 // //returns 88 //string ofGetTimestampString(); 89 //string ofGetTimestampString(string timestampFormat); 90 91 92 //int ofGetYear(); 93 //int ofGetMonth(); 94 //int ofGetDay(); 95 //int ofGetWeekday(); 96 97 //void ofLaunchBrowser(string url, bool uriEncodeQuery=false); 98 99 //void ofEnableDataPath(); 100 //void ofDisableDataPath(); 101 //string ofToDataPath(string path, bool absolute=false); 102 103 //template<class T> 104 //void ofRandomize(vector<T>& values) { 105 // random_shuffle(values.begin(), values.end()); 106 //} 107 108 //template<class T, class BoolFunction> 109 //void ofRemove(vector<T>& values, BoolFunction shouldErase) { 110 // values.erase(remove_if(values.begin(), values.end(), shouldErase), values.end()); 111 //} 112 113 //template<class T> 114 //void ofSort(vector<T>& values) { 115 // sort(values.begin(), values.end()); 116 //} 117 //template<class T, class BoolFunction> 118 //void ofSort(vector<T>& values, BoolFunction compare) { 119 // sort(values.begin(), values.end(), compare); 120 //} 121 122 //template <class T> 123 //uint ofFind(const vector<T>& values, const T& target) { 124 // return distance(values.begin(), find(values.begin(), values.end(), target)); 125 //} 126 127 //template <class T> 128 //bool ofContains(const vector<T>& values, const T& target) { 129 // return ofFind(values, target) != values.size(); 130 //} 131 132 //void ofSetWorkingDirectoryToDefault(); 133 134 ////set the root path that ofToDataPath will use to search for files relative to the app 135 ////the path must have a trailing slash (/) !!!! 136 //void ofSetDataPathRoot( string root ); 137 138 //template <class T> 139 //string ofToString(const T& value){ 140 // ostringstream out; 141 // out << value; 142 // return out.str(); 143 //} 144 145 ///// like sprintf "%4f" format, in this example precision=4 146 //template <class T> 147 //string ofToString(const T& value, int precision){ 148 // ostringstream out; 149 // out << fixed << setprecision(precision) << value; 150 // return out.str(); 151 //} 152 153 ///// like sprintf "% 4d" or "% 4f" format, in this example width=4, fill=' ' 154 //template <class T> 155 //string ofToString(const T& value, int width, char fill ){ 156 // ostringstream out; 157 // out << fixed << setfill(fill) << setw(width) << value; 158 // return out.str(); 159 //} 160 161 ///// like sprintf "%04.2d" or "%04.2f" format, in this example precision=2, width=4, fill='0' 162 //template <class T> 163 //string ofToString(const T& value, int precision, int width, char fill ){ 164 // ostringstream out; 165 // out << fixed << setfill(fill) << setw(width) << setprecision(precision) << value; 166 // return out.str(); 167 //} 168 169 //template<class T> 170 //string ofToString(const vector<T>& values) { 171 // stringstream out; 172 // int n = values.size(); 173 // out << "{"; 174 // if(n > 0) { 175 // for(int i = 0; i < n - 1; i++) { 176 // out << values[i] << ", "; 177 // } 178 // out << values[n - 1]; 179 // } 180 // out << "}"; 181 // return out.str(); 182 //} 183 184 //template<class T> 185 //T ofFromString(const string & value){ 186 // T data; 187 // stringstream ss; 188 // ss << value; 189 // ss >> data; 190 // return data; 191 //} 192 193 //template<> 194 //string ofFromString(const string & value); 195 196 //template<> 197 //const char * ofFromString(const string & value); 198 199 //template <class T> 200 //string ofToHex(const T& value) { 201 // ostringstream out; 202 // // pretend that the value is a bunch of bytes 203 // unsigned char* valuePtr = (unsigned char*) &value; 204 // // the number of bytes is determined by the datatype 205 // int numBytes = sizeof(T); 206 // // the bytes are stored backwards (least significant first) 207 // for(int i = numBytes - 1; i >= 0; i--) { 208 // // print each byte out as a 2-character wide hex value 209 // out << setfill('0') << setw(2) << hex << (int) valuePtr[i]; 210 // } 211 // return out.str(); 212 //} 213 //template <> 214 //string ofToHex(const string& value); 215 //string ofToHex(const char* value); 216 217 //int ofHexToInt(const string& intHexString); 218 //char ofHexToChar(const string& charHexString); 219 //float ofHexToFloat(const string& floatHexString); 220 //string ofHexToString(const string& stringHexString); 221 222 //int ofToInt(const string& intString); 223 //char ofToChar(const string& charString); 224 //float ofToFloat(const string& floatString); 225 //double ofToDouble(const string& doubleString); 226 //bool ofToBool(const string& boolString); 227 228 //template <class T> 229 //string ofToBinary(const T& value) { 230 // ostringstream out; 231 // const char* data = (const char*) &value; 232 // // the number of bytes is determined by the datatype 233 // int numBytes = sizeof(T); 234 // // the bytes are stored backwards (least significant first) 235 // for(int i = numBytes - 1; i >= 0; i--) { 236 // bitset<8> cur(data[i]); 237 // out << cur; 238 // } 239 // return out.str(); 240 //} 241 //template <> 242 //string ofToBinary(const string& value); 243 //string ofToBinary(const char* value); 244 245 //int ofBinaryToInt(const string& value); 246 //char ofBinaryToChar(const string& value); 247 //float ofBinaryToFloat(const string& value); 248 //string ofBinaryToString(const string& value); 249 250 251 //string graphiteVersionInfo() @property; 252 //{ 253 // return format("%s.%s.%s", GRAPHITE_VERSION_MAJOR, 254 // GRAPHITE_VERSION_MINOR, 255 // GRAPHITE_VERSION_PATCH); 256 //} 257 258 259 //alias graphiteVersionMajor = GRAPHITE_VERSION_MAJOR; 260 //alias graphiteVersionMinor = GRAPHITE_VERSION_MINOR; 261 //alias graphiteVersionPatch = GRAPHITE_VERSION_PATCH; 262 263 //void saveScreen(string filename); 264 //{ 265 // Image!() screen; 266 // screen.allocate(getWidth(), getHeight(), IMAGE_COLOR); 267 // screen.grabScreen(0, 0, getWidth(), getHeight()); 268 // screen.saveImage(filename); 269 //} 270 271 //void saveViewport(string filename); 272 //{ 273 // Image!() screen; 274 // auto view = currentViewport(); 275 // screen.allocate(view.width, view.height, IMAGE_COLOR); 276 // screen.grabScreen(0, 0, view.width, view.height); 277 // screen.saveImage(filename); 278 //} 279 280 281 //private size_t _saveImageCounter; 282 //void saveFrame(bool bUseViewport = false); 283 //{ 284 // auto filename = _saveImageCounter.to!string ~ ".png"; 285 286 // if(bUseViewport) 287 // saveViewport(filename); 288 // else 289 // saveScreen(filename); 290 291 // ++_saveImageCounter; 292 //} 293 294 //-------------------------------------------------- 295 //vector <string> ofSplitString(const string & source, const string & delimiter, bool ignoreEmpty = false, bool trim = false); 296 //string ofJoinString(vector <string> stringElements, const string & delimiter); 297 //void ofStringReplace(string& input, string searchStr, string replaceStr); 298 //bool ofIsStringInString(string haystack, string needle); 299 //int ofStringTimesInString(string haystack, string needle); 300 301 //string ofToLower(const string & src); 302 //string ofToUpper(const string & src); 303 304 //string ofVAArgsToString(const char * format, ...); 305 //string ofVAArgsToString(const char * format, va_list args); 306 307 //string ofSystem(string command);