1 /* 2 * Copyright (C) 2023 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 */ 15 //! base 16 #![allow(missing_docs)] 17 18 pub struct Base {} 19 20 impl Base { split_command_to_args(command_line: &String) -> (Vec<String>, u32)21 pub fn split_command_to_args(command_line: &String) -> (Vec<String>, u32) { 22 let mut argv: Vec<String> = Vec::new(); 23 let mut argc = 0; 24 let _a = 0; 25 let mut is_quoted = false; 26 let mut is_text = false; 27 let mut is_space = true; 28 29 let len = command_line.len(); 30 if len < 1 { 31 return (Vec::new(), 0); 32 } 33 argv.push(String::new()); 34 for _a in command_line.chars() { 35 if is_quoted { 36 if _a == '\"' { 37 is_quoted = false; 38 } else { 39 argv.last_mut().unwrap().push(_a); 40 } 41 } else { 42 match _a { 43 '\"' => { 44 is_quoted = true; 45 is_text = true; 46 if is_space { 47 argc += 1; 48 } 49 is_space = false; 50 } 51 x if x == ' ' || x == '\t' || x == '\n' || x == '\r' => { 52 if is_text { 53 argv.push(String::new()); 54 } 55 is_text = false; 56 is_space = true; 57 } 58 _ => { 59 is_text = true; 60 if is_space { 61 argc += 1; 62 } 63 argv.last_mut().unwrap().push(_a); 64 is_space = false; 65 } 66 } 67 } 68 } 69 70 (argv, argc) 71 } 72 get_path_sep() -> char73 pub fn get_path_sep() -> char { 74 if cfg!(target_os = "windows") { 75 '\\' 76 } else { 77 '/' 78 } 79 } 80 get_char(str: &str, index: usize) -> char81 pub fn get_char(str: &str, index: usize) -> char { 82 str.chars().nth(index).unwrap() 83 } 84 is_absolute_path(path: &str) -> bool85 pub fn is_absolute_path(path: &str) -> bool { 86 if cfg!(target_os = "windows") { 87 let tmp = path.to_lowercase(); 88 let p = tmp.as_str(); 89 p.len() >= 3 90 && (Self::get_char(p, 0) >= 'a' && Self::get_char(p, 0) <= 'z') 91 && Self::get_char(p, 1) == ':' 92 && Self::get_char(p, 2) == '\\' 93 } else { 94 path.starts_with('/') 95 } 96 } 97 get_file_name(s: &mut String) -> Option<String>98 pub fn get_file_name(s: &mut String) -> Option<String> { 99 let temp = s.to_owned(); 100 let chars: std::str::Chars<'_> = temp.chars(); 101 let mut result = String::new(); 102 let mut len = (chars.clone().count() - 1) as i32; 103 while len >= 0 && chars.clone().nth(len as usize) == Some(Self::get_path_sep()) { 104 len -= 1; 105 } 106 let begin = len; 107 while len >= 0 && chars.clone().nth(len as usize) != Some(Self::get_path_sep()) { 108 len -= 1; 109 } 110 for i in (len + 1) as usize..(begin + 1) as usize { 111 result.push(chars.clone().nth(i).unwrap()); 112 } 113 Some(result) 114 } 115 extract_relative_path(_cwd: &str, mut _path: &str) -> String116 pub fn extract_relative_path(_cwd: &str, mut _path: &str) -> String { 117 if !Base::is_absolute_path(_path) { 118 let mut path2 = _cwd.to_owned(); 119 path2.push_str(_path); 120 return path2; 121 } 122 _path.to_owned() 123 } 124 } 125