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 30export function isAccess(path: string): boolean { 31 var isAccess = false; 32 try { 33 fileio.accessSync(path); 34 isAccess = true; 35 } catch (err) { 36 SPLogger.DEBUG(TAG, 'accessSync failed with error:' + err + 'path:' + path); 37 } 38 return isAccess; 39} 40 41/** 42 * 通用文件节点打开 43 * @param path 44 */ 45export function fileOpen(path: string): String { 46 if (!isAccess(path)) { 47 return 'null'; 48 } 49 50 try { 51 var fd = -1; 52 fd = fileio.openSync(path, 0o0); 53 let buf = new ArrayBuffer(BUFF_SIZE); 54 fileio.readSync(fd, buf); 55 var result: String = String.fromCharCode.apply(null, new Uint8Array(buf)); 56 return util.printf('%s', result.substring(0, lastIndex(result))).toString(); 57 } catch (err) { 58 SPLogger.ERROR(TAG, 'fileOpen path:' + path + ' error:' + err); 59 let errStr: String = err; 60 if (errStr.indexOf('denied') > 0) { 61 return 'null'; 62 } 63 } finally { 64 fileio.closeSync(fd); 65 } 66 return 'null'; 67} 68 69/** 70 * 通用遍历目录节点 71 * @param path 72 * @param regexp 73 */ 74export function travelFile(path: string, regexp: string): Array<String> { 75 let dir; 76 let fileNames = []; 77 78 if (!isAccess(path)) { 79 return []; 80 } 81 82 try { 83 dir = fileio.opendirSync(path); 84 do { 85 var dirent = dir.readSync(); 86 if (dirent == null) { 87 break; 88 } 89 let name: String = dirent.name; 90 if (name.match(regexp)) { 91 fileNames.push(name); 92 } 93 if (regexp == '') { 94 fileNames.push(name); 95 } 96 } while (dirent != null); 97 } catch (err) { 98 SPLogger.ERROR(TAG, 'travelFile get err:' + err); 99 SPLogger.ERROR(TAG, 'travelFile err path:' + path); 100 } finally { 101 dir.closeSync(); 102 } 103 return fileNames; 104} 105 106/** 107 * 返回字符结尾符坐标 108 * @param str 109 */ 110function lastIndex(str) { 111 var index = -1; 112 for (var i = 0; i < str.length; i++) { 113 var temp = str.charCodeAt(i).toString(16); 114 if (temp == 'a') { 115 return i; 116 } 117 } 118 return index; 119} 120 121/** 122 * 睡眠函数 123 * @param numberMillis 124 */ 125export function sleep(numberMillis) { 126 var now = new Date(); 127 var exitTime = now.getTime() + numberMillis; 128 while (true) { 129 now = new Date(); 130 if (now.getTime() > exitTime) { 131 return; 132 } 133 } 134} 135/** 136 * 提取字符串数字 137 */ 138export function extractNumber(originStr) { 139 let result = ''; 140 for (var index = 0; index < originStr.length; index++) { 141 const element: string = originStr[index]; 142 if (element.match('^[0-9]*$')) { 143 result += element; 144 } 145 } 146 return result; 147} 148