generalUtils.brs (10924B)
1 '********************************************************** 2 '** Video Player Example Application - General Utilities 3 '** November 2009 4 '** Copyright (c) 2009 Roku Inc. All Rights Reserved. 5 '********************************************************** 6 7 '****************************************************** 8 'Registry Helper Functions 9 '****************************************************** 10 Function RegRead(key, section=invalid) 11 if section = invalid then section = "Default" 12 sec = CreateObject("roRegistrySection", section) 13 if sec.Exists(key) then return sec.Read(key) 14 return invalid 15 End Function 16 17 Function RegWrite(key, val, section=invalid) 18 if section = invalid then section = "Default" 19 sec = CreateObject("roRegistrySection", section) 20 sec.Write(key, val) 21 sec.Flush() 'commit it 22 End Function 23 24 Function RegDelete(key, section=invalid) 25 if section = invalid then section = "Default" 26 sec = CreateObject("roRegistrySection", section) 27 sec.Delete(key) 28 sec.Flush() 29 End Function 30 31 32 '****************************************************** 33 'Insertion Sort 34 'Will sort an array directly, or use a key function 35 '****************************************************** 36 Sub Sort(A as Object, key=invalid as dynamic) 37 38 if type(A)<>"roArray" then return 39 40 if (key=invalid) then 41 for i = 1 to A.Count()-1 42 value = A[i] 43 j = i-1 44 while j>= 0 and A[j] > value 45 A[j + 1] = A[j] 46 j = j-1 47 end while 48 A[j+1] = value 49 next 50 51 else 52 if type(key)<>"Function" then return 53 for i = 1 to A.Count()-1 54 valuekey = key(A[i]) 55 value = A[i] 56 j = i-1 57 while j>= 0 and key(A[j]) > valuekey 58 A[j + 1] = A[j] 59 j = j-1 60 end while 61 A[j+1] = value 62 next 63 64 end if 65 66 End Sub 67 68 69 '****************************************************** 70 'Convert anything to a string 71 ' 72 'Always returns a string 73 '****************************************************** 74 Function tostr(any) 75 ret = AnyToString(any) 76 if ret = invalid ret = type(any) 77 if ret = invalid ret = "unknown" 'failsafe 78 return ret 79 End Function 80 81 82 '****************************************************** 83 'Get a " char as a string 84 '****************************************************** 85 Function Quote() 86 q$ = Chr(34) 87 return q$ 88 End Function 89 90 91 '****************************************************** 92 'islist 93 ' 94 'Determine if the given object supports the ifList interface 95 '****************************************************** 96 Function islist(obj as dynamic) As Boolean 97 if obj = invalid return false 98 if GetInterface(obj, "ifArray") = invalid return false 99 return true 100 End Function 101 102 103 '****************************************************** 104 'isint 105 ' 106 'Determine if the given object supports the ifInt interface 107 '****************************************************** 108 Function isint(obj as dynamic) As Boolean 109 if obj = invalid return false 110 if GetInterface(obj, "ifInt") = invalid return false 111 return true 112 End Function 113 114 '****************************************************** 115 ' validstr 116 ' 117 ' always return a valid string. if the argument is 118 ' invalid or not a string, return an empty string 119 '****************************************************** 120 Function validstr(obj As Dynamic) As String 121 if isnonemptystr(obj) return obj 122 return "" 123 End Function 124 125 126 '****************************************************** 127 'isstr 128 ' 129 'Determine if the given object supports the ifString interface 130 '****************************************************** 131 Function isstr(obj as dynamic) As Boolean 132 if obj = invalid return false 133 if GetInterface(obj, "ifString") = invalid return false 134 return true 135 End Function 136 137 138 '****************************************************** 139 'isnonemptystr 140 ' 141 'Determine if the given object supports the ifString interface 142 'and returns a string of non zero length 143 '****************************************************** 144 Function isnonemptystr(obj) 145 if isnullorempty(obj) return false 146 return true 147 End Function 148 149 150 '****************************************************** 151 'isnullorempty 152 ' 153 'Determine if the given object is invalid or supports 154 'the ifString interface and returns a string of non zero length 155 '****************************************************** 156 Function isnullorempty(obj) 157 if obj = invalid return true 158 if not isstr(obj) return true 159 if Len(obj) = 0 return true 160 return false 161 End Function 162 163 164 '****************************************************** 165 'isbool 166 ' 167 'Determine if the given object supports the ifBoolean interface 168 '****************************************************** 169 Function isbool(obj as dynamic) As Boolean 170 if obj = invalid return false 171 if GetInterface(obj, "ifBoolean") = invalid return false 172 return true 173 End Function 174 175 176 '****************************************************** 177 'isfloat 178 ' 179 'Determine if the given object supports the ifFloat interface 180 '****************************************************** 181 Function isfloat(obj as dynamic) As Boolean 182 if obj = invalid return false 183 if GetInterface(obj, "ifFloat") = invalid return false 184 return true 185 End Function 186 187 188 '****************************************************** 189 'strtobool 190 ' 191 'Convert string to boolean safely. Don't crash 192 'Looks for certain string values 193 '****************************************************** 194 Function strtobool(obj As dynamic) As Boolean 195 if obj = invalid return false 196 if type(obj) <> "roString" return false 197 o = strTrim(obj) 198 o = Lcase(o) 199 if o = "true" return true 200 if o = "t" return true 201 if o = "y" return true 202 if o = "1" return true 203 return false 204 End Function 205 206 207 '****************************************************** 208 'itostr 209 ' 210 'Convert int to string. This is necessary because 211 'the builtin Stri(x) prepends whitespace 212 '****************************************************** 213 Function itostr(i As Integer) As String 214 str = Stri(i) 215 return strTrim(str) 216 End Function 217 218 219 '****************************************************** 220 'Get remaining hours from a total seconds 221 '****************************************************** 222 Function hoursLeft(seconds As Integer) As Integer 223 hours% = seconds / 3600 224 return hours% 225 End Function 226 227 228 '****************************************************** 229 'Get remaining minutes from a total seconds 230 '****************************************************** 231 Function minutesLeft(seconds As Integer) As Integer 232 hours% = seconds / 3600 233 mins% = seconds - (hours% * 3600) 234 mins% = mins% / 60 235 return mins% 236 End Function 237 238 239 '****************************************************** 240 'Pluralize simple strings like "1 minute" or "2 minutes" 241 '****************************************************** 242 Function Pluralize(val As Integer, str As String) As String 243 ret = itostr(val) + " " + str 244 if val <> 1 ret = ret + "s" 245 return ret 246 End Function 247 248 249 '****************************************************** 250 'Trim a string 251 '****************************************************** 252 Function strTrim(str As String) As String 253 st=CreateObject("roString") 254 st.SetString(str) 255 return st.Trim() 256 End Function 257 258 259 '****************************************************** 260 'Tokenize a string. Return roList of strings 261 '****************************************************** 262 Function strTokenize(str As String, delim As String) As Object 263 st=CreateObject("roString") 264 st.SetString(str) 265 return st.Tokenize(delim) 266 End Function 267 268 269 '****************************************************** 270 'Replace substrings in a string. Return new string 271 '****************************************************** 272 Function strReplace(basestr As String, oldsub As String, newsub As String) As String 273 newstr = "" 274 275 i = 1 276 while i <= Len(basestr) 277 x = Instr(i, basestr, oldsub) 278 if x = 0 then 279 newstr = newstr + Mid(basestr, i) 280 exit while 281 endif 282 283 if x > i then 284 newstr = newstr + Mid(basestr, i, x-i) 285 i = x 286 endif 287 288 newstr = newstr + newsub 289 i = i + Len(oldsub) 290 end while 291 292 return newstr 293 End Function 294 295 296 '****************************************************** 297 'Try to convert anything to a string. Only works on simple items. 298 ' 299 'Test with this script... 300 ' 301 ' s$ = "yo1" 302 ' ss = "yo2" 303 ' i% = 111 304 ' ii = 222 305 ' f! = 333.333 306 ' ff = 444.444 307 ' d# = 555.555 308 ' dd = 555.555 309 ' bb = true 310 ' 311 ' so = CreateObject("roString") 312 ' so.SetString("strobj") 313 ' io = CreateObject("roInt") 314 ' io.SetInt(666) 315 ' tm = CreateObject("roTimespan") 316 ' 317 ' Dbg("", s$ ) 'call the Dbg() function which calls AnyToString() 318 ' Dbg("", ss ) 319 ' Dbg("", "yo3") 320 ' Dbg("", i% ) 321 ' Dbg("", ii ) 322 ' Dbg("", 2222 ) 323 ' Dbg("", f! ) 324 ' Dbg("", ff ) 325 ' Dbg("", 3333.3333 ) 326 ' Dbg("", d# ) 327 ' Dbg("", dd ) 328 ' Dbg("", so ) 329 ' Dbg("", io ) 330 ' Dbg("", bb ) 331 ' Dbg("", true ) 332 ' Dbg("", tm ) 333 ' 334 'try to convert an object to a string. return invalid if can't 335 '****************************************************** 336 Function AnyToString(any As Dynamic) As dynamic 337 if any = invalid return "invalid" 338 if isstr(any) return any 339 if isint(any) return itostr(any) 340 if isbool(any) 341 if any = true return "true" 342 return "false" 343 endif 344 if isfloat(any) return Str(any) 345 if type(any) = "roTimespan" return itostr(any.TotalMilliseconds()) + "ms" 346 return invalid 347 End Function 348 349 350 '****************************************************** 351 'Validate parameter is the correct type 352 '****************************************************** 353 Function validateParam(param As Object, paramType As String,functionName As String, allowInvalid = false) As Boolean 354 if type(param) = paramType then 355 return true 356 endif 357 358 if allowInvalid = true then 359 if type(param) = invalid then 360 return true 361 endif 362 endif 363 364 print "invalid parameter of type "; type(param); " for "; paramType; " in function "; functionName 365 return false 366 End Function 367 368 '****************************************************** 369 '** Join the members of a list with a string 370 '****************************************************** 371 Function joinString(list as Object, j as String, first=false, last=false) As String 372 if first then 373 str = j 374 else 375 str = "" 376 end if 377 378 for each f in list 379 str = str + f 380 if last or list.Peek() <> f then 381 str = str + j 382 end if 383 end for 384 385 return str 386 End Function 387 388 '******************************************** 389 ' Return the last part of a / separated path 390 '******************************************** 391 Function getLastElement(url As String) As String 392 ' Get last element of URL 393 toks = url.tokenize("/") 394 return toks[toks.Count()-1] 395 End Function 396