Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "utils.h" |
| 18 | |
Yabin Cui | 5beebc8 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <base/logging.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 26 | |
| 27 | void PrintIndented(size_t indent, const char* fmt, ...) { |
| 28 | va_list ap; |
| 29 | va_start(ap, fmt); |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 30 | printf("%*s", static_cast<int>(indent * 2), ""); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 31 | vprintf(fmt, ap); |
| 32 | va_end(ap); |
| 33 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 34 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 35 | bool IsPowerOfTwo(uint64_t value) { |
| 36 | return (value != 0 && ((value & (value - 1)) == 0)); |
| 37 | } |
| 38 | |
| 39 | bool NextArgumentOrError(const std::vector<std::string>& args, size_t* pi) { |
| 40 | if (*pi + 1 == args.size()) { |
| 41 | LOG(ERROR) << "No argument following " << args[*pi] << " option. Try `simpleperf help " |
| 42 | << args[0] << "`"; |
| 43 | return false; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 44 | } |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 45 | ++*pi; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 46 | return true; |
| 47 | } |
Yabin Cui | 5beebc8 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 48 | |
| 49 | void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files, |
| 50 | std::vector<std::string>* subdirs) { |
| 51 | if (files != nullptr) { |
| 52 | files->clear(); |
| 53 | } |
| 54 | if (subdirs != nullptr) { |
| 55 | subdirs->clear(); |
| 56 | } |
| 57 | DIR* dir = opendir(dirpath.c_str()); |
| 58 | if (dir == nullptr) { |
| 59 | PLOG(DEBUG) << "can't open dir " << dirpath; |
| 60 | return; |
| 61 | } |
| 62 | dirent* entry; |
| 63 | while ((entry = readdir(dir)) != nullptr) { |
| 64 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { |
| 65 | continue; |
| 66 | } |
| 67 | if (entry->d_type == DT_DIR) { |
| 68 | if (subdirs != nullptr) { |
| 69 | subdirs->push_back(entry->d_name); |
| 70 | } |
| 71 | } else { |
| 72 | if (files != nullptr) { |
| 73 | files->push_back(entry->d_name); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | closedir(dir); |
| 78 | } |