| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // name: util_string.cpp |
| 28 | // desc: string functions utility |
| 29 | // |
| 30 | // author: Ge Wang ([email protected] | [email protected]) |
| 31 | // Spencer Salazar ([email protected]) |
| 32 | // date: Summer 2005 |
| 33 | //----------------------------------------------------------------------------- |
| 34 | #include "util_string.h" |
| 35 | #include "util_platforms.h" |
| 36 | #include "chuck_errmsg.h" |
| 37 | |
| 38 | #ifdef __PLATFORM_WINDOWS__ |
| 39 | #ifndef __CHUNREAL_ENGINE__ |
| 40 | #include <windows.h> |
| 41 | #else |
| 42 | // 1.5.0.0 (ge) | #chunreal |
| 43 | // unreal engine on windows disallows including windows.h |
| 44 | #include "Windows/MinWindows.h" |
| 45 | #endif // #ifndef __CHUNREAL_ENGINE__ |
| 46 | #endif // #ifdef __PLATFORM_WINDOWS__ |
| 47 | |
| 48 | #include <time.h> |
| 49 | #include <limits.h> |
| 50 | #include <stdio.h> |
| 51 | |
| 52 | #include <algorithm> |
| 53 | using namespace std; |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | //----------------------------------------------------------------------------- |
| 59 | // name: ck_itoa() |
| 60 | // desc: int to ascii |
| 61 | //----------------------------------------------------------------------------- |
| 62 | string ck_itoa( t_CKINT val ) |
| 63 | { |
| 64 | char buffer[128]; |
| 65 | #ifdef _WIN64 |
| 66 | snprintf( buffer, 128, "%lld", val ); |
| 67 | #else |
| 68 | snprintf( buffer, 128, "%ld", val ); |
| 69 | #endif |
| 70 | return string(buffer); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | //----------------------------------------------------------------------------- |
| 77 | // name: ck_ftoa() |
| 78 | // desc: float to ascii |
| 79 | //----------------------------------------------------------------------------- |
| 80 | string ck_ftoa( t_CKFLOAT val, t_CKUINT precision ) |
| 81 | { |
| 82 | char str[32]; |
| 83 | char buffer[128]; |
| 84 | if( precision > 32 ) precision = 32; |
| 85 | snprintf( str, 32, "%%.%lif", (long)precision ); |
| 86 | snprintf( buffer, 128, str, val ); |
| 87 | return string(buffer); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | //----------------------------------------------------------------------------- |
| 94 | // name: ck_atoul() |
| 95 | // desc: ascii to unsigned long |
| 96 | //----------------------------------------------------------------------------- |
| 97 | unsigned long ck_atoul( const std::string & s, int base ) |
| 98 | { |
| 99 | return strtoul( s.c_str(), NULL, base ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | //----------------------------------------------------------------------------- |
| 106 | // ascii to signed long |
| 107 | //----------------------------------------------------------------------------- |
| 108 | long ck_atol( const std::string & s, int base ) |
| 109 | { |
| 110 | return strtol( s.c_str(), NULL, base ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | //----------------------------------------------------------------------------- |
| 117 | // name: ptoa() |
| 118 | // desc: pointer to ascii |
| 119 | //----------------------------------------------------------------------------- |
| 120 | string ptoa( void * val ) |
| 121 | { |
| 122 | char buffer[128]; |
| 123 | snprintf( buffer, 128, "%p", val ); |
| 124 | return string(buffer); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | //----------------------------------------------------------------------------- |
| 131 | // name: tolower() |
| 132 | // desc: ... |
| 133 | //----------------------------------------------------------------------------- |
| 134 | string tolower( const string & str ) |
| 135 | { |
| 136 | string s = str; |
| 137 | t_CKUINT len = s.length(); |
| 138 | |
| 139 | // loop |
| 140 | for( t_CKUINT i = 0; i < len; i++ ) |
| 141 | if( s[i] >= 'A' && s[i] <= 'Z' ) |
| 142 | s[i] += 32; |
| 143 | |
| 144 | return s; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | //----------------------------------------------------------------------------- |
| 151 | // name: toupper() |
| 152 | // desc: ... |
| 153 | //----------------------------------------------------------------------------- |
| 154 | string toupper( const string & str ) |
| 155 | { |
| 156 | string s = str; |
| 157 | t_CKUINT len = s.length(); |
| 158 | |
| 159 | // loop |
| 160 | for( t_CKUINT i = 0; i < len; i++ ) |
| 161 | if( s[i] >= 'a' && s[i] <= 'z' ) |
| 162 | s[i] -= 32; |
| 163 | |
| 164 | return s; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | //----------------------------------------------------------------------------- |
| 171 | // name: capitalize() |
| 172 | // desc: capiitalize first character |
| 173 | //----------------------------------------------------------------------------- |
| 174 | string capitalize( const string & s ) |
| 175 | { |
| 176 | // copy |
| 177 | string retval = s; |
| 178 | // if not empty and first character is a lower-case letter |
| 179 | if( retval.length() > 0 && retval[0] >= 'a' && retval[0] <= 'z' ) |
| 180 | retval[0] -= 32; |
| 181 | // done |
| 182 | return retval; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | //----------------------------------------------------------------------------- |
| 189 | // name: capitalize_and_periodize() |
| 190 | // desc: capitalize first character and ensure trailing period |
| 191 | //----------------------------------------------------------------------------- |
| 192 | string capitalize_and_periodize( const string & s ) |
| 193 | { |
| 194 | // copy |
| 195 | string retval = capitalize( trim(s) ); |
| 196 | // check |
| 197 | if( retval.length() > 0 ) |
| 198 | { |
| 199 | char c = retval[retval.length()-1]; |
| 200 | // check for other punctuation |
| 201 | switch( c ) |
| 202 | { |
| 203 | case '.': |
| 204 | case ',': |
| 205 | case '!': |
| 206 | case '?': |
| 207 | case ')': |
| 208 | case '(': |
| 209 | case '#': |
| 210 | case '\'': |
| 211 | case '\"': |
| 212 | case '\n': |
| 213 | break; |
| 214 | |
| 215 | // in all other cases, append . |
| 216 | default: |
| 217 | retval += '.'; |
| 218 | } |
| 219 | } |
| 220 | // done |
| 221 | return retval; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | //----------------------------------------------------------------------------- |
| 228 | // name: trim() |
| 229 | // desc: return a whitespace (spaces and tabs)-trimmed string |
| 230 | //----------------------------------------------------------------------------- |
| 231 | string trim( const string & val ) |
| 232 | { |
| 233 | // two ends |
| 234 | t_CKINT start = 0; |
| 235 | t_CKINT end = val.length() - 1; |
| 236 | |
| 237 | // left trim |
| 238 | for( start = 0; start <= end; start++ ) |
| 239 | { |
| 240 | // non-white space |
| 241 | if( val[start] != ' ' && val[start] != '\t' ) |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | // if start > end, then all white space |
| 246 | if( start > end ) return ""; |
| 247 | |
| 248 | // right trim |
| 249 | for( ; end >= start; end-- ) |
| 250 | { |
| 251 | // non-white space |
| 252 | if( val[end] != ' ' && val[end] != '\t' ) |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | // cannot be |
| 257 | assert( end >= start ); |
| 258 | |
| 259 | // return |
| 260 | return val.substr( start, end - start + 1 ); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | //----------------------------------------------------------------------------- |
| 267 | // name: ltrim() |
| 268 | // desc: left trim |
| 269 | //----------------------------------------------------------------------------- |
| 270 | string ltrim( const string & val ) |
| 271 | { |
| 272 | // two ends |
| 273 | t_CKINT start = 0; |
| 274 | t_CKINT end = val.length() - 1; |
| 275 | |
| 276 | // left trim |
| 277 | for( start = 0; start < end; start++ ) |
| 278 | { |
| 279 | // non-white space |
| 280 | if( val[start] != ' ' && val[start] != '\t' ) |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | // if start > end, then all white space |
| 285 | if( start > end ) return ""; |
| 286 | |
| 287 | // return |
| 288 | return val.substr( start, end - start + 1 ); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | //----------------------------------------------------------------------------- |
| 295 | // name: rtrim() |
| 296 | // desc: right trim |
| 297 | //----------------------------------------------------------------------------- |
| 298 | string rtrim( const string & val ) |
| 299 | { |
| 300 | // two ends |
| 301 | t_CKINT start = 0; |
| 302 | t_CKINT end = val.length() - 1; |
| 303 | |
| 304 | // right trim |
| 305 | for( ; end >= start; end-- ) |
| 306 | { |
| 307 | // non-white space |
| 308 | if( val[end] != ' ' && val[end] != '\t' ) |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | // if end < start, then all white space |
| 313 | if( end < start ) return ""; |
| 314 | |
| 315 | // return |
| 316 | return val.substr( start, end - start + 1 ); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | //----------------------------------------------------------------------------- |
| 323 | // name: replace_tabs() |
| 324 | // desc: replace each tab in a string |
| 325 | //----------------------------------------------------------------------------- |
| 326 | std::string replace_tabs( const std::string & s, char replaceEachTabWithThis ) |
| 327 | { |
| 328 | string str = s; |
| 329 | std::replace( str.begin(), str.end(), '\t', replaceEachTabWithThis ); |
| 330 | return str; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | |
| 335 | #include <iostream> |
| 336 | //----------------------------------------------------------------------------- |
| 337 | // name: snippet() |
| 338 | // desc: snippet a string around an offset; useful for displaying |
| 339 | // long line of test with caret ^ offset |
| 340 | //----------------------------------------------------------------------------- |
| 341 | std::string snippet( const std::string & str, t_CKINT desiredLength, |
| 342 | t_CKINT desiredLeftPadding, t_CKINT & targetPosition, |
| 343 | t_CKINT * pLeftTrimmed, t_CKINT * pRightTrimmed ) |
| 344 | { |
| 345 | // zero out optional fields |
| 346 | if( pLeftTrimmed ) *pLeftTrimmed = 0; |
| 347 | if( pRightTrimmed ) *pRightTrimmed = 0; |
| 348 | |
| 349 | // check: str already within desired length |
| 350 | if( str.length() < desiredLength ) return str; |
| 351 | // check: targetPosition out of bounds |
| 352 | if( targetPosition < 0 || targetPosition >= str.length() ) return str; |
| 353 | |
| 354 | // ensure |
| 355 | if( desiredLeftPadding > desiredLength ) |
| 356 | desiredLeftPadding = desiredLength; |
| 357 | |
| 358 | // where to start the snippet |
| 359 | t_CKINT left = 0; |
| 360 | |
| 361 | // if target beyond left padding |
| 362 | if( targetPosition > desiredLeftPadding ) |
| 363 | { |
| 364 | // where to start |
| 365 | left += (targetPosition-desiredLeftPadding); |
| 366 | // report back |
| 367 | targetPosition = desiredLeftPadding; |
| 368 | // optional info (how much was trimmed on the left) |
| 369 | if( pLeftTrimmed ) *pLeftTrimmed = left; |
| 370 | } |
| 371 | |
| 372 | // optional info (how much was trimmed on the right) |
| 373 | if( pRightTrimmed ) |
| 374 | { |
| 375 | t_CKINT rtrim = (str.length()-left) - desiredLength; |
| 376 | if( rtrim > 0 ) *pRightTrimmed = rtrim; |
| 377 | } |
| 378 | |
| 379 | // start from left, ensure no greater than desiredLength |
| 380 | return str.substr( left, desiredLength ); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | //----------------------------------------------------------------------------- |
| 387 | // name: extract_args() |
| 388 | // desc: extract argument from format filename:arg1:arg2:etc |
| 389 | //----------------------------------------------------------------------------- |
| 390 | t_CKBOOL extract_args( const string & token, |
| 391 | string & filename, vector<string> & args ) |
| 392 | { |
| 393 | // clear vector |
| 394 | args.clear(); |
| 395 | // clear filename |
| 396 | filename = ""; |
| 397 | |
| 398 | // last : found pos |
| 399 | t_CKINT prev_pos = 0; |
| 400 | // curr : pos |
| 401 | t_CKINT i = 0; |
| 402 | |
| 403 | string tmp; |
| 404 | |
| 405 | // copy and trim |
| 406 | string s = trim( token ); |
| 407 | |
| 408 | // ignore second character as arg separator if its : on Windows (e.g., for C:\) |
| 409 | t_CKBOOL ignoreSecond = FALSE; |
| 410 | #ifdef __PLATFORM_WINDOWS__ |
| 411 | ignoreSecond = TRUE; |
| 412 | #endif // __PLATFORM_WINDOWS__ |
| 413 | |
| 414 | // detect |
| 415 | t_CKBOOL scan = FALSE; |
| 416 | t_CKBOOL ret = TRUE; |
| 417 | t_CKBOOL ignoreNext = FALSE; |
| 418 | |
| 419 | char * mask = NULL; |
| 420 | for( i = 0; i < s.length(); i++ ) |
| 421 | if( s[i] == '\\' ) |
| 422 | { |
| 423 | scan = TRUE; |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | // mad... |
| 428 | if( scan ) |
| 429 | { |
| 430 | mask = new char[s.length()]; |
| 431 | // zero |
| 432 | memset(mask, 0, s.length()*sizeof(char)); |
| 433 | |
| 434 | // loop through |
| 435 | for( i = 0; i < s.length(); i++ ) |
| 436 | { |
| 437 | // escape (tom and ge fixed this, which used to look for \\ then : and randomly incremented i) |
| 438 | // added '/' 1.3.1.1 |
| 439 | // if( s[i] == ':' && (i+1) < s.length() && ( s[i+1] == '\\' || s[i+1] == '/' ) ) |
| 440 | // { |
| 441 | // mask[i] = 1; |
| 442 | // break; // added 1.3.1.1 |
| 443 | // } |
| 444 | |
| 445 | // 1.3.2.0: spencer and ge fixed this, requiring \ to escape : |
| 446 | if( s[i] == '\\' && (i+1) < s.length() ) |
| 447 | { |
| 448 | // check next char |
| 449 | if( s[i+1] == ':' || s[i+1] == '\\' ) |
| 450 | { |
| 451 | // add |
| 452 | mask[i] = 1; |
| 453 | // skip next character |
| 454 | i++; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | // error: recognized escape target |
| 459 | CK_FPRINTF_STDERR( "[chuck]: unrecognized escape sequence: \\%c", s[i+1] ); |
| 460 | // return false |
| 461 | ret = FALSE; |
| 462 | // go clean |
| 463 | goto done; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // loop through |
| 470 | for( i = 0; i < s.length(); i++ ) |
| 471 | { |
| 472 | // check mask |
| 473 | if( mask && mask[i] ) |
| 474 | { |
| 475 | //CK_FPRINTF_STDERR( "skipping %c\n", s[i] ); |
| 476 | // mark the next as false |
| 477 | ignoreNext = TRUE; |
| 478 | // skip this one |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | // look for : (also test for windows drive letter, e.g., X:\ or X:/) |
| 483 | if( !ignoreNext && s[i] == ':' && !(ignoreSecond && i == 1 && s.length() > 2 && ( s[2] == '\\' || s[2] == '/' ) ) ) |
| 484 | { |
| 485 | // sanity |
| 486 | if( i == 0 ) |
| 487 | { |
| 488 | ret = FALSE; |
| 489 | goto done; |
| 490 | } |
| 491 | |
| 492 | // copy |
| 493 | if( filename == "" ) |
| 494 | filename = tmp; |
| 495 | else |
| 496 | args.push_back( tmp ); |
| 497 | |
| 498 | tmp = ""; |
| 499 | |
| 500 | // update |
| 501 | prev_pos = i + 1; |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | tmp.append(1, s[i]); |
| 506 | } |
| 507 | |
| 508 | // reset |
| 509 | ignoreNext = FALSE; |
| 510 | } |
| 511 | |
| 512 | // get the remainder, if any |
| 513 | if( tmp.length() ) |
| 514 | { |
| 515 | // copy |
| 516 | if( filename == "" ) |
| 517 | filename = tmp; |
| 518 | else |
| 519 | args.push_back( tmp ); |
| 520 | } |
| 521 | |
| 522 | // testing code - spencer 1.3.2.0 |
| 523 | // CK_FPRINTF_STDERR( "INPUT: %s\n", token.c_str() ); |
| 524 | // CK_FPRINTF_STDERR( "FILENAME: %s\n", filename.c_str() ); |
| 525 | // for(i = 0; i < args.size(); i++) |
| 526 | // CK_FPRINTF_STDERR( "ARG: %s\n", args[i].c_str() ); |
| 527 | |
| 528 | done: |
| 529 | // reclaim |
| 530 | CK_SAFE_DELETE_ARRAY( mask ); |
| 531 | |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | //----------------------------------------------------------------------------- |
| 539 | // name: extract_args() |
| 540 | // desc: extract argument from format filename:arg1:arg2:etc |
| 541 | //----------------------------------------------------------------------------- |
| 542 | t_CKBOOL extract_args( const string & token, |
| 543 | string & filename, string & args ) |
| 544 | { |
| 545 | // clear |
| 546 | filename = ""; args = ""; |
| 547 | |
| 548 | // vector |
| 549 | vector<string> argv; |
| 550 | // extract |
| 551 | if( !extract_args( token, filename, argv ) ) return FALSE; |
| 552 | |
| 553 | // concat |
| 554 | for( t_CKUINT i = 0; i < argv.size(); i++ ) |
| 555 | { |
| 556 | // the arg |
| 557 | args += argv[i]; |
| 558 | // if not last |
| 559 | if( i < (argv.size()-1) ) args += ":"; |
| 560 | } |
| 561 | |
| 562 | return TRUE; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | //----------------------------------------------------------------------------- |
| 569 | // path expansion using wordexp and glob on UNIX systems |
| 570 | //----------------------------------------------------------------------------- |
| 571 | #if !defined(__PLATFORM_WINDOWS__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(__CHIP_MODE__) |
| 572 | |
| 573 | #include <wordexp.h> |
| 574 | //----------------------------------------------------------------------------- |
| 575 | // name: expandTildePath() |
| 576 | // desc: expand ~ path, does not care whether path is valid or not |
| 577 | //----------------------------------------------------------------------------- |
| 578 | std::string expandTildePath( const std::string & path ) |
| 579 | { |
| 580 | // wordexp result |
| 581 | wordexp_t we; |
| 582 | // the result |
| 583 | std::string exp; |
| 584 | |
| 585 | // "perform shell-style word expansions" |
| 586 | if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success |
| 587 | { |
| 588 | // loop word wordc | 1.5.0.4 (ge) updated |
| 589 | for( t_CKINT i = 0; i < we.we_wordc; i++ ) |
| 590 | { |
| 591 | // concatenate in case there are spaces in the path |
| 592 | exp += string(i > 0 ? " " : "") + we.we_wordv[i]; |
| 593 | } |
| 594 | // free up |
| 595 | wordfree( &we ); |
| 596 | } |
| 597 | |
| 598 | // if still empty for any reason, default to original |
| 599 | if( exp == "" ) exp = path; |
| 600 | |
| 601 | // return result |
| 602 | return exp; |
| 603 | } |
| 604 | |
| 605 | #include <glob.h> |
| 606 | //----------------------------------------------------------------------------- |
| 607 | // name: globTildePath() |
| 608 | // desc: expand ~ path, making sure the path actually exists |
| 609 | // adapted from CreatePathByExpandingTildePath below |
| 610 | //----------------------------------------------------------------------------- |
| 611 | std::string globTildePath( const std::string & path ) |
| 612 | { |
| 613 | glob_t globbuf; |
| 614 | // default is the original path |
| 615 | std::string result = path; |
| 616 | |
| 617 | // search for pathnames matching pattern |
| 618 | if( glob(path.c_str(), GLOB_TILDE, NULL, &globbuf) == 0 ) // success |
| 619 | { |
| 620 | // number of matched pathnames |
| 621 | if( globbuf.gl_pathc > 0 ) |
| 622 | { |
| 623 | // copy |
| 624 | result = globbuf.gl_pathv[0]; |
| 625 | } |
| 626 | // free up |
| 627 | globfree(&globbuf); |
| 628 | } |
| 629 | |
| 630 | // return result |
| 631 | return result; |
| 632 | } |
| 633 | |
| 634 | // original |
| 635 | // from http://developer.apple.com/library/mac/#qa/qa1549/_index.html |
| 636 | //char* CreatePathByExpandingTildePath(const char* path) |
| 637 | //{ |
| 638 | // glob_t globbuf; |
| 639 | // char **v; |
| 640 | // char *expandedPath = NULL, *result = NULL; |
| 641 | // |
| 642 | // assert(path != NULL); |
| 643 | // |
| 644 | // if (glob(path, GLOB_TILDE, NULL, &globbuf) == 0) //success |
| 645 | // { |
| 646 | // v = globbuf.gl_pathv; //list of matched pathnames |
| 647 | // expandedPath = v[0]; //number of matched pathnames, gl_pathc == 1 |
| 648 | // |
| 649 | // size_t toCopy = strlen(expandedPath) + 1; //the extra char is for the null-termination |
| 650 | // result = (char*)calloc(1, toCopy); |
| 651 | // if(result) |
| 652 | // { |
| 653 | // //copy the null-termination as well |
| 654 | // memcpy(result, expandedPath, toCopy); |
| 655 | // // was: strncpy(result, expandedPath,strlen(expandedPath) + 1); |
| 656 | // // which was generating a warning on linux/gcc |
| 657 | // // warning: ‘char* strncpy(char*, const char*, size_t)’ specified |
| 658 | // // bound depends on the length of the source argument [-Wstringop-overflow=] |
| 659 | // } |
| 660 | // globfree(&globbuf); |
| 661 | // } |
| 662 | // |
| 663 | // return result; |
| 664 | //} |
| 665 | #endif // not __PLATFORM_WINDOWS__ && __EMSCRIPTEN__ && __ANDROID__ && __CHIP_MODE__ |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | //----------------------------------------------------------------------------- |
| 671 | // path expansion using homemade duct tape on Windows |
| 672 | //----------------------------------------------------------------------------- |
| 673 | #if defined(__PLATFORM_WINDOWS__) |
| 674 | //----------------------------------------------------------------------------- |
| 675 | // name: getUserNameWindows() |
| 676 | // desc: get the user name on windows |
| 677 | //----------------------------------------------------------------------------- |
| 678 | std::string getUserNameWindows() |
| 679 | { |
| 680 | // allocate buffer |
| 681 | DWORD buflen = 128; |
| 682 | char * buf = new char[buflen]; |
| 683 | // get user name |
| 684 | BOOL retval = GetUserNameA( buf, &buflen ); |
| 685 | // result to return |
| 686 | string result; |
| 687 | // check if succeeded |
| 688 | if( retval ) result = buf; |
| 689 | // clean up |
| 690 | CK_SAFE_DELETE_ARRAY( buf ); |
| 691 | // return |
| 692 | return result; |
| 693 | } |
| 694 | |
| 695 | //----------------------------------------------------------------------------- |
| 696 | // name: expandFilePathWindows() |
| 697 | // desc: expand file path (windows edition) |
| 698 | //----------------------------------------------------------------------------- |
| 699 | std::string expandFilePathWindows( const string & path ) |
| 700 | { |
| 701 | // expansion |
| 702 | string exp; |
| 703 | // tokens |
| 704 | vector<string> tokens; |
| 705 | |
| 706 | // trim of white spaces |
| 707 | string thePath = trim( path ); |
| 708 | |
| 709 | // tokenize |
| 710 | tokenize( thePath, tokens, "%" ); |
| 711 | // every other one is an environment variable |
| 712 | t_CKBOOL expand = (thePath.length() && thePath[0] == '%'); |
| 713 | // loop; flip 'expand' |
| 714 | // HACK: assume path does not contain immediately consecutive env-vars |
| 715 | for( t_CKINT i = 0; i < tokens.size(); i++, expand = !expand ) |
| 716 | { |
| 717 | // connstruct expansion |
| 718 | if( !expand ) exp += tokens[i]; |
| 719 | else |
| 720 | { |
| 721 | // check for special vars |
| 722 | if( tolower( tokens[i] ) == "username" ) |
| 723 | { |
| 724 | // this is supposedly safer than querying the env-var, which is easier to spoof |
| 725 | string u = getUserNameWindows(); |
| 726 | // if got something |
| 727 | if( u != "" ) |
| 728 | { |
| 729 | // append |
| 730 | exp += u; |
| 731 | // move to next token |
| 732 | continue; |
| 733 | } |
| 734 | } |
| 735 | // get environment variable |
| 736 | char * v = getenv( tokens[i].c_str() ); |
| 737 | // if got something back |
| 738 | if( v ) exp += v; |
| 739 | else |
| 740 | { |
| 741 | // error message |
| 742 | EM_log( CK_LOG_SYSTEM, "ERROR expanding %%%s%% in path...", tokens[i].c_str() ); |
| 743 | // reset |
| 744 | exp = ""; |
| 745 | // outta here |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // if exp still empty, no change |
| 752 | if( exp == "" ) exp = thePath; |
| 753 | |
| 754 | // done |
| 755 | return exp; |
| 756 | } |
| 757 | #endif // __PLATFORM_WINDOWS__ |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | //----------------------------------------------------------------------------- |
| 763 | // name: normalize_filepath_append_ck() |
| 764 | // desc: get full path to file; if treatAsDir is TRUE, then don't auto-match .ck |
| 765 | //----------------------------------------------------------------------------- |
| 766 | std::string normalize_filepath_append_ck( const std::string & fp ) |
| 767 | { |
| 768 | #ifndef __PLATFORM_WINDOWS__ |
| 769 | |
| 770 | char buf[PATH_MAX]; |
| 771 | char * result = realpath(fp.c_str(), buf); |
| 772 | |
| 773 | // try with .ck extension |
| 774 | if( result == NULL && !extension_matches(fp, ".ck") ) |
| 775 | result = realpath((fp + ".ck").c_str(), buf); |
| 776 | |
| 777 | // get the return value |
| 778 | string ret = result ? buf : fp; |
| 779 | // return |
| 780 | return ret; |
| 781 | |
| 782 | #else // windows |
| 783 | |
| 784 | char buf[MAX_PATH]; |
| 785 | #ifndef __CHUNREAL_ENGINE__ |
| 786 | DWORD result = GetFullPathName(fp.c_str(), MAX_PATH, buf, NULL); |
| 787 | #else |
| 788 | // #chunreal explicitly use ASCII version |
| 789 | DWORD result = GetFullPathNameA(fp.c_str(), MAX_PATH, buf, NULL); |
| 790 | #endif |
| 791 | |
| 792 | // if successful |
| 793 | if( result ) |
| 794 | { |
| 795 | // check if file exists; if not reset result |
| 796 | result = ck_fileexists( fp ) ? result : 0; |
| 797 | } |
| 798 | |
| 799 | // try with .ck extension |
| 800 | if( result == 0 && !extension_matches(fp, ".ck") ) |
| 801 | { |
| 802 | #ifndef __CHUNREAL_ENGINE__ |
| 803 | result = GetFullPathName((fp + ".ck").c_str(), MAX_PATH, buf, NULL); |
| 804 | #else |
| 805 | // #chunreal explicitly use ASCII version |
| 806 | result = GetFullPathNameA((fp + ".ck").c_str(), MAX_PATH, buf, NULL); |
| 807 | #endif |
| 808 | } |
| 809 | |
| 810 | // get the return value |
| 811 | string ret = result ? normalize_directory_separator(buf) : fp; |
| 812 | // return |
| 813 | return ret; |
| 814 | |
| 815 | #endif // __PLATFORM_WINDOWS__ |
| 816 | } |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | //----------------------------------------------------------------------------- |
| 822 | // name: normalize_filepath() |
| 823 | // desc: normalize file path e.g., using realpath() on macOS and linux, and |
| 824 | // GetFullPathName() on windows | 1.5.4.5 (ge & nshaheed) added |
| 825 | //----------------------------------------------------------------------------- |
| 826 | std::string normalize_filepath( const std::string & fp, t_CKBOOL treatAsDir ) |
| 827 | { |
| 828 | #ifndef __PLATFORM_WINDOWS__ |
| 829 | |
| 830 | char buf[PATH_MAX]; |
| 831 | char * result = realpath(fp.c_str(), buf); |
| 832 | |
| 833 | // get the return value |
| 834 | string ret = result ? buf : fp; |
| 835 | // in case fp names a directory, this ensures trailing / |
| 836 | if( treatAsDir ) ret = normalize_directory_name(ret); |
| 837 | // return |
| 838 | return ret; |
| 839 | |
| 840 | #else // windows |
| 841 | |
| 842 | char buf[MAX_PATH]; |
| 843 | #ifndef __CHUNREAL_ENGINE__ |
| 844 | DWORD result = GetFullPathName(fp.c_str(), MAX_PATH, buf, NULL); |
| 845 | #else |
| 846 | // #chunreal explicitly use ASCII version |
| 847 | DWORD result = GetFullPathNameA(fp.c_str(), MAX_PATH, buf, NULL); |
| 848 | #endif |
| 849 | |
| 850 | // if successful |
| 851 | if( result ) |
| 852 | { |
| 853 | // check if file exists; if not reset result |
| 854 | result = ck_fileexists( fp ) ? result : 0; |
| 855 | } |
| 856 | |
| 857 | // get the return value |
| 858 | string ret = result ? normalize_directory_separator(buf) : fp; |
| 859 | // if treat as dir, ensure trailing / 1.5.4.2 (ge & nshaheed) added |
| 860 | if( treatAsDir ) ret = normalize_directory_name(ret); |
| 861 | // return |
| 862 | return ret; |
| 863 | |
| 864 | #endif // __PLATFORM_WINDOWS__ |
| 865 | } |
| 866 | |
| 867 | |
| 868 | |
| 869 | //----------------------------------------------------------------------------- |
| 870 | // name: expand_filepath() |
| 871 | // desc: if possible return expand path (e.g., from the unix ~) |
| 872 | //----------------------------------------------------------------------------- |
| 873 | std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists ) |
| 874 | { |
| 875 | #if defined(__EMSCRIPTEN__) || defined(__ANDROID__) || defined(__CHIP_MODE__) |
| 876 | // no expansion in Emscripten (webchuck) or Android or iOS |
| 877 | return fp; |
| 878 | #elif defined(__PLATFORM_WINDOWS__) |
| 879 | // 1.5.0.4 (ge) added |
| 880 | return expandFilePathWindows( fp ); |
| 881 | #else |
| 882 | // expand ~ to full path |
| 883 | // 1.5.0.4 (ge) always expand ~, since glob does not... |
| 884 | // 1.5.1.3 (ge) only expand if ~ detected |
| 885 | // reason: on macOS wordexp() causes audio interruptions |
| 886 | string ep = fp.find('~') != string::npos ? expandTildePath( fp ) : fp; |
| 887 | |
| 888 | // if also ensure exists |
| 889 | if( ensurePathExists ) { return globTildePath( ep ); } |
| 890 | else { return ep; } |
| 891 | #endif |
| 892 | } |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | //----------------------------------------------------------------------------- |
| 898 | // name: extract_filepath_dir() |
| 899 | // desc: return the directory portion of a file path, excluding the filename |
| 900 | //----------------------------------------------------------------------------- |
| 901 | std::string extract_filepath_dir( const std::string & filepath ) |
| 902 | { |
| 903 | // normalized internal file path separator |
| 904 | char path_separator = '/'; |
| 905 | |
| 906 | // normalize for searching, e.g., \ replaced with / | 1.5.2.5 (ge) added |
| 907 | string normalize = normalize_directory_separator( trim(filepath) ); |
| 908 | // look for separator from the right |
| 909 | t_CKINT i = normalize.rfind( path_separator ); |
| 910 | // if not separator found, return empty string |
| 911 | if( i == std::string::npos ) return ""; |
| 912 | // skip any/all extra trailing slashes |
| 913 | while( i > 0 && normalize[i-1] == path_separator ) i--; |
| 914 | |
| 915 | // change spencer 2014-7-17: include trailing slash |
| 916 | return std::string( filepath, 0, i+1 ); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | //----------------------------------------------------------------------------- |
| 923 | // name: extract_filepath_file() | 1.5.2.5 (ge) added |
| 924 | // desc: return the file portion of a file path, excluding the directory portion |
| 925 | //----------------------------------------------------------------------------- |
| 926 | std::string extract_filepath_file( const std::string & filepath ) |
| 927 | { |
| 928 | // normalized internal file path separator |
| 929 | char path_separator = '/'; |
| 930 | |
| 931 | // normalize for searching, e.g., \ replaced with / |
| 932 | string normalize = normalize_directory_separator( trim(filepath) ); |
| 933 | // look for separator from the right |
| 934 | size_t i = normalize.rfind( path_separator ); |
| 935 | // if not separator found, return the path unchanged |
| 936 | if( i == std::string::npos ) return filepath; |
| 937 | |
| 938 | // substring after the last / |
| 939 | return std::string( filepath, i+1, filepath.length()-i ); |
| 940 | } |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | //----------------------------------------------------------------------------- |
| 946 | // name: extract_filepath_ext() | 1.5.4.0 (ge) added |
| 947 | // desc: return the extension portion of a file path, including the . |
| 948 | //----------------------------------------------------------------------------- |
| 949 | std::string extract_filepath_ext( const std::string & filepath ) |
| 950 | { |
| 951 | // normalized internal file path separator |
| 952 | char path_separator = '/'; |
| 953 | // extension separator |
| 954 | char ext_separator = '.'; |
| 955 | |
| 956 | // normalize for searching, e.g., \ replaced with / |
| 957 | string normalize = normalize_directory_separator( trim(filepath) ); |
| 958 | |
| 959 | // look for ext separator from the right |
| 960 | size_t extPos = normalize.rfind( ext_separator ); |
| 961 | // if no ext separator found |
| 962 | if( extPos == std::string::npos ) return ""; |
| 963 | |
| 964 | // look for separator from the right |
| 965 | size_t pathPos = normalize.rfind( path_separator ); |
| 966 | // if path separator found after the rightmost ext separator |
| 967 | if( (pathPos != std::string::npos) && (pathPos > extPos) ) return ""; |
| 968 | |
| 969 | // substring after the rightmost ext separator |
| 970 | return std::string( normalize, extPos ); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | //----------------------------------------------------------------------------- |
| 977 | // name: transplant_filepath() |
| 978 | // desc: synthesize absolute path using existing filepath and incoming path |
| 979 | // EG: existing == "foo/bar.ck", incoming == "thing/poo.ck" => returns foo/thing/poo.ck |
| 980 | // NOTE: if incoming is detected as absolute path, incoming is returned without change |
| 981 | // (intended for use with import paths being relative to the file importing them) |
| 982 | //----------------------------------------------------------------------------- |
| 983 | std::string transplant_filepath( const std::string & existing, const std::string & incoming ) |
| 984 | { |
| 985 | // expand e.g., ~ |
| 986 | std::string base = expand_filepath( existing ); |
| 987 | std::string inc = expand_filepath( incoming ); |
| 988 | std::string ret = ""; |
| 989 | |
| 990 | // check if already absolute path |
| 991 | if( is_absolute_path(inc) ) |
| 992 | { |
| 993 | // return inc |
| 994 | ret = inc; |
| 995 | } |
| 996 | else // if not absolute path |
| 997 | { |
| 998 | // generate absolute path relative to target that is importing it |
| 999 | ret = extract_filepath_dir(existing) + inc; |
| 1000 | } |
| 1001 | |
| 1002 | // normalize_filepath() will also resolve symlinks . and .. |
| 1003 | return normalize_filepath_append_ck( ret ); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | //----------------------------------------------------------------------------- |
| 1010 | // file: dir_go_up() |
| 1011 | // desc: return directory path 'numUp' levels up the chain |
| 1012 | // added: ge 1.3.2.0 |
| 1013 | //----------------------------------------------------------------------------- |
| 1014 | string dir_go_up( const string & dir, t_CKINT numUp ) |
| 1015 | { |
| 1016 | // separator |
| 1017 | char path_separator = '/'; |
| 1018 | |
| 1019 | // sanity check |
| 1020 | if( numUp < 0 ) numUp = -numUp; |
| 1021 | |
| 1022 | // pos |
| 1023 | size_t pos = dir.length(); |
| 1024 | |
| 1025 | // skip any trailing slashes |
| 1026 | while( pos > 0 && dir[pos-1] == path_separator ) |
| 1027 | pos--; |
| 1028 | |
| 1029 | // loop |
| 1030 | while( numUp > 0 ) |
| 1031 | { |
| 1032 | // find the last slash |
| 1033 | pos = dir.rfind( path_separator, pos-1 ); |
| 1034 | // if no separator found, return empty string |
| 1035 | if (pos == string::npos) |
| 1036 | return ""; |
| 1037 | // skip any extra slashes |
| 1038 | while( pos > 0 && dir[pos-1] == path_separator ) |
| 1039 | pos--; |
| 1040 | if( pos == 0 ) |
| 1041 | return "/"; |
| 1042 | |
| 1043 | // TODO: what about windows? |
| 1044 | // TODO: what about windows network: with two backslashes? "\\\\" |
| 1045 | |
| 1046 | // decrement |
| 1047 | numUp--; |
| 1048 | } |
| 1049 | |
| 1050 | // otherwise |
| 1051 | // change spencer 2014-7-17: include trailing slash |
| 1052 | string str = string( dir, 0, pos ); |
| 1053 | if( str[str.length()-1] != '/' ) str = str + "/"; |
| 1054 | return str; |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | //----------------------------------------------------------------------------- |
| 1061 | // name: parse_path_list() |
| 1062 | // desc: split "x:y:z"-style path list into {"x","y","z"}, with trailing '/' |
| 1063 | //----------------------------------------------------------------------------- |
| 1064 | void parse_path_list( const std::string & str, std::list<std::string> & lst ) |
| 1065 | { |
| 1066 | #if defined(__PLATFORM_WINDOWS__) |
| 1067 | const char separator = ';'; |
| 1068 | #else |
| 1069 | const char separator = ':'; |
| 1070 | #endif |
| 1071 | std::string::size_type i = 0, last = 0; |
| 1072 | while( last < str.size() && |
| 1073 | ( i = str.find( separator, last ) ) != std::string::npos ) |
| 1074 | { |
| 1075 | // add to list |
| 1076 | lst.push_back( normalize_directory_name(str.substr(last,i-last)) ); |
| 1077 | last = i + 1; |
| 1078 | } |
| 1079 | |
| 1080 | // push the final path |
| 1081 | lst.push_back( normalize_directory_name(str.substr(last,str.size()-last)) ); |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | //----------------------------------------------------------------------------- |
| 1088 | // name: append_path_list() |
| 1089 | // desc: append a list (appendThis) to a list (list) |
| 1090 | //----------------------------------------------------------------------------- |
| 1091 | void append_path_list( std::list<std::string> & list, |
| 1092 | const std::list<std::string> & appendMe ) |
| 1093 | { |
| 1094 | // first |
| 1095 | std::list<std::string>::const_iterator it = appendMe.begin(); |
| 1096 | // iterate |
| 1097 | for( ; it != appendMe.end(); it ++ ) |
| 1098 | { |
| 1099 | // append element |
| 1100 | list.push_back( *it ); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | //----------------------------------------------------------------------------- |
| 1108 | // name: normalize_directory_separator() |
| 1109 | // desc: unify directory separator to be consistent across platforms; |
| 1110 | // inside chuck, we are going with the single forward slash '/' |
| 1111 | // as the generic directory separator; other separators will |
| 1112 | // be converted to it, including multiple consecutive /// |
| 1113 | //----------------------------------------------------------------------------- |
| 1114 | std::string normalize_directory_separator( const std::string & filepath ) |
| 1115 | { |
| 1116 | // make a copy |
| 1117 | std::string new_filepath = filepath; |
| 1118 | // string length |
| 1119 | size_t len = new_filepath.size(); |
| 1120 | |
| 1121 | #ifdef __PLATFORM_WINDOWS__ |
| 1122 | // iterate over characters |
| 1123 | for( long i = 0; i < len; i++ ) |
| 1124 | { |
| 1125 | // replace \ with / |
| 1126 | if( new_filepath[i] == '\\' ) new_filepath[i] = '/'; |
| 1127 | } |
| 1128 | #endif |
| 1129 | |
| 1130 | // result |
| 1131 | std::string pruned; |
| 1132 | // state |
| 1133 | char lastChar = 0; |
| 1134 | // iterate over characters |
| 1135 | for( long i = 0; i < len; i++ ) |
| 1136 | { |
| 1137 | // append everything that is not a consecutive / |
| 1138 | if( new_filepath[i] != '/' || lastChar != '/' ) pruned += new_filepath[i]; |
| 1139 | // set last char |
| 1140 | lastChar = new_filepath[i]; |
| 1141 | } |
| 1142 | |
| 1143 | // return |
| 1144 | return pruned; |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | //----------------------------------------------------------------------------- |
| 1151 | // name: normalize_directory_name() |
| 1152 | // desc: normalize directory name (including always with trailing /) |
| 1153 | //----------------------------------------------------------------------------- |
| 1154 | std::string normalize_directory_name( const std::string & dir ) |
| 1155 | { |
| 1156 | // add trailing / (if extraneous, will be pruned by normalize below) |
| 1157 | std::string dirname = trim(dir) + '/'; |
| 1158 | // normalize |
| 1159 | return normalize_directory_separator( dirname ); |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | //----------------------------------------------------------------------------- |
| 1166 | // name: is_absolute_path() |
| 1167 | // desc: return true if path names an absolute path on the underlying platform |
| 1168 | // added: 1.4.1.1 (ge) |
| 1169 | //----------------------------------------------------------------------------- |
| 1170 | t_CKBOOL is_absolute_path( const std::string & path ) |
| 1171 | { |
| 1172 | #ifdef __PLATFORM_WINDOWS__ |
| 1173 | // a little more involved in windows: [letter]:[/ or \] |
| 1174 | if( path.length() >= 3 && path[1] == ':' && ( path[2] == '\\' || path[2] == '/' ) ) |
| 1175 | { |
| 1176 | // check drive letter (apparently windows limited to 26 drive letters) |
| 1177 | if( (path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z') ) |
| 1178 | return true; |
| 1179 | } |
| 1180 | // if we got here, then not absolute path |
| 1181 | return false; |
| 1182 | #else |
| 1183 | // does the path begin with '/'? |
| 1184 | return( path.length() != 0 && path[0] == '/' ); |
| 1185 | #endif |
| 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | //----------------------------------------------------------------------------- |
| 1192 | // name: extension_matches() |
| 1193 | // desc: test whether a filename ends in a particular extension |
| 1194 | //----------------------------------------------------------------------------- |
| 1195 | t_CKBOOL extension_matches( const std::string & filename, |
| 1196 | const std::string & extension, |
| 1197 | t_CKBOOL ignoreCase ) |
| 1198 | { |
| 1199 | // if ignore case, lower case both |
| 1200 | string f = ignoreCase ? tolower(filename) : filename; |
| 1201 | string e = ignoreCase ? tolower(extension) : extension; |
| 1202 | // look for extension from the end |
| 1203 | std::size_t pos = f.rfind( e ); |
| 1204 | // check result |
| 1205 | return (pos != std::string::npos) && (pos == (f.length()-e.length())); |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | //----------------------------------------------------------------------------- |
| 1212 | // name: extension_removed() |
| 1213 | // desc: return filename without extension |
| 1214 | //----------------------------------------------------------------------------- |
| 1215 | std::string extension_removed( const std::string & filename, |
| 1216 | const std::string & extension, |
| 1217 | t_CKBOOL ignoreCase ) |
| 1218 | { |
| 1219 | // if ignore case, lower case both |
| 1220 | string f = ignoreCase ? tolower(filename) : filename; |
| 1221 | string e = ignoreCase ? tolower(extension) : extension; |
| 1222 | // look for extension from the end |
| 1223 | std::size_t pos = f.rfind( e ); |
| 1224 | // no match, return original |
| 1225 | if( pos == std::string::npos ) return filename; |
| 1226 | // return substring |
| 1227 | return filename.substr( 0, pos ); |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | //----------------------------------------------------------------------------- |
| 1234 | // name: subdir_ok2recurse() |
| 1235 | // desc: test if a directory name is OK to recurse, e.g., in search for chugins |
| 1236 | // the reason we have this function is for macOS bundles that are actually |
| 1237 | // directories, e.g., if Faust.chug is directory and the actual chugin |
| 1238 | // is located in side in /Contents/MacOS -- in which case we would not |
| 1239 | // search for more chugins within the Faust.chug directory; |
| 1240 | // HOWEVER, something like .chug (a directory name starting with '.') is |
| 1241 | // okay, as that could be hidden directory |
| 1242 | //----------------------------------------------------------------------------- |
| 1243 | t_CKBOOL subdir_ok2recurse( const std::string & dirName, |
| 1244 | const std::string & extension, |
| 1245 | t_CKBOOL ignoreCase ) |
| 1246 | { |
| 1247 | // if ignore case, lower case both |
| 1248 | string f = ignoreCase ? tolower(dirName) : dirName; |
| 1249 | string e = ignoreCase ? tolower(extension) : extension; |
| 1250 | // if the two are equal, e.g., ".chug". OK |
| 1251 | if( f == e ) return TRUE; |
| 1252 | |
| 1253 | // return the opposite of extension matches |
| 1254 | // i.e., if extension matches, don't recurse |
| 1255 | return !extension_matches( dirName, extension, ignoreCase ); |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | //----------------------------------------------------------------------------- |
| 1262 | // name: str_endsin() |
| 1263 | // desc: return true if the first string ends with the second |
| 1264 | // *** CURRENTLY NOT USED; see extension_matches() *** |
| 1265 | //----------------------------------------------------------------------------- |
| 1266 | t_CKBOOL str_endsin( const char * str, const char * end ) |
| 1267 | { |
| 1268 | size_t len = strlen(str); |
| 1269 | size_t endlen = strlen(end); |
| 1270 | |
| 1271 | return strncmp(str+(len-endlen), end, endlen) == 0; |
| 1272 | } |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | //----------------------------------------------------------------------------- |
| 1278 | // name: timestamp_formatted() | 1.5.0.4 (ge) added |
| 1279 | // desc: return formatted string of current local time; no newline |
| 1280 | //----------------------------------------------------------------------------- |
| 1281 | std::string timestamp_formatted() |
| 1282 | { |
| 1283 | // timestamp |
| 1284 | char datetime_buf[32]; |
| 1285 | // how computer sees time |
| 1286 | time_t datetime; |
| 1287 | // get local time |
| 1288 | time( &datetime ); |
| 1289 | // format time |
| 1290 | const char * timestr = ctime(&datetime); |
| 1291 | // check if result ok |
| 1292 | if( !timestr ) return "[unable to determine/format time]"; |
| 1293 | |
| 1294 | // copy formatted date; ctime() always returns fixed length |
| 1295 | // e.g., "Sat Jun 24 04:18:42 2023" -- 24 characters...okay |
| 1296 | // what happens when the year becomes 5 or more digits? |
| 1297 | // in the year 10000... |
| 1298 | // will there still be computers? or humans? computer music?!? |
| 1299 | strncpy( datetime_buf, timestr, 24 ); |
| 1300 | // terminate the string |
| 1301 | datetime_buf[24] = '\0'; |
| 1302 | |
| 1303 | // return the formatted timestamp |
| 1304 | return datetime_buf; |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | //----------------------------------------------------------------------------- |
| 1311 | // name: autoFilename() |
| 1312 | // desc: generate auto filename | 1.5.0.0 (ge) refactored into this function |
| 1313 | //----------------------------------------------------------------------------- |
| 1314 | std::string autoFilename( const std::string & prefix, const std::string & fileExt ) |
| 1315 | { |
| 1316 | char buffer[1024]; |
| 1317 | time_t t; time(&t); |
| 1318 | strcpy( buffer, prefix.c_str() ); |
| 1319 | strcat( buffer, "(" ); |
| 1320 | strncat( buffer, ctime(&t), 24 ); |
| 1321 | buffer[strlen(prefix.c_str())+14] = 'h'; |
| 1322 | buffer[strlen(prefix.c_str())+17] = 'm'; |
| 1323 | strcat( buffer, ")." ); |
| 1324 | strcat( buffer, fileExt.c_str() ); |
| 1325 | // return |
| 1326 | return buffer; |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | #ifdef WIN32 |
| 1333 | #define stat _stat |
| 1334 | #endif |
| 1335 | //----------------------------------------------------------------------------- |
| 1336 | // name: file_last_write_time() |
| 1337 | // desc: unformatted last-write timestamp of a file | 1.5.4.0 (ge) |
| 1338 | //----------------------------------------------------------------------------- |
| 1339 | time_t file_last_write_time( const std::string & filename ) |
| 1340 | { |
| 1341 | struct stat result; |
| 1342 | if( stat( filename.c_str(), &result ) == 0 ) |
| 1343 | { |
| 1344 | // return result |
| 1345 | return result.st_mtime; |
| 1346 | } |
| 1347 | |
| 1348 | // stat encountered an error, e.g., couldn't open file |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | static t_CKBOOL str_contains( const string & s, char c ) |
| 1356 | { return s.find( c ) != string::npos; } |
| 1357 | |
| 1358 | //----------------------------------------------------------------------------- |
| 1359 | // name: tokenize() | 1.5.0.4 (ge) added |
| 1360 | // desc: tokenize a string into a vector of strings, by delimiters |
| 1361 | //----------------------------------------------------------------------------- |
| 1362 | void tokenize( const std::string & str, std::vector<string> & tokens, const std::string & delimiters ) |
| 1363 | { |
| 1364 | // string length |
| 1365 | t_CKINT len = str.length(); |
| 1366 | // clear vector |
| 1367 | tokens.clear(); |
| 1368 | // token start index |
| 1369 | t_CKINT start = 0; |
| 1370 | |
| 1371 | // scan through str |
| 1372 | for( t_CKINT i = 0; i < len; i++ ) |
| 1373 | { |
| 1374 | // check for delimiters |
| 1375 | if( str_contains( delimiters, str[i] ) ) |
| 1376 | { |
| 1377 | // substr len |
| 1378 | t_CKINT slen = i-start; |
| 1379 | // check |
| 1380 | if( slen > 0 ) |
| 1381 | { |
| 1382 | // push back the substr |
| 1383 | tokens.push_back( str.substr( start, slen ) ); |
| 1384 | } |
| 1385 | // update the start pos |
| 1386 | start = i+1; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | // get the remainder |
| 1391 | t_CKINT slen = len-start; |
| 1392 | // check |
| 1393 | if( slen > 0 ) |
| 1394 | { |
| 1395 | // push back the substr |
| 1396 | tokens.push_back( str.substr( start, slen ) ); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | //----------------------------------------------------------------------------- |
| 1404 | // TC static instantiation |
| 1405 | //----------------------------------------------------------------------------- |
| 1406 | t_CKBOOL TC::isEnabled = TRUE; |
| 1407 | t_CKBOOL TC::globalBypass = TRUE; |
| 1408 | //----------------------------------------------------------------------------- |
| 1409 | // on/off switches |
| 1410 | //----------------------------------------------------------------------------- |
| 1411 | void TC::on() { isEnabled = TRUE; } |
| 1412 | void TC::off() { isEnabled = FALSE; } |
| 1413 | //----------------------------------------------------------------------------- |
| 1414 | // a more global disable, overriding on() for the text transformation and TC::set*() |
| 1415 | // the former will bypass and return the input without modification; |
| 1416 | // the latter will return empty strings. |
| 1417 | // does not affect sequences constructed manually outside of TC |
| 1418 | // FYI this option is typically used on systems with no color terminal capabilities |
| 1419 | // also see command line flags --color --no-color |
| 1420 | //----------------------------------------------------------------------------- |
| 1421 | void TC::globalDisableOverride( t_CKBOOL setTrueToEngage ) |
| 1422 | { |
| 1423 | // set flag, as long as this is true, TC will always bypass, |
| 1424 | // regardless of on() and off() and isEnabled state |
| 1425 | globalBypass = setTrueToEngage; |
| 1426 | } |
| 1427 | |
| 1428 | //----------------------------------------------------------------------------- |
| 1429 | // get bold escape sequence |
| 1430 | //----------------------------------------------------------------------------- |
| 1431 | std::string TC::bold( const std::string & text ) |
| 1432 | { |
| 1433 | if( globalBypass || !isEnabled ) return text; |
| 1434 | return TC::bold() + text + TC::reset(); |
| 1435 | } |
| 1436 | |
| 1437 | //----------------------------------------------------------------------------- |
| 1438 | // get color escape sequences |
| 1439 | //----------------------------------------------------------------------------- |
| 1440 | std::string TC::green( const std::string & text, t_CKBOOL bold ) |
| 1441 | { |
| 1442 | if( globalBypass || !isEnabled ) return text; |
| 1443 | return TC::set(FG_GREEN) + (bold?TC::bold():"") + text + TC::reset(); |
| 1444 | } |
| 1445 | |
| 1446 | std::string TC::orange( const std::string & text, t_CKBOOL bold ) |
| 1447 | { |
| 1448 | if( globalBypass || !isEnabled ) return text; |
| 1449 | return std::string( "\033[38;5;208m" ) + (bold?TC::bold():"") + text + TC::reset(); |
| 1450 | } |
| 1451 | |
| 1452 | std::string TC::blue( const std::string & text, t_CKBOOL bold ) |
| 1453 | { |
| 1454 | if( globalBypass || !isEnabled ) return text; |
| 1455 | return std::string( "\033[38;5;39m" ) + (bold?TC::bold():"") + text + TC::reset(); |
| 1456 | } |
| 1457 | |
| 1458 | std::string TC::red( const std::string & text, t_CKBOOL bold ) |
| 1459 | { |
| 1460 | if( globalBypass || !isEnabled ) return text; |
| 1461 | return TC::set(FG_RED) + (bold?TC::bold():"") + text + TC::reset(); |
| 1462 | } |
| 1463 | |
| 1464 | std::string TC::yellow( const std::string & text, t_CKBOOL bold ) |
| 1465 | { |
| 1466 | if( globalBypass || !isEnabled ) return text; |
| 1467 | return TC::set(FG_YELLOW) + (bold?TC::bold():"") + text + TC::reset(); |
| 1468 | } |
| 1469 | |
| 1470 | std::string TC::magenta( const std::string & text, t_CKBOOL bold ) |
| 1471 | { |
| 1472 | if( globalBypass || !isEnabled ) return text; |
| 1473 | return std::string( "\033[38;5;170m" ) + (bold?TC::bold():"") + text + TC::reset(); |
| 1474 | } |
| 1475 | |
| 1476 | // set custom color |
| 1477 | std::string TC::color( TC::TerminalCode code, const std::string & text, t_CKBOOL bold ) |
| 1478 | { |
| 1479 | if( globalBypass || !isEnabled ) return text; |
| 1480 | return TC::set(code) + (bold?TC::bold():"") + text + TC::reset(); |
| 1481 | } |
| 1482 | |
| 1483 | std::string TC::set_green( t_CKBOOL bold ) |
| 1484 | { |
| 1485 | if( globalBypass || !isEnabled ) return ""; |
| 1486 | return TC::set(FG_GREEN) + (bold?TC::bold():""); |
| 1487 | } |
| 1488 | |
| 1489 | std::string TC::set_orange( t_CKBOOL bold ) |
| 1490 | { |
| 1491 | if( globalBypass || !isEnabled ) return ""; |
| 1492 | return std::string( "\033[38;5;208m" ) + (bold?TC::bold():""); |
| 1493 | } |
| 1494 | |
| 1495 | std::string TC::set_blue( t_CKBOOL bold ) |
| 1496 | { |
| 1497 | if( globalBypass || !isEnabled ) return ""; |
| 1498 | return std::string( "\033[38;5;39m" ) + (bold?TC::bold():""); |
| 1499 | } |
| 1500 | |
| 1501 | //----------------------------------------------------------------------------- |
| 1502 | // set*() methods -- returns escape sequences o insert into output |
| 1503 | //----------------------------------------------------------------------------- |
| 1504 | // set a terminal code |
| 1505 | std::string TC::set( TerminalCode code ) |
| 1506 | { |
| 1507 | if( globalBypass || !isEnabled ) return ""; |
| 1508 | return std::string("\033[") + ck_itoa(code) + "m"; |
| 1509 | } |
| 1510 | |
| 1511 | // set using an integer |
| 1512 | std::string TC::seti( t_CKUINT code ) |
| 1513 | { |
| 1514 | if( globalBypass || !isEnabled ) return ""; |
| 1515 | return std::string("\033[") + ck_itoa(code) + "m"; |
| 1516 | } |
| 1517 | |
| 1518 | // set foreground default color |
| 1519 | std::string TC::set_fg_default() |
| 1520 | { |
| 1521 | if( globalBypass || !isEnabled ) return ""; |
| 1522 | return TC::set(FG_DEFAULT); |
| 1523 | } |
| 1524 | |
| 1525 | // set background default color |
| 1526 | std::string TC::set_bg_default() |
| 1527 | { |
| 1528 | if( globalBypass || !isEnabled ) return ""; |
| 1529 | return TC::set(BG_DEFAULT); |
| 1530 | } |