Root/src/utilities.h

Source at commit 00d3c3b57008bddf8770c51271db9d766acaf854 created 7 years 5 months ago.
By Maarten ter Huurne, Gave Layer class a protected constructor
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
30typedef std::unordered_map<std::string, std::string, std::hash<std::string>> ConfStrHash;
31typedef std::unordered_map<std::string, int, std::hash<std::string>> ConfIntHash;
32
33class case_less {
34public:
35    bool operator()(const std::string &left, const std::string &right) const;
36};
37
38inline bool isUTF8Starter(char c) {
39    return (c & 0xC0) != 0x80;
40}
41
42/** Returns the string with whitespace stripped from both ends. */
43std::string trim(const std::string& s);
44/** Returns the string with whitespace stripped from the start. */
45std::string ltrim(const std::string& s);
46/** Returns the string with whitespace stripped from the end. */
47std::string rtrim(const std::string& s);
48
49/** Returns the contents of the given file as a string. */
50std::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 */
58bool 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 */
64bool syncDir(std::string const& dirname);
65
66std::string strreplace(std::string orig, const std::string &search, const std::string &replace);
67std::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 */
75std::string parentDir(std::string const& dir);
76
77inline std::string trimExtension(std::string const& filename) {
78    return filename.substr(0, filename.rfind('.'));
79}
80
81bool fileExists(const std::string &file);
82bool rmtree(std::string path);
83
84int constrain(int x, int imin, int imax);
85
86int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax);
87
88/**
89 * Splits the given string on the given delimiter, returning the split elements
90 * in the given vector.
91 * A delimiter can be a string of multiple characters, in which case that
92 * entire delimiter string acts as a single delimiter.
93 * If the delimiter is empty, the entire string is returned as a single element.
94 * Any previous contents of the vector are discarded.
95 */
96void split(std::vector<std::string>& vec, std::string const& str,
97        std::string const& delim);
98
99int intTransition(int from, int to, long int tickStart, long duration=500,
100        long tickNow=-1);
101
102void inject_user_event(enum EventCode code = REPAINT_MENU,
103            void *data1 = NULL, void *data2 = NULL);
104
105#endif // UTILITIES_H
106

Archive Download this file



interactive