| 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.h |
| 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 | #ifndef __UTIL_STRING_H__ |
| 35 | #define __UTIL_STRING_H__ |
| 36 | |
| 37 | #include "chuck_def.h" |
| 38 | #include <string> |
| 39 | #include <vector> |
| 40 | #include <list> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/stat.h> |
| 43 | #ifndef WIN32 |
| 44 | #include <unistd.h> |
| 45 | #endif |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | // int to ascii |
| 51 | std::string ck_itoa( t_CKINT val ); |
| 52 | // float to ascii |
| 53 | std::string ck_ftoa( t_CKFLOAT val, t_CKUINT precision ); |
| 54 | // ascii to unsigned long |
| 55 | unsigned long ck_atoul( const std::string & s, int base = 10 ); |
| 56 | // ascii to signed long |
| 57 | long ck_atol( const std::string & s, int base = 10 ); |
| 58 | |
| 59 | // pointer to ascii |
| 60 | std::string ptoa( void * val ); |
| 61 | // lower-case string |
| 62 | std::string tolower( const std::string & val ); |
| 63 | // upper-case string |
| 64 | std::string toupper( const std::string & val ); |
| 65 | // trim a string |
| 66 | std::string trim( const std::string & val ); |
| 67 | // left trim |
| 68 | std::string ltrim( const std::string & val ); |
| 69 | // right trim |
| 70 | std::string rtrim( const std::string & val ); |
| 71 | |
| 72 | // return capitalized copy of string |
| 73 | std::string capitalize( const std::string & s ); |
| 74 | |
| 75 | // return capiitalized copy of string with trailing punc, adding . as needed |
| 76 | std::string capitalize_and_periodize( const std::string & s ); |
| 77 | |
| 78 | // replace tabs |
| 79 | std::string replace_tabs( const std::string & s, |
| 80 | char replaceEachTabWithThis ); |
| 81 | |
| 82 | // snippet a string around an offset; useful for displaying |
| 83 | // long line of test with caret ^ offset; returns result and |
| 84 | // potentially modifies targetPositionMutable argument |
| 85 | std::string snippet( const std::string & str, t_CKINT desiredLength, |
| 86 | t_CKINT desiredLeftPadding, t_CKINT & targetPositionMutable, |
| 87 | t_CKINT * optionalLeftTrimmed = NULL, |
| 88 | t_CKINT * optionalRightTrimmed = NULL ); |
| 89 | |
| 90 | // argument extraction |
| 91 | t_CKBOOL extract_args( const std::string & token, |
| 92 | std::string & filename, |
| 93 | std::vector<std::string> & args ); |
| 94 | // argument extraction (instead of vector, get args as one string) |
| 95 | t_CKBOOL extract_args( const std::string & token, |
| 96 | std::string & filename, |
| 97 | std::string & args ); |
| 98 | |
| 99 | // take existing path, and attempt to dir up |
| 100 | std::string dir_go_up( const std::string & dir, t_CKINT numUp ); |
| 101 | |
| 102 | // normalize file path (e.g., using realpath() on macOS and linux, and GetFullPathName() on windows) | 1.5.4.5 (ge & nshaheed) added |
| 103 | std::string normalize_filepath( const std::string & fp, t_CKBOOL treatAsDirectory = FALSE ); |
| 104 | |
| 105 | // normalize file path, and if not found and without an existing .ck extension, attempt to append ".ck" extension |
| 106 | // was: get_full_path( const std::string & fp, t_CKBOOL treatAsDirectory = FALSE ) |
| 107 | std::string normalize_filepath_append_ck( const std::string & fp ); |
| 108 | |
| 109 | // perform filepath expansion (e.g., with ~ on unix systems and some windows) |
| 110 | std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists = FALSE ); |
| 111 | |
| 112 | // get directory portion of a filepath (minus the file itself) |
| 113 | std::string extract_filepath_dir( const std::string & filepath ); |
| 114 | |
| 115 | // get filename portion of a filepath (minus the directory portion) | 1.5.2.5 (ge) added |
| 116 | std::string extract_filepath_file( const std::string & filepath ); |
| 117 | |
| 118 | // get extension portion of a filepath (minus the directory and file portions) | 1.5.4.0 (ge) added |
| 119 | std::string extract_filepath_ext( const std::string & filepath ); |
| 120 | |
| 121 | // desc: create absolute path using existing filepath and incoming path |
| 122 | // EG: existing == "foo/bar.ck", incoming == "thing/poo.ck" => returns foo/thing/poo.ck |
| 123 | // NOTE: if incoming is detected as absolute path, incoming is returned without change |
| 124 | // (intended for use with import paths being relative to the file importing them) |
| 125 | std::string transplant_filepath( const std::string & existing, const std::string & incoming ); |
| 126 | |
| 127 | // convert \ to / (on Windows) |
| 128 | std::string normalize_directory_separator( const std::string & filepath ); |
| 129 | |
| 130 | // normalize directory name (including always with trailing /) |
| 131 | std::string normalize_directory_name( const std::string & dir ); |
| 132 | |
| 133 | // check if path is absolute on the underlying platform |
| 134 | t_CKBOOL is_absolute_path( const std::string & path ); |
| 135 | |
| 136 | // split "x:y:z"-style path list into {"x","y","z"} |
| 137 | void parse_path_list( const std::string & str, std::list<std::string> & lst ); |
| 138 | // append list to list |
| 139 | void append_path_list( std::list<std::string> & list, |
| 140 | const std::list<std::string> & appendMe ); |
| 141 | |
| 142 | // generate auto filename (usually with date-time) with file prefix and extension |
| 143 | std::string autoFilename( const std::string & prefix, const std::string & extension ); |
| 144 | |
| 145 | // test whether a filename ends in a particular extension |
| 146 | t_CKBOOL extension_matches( const std::string & filename, |
| 147 | const std::string & extension, |
| 148 | t_CKBOOL ignoreCase = TRUE ); |
| 149 | |
| 150 | // return filename without the extension |
| 151 | std::string extension_removed( const std::string & filename, |
| 152 | const std::string & extension, |
| 153 | t_CKBOOL ignoreCase = TRUE ); |
| 154 | |
| 155 | // test if a directory name is OK to recurse, e.g., in search for chugins |
| 156 | // the reason we have this function is for macOS bundles that are actually |
| 157 | // directories, e.g., if Faust.chug is directory and the actual chugin |
| 158 | // is located in side in /Contents/MacOS -- in which case we would not |
| 159 | // search for more chugins within the Faust.chug directory; |
| 160 | // HOWEVER, something like .chug (a directory name starting with '.') is |
| 161 | // okay, as that could be hidden directory |
| 162 | t_CKBOOL subdir_ok2recurse( const std::string & dirName, |
| 163 | const std::string & extension, |
| 164 | t_CKBOOL ignoreCase = TRUE ); |
| 165 | |
| 166 | // get formatted timestamp of current system time; no new line |
| 167 | std::string timestamp_formatted(); // e.g., "Sat Jun 24 04:18:42 2023" |
| 168 | |
| 169 | // unformatted last-write timestamp of a file | 1.5.4.0 (ge) |
| 170 | time_t file_last_write_time( const std::string & filename ); |
| 171 | |
| 172 | // tokenize a string into a vector of strings, by delimiters |
| 173 | void tokenize( const std::string & str, std::vector<std::string> & tokens, const std::string & delimiters ); |
| 174 | |
| 175 | |
| 176 | //----------------------------------------------------------------------------- |
| 177 | // name: class TC | 1.5.0.5 (ge) added |
| 178 | // desc: terminal code for enhanced output, like color text |
| 179 | // |
| 180 | // NOTE: by default TC's globalDisableOverride is ENGAGED, meaning for TC to |
| 181 | // actually process terminal codes, it must be explicitly enabled; |
| 182 | // not all output supports terminal escape sequences and some may even |
| 183 | // print the escape sequence |
| 184 | // |
| 185 | // when chuck is integrated, the recommended way to enable this: |
| 186 | // `chuck->toggleGlobalColorTextoutput( true );` |
| 187 | // where `chuck` is a ChucK instance; this will also set the |
| 188 | // corresponding setting value inside the ChucK instance |
| 189 | // |
| 190 | // calling TC::globalDisableOverride( false ) is not recommended! |
| 191 | // |
| 192 | // (still working on making TC more flexible!) more resources: |
| 193 | // https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences |
| 194 | // https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal |
| 195 | // https://cplusplus.com/forum/general/18200/ |
| 196 | // FYI nifty tool: https://michurin.github.io/xterm256-color-picker/ |
| 197 | //----------------------------------------------------------------------------- |
| 198 | class TC |
| 199 | { |
| 200 | public: |
| 201 | // reset all attributes |
| 202 | static std::string reset() { return "\033[0m"; } |
| 203 | // enable bold |
| 204 | static std::string bold() { return std::string("\033[1m"); } |
| 205 | // enable TC |
| 206 | static void on(); |
| 207 | // disable TC (bypass) |
| 208 | static void off(); |
| 209 | |
| 210 | public: |
| 211 | // a more global switch, if engaged, will override on() for all TC:: methods |
| 212 | // tranform text methods will bypass and return the input without modification; |
| 213 | // TC::set*() will return empty strings; |
| 214 | // if this switch is NOT engaged, will defer to on() and off() |
| 215 | // does not affect sequences constructed manually outside of TC |
| 216 | // FYI this option is typically used on systems with no color terminal capabilities |
| 217 | // also see command line flags --color --no-color |
| 218 | static void globalDisableOverride( t_CKBOOL setTrueToEngage ); |
| 219 | |
| 220 | public: |
| 221 | // transform text; make bold |
| 222 | static std::string bold( const std::string & text ); |
| 223 | // transform text; make color, with optional bold attribute |
| 224 | static std::string green( const std::string & text, t_CKBOOL bold = FALSE ); |
| 225 | static std::string orange( const std::string & text, t_CKBOOL bold = FALSE ); |
| 226 | static std::string blue( const std::string & text, t_CKBOOL bold = FALSE ); |
| 227 | static std::string red( const std::string & text, t_CKBOOL bold = FALSE ); |
| 228 | static std::string yellow( const std::string & text, t_CKBOOL bold = FALSE ); |
| 229 | static std::string magenta( const std::string & text, t_CKBOOL bold = FALSE ); |
| 230 | |
| 231 | public: |
| 232 | // terminal codes; use with TC::set() |
| 233 | enum TerminalCode { |
| 234 | RESET_ALL = 0, |
| 235 | BOLD = 1, |
| 236 | DIM = 2, |
| 237 | BLINK = 5, |
| 238 | FG_BLACK = 30, |
| 239 | FG_RED = 31, |
| 240 | FG_GREEN = 32, |
| 241 | FG_YELLOW = 33, |
| 242 | FG_BLUE = 34, |
| 243 | FG_MAGENTA = 35, |
| 244 | FG_CYAN = 36, |
| 245 | FG_WHITE = 37, |
| 246 | FG_DEFAULT = 39, |
| 247 | |
| 248 | BG_RED = 41, |
| 249 | BG_GREEN = 42, |
| 250 | BG_YELLOW = 43, |
| 251 | BG_BLUE = 44, |
| 252 | BG_MAGENTA = 45, |
| 253 | BG_CYAN = 46, |
| 254 | BG_WHITE = 47, |
| 255 | BG_DEFAULT = 49, |
| 256 | |
| 257 | FG_DARK_GRAY = 90, |
| 258 | FG_LIGHT_RED = 91, |
| 259 | FG_LIGHT_GREEN = 92, |
| 260 | FG_LIGHT_YELLOW = 93, |
| 261 | FG_LIGHT_BLUE = 94, |
| 262 | FG_LIGHT_MAGENTA = 95, |
| 263 | FG_LIGHT_CYAN = 96, |
| 264 | }; |
| 265 | |
| 266 | // set custom color |
| 267 | static std::string color( TerminalCode code, const std::string & text, t_CKBOOL bold = FALSE ); |
| 268 | // set a terminal code |
| 269 | static std::string set( TerminalCode code ); |
| 270 | // set using an integer |
| 271 | static std::string seti( t_CKUINT code ); |
| 272 | // set foreground default color |
| 273 | static std::string set_fg_default(); |
| 274 | // set background default color |
| 275 | static std::string set_bg_default(); |
| 276 | // set specific colors |
| 277 | static std::string set_green( t_CKBOOL bold = FALSE ); |
| 278 | static std::string set_orange( t_CKBOOL bold = FALSE ); |
| 279 | static std::string set_blue( t_CKBOOL bold = FALSE ); |
| 280 | |
| 281 | |
| 282 | protected: |
| 283 | // whether TC is enabled |
| 284 | static t_CKBOOL isEnabled; |
| 285 | // global bypass |
| 286 | static t_CKBOOL globalBypass; |
| 287 | }; |
| 288 | |
| 289 | |
| 290 | #endif |