• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <dirent.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 
25 #include <base/logging.h>
26 
PrintIndented(size_t indent,const char * fmt,...)27 void PrintIndented(size_t indent, const char* fmt, ...) {
28   va_list ap;
29   va_start(ap, fmt);
30   printf("%*s", static_cast<int>(indent * 2), "");
31   vprintf(fmt, ap);
32   va_end(ap);
33 }
34 
IsPowerOfTwo(uint64_t value)35 bool IsPowerOfTwo(uint64_t value) {
36   return (value != 0 && ((value & (value - 1)) == 0));
37 }
38 
NextArgumentOrError(const std::vector<std::string> & args,size_t * pi)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;
44   }
45   ++*pi;
46   return true;
47 }
48 
GetEntriesInDir(const std::string & dirpath,std::vector<std::string> * files,std::vector<std::string> * subdirs)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 }
79