luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utils.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef UTILS_H
23 #define UTILS_H
24 
25 #include "Common.h"
26 
27 #include <time.h>
28 #include <string>
29 #include <vector>
30 #include <sstream>
31 #include <iostream>
32 #include <regex.h>
33 #include <QByteArray>
34 
35 #include <QUrl>
36 #include <QGraphicsItem>
37 #include <glib.h>
38 
39 void setPosTopLeft(QGraphicsItem* item, int x, int y);
40 
41 char* readFile(const char* filePath);
42 bool writeFile(const std::string& filePath, const std::string& buffer);
43 bool writeFile(const std::string& filePath, QByteArray buffer);
44 bool writeToTempFile(const std::string& data,const std::string& tempDir,std::string& r_outputFile);
45 
46 bool deleteFile(const char* filePath);
47 time_t localTime();
48 unsigned long currentTimeMs( );
49 
50 template <class T>
51 std::string toSTLString(const T &arg) {
52  std::ostringstream out;
53  out << arg;
54  return(out.str());
55 }
56 
57 std::string getResourceNameFromUrl(const QUrl& url);
58 std::string trimWhitespace(const std::string& s,const std::string& drop = "\r\n\t ");
59 bool getNthSubstring(unsigned int n,std::string& target, const std::string& str,const std::string& delims = " \t\n\r");
60 int splitFileAndPath(const std::string& srcPathAndFile,std::string& pathPart,std::string& filePart);
61 int splitFileAndExtension(const std::string& srcFileAndExt,std::string& filePart,std::string& extensionPart);
62 int splitStringOnKey(std::vector<std::string>& returnSplitSubstrings,const std::string& baseStr,const std::string& delims);
63 int remap_stdx_pipes(int readPipe, int writePipe);
64 bool isNonErrorProcExit(int ecode,int normalCode=0);
65 
66 std::string windowIdentifierFromAppAndProcessId(const std::string& appId, const std::string& processId);
67 bool splitWindowIdentifierToAppAndProcessId(const std::string& id, std::string& appId, std::string& processId);
68 
69 std::string getHtml5DatabaseFolderNameForApp(const std::string& appId,std::string appFolderPath);
70 
71 void threadCleanup();
72 
73 bool extractFromJson(const std::string& jsonString,const std::string& key,std::string& r_value);
74 bool extractFromJson(struct json_object * root,const std::string& key,std::string& r_value);
75 bool extractFromJson(struct json_object * root,const std::string& key,int& r_value);
76 bool extractFromJson(struct json_object * root,const std::string& key,bool& r_value);
77 struct json_object * JsonGetObject(struct json_object * root,const std::string& key);
78 
79 std::string base64_encode(unsigned char const* , unsigned int len);
80 std::string base64_decode(std::string const& s);
81 
82 bool doesExistOnFilesystem(const char * pathAndFile);
83 int fileCopy(const char * srcFileAndPath,const char * dstFileAndPath);
84 
85 gboolean compare_regex (const gchar * regex,const gchar * string);
86 
87 int determineEnclosingDir(const std::string& fileNameAndPath,std::string& r_enclosingDir);
88 
89 // Build an std::string using printf-style formatting
90 std::string string_printf(const char *format, ...) G_GNUC_PRINTF(1, 2);
91 
92 // Append a printf-style string to an existing std::string
93 std::string & append_format(std::string & str, const char * format, ...) G_GNUC_PRINTF(2, 3);
94 
95 #endif /* UTILS_H */