1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import fileio from '@ohos.fileio'; 16import util from '@ohos.util'; 17import { GPData } from '../../entity/DatabaseEntity'; 18import SPLogger from '../../utils/SPLogger'; 19 20const BUFF_SIZE = 1024 21const TAG="BaseProfilerUtils" 22/** 23 * 构造GPData 24 */ 25export function createGPData(moduleType: string, values: Map<String, String>): GPData { 26 var now = new Date(); 27 return new GPData(moduleType, now.getTime(), values) 28} 29 30 31export function isAccess(path: string):boolean{ 32 SPLogger.DEBUG("BaseProfilerUtils","ets check path is start..."+ "path:" + path); 33 var isAccess = false 34 try { 35 fileio.accessSync(path); 36 isAccess = true 37 SPLogger.DEBUG("BaseProfilerUtils","ets check path is finish..."+ "path:" + path); 38 } catch(err) { 39 SPLogger.DEBUG(TAG,"accessSync failed with error:"+ err + "path:" + path); 40 } 41 return isAccess 42} 43 44/** 45 * 通用文件节点打开 46 * @param path 47 */ 48export function fileOpen(path: string): String { 49 50 if (!isAccess(path)) { 51 return "null" 52 } 53 54 try { 55 var fd = -1 56 fd = fileio.openSync(path, 0o0) 57 let buf = new ArrayBuffer(BUFF_SIZE); 58 fileio.readSync(fd, buf); 59 var result: String = String.fromCharCode.apply(null, new Uint8Array(buf)) 60 SPLogger.DEBUG(TAG,"fileOpen path data:"+result); 61 return util.printf("%s", result.substring(0, lastIndex(result))).toString() 62 } catch (err) { 63 SPLogger.ERROR(TAG,"fileOpen path:" + path + " error:" + err); 64 let errStr:String = err 65 if(errStr.indexOf("denied")>0){ 66 return "null" 67 } 68 } finally { 69 fileio.closeSync(fd) 70 } 71 return "null" 72} 73 74/** 75 * 通用遍历目录节点 76 * @param path 77 * @param regexp 78 */ 79export function travelFile(path: string, regexp: string): Array<String> { 80 let dir 81 let fileNames = [] 82 83 if (!isAccess(path)) { 84 return [] 85 } 86 87 try { 88 dir = fileio.opendirSync(path); 89 do { 90 var dirent = dir.readSync() 91 if (dirent == null) { 92 break 93 } 94 let name: String = dirent.name 95 if (name.match(regexp)) { 96 SPLogger.DEBUG(TAG, "travelFile get name:" + dirent.name) 97 fileNames.push(name) 98 } 99 if (regexp == "") { 100 fileNames.push(name) 101 } 102 } while (dirent != null) 103 } catch (err) { 104 SPLogger.ERROR(TAG, "travelFile get err:" + err); 105 SPLogger.ERROR(TAG, "travelFile err path:" + path); 106 } finally { 107 dir.closeSync(); 108 } 109 return fileNames 110} 111 112 113 114/** 115 * 返回字符结尾符坐标 116 * @param str 117 */ 118function lastIndex(str) { 119 var index = -1; 120 for (var i = 0; i < str.length; i++) { 121 var temp = str.charCodeAt(i).toString(16); 122 if (temp == 'a') { 123 return i; 124 } 125 } 126 return index; 127} 128 129/** 130 * 睡眠函数 131 * @param numberMillis 132 */ 133export function sleep(numberMillis) { 134 var now = new Date(); 135 var exitTime = now.getTime() + numberMillis; 136 while (true) { 137 now = new Date(); 138 if (now.getTime() > exitTime) 139 return; 140 } 141} 142/** 143 * 提取字符串数字 144 */ 145export function extractNumber(originStr) { 146 let result = "" 147 for (var index = 0; index < originStr.length; index++) { 148 const element: string = originStr[index]; 149 if (element.match("^[0-9]*$")) { 150 result += element 151 } 152 } 153 return result 154}