blob: 349cf5d1ddd4ef508173bdece13979fb9a6dad34 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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 Cui5beebc82015-05-04 20:27:57 -070019#include <dirent.h>
Yabin Cui323e9452015-04-20 18:07:17 -070020#include <errno.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070021#include <stdarg.h>
22#include <stdio.h>
Yabin Cui323e9452015-04-20 18:07:17 -070023#include <unistd.h>
24
25#include <base/logging.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070026
27void PrintIndented(size_t indent, const char* fmt, ...) {
28 va_list ap;
29 va_start(ap, fmt);
Yabin Cuied91cd92015-04-28 15:54:13 -070030 printf("%*s", static_cast<int>(indent * 2), "");
Yabin Cui67d3abd2015-04-16 15:26:31 -070031 vprintf(fmt, ap);
32 va_end(ap);
33}
Yabin Cui323e9452015-04-20 18:07:17 -070034
Yabin Cuied91cd92015-04-28 15:54:13 -070035bool IsPowerOfTwo(uint64_t value) {
36 return (value != 0 && ((value & (value - 1)) == 0));
37}
38
39bool 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 Cui323e9452015-04-20 18:07:17 -070044 }
Yabin Cuied91cd92015-04-28 15:54:13 -070045 ++*pi;
Yabin Cui323e9452015-04-20 18:07:17 -070046 return true;
47}
Yabin Cui5beebc82015-05-04 20:27:57 -070048
49void 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}