1 /*
2  * utils.hpp
3  * Copyright (C) 2017  Belledonne Communications, Grenoble, France
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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 Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  *  Created on: February 2, 2017
20  *      Author: Ronan Abhamon
21  */
22 
23 #ifndef UTILS_H_
24 #define UTILS_H_
25 
26 #include <QString>
27 
28 // =============================================================================
29 
30 /*
31  * Define telling g++ that a 'break' statement has been deliberately omitted
32  * in switch block.
33  */
34 #ifndef UTILS_NO_BREAK
35   #if defined(__GNUC__) && __GNUC__ >= 7
36     #define UTILS_NO_BREAK __attribute__((fallthrough))
37   #else
38     #define UTILS_NO_BREAK
39   #endif // if defined(__GNUC__) && __GNUC__ >= 7
40 #endif // ifndef UTILS_NO_BREAK
41 
42 namespace Utils {
coreStringToAppString(const std::string & string)43   inline QString coreStringToAppString (const std::string &string) {
44     return QString::fromLocal8Bit(string.c_str(), static_cast<int>(string.size()));
45   }
46 
appStringToCoreString(const QString & string)47   inline std::string appStringToCoreString (const QString &string) {
48     return string.toLocal8Bit().constData();
49   }
50 
51   // Reverse function of strstr.
52   char *rstrstr (const char *a, const char *b);
53 
54   // Returns the same path given in parameter if `filePath` exists.
55   // Otherwise returns a safe path with a unique number before the extension.
56   QString getSafeFilePath (const QString &filePath, bool *soFarSoGood = nullptr);
57 }
58 
59 #endif // UTILS_H_
60