|
| 1 | +require 'uri' |
| 2 | +require 'fileutils' |
| 3 | +require 'ffi-xattr' |
| 4 | + |
| 5 | +module iDB |
| 6 | + def GetXattrKeys(aFile) |
| 7 | + result = nil |
| 8 | + x = Xattr.new(aFile) |
| 9 | + result=x.list() |
| 10 | + return result |
| 11 | + end |
| 12 | + |
| 13 | + def GetXattrValue(aFile, aKey): |
| 14 | + result = nil |
| 15 | + x = Xattr.new(aFile) |
| 16 | + result = x[aKey] |
| 17 | + return result |
| 18 | + end |
| 19 | + |
| 20 | + def SetXattrValue(aFile, aKey, aValue): |
| 21 | + result = nil |
| 22 | + x = xattr(aFile) |
| 23 | + x[aKey] = aValue |
| 24 | + result = true |
| 25 | + return result |
| 26 | + end |
| 27 | + def IsXattrValueExists(aFile, aKey): |
| 28 | + result = False |
| 29 | + x = xattr(aFile) |
| 30 | + result = x.has_key(aKey) |
| 31 | + return result |
| 32 | + end |
| 33 | + |
| 34 | + # return the dir's count in aDir |
| 35 | + def GetDirCount(aDir): |
| 36 | + return Dir[File.join(aDir, '*')].count {|file| File.directory?(file)} |
| 37 | + end |
| 38 | + |
| 39 | + # return the file's count in aDir |
| 40 | + def GetFileCount(aDir): |
| 41 | + return Dir[File.join(aDir, '*')].count {|file| File.file?(file)} |
| 42 | + end |
| 43 | + |
| 44 | + # Create all missed directoy |
| 45 | + def CreateDir(aDir): |
| 46 | + FileUtils.mkdir_p(aDir) |
| 47 | + end |
| 48 | + def TouchFile(aFileName, aTimeStamp = None): |
| 49 | + FileUtils.touch(aFileName) |
| 50 | + end |
| 51 | + |
| 52 | + # the Conversion functions: |
| 53 | + def Str2Int(value): |
| 54 | + if value != nil |
| 55 | + base = 10 |
| 56 | + if len(value) >= 2: |
| 57 | + if value[0] == '"' and value[-1] == '"': |
| 58 | + value = value[1:-1] |
| 59 | + elsif value[0] == "'" and value[-1] == "'": |
| 60 | + value = value[1:-1] |
| 61 | + end |
| 62 | + end |
| 63 | + vSign = '' |
| 64 | + if value[0] == '+' or value[0] == '-': |
| 65 | + vSign = value[0] |
| 66 | + value = value[1:] |
| 67 | + end |
| 68 | + if value[0] == '$': |
| 69 | + value = value[1:] |
| 70 | + base = 16 |
| 71 | + elsif value[0:2] == '0x': |
| 72 | + value = value[2:] |
| 73 | + base = 16 |
| 74 | + end |
| 75 | + if vSign: |
| 76 | + value = vSign + value |
| 77 | + end |
| 78 | + result = value.to_i(base) |
| 79 | + else |
| 80 | + result = nil |
| 81 | + end |
| 82 | + |
| 83 | + return result |
| 84 | + end |
| 85 | + |
| 86 | + def Str2Hex(value): |
| 87 | + if value[0] == '$': |
| 88 | + value = value[1:] |
| 89 | + end |
| 90 | + return int(value, 16) |
| 91 | + |
| 92 | + def Hex2Str(value): |
| 93 | + return '$' + hex(value)[2:] |
| 94 | + end |
| 95 | + |
| 96 | + def Str2Bool(value): |
| 97 | + """ |
| 98 | + Converts 'something' to boolean. Raises exception if it gets a string it doesn't handle. |
| 99 | + Case is ignored for strings. These string values are handled: |
| 100 | + True: 'True', "TRue", "yes", "y", "t" |
| 101 | + False: "", "faLse", "no", "n", "f" |
| 102 | + #Non-string values are passed to bool. |
| 103 | + """ |
| 104 | + #if type(value) == type(''): |
| 105 | + if value.lower() in ("yes", "y", "true", "t"): |
| 106 | + return True |
| 107 | + end |
| 108 | + if value.lower() in ("no", "n", "false", "f"): |
| 109 | + return False |
| 110 | + end |
| 111 | + raise ValueError('Invalid value for boolean conversion: ' + value) |
| 112 | + #return bool(value) |
| 113 | + end |
| 114 | +end |
0 commit comments