mirror of
https://github.com/tuxbox-fork-migrations/recycled-ni-neutrino.git
synced 2025-08-28 07:51:11 +02:00
Merge remote-tracking branch 'tuxbox/master'
* needs compile fixes
* needs additional tests, of course :-)
Origin commit data
------------------
Commit: 13ab1ebc8a
Author: Stefan Seyfried <seife@tuxbox-git.slipkontur.de>
Date: 2016-10-22 (Sat, 22 Oct 2016)
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
#include <linux/hdreg.h>
|
||||
#include <linux/fs.h>
|
||||
#include "debug.h"
|
||||
#include <global.h>
|
||||
#include <system/helpers.h>
|
||||
#include <gui/update_ext.h>
|
||||
using namespace std;
|
||||
@@ -440,6 +441,28 @@ std::string trim(std::string &str, const std::string &trimChars /*= " \n\r\t"*/)
|
||||
return result.erase(0, result.find_first_not_of(trimChars));
|
||||
}
|
||||
|
||||
std::string cutString(const std::string str, int msgFont, const int width)
|
||||
{
|
||||
Font *msgFont_ = g_Font[msgFont];
|
||||
std::string ret = str;
|
||||
ret = trim(ret);
|
||||
int sw = msgFont_->getRenderWidth(ret);
|
||||
if (sw <= width)
|
||||
return ret;
|
||||
else {
|
||||
std::string z = "...";
|
||||
int zw = msgFont_->getRenderWidth(z);
|
||||
if (width <= 2*zw)
|
||||
return ret;
|
||||
do {
|
||||
ret = ret.substr(0, ret.length()-1);
|
||||
sw = msgFont_->getRenderWidth(ret);
|
||||
} while (sw+zw > width);
|
||||
ret = trim(ret) + z;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string strftime(const char *format, const struct tm *tm)
|
||||
{
|
||||
char buf[4096];
|
||||
@@ -489,6 +512,8 @@ std::string& htmlEntityDecode(std::string& text)
|
||||
};
|
||||
decode_table dt[] =
|
||||
{
|
||||
{"\n", "
"},
|
||||
{"\n", "
"},
|
||||
{" ", " "},
|
||||
{"&", "&"},
|
||||
{"<", "<"},
|
||||
@@ -512,14 +537,28 @@ std::string& htmlEntityDecode(std::string& text)
|
||||
|
||||
CFileHelpers::CFileHelpers()
|
||||
{
|
||||
FileBufSize = 0xFFFF;
|
||||
FileBuf = new char[FileBufSize];
|
||||
FileBufMaxSize = 0xFFFF;
|
||||
ConsoleQuiet = false;
|
||||
clearDebugInfo();
|
||||
}
|
||||
|
||||
CFileHelpers::~CFileHelpers()
|
||||
{
|
||||
if (FileBuf != NULL)
|
||||
delete [] FileBuf;
|
||||
}
|
||||
|
||||
char* CFileHelpers::initFileBuf(char* buf, uint32_t size)
|
||||
{
|
||||
if (buf == NULL)
|
||||
buf = new char[size];
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* CFileHelpers::deleteFileBuf(char* buf)
|
||||
{
|
||||
if (buf != NULL)
|
||||
delete [] buf;
|
||||
buf = NULL;
|
||||
return buf;
|
||||
}
|
||||
|
||||
CFileHelpers* CFileHelpers::getInstance()
|
||||
@@ -530,20 +569,198 @@ CFileHelpers* CFileHelpers::getInstance()
|
||||
return FileHelpers;
|
||||
}
|
||||
|
||||
bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode)
|
||||
void CFileHelpers::clearDebugInfo()
|
||||
{
|
||||
unlink(Dst);
|
||||
DebugInfo.msg.clear();
|
||||
DebugInfo.file.clear();
|
||||
DebugInfo.func.clear();
|
||||
DebugInfo.line = 0;
|
||||
}
|
||||
|
||||
void CFileHelpers::setDebugInfo(const char* msg, const char* file, const char* func, int line)
|
||||
{
|
||||
DebugInfo.msg = msg;
|
||||
DebugInfo.file = file;
|
||||
DebugInfo.func = func;
|
||||
DebugInfo.line = line;
|
||||
}
|
||||
|
||||
void CFileHelpers::readDebugInfo(helpersDebugInfo* di)
|
||||
{
|
||||
di->msg = DebugInfo.msg;
|
||||
di->file = DebugInfo.file;
|
||||
di->func = DebugInfo.func;
|
||||
di->line = DebugInfo.line;
|
||||
}
|
||||
|
||||
void CFileHelpers::printDebugInfo()
|
||||
{
|
||||
if (!ConsoleQuiet)
|
||||
printf(">>>> [%s:%d] %s\n", DebugInfo.func.c_str(), DebugInfo.line, DebugInfo.msg.c_str());
|
||||
}
|
||||
|
||||
bool CFileHelpers::cp(const char *Src, const char *Dst, const char *Flags/*=""*/)
|
||||
{
|
||||
clearDebugInfo();
|
||||
if ((Src == NULL) || (Dst == NULL)) {
|
||||
setDebugInfo("One or more parameters are NULL", __path_file__, __func__, __LINE__);
|
||||
printDebugInfo();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string src = Src;
|
||||
src = trim(src);
|
||||
if (src.find_first_of("/") != 0)
|
||||
src = "./" + src;
|
||||
size_t pos = src.find_last_of("/");
|
||||
if (pos == src.length()-1)
|
||||
src = src.substr(0, pos);
|
||||
|
||||
std::string dst = Dst;
|
||||
dst = trim(dst);
|
||||
if (dst.find_first_of("/") != 0)
|
||||
dst = "./" + dst;
|
||||
pos = dst.find_last_of("/");
|
||||
if (pos == dst.length()-1)
|
||||
dst = dst.substr(0, pos);
|
||||
|
||||
bool wildcards = (src.find("*") != std::string::npos);
|
||||
bool recursive = ((strchr(Flags, 'r') != NULL) || (strchr(Flags, 'a') != NULL));
|
||||
bool no_dereference = ((strchr(Flags, 'd') != NULL) || (strchr(Flags, 'a') != NULL));
|
||||
|
||||
static struct stat FileInfo;
|
||||
char buf[PATH_MAX];
|
||||
if (wildcards == false) {
|
||||
if (!file_exists(src.c_str())) {
|
||||
setDebugInfo("Source file not exist", __path_file__, __func__, __LINE__);
|
||||
printDebugInfo();
|
||||
return false;
|
||||
}
|
||||
if (lstat(src.c_str(), &FileInfo) == -1) {
|
||||
setDebugInfo("lstat error", __path_file__, __func__, __LINE__);
|
||||
printDebugInfo();
|
||||
return false;
|
||||
}
|
||||
|
||||
pos = src.find_last_of("/");
|
||||
std::string fname = src.substr(pos);
|
||||
|
||||
static struct stat FileInfo2;
|
||||
// is symlink
|
||||
if (S_ISLNK(FileInfo.st_mode)) {
|
||||
int len = readlink(src.c_str(), buf, sizeof(buf)-1);
|
||||
if (len != -1) {
|
||||
buf[len] = '\0';
|
||||
if (!no_dereference) { /* copy */
|
||||
std::string buf_ = (std::string)buf;
|
||||
char buf2[PATH_MAX + 1];
|
||||
if (buf[0] != '/')
|
||||
buf_ = getPathName(src) + "/" + buf_;
|
||||
buf_ = (std::string)realpath(buf_.c_str(), buf2);
|
||||
//printf("\n>>>> RealPath: %s\n \n", buf_.c_str());
|
||||
if (file_exists(dst.c_str()) && (lstat(dst.c_str(), &FileInfo2) != -1)){
|
||||
if (S_ISDIR(FileInfo2.st_mode))
|
||||
copyFile(buf_.c_str(), (dst + fname).c_str());
|
||||
else {
|
||||
unlink(dst.c_str());
|
||||
copyFile(buf_.c_str(), dst.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
copyFile(buf_.c_str(), dst.c_str());
|
||||
}
|
||||
else { /* link */
|
||||
if (file_exists(dst.c_str()) && (lstat(dst.c_str(), &FileInfo2) != -1)){
|
||||
if (S_ISDIR(FileInfo2.st_mode))
|
||||
symlink(buf, (dst + fname).c_str());
|
||||
else {
|
||||
unlink(dst.c_str());
|
||||
symlink(buf, dst.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
symlink(buf, dst.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
// is directory
|
||||
else if (S_ISDIR(FileInfo.st_mode)) {
|
||||
if (recursive)
|
||||
copyDir(src.c_str(), dst.c_str());
|
||||
else {
|
||||
setDebugInfo("'recursive flag' must be set to copy dir.", __path_file__, __func__, __LINE__);
|
||||
printDebugInfo();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// is file
|
||||
else if (S_ISREG(FileInfo.st_mode)) {
|
||||
if (file_exists(dst.c_str()) && (lstat(dst.c_str(), &FileInfo2) != -1)){
|
||||
if (S_ISDIR(FileInfo2.st_mode))
|
||||
copyFile(src.c_str(), (dst + fname).c_str());
|
||||
else {
|
||||
unlink(dst.c_str());
|
||||
copyFile(src.c_str(), dst.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
copyFile(src.c_str(), dst.c_str());
|
||||
}
|
||||
else {
|
||||
setDebugInfo("Currently unsupported st_mode.", __path_file__, __func__, __LINE__);
|
||||
printDebugInfo();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
setDebugInfo("Wildcard feature not yet realized.", __path_file__, __func__, __LINE__);
|
||||
printDebugInfo();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t forceMode/*=0*/)
|
||||
{
|
||||
/*
|
||||
set mode for Dst
|
||||
----------------
|
||||
when forceMode==0 (default) then
|
||||
when Dst exists
|
||||
mode = mode from Dst
|
||||
else
|
||||
mode = mode from Src
|
||||
else
|
||||
mode = forceMode
|
||||
*/
|
||||
mode_t mode = forceMode & 0x0FFF;
|
||||
if (mode == 0) {
|
||||
static struct stat FileInfo;
|
||||
const char *f = Dst;
|
||||
if (!file_exists(Dst))
|
||||
f = Src;
|
||||
if (lstat(f, &FileInfo) == -1)
|
||||
return false;
|
||||
mode = FileInfo.st_mode & 0x0FFF;
|
||||
}
|
||||
|
||||
if ((fd1 = open(Src, O_RDONLY)) < 0)
|
||||
return false;
|
||||
if (file_exists(Dst))
|
||||
unlink(Dst);
|
||||
if ((fd2 = open(Dst, O_WRONLY | O_CREAT, mode)) < 0) {
|
||||
close(fd1);
|
||||
return false;
|
||||
}
|
||||
|
||||
char* FileBuf = NULL;
|
||||
uint32_t block;
|
||||
off64_t fsizeSrc64 = lseek64(fd1, 0, SEEK_END);
|
||||
lseek64(fd1, 0, SEEK_SET);
|
||||
off64_t fsize64 = fsizeSrc64;
|
||||
uint32_t FileBufSize = (fsizeSrc64 < (off_t)FileBufMaxSize) ? (uint32_t)fsizeSrc64 : FileBufMaxSize;
|
||||
FileBuf = initFileBuf(FileBuf, FileBufSize);
|
||||
block = FileBufSize;
|
||||
//printf("#####[%s] fsizeSrc64: %lld 0x%010llX - large file\n", __func__, fsizeSrc64, fsizeSrc64);
|
||||
while (fsize64 > 0) {
|
||||
@@ -558,11 +775,13 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode)
|
||||
if (fsizeSrc64 != fsizeDst64) {
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
FileBuf = deleteFileBuf(FileBuf);
|
||||
return false;
|
||||
}
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
|
||||
FileBuf = deleteFileBuf(FileBuf);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -633,7 +852,7 @@ bool CFileHelpers::copyDir(const char *Src, const char *Dst, bool backupMode)
|
||||
if (backupMode && (CExtUpdate::getInstance()->isBlacklistEntry(srcPath)))
|
||||
save = ".save";
|
||||
#endif
|
||||
copyFile(srcPath, (dstPath + save).c_str(), FileInfo.st_mode & 0x0FFF);
|
||||
copyFile(srcPath, (dstPath + save).c_str()); /* mode is set by copyFile */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -645,6 +864,8 @@ bool CFileHelpers::copyDir(const char *Src, const char *Dst, bool backupMode)
|
||||
// false - errno is set
|
||||
bool CFileHelpers::createDir(string& Dir, mode_t mode)
|
||||
{
|
||||
CFileHelpers* fh = CFileHelpers::getInstance();
|
||||
fh->clearDebugInfo();
|
||||
int res = 0;
|
||||
for(string::iterator iter = Dir.begin() ; iter != Dir.end();) {
|
||||
string::iterator newIter = find(iter, Dir.end(), '/' );
|
||||
@@ -658,7 +879,12 @@ bool CFileHelpers::createDir(string& Dir, mode_t mode)
|
||||
// We can assume that if an error
|
||||
// occured, following will fail too,
|
||||
// so break here.
|
||||
dprintf(DEBUG_NORMAL, "[CFileHelpers %s] creating directory %s: %s\n", __func__, newPath.c_str(), strerror(errno));
|
||||
if (!fh->getConsoleQuiet())
|
||||
dprintf(DEBUG_NORMAL, "[CFileHelpers %s] creating directory %s: %s\n", __func__, newPath.c_str(), strerror(errno));
|
||||
char buf[1024];
|
||||
memset(buf, '\0', sizeof(buf));
|
||||
snprintf(buf, sizeof(buf)-1, "creating directory %s: %s", newPath.c_str(), strerror(errno));
|
||||
fh->setDebugInfo(buf, __path_file__, __func__, __LINE__);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -673,13 +899,22 @@ bool CFileHelpers::createDir(string& Dir, mode_t mode)
|
||||
|
||||
bool CFileHelpers::removeDir(const char *Dir)
|
||||
{
|
||||
CFileHelpers* fh = CFileHelpers::getInstance();
|
||||
fh->clearDebugInfo();
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
char path[PATH_MAX];
|
||||
|
||||
dir = opendir(Dir);
|
||||
if (dir == NULL) {
|
||||
printf("Error opendir()\n");
|
||||
if (errno == ENOENT)
|
||||
return true;
|
||||
if (!fh->getConsoleQuiet())
|
||||
dprintf(DEBUG_NORMAL, "[CFileHelpers %s] remove directory %s: %s\n", __func__, Dir, strerror(errno));
|
||||
char buf[1024];
|
||||
memset(buf, '\0', sizeof(buf));
|
||||
snprintf(buf, sizeof(buf)-1, "remove directory %s: %s", Dir, strerror(errno));
|
||||
fh->setDebugInfo(buf, __path_file__, __func__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
@@ -820,6 +1055,26 @@ bool split_config_string(const std::string &str, std::map<std::string,std::strin
|
||||
return !smap.empty();
|
||||
}
|
||||
|
||||
/* align for hw blit */
|
||||
uint32_t GetWidth4FB_HW_ACC(const uint32_t _x, const uint32_t _w, const bool max)
|
||||
{
|
||||
uint32_t ret = _w;
|
||||
static uint32_t xRes = 0;
|
||||
if (xRes == 0)
|
||||
xRes = CFrameBuffer::getInstance()->getScreenWidth(true);
|
||||
if ((_x + ret) >= xRes)
|
||||
ret = xRes-_x-1;
|
||||
if (ret%4 == 0)
|
||||
return ret;
|
||||
|
||||
int add = (max) ? 3 : 0;
|
||||
ret = ((ret + add) / 4) * 4;
|
||||
if ((_x + ret) >= xRes)
|
||||
ret -= 4;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<std::string> split(const std::string &s, char delim)
|
||||
{
|
||||
std::vector<std::string> vec;
|
||||
@@ -872,6 +1127,38 @@ std::string to_string(unsigned long long i)
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* C++ version 0.4 std::string style "itoa":
|
||||
* Contributions from Stuart Lowe, Ray-Yuan Sheu,
|
||||
|
||||
* Rodrigo de Salvo Braz, Luc Gallant, John Maloney
|
||||
* and Brian Hunt
|
||||
*/
|
||||
std::string itoa(int value, int base)
|
||||
{
|
||||
std::string buf;
|
||||
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 16) return buf;
|
||||
|
||||
enum { kMaxDigits = 35 };
|
||||
buf.reserve( kMaxDigits ); // Pre-allocate enough space.
|
||||
|
||||
int quotient = value;
|
||||
|
||||
// Translating number to string with base:
|
||||
do {
|
||||
buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
|
||||
quotient /= base;
|
||||
} while ( quotient );
|
||||
|
||||
// Append the negative sign
|
||||
if ( value < 0) buf += '-';
|
||||
|
||||
std::reverse( buf.begin(), buf.end() );
|
||||
return buf;
|
||||
}
|
||||
|
||||
std::string getJFFS2MountPoint(int mtdPos)
|
||||
{
|
||||
FILE* fd = fopen("/proc/mounts", "r");
|
||||
@@ -890,3 +1177,36 @@ std::string getJFFS2MountPoint(int mtdPos)
|
||||
fclose(fd);
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string Lang2ISO639_1(std::string& lang)
|
||||
{
|
||||
std::string ret = "";
|
||||
if ((lang == "deutsch") || (lang == "bayrisch") || (lang == "ch-baslerdeutsch") || (lang == "ch-berndeutsch"))
|
||||
ret = "de";
|
||||
else if (lang == "english")
|
||||
ret = "en";
|
||||
else if (lang == "nederlands")
|
||||
ret = "nl";
|
||||
else if (lang == "slovak")
|
||||
ret = "sk";
|
||||
else if (lang == "bosanski")
|
||||
ret = "bs";
|
||||
else if (lang == "czech")
|
||||
ret = "cs";
|
||||
else if (lang == "francais")
|
||||
ret = "fr";
|
||||
else if (lang == "italiano")
|
||||
ret = "it";
|
||||
else if (lang == "polski")
|
||||
ret = "pl";
|
||||
else if (lang == "portugues")
|
||||
ret = "pt";
|
||||
else if (lang == "russkij")
|
||||
ret = "ru";
|
||||
else if (lang == "suomi")
|
||||
ret = "fi";
|
||||
else if (lang == "svenska")
|
||||
ret = "sv";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user