2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
2011-10-23 17:51:59 +03:00
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
2010-02-04 13:33:47 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifndef UTILITIES_H
|
|
|
|
#define UTILITIES_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-07-16 05:15:10 +03:00
|
|
|
#include <unordered_map>
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2013-08-28 20:14:08 +03:00
|
|
|
#include "inputmanager.h"
|
|
|
|
|
2014-08-16 06:25:23 +03:00
|
|
|
typedef std::unordered_map<std::string, std::string, std::hash<std::string>> ConfStrHash;
|
|
|
|
typedef std::unordered_map<std::string, int, std::hash<std::string>> ConfIntHash;
|
2014-07-16 05:15:10 +03:00
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
class case_less {
|
|
|
|
public:
|
2011-10-23 17:43:56 +03:00
|
|
|
bool operator()(const std::string &left, const std::string &right) const;
|
2010-02-04 13:33:47 +02:00
|
|
|
};
|
|
|
|
|
2014-07-19 03:33:55 +03:00
|
|
|
inline bool isUTF8Starter(char c) {
|
|
|
|
return (c & 0xC0) != 0x80;
|
|
|
|
}
|
|
|
|
|
2014-07-19 03:32:30 +03:00
|
|
|
/** Returns the string with whitespace stripped from both ends. */
|
2011-10-23 17:43:56 +03:00
|
|
|
std::string trim(const std::string& s);
|
2014-07-19 03:33:55 +03:00
|
|
|
/** Returns the string with whitespace stripped from the start. */
|
|
|
|
std::string ltrim(const std::string& s);
|
|
|
|
/** Returns the string with whitespace stripped from the end. */
|
|
|
|
std::string rtrim(const std::string& s);
|
2014-07-19 03:32:30 +03:00
|
|
|
|
2014-07-24 11:41:20 +03:00
|
|
|
/** Returns the contents of the given file as a string. */
|
2014-08-18 15:22:18 +03:00
|
|
|
std::string readFileAsString(std::string const& filename);
|
2014-07-24 11:41:20 +03:00
|
|
|
|
2014-08-18 16:35:56 +03:00
|
|
|
/**
|
|
|
|
* Writes the given string to a file.
|
|
|
|
* The update is done atomically but not durably; if you need durability
|
|
|
|
* when fsync() the parent directory afterwards.
|
|
|
|
* @return True iff the file was written successfully.
|
|
|
|
*/
|
|
|
|
bool writeStringToFile(std::string const& filename, std::string const& data);
|
|
|
|
|
2014-08-18 16:51:37 +03:00
|
|
|
/**
|
|
|
|
* Tells the file system to commit the given directory to disk.
|
|
|
|
* @return True iff the sync was successful.
|
|
|
|
*/
|
|
|
|
bool syncDir(std::string const& dirname);
|
|
|
|
|
2011-10-23 17:51:59 +03:00
|
|
|
std::string strreplace(std::string orig, const std::string &search, const std::string &replace);
|
|
|
|
std::string cmdclean(std::string cmdline);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-17 11:05:21 +03:00
|
|
|
/**
|
|
|
|
* Returns the parent directory of the given directory path, or "/" if there is
|
|
|
|
* no parent.
|
|
|
|
* This function does not check the file system: it is only string manipulation.
|
|
|
|
* @return The parent directory path, including a trailing '/'.
|
|
|
|
*/
|
|
|
|
std::string parentDir(std::string const& dir);
|
|
|
|
|
2014-08-12 10:07:37 +03:00
|
|
|
inline std::string trimExtension(std::string const& filename) {
|
|
|
|
return filename.substr(0, filename.rfind('.'));
|
|
|
|
}
|
|
|
|
|
2011-10-23 17:43:56 +03:00
|
|
|
bool fileExists(const std::string &file);
|
|
|
|
bool rmtree(std::string path);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2011-10-23 17:43:56 +03:00
|
|
|
int constrain(int x, int imin, int imax);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-16 05:15:10 +03:00
|
|
|
int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-10-04 08:46:07 +03:00
|
|
|
/**
|
|
|
|
* Splits the given string on the given delimiter, returning the split elements
|
|
|
|
* in the given vector.
|
|
|
|
* A delimiter can be a string of multiple characters, in which case that
|
|
|
|
* entire delimiter string acts as a single delimiter.
|
|
|
|
* If the delimiter is empty, the entire string is returned as a single element.
|
|
|
|
* Any previous contents of the vector are discarded.
|
|
|
|
*/
|
|
|
|
void split(std::vector<std::string>& vec, std::string const& str,
|
|
|
|
std::string const& delim);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2011-10-23 17:43:56 +03:00
|
|
|
int intTransition(int from, int to, long int tickStart, long duration=500,
|
|
|
|
long tickNow=-1);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2013-08-28 20:14:08 +03:00
|
|
|
void inject_user_event(enum EventCode code = REPAINT_MENU,
|
|
|
|
void *data1 = NULL, void *data2 = NULL);
|
|
|
|
|
2011-10-23 17:43:56 +03:00
|
|
|
#endif // UTILITIES_H
|