1 // 2 // Copyright (c) 2010-2011 Linaro Limited 3 // 4 // All rights reserved. This program and the accompanying materials 5 // are made available under the terms of the MIT License which accompanies 6 // this distribution, and is available at 7 // http://www.opensource.org/licenses/mit-license.php 8 // 9 // Contributors: 10 // Alexandros Frantzis <alexandros.frantzis@linaro.org> 11 // Jesse Barker <jesse.barker@linaro.org> 12 // 13 #ifndef UTIL_H_ 14 #define UTIL_H_ 15 16 #include <string> 17 #include <vector> 18 #include <istream> 19 #include <sstream> 20 #include <stdint.h> 21 22 #ifdef ANDROID 23 #include <android/asset_manager_jni.h> 24 #endif 25 26 struct Util { 27 /** 28 * How to perform the split() operation 29 */ 30 enum SplitMode { 31 /** Normal split operation */ 32 SplitModeNormal, 33 /** Allow for spaces and multiple consecutive occurences of the delimiter */ 34 SplitModeFuzzy, 35 /** Take into account bash-like quoting and escaping rules */ 36 SplitModeQuoted 37 }; 38 39 /** 40 * split() - Splits a string into elements using a provided delimiter 41 * 42 * @s: the string to split 43 * @delim: the delimiter to use 44 * @elems: the string vector to populate 45 * @mode: the SplitMode to use 46 * 47 * Using @delim to determine field boundaries, splits @s into separate 48 * string elements. These elements are returned in the string vector 49 * @elems. As long as @s is non-empty, there will be at least one 50 * element in @elems. 51 */ 52 static void split(const std::string& src, char delim, 53 std::vector<std::string>& elems, 54 Util::SplitMode mode); 55 /** 56 * get_timestamp_us() - Returns the current time in microseconds 57 */ 58 static uint64_t get_timestamp_us(); 59 /** 60 * get_resource() - Gets an input filestream for a given file. 61 * 62 * @path: the path to the file 63 * 64 * Returns a pointer to an input stream, which must be deleted when no 65 * longer in use. 66 */ 67 static std::istream *get_resource(const std::string &path); 68 /** 69 * list_files() - Get a list of the files in a given directory. 70 * 71 * @dirName: the directory path to be listed. 72 * @fileVec: the string vector to populate. 73 * 74 * Obtains a list of the files in @dirName, and returns them in the string 75 * vector @fileVec. 76 */ 77 static void list_files(const std::string& dirName, std::vector<std::string>& fileVec); 78 /** 79 * dispose_pointer_vector() - cleans up a vector of pointers 80 * 81 * @vec: vector of pointers to objects or plain-old-data 82 * 83 * Iterates across @vec and deletes the data pointed to by each of the 84 * elements. Clears the vector, resetting it for reuse. 85 */ dispose_pointer_vectorUtil86 template <class T> static void dispose_pointer_vector(std::vector<T*> &vec) 87 { 88 for (typename std::vector<T*>::const_iterator iter = vec.begin(); 89 iter != vec.end(); 90 iter++) 91 { 92 delete *iter; 93 } 94 95 vec.clear(); 96 } 97 /** 98 * toString() - Converts a string to a plain-old-data type. 99 * 100 * @asString: a string representation of plain-old-data. 101 */ 102 template<typename T> 103 static T fromStringUtil104 fromString(const std::string& asString) 105 { 106 std::stringstream ss(asString); 107 T retVal = T(); 108 ss >> retVal; 109 return retVal; 110 } 111 /** 112 * toString() - Converts a plain-old-data type to a string. 113 * 114 * @t: a simple value to be converted to a string 115 */ 116 template<typename T> 117 static std::string toStringUtil118 toString(const T t) 119 { 120 std::stringstream ss; 121 ss << t; 122 return ss.str(); 123 } 124 /** 125 * appname_from_path() - get the name of an executable from an absolute path 126 * 127 * @path: absolute path of the running application (argv[0]) 128 * 129 * Returns the last portion of @path (everything after the final '/'). 130 */ 131 static std::string 132 appname_from_path(const std::string& path); 133 134 #ifdef ANDROID 135 static void android_set_asset_manager(AAssetManager *asset_manager); 136 static AAssetManager *android_get_asset_manager(void); 137 private: 138 static AAssetManager *android_asset_manager; 139 #endif 140 }; 141 142 #endif /* UTIL_H */ 143