Root/
| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
| 3 | * massimiliano.torromeo@gmail.com * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #ifndef UTILITIES_H |
| 22 | #define UTILITIES_H |
| 23 | |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | #include <unordered_map> |
| 27 | |
| 28 | #include "inputmanager.h" |
| 29 | |
| 30 | typedef std::unordered_map<std::string, std::string, std::hash<std::string>> ConfStrHash; |
| 31 | typedef std::unordered_map<std::string, int, std::hash<std::string>> ConfIntHash; |
| 32 | |
| 33 | class case_less { |
| 34 | public: |
| 35 | bool operator()(const std::string &left, const std::string &right) const; |
| 36 | }; |
| 37 | |
| 38 | inline bool isUTF8Starter(char c) { |
| 39 | return (c & 0xC0) != 0x80; |
| 40 | } |
| 41 | |
| 42 | /** Returns the string with whitespace stripped from both ends. */ |
| 43 | std::string trim(const std::string& s); |
| 44 | /** Returns the string with whitespace stripped from the start. */ |
| 45 | std::string ltrim(const std::string& s); |
| 46 | /** Returns the string with whitespace stripped from the end. */ |
| 47 | std::string rtrim(const std::string& s); |
| 48 | |
| 49 | /** Returns the contents of the given file as a string. */ |
| 50 | std::string readFileAsString(std::string const& filename); |
| 51 | |
| 52 | /** |
| 53 | * Writes the given string to a file. |
| 54 | * The update is done atomically but not durably; if you need durability |
| 55 | * when fsync() the parent directory afterwards. |
| 56 | * @return True iff the file was written successfully. |
| 57 | */ |
| 58 | bool writeStringToFile(std::string const& filename, std::string const& data); |
| 59 | |
| 60 | /** |
| 61 | * Tells the file system to commit the given directory to disk. |
| 62 | * @return True iff the sync was successful. |
| 63 | */ |
| 64 | bool syncDir(std::string const& dirname); |
| 65 | |
| 66 | std::string strreplace(std::string orig, const std::string &search, const std::string &replace); |
| 67 | std::string cmdclean(std::string cmdline); |
| 68 | |
| 69 | /** |
| 70 | * Returns the parent directory of the given directory path, or "/" if there is |
| 71 | * no parent. |
| 72 | * This function does not check the file system: it is only string manipulation. |
| 73 | * @return The parent directory path, including a trailing '/'. |
| 74 | */ |
| 75 | std::string parentDir(std::string const& dir); |
| 76 | |
| 77 | inline std::string trimExtension(std::string const& filename) { |
| 78 | return filename.substr(0, filename.rfind('.')); |
| 79 | } |
| 80 | |
| 81 | bool fileExists(const std::string &file); |
| 82 | |
| 83 | /** |
| 84 | * Constructs a non-existing path in a given directory based on the given name. |
| 85 | */ |
| 86 | std::string uniquePath(std::string const& dir, std::string const& name); |
| 87 | |
| 88 | int constrain(int x, int imin, int imax); |
| 89 | |
| 90 | int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax); |
| 91 | |
| 92 | /** |
| 93 | * Splits the given string on the given delimiter, returning the split elements |
| 94 | * in the given vector. |
| 95 | * A delimiter can be a string of multiple characters, in which case that |
| 96 | * entire delimiter string acts as a single delimiter. |
| 97 | * If the delimiter is empty, the entire string is returned as a single element. |
| 98 | * Any previous contents of the vector are discarded. |
| 99 | */ |
| 100 | void split(std::vector<std::string>& vec, std::string const& str, |
| 101 | std::string const& delim); |
| 102 | |
| 103 | int intTransition(int from, int to, long int tickStart, long duration=500, |
| 104 | long tickNow=-1); |
| 105 | |
| 106 | void inject_user_event(enum EventCode code = REPAINT_MENU, |
| 107 | void *data1 = NULL, void *data2 = NULL); |
| 108 | |
| 109 | #endif // UTILITIES_H |
| 110 |
Branches:
install_locations
master
opkrun
packages
