.Functions4U: Added functions

git-svn-id: svn://ultimatepp.org/upp/trunk@2431 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
koldo 2010-05-28 15:07:43 +00:00
parent 79280d0dee
commit 306afc1db9
10 changed files with 724 additions and 520 deletions

View file

@ -1,19 +1,42 @@
#include <Core/Core.h>
#if defined(PLATFORM_WIN32)
#include <shellapi.h>
#endif
#include "Functions4U.h"
#if defined(PLATFORM_WIN32)
#define Ptr Ptr_
#define byte byte_
#define CY win32_CY_
#include <shellapi.h>
#include <wincon.h>
#include <shlobj.h>
#undef Ptr
#undef byte
#undef CY
#endif
/*
Hi Koldo,
I checked the functions in Functions4U. Here are some notes about trashing:
* On older systems, the trash folder was $HOME/.Trash
* Your implementation of disregards the folder $HOME/.local/share/trash/info. You should create there a .trashinfo file when moving something in trash and delete it when deleting corresponding file permanently.
* If you delete something on different partition than $HOME, you should also check if .Trash-XXXX exists in root of that partition (XXXX is id of user who deleted the files in it).
* Your implementation of disregards the folder $HOME/.local/share/trash/info. You should create
there a .trashinfo file when moving something in trash and delete it when deleting corresponding file permanently.
* If you delete something on different partition than $HOME, you should also check if .Trash-XXXX
exists in root of that partition (XXXX is id of user who deleted the files in it).
.local/share/Trash/files
.local/share/Trash/info
Un fichero por cada vez que se borra con
KK.trashinfo
[Trash Info]
Path=/home/pubuntu/KK
DeletionDate=2010-05-19T18:00:52
You might also be interested in following:
@ -168,8 +191,11 @@ bool FileMoveX(const char *oldpath, const char *newpath, int flags) {
bool FileDeleteX(const char *path, int flags) {
if (flags & USE_TRASH_BIN)
return FileToTrashBin(path);
else
else {
if (flags & DELETE_READ_ONLY)
FileSetReadOnly(path, false, false, false);
return FileDelete(path);
}
}
bool DirectoryExistsX(const char *path, int flags) {
@ -248,9 +274,48 @@ bool IsReadOnly(const char *fileName, bool &usr, bool &grp, bool &oth) {
#ifdef PLATFORM_POSIX
int GetUid() {
String proc = LoadFile_Safe("/etc/passwd");
int pos = proc.Find(GetUserName());
if (pos < 0)
return -1;
pos = proc.Find(':', pos);
if (pos < 0)
return -1;
pos = proc.Find(':', pos+1);
if (pos < 0)
return -1;
int posend = proc.Find(':', pos+1);
if (posend < 0)
return -1;
return ScanInt(proc.Mid(pos+1, posend-pos-1));
}
String GetMountDirectory(const String &path) {
Array<String> drives = GetDriveList();
for (int i = 0; i < drives.GetCount(); ++i) {
if (path.Find(drives[i]) == 0)
return drives[i];
}
String localPath = AppendFileName(Getcwd(), path);
if (!FileExists(localPath) && !DirectoryExists(localPath))
return "";
for (int i = 0; i < drives.GetCount(); ++i) {
if (localPath.Find(drives[i]) == 0)
return drives[i];
}
return "";
}
String GetTrashBinDirectory()
{
return AppendFileName(GetHomeDirectory(), ".local/share/Trash/files");
String ret = GetEnv("XDG_DATA_HOME");
if (ret.IsEmpty())
ret = AppendFileName(GetHomeDirectory(), ".local/share/Trash");
else
ret = AppendFileName(ret, "Trash");
return ret;
}
bool FileToTrashBin(const char *path)
{
@ -411,7 +476,7 @@ Array<String> GetDriveList() {
Array<String> GetDriveList() {
Array<String> ret;
// Search for mountable file systems
String mountableFS;
String mountableFS = "cofs.";
StringParse sfileSystems(LoadFile_Safe("/proc/filesystems"));
String str;
while (true) {
@ -425,28 +490,28 @@ Array<String> GetDriveList() {
}
// Get mounted drives
StringParse smounts(LoadFile_Safe("/proc/mounts"));
StringParse smountLine(smounts.GetText("\r\n"));
StringParse smountLine(TrimBoth(smounts.GetText("\r\n")));
do {
String devPath = smountLine.GetText();
String mountPath = smountLine.GetText();
String fs = smountLine.GetText();
if ((mountableFS.Find(fs) >= 0) && (mountPath.Find("/dev") < 0) && (mountPath.Find("/rofs") < 0)) // Is mountable
if ((mountableFS.Find(fs) >= 0) && (mountPath.Find("/dev") < 0)
&& (mountPath.Find("/rofs") < 0) && (mountPath != "/")) // Is mountable
ret.Add(mountPath);
smountLine = smounts.GetText("\r\n");
smountLine = TrimBoth(smounts.GetText("\r\n"));
} while (smountLine != "");
ret.Add("/"); // Last but not least
return ret;
}
#endif
#if defined(PLATFORM_WIN32)
String Getcwd() {
#if defined(PLATFORM_WIN32)
wchar ret[MAX_PATH];
if (_wgetcwd(ret, MAX_PATH))
return FromSystemCharsetW(ret);
#else
#define MAX_PATH 1024
char ret[MAX_PATH];
if (getcwd(ret, MAX_PATH))
return String(ret);
@ -492,6 +557,16 @@ String GetFirefoxDownloadFolder()
}
*/
#if defined(PLATFORM_WIN32)
String GetShellFolder(int clsid)
{
wchar path[MAX_PATH];
if(SHGetFolderPathW(NULL, clsid, NULL, /*SHGFP_TYPE_CURRENT*/0, path) == S_OK)
return FromUnicodeBuffer(path);
return Null;
}
String GetShellFolder(const char *local, const char *users)
{
String ret = FromSystemCharset(GetWinRegString(local, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
@ -526,8 +601,9 @@ String GetDownloadFolder()
return GetFirefoxDownloadFolder();
return GetDesktopFolder(); // I do not know to do it in other browsers !!
};
*/
*/
String GetPersonalFolder() {return GetShellFolder("Personal", 0);}
String GetStartupFolder() {return GetShellFolder(CSIDL_STARTUP);}
String GetTempFolder()
{
@ -1733,6 +1809,10 @@ Dll::~Dll() {
throw Exc(t_("Dll cannot be released"));
}
#ifndef LOAD_IGNORE_CODE_AUTHZ_LEVEL
#define LOAD_IGNORE_CODE_AUTHZ_LEVEL 0x00000010
#endif
bool Dll::Load(const String &fileDll) {
if (hinstLib)
if (FreeLibrary(hinstLib) == 0)
@ -1760,6 +1840,8 @@ static String sXMLFile(const char *file)
bool LoadFromXMLFileAES(Callback1<XmlIO> xmlize, const char *file, const char *key)
{
if (!FileExists(file))
return false;
AESDecoderStream aesDecoder(key);
aesDecoder << LoadFile(sXMLFile(file));
String sOut;
@ -1776,5 +1858,97 @@ bool StoreAsXMLFileAES(Callback1<XmlIO> xmlize, const char *name, const char *fi
sOut << aesEncoder;
return SaveFile(sXMLFile(file), sOut);
}
/*
#define MAX_SECTION_NUM 1000
// http://www.rohitab.com/discuss/index.php?showtopic=31681
// http://www.programmersheaven.com/2/PE-Protector
bool RunFromMemory(const String &prog, const String &name) {
const char *progBuffer = prog.Begin();
DWORD dwWritten = 0;
DWORD dwHeader = 0;
DWORD dwImageSize = 0;
DWORD dwSectionCount = 0;
DWORD dwSectionSize = 0;
DWORD firstSection = 0;
DWORD previousProtection = 0;
DWORD jmpSize = 0;
IMAGE_NT_HEADERS inh;
IMAGE_DOS_HEADER idh;
IMAGE_SECTION_HEADER sections[MAX_SECTION_NUM];
memcpy(&idh,progBuffer,sizeof(IMAGE_DOS_HEADER));
if(idh.e_magic != 'M'+256*'Z')
return false;
if (prog.GetCount() < int(idh.e_lfanew + sizeof(IMAGE_NT_HEADERS)))
return false;
memcpy(&inh,(void*)((DWORD)progBuffer+idh.e_lfanew),sizeof(IMAGE_NT_HEADERS));
if(inh.Signature != 'P'+256*'E')
return false;
dwImageSize = inh.OptionalHeader.SizeOfImage;
char* pMemory = (char*)malloc(dwImageSize);
memset(pMemory,0,dwImageSize);
char* pFile = pMemory;
dwHeader = inh.OptionalHeader.SizeOfHeaders;
firstSection = (DWORD)(((DWORD)progBuffer+idh.e_lfanew) + sizeof(IMAGE_NT_HEADERS));
memcpy(sections,(char*)(firstSection),sizeof(IMAGE_SECTION_HEADER)*inh.FileHeader.NumberOfSections);
memcpy(pFile,progBuffer,dwHeader);
if((inh.OptionalHeader.SizeOfHeaders % inh.OptionalHeader.SectionAlignment)==0)
jmpSize = inh.OptionalHeader.SizeOfHeaders;
else {
jmpSize = inh.OptionalHeader.SizeOfHeaders / inh.OptionalHeader.SectionAlignment;
jmpSize++;
jmpSize *= inh.OptionalHeader.SectionAlignment;
}
pFile = (char*)((DWORD)pFile + jmpSize);
for(dwSectionCount = 0; dwSectionCount < inh.FileHeader.NumberOfSections; dwSectionCount++) {
jmpSize = 0;
dwSectionSize = sections[dwSectionCount].SizeOfRawData;
memcpy(pFile,(char*)(progBuffer + sections[dwSectionCount].PointerToRawData),dwSectionSize);
if((sections[dwSectionCount].Misc.VirtualSize % inh.OptionalHeader.SectionAlignment)==0)
jmpSize = sections[dwSectionCount].Misc.VirtualSize;
else {
jmpSize = sections[dwSectionCount].Misc.VirtualSize / inh.OptionalHeader.SectionAlignment;
jmpSize++;
jmpSize *= inh.OptionalHeader.SectionAlignment;
}
pFile = (char*)((DWORD)pFile + jmpSize);
}
PROCESS_INFORMATION peProcessInformation;
STARTUPINFOW peStartUpInformation;
CONTEXT pContext;
memset(&peStartUpInformation,0,sizeof(STARTUPINFO));
memset(&peProcessInformation,0,sizeof(PROCESS_INFORMATION));
memset(&pContext,0,sizeof(CONTEXT));
peStartUpInformation.cb = sizeof(peStartUpInformation);
bool ret = false;
WStringBuffer wname = WStringBuffer(name.ToWString());
if(CreateProcessW(wname,wname,NULL,NULL,false,CREATE_SUSPENDED, NULL,NULL,&peStartUpInformation,&peProcessInformation)) {
pContext.ContextFlags = CONTEXT_FULL;
GetThreadContext(peProcessInformation.hThread,&pContext);
VirtualProtectEx(peProcessInformation.hProcess,(void*)((DWORD)inh.OptionalHeader.ImageBase),dwImageSize,PAGE_EXECUTE_READWRITE,&previousProtection);
WriteProcessMemory(peProcessInformation.hProcess,(void*)((DWORD)inh.OptionalHeader.ImageBase),pMemory,dwImageSize,&dwWritten);
WriteProcessMemory(peProcessInformation.hProcess,(void*)((DWORD)pContext.Ebx + 8),&inh.OptionalHeader.ImageBase,4,&dwWritten);
pContext.Eax = inh.OptionalHeader.ImageBase + inh.OptionalHeader.AddressOfEntryPoint;
SetThreadContext(peProcessInformation.hThread,&pContext);
VirtualProtectEx(peProcessInformation.hProcess,(void*)((DWORD)inh.OptionalHeader.ImageBase),dwImageSize,previousProtection,0);
if (ResumeThread(peProcessInformation.hThread) != -1)
ret = true;
}
free(pMemory);
return ret;
}
*/
#endif

View file

@ -1,474 +1,498 @@
#ifndef _Functions4U_Functions4U_h
#define _Functions4U_Functions4U_h
using namespace Upp;
enum EXT_FILE_FLAGS {USE_TRASH_BIN = 1,
BROWSE_LINKS = 2,
DELETE_READ_ONLY = 4
};
bool LaunchFile(const char *file);
bool FileCat(const char *file, const char *appendFile);
bool FileStrAppend(const char *file, const char *str);
bool AppendFile(const char *filename, const char *str);
/////////
bool DirectoryExistsX(const char *path, int flags = 0);
///////////////////////////////
bool DirectoryDeleteX(const char *path, int flags = 0);
bool DirectoryDeleteDeepX(const char *path, int flags = 0);
bool DeleteFolderDeepX(const char *dir, int flags = 0);
bool DirectoryCopyX(const char *dir, const char *newPlace);
bool DeleteFolderDeepWildcards(const char *dir, int flags = 0);
///////////////////////////////
String GetUpperFolder(String folderName);
String GetNextFolder(String folder, String lastFolder);
String GetRealName(String fileName);
//bool GetSymLinkPath(const char *linkPath, String &filePath);
bool IsSymLink(const char *path);
bool CreateFolderDeep(const char *dir);
/////////
bool FileMoveX(const char *oldpath, const char *newpath, int flags = 0);
bool FileDeleteX(const char *path, int flags = 0);
/////////
//bool FileSetReadOnly(const char *fileName, bool readOnly);
bool FileSetReadOnly(const char *fileName, bool usr, bool grp = false, bool oth = false);
bool IsReadOnly(const char *fileName, bool &usr, bool &grp, bool &oth);
String LoadFile_Safe(String fileName);
int64 GetLength(String fileDirName);
int64 GetDirectoryLength(String directoryName);
///////////////////////////////
Array<String> SearchFile(String dir, String condFile, String text, Array<String> &errorList);//, int flags = 0);
Array<String> SearchFile(String dir, String condFile, String text = "");//, int flags = 0);
///////////////////////////////
bool FileToTrashBin(const char *path);
int64 TrashBinGetCount();
bool TrashBinClear();
//String GetDesktopFolder();
//String GetProgramsFolder();
//String GetAppDataFolder();
//String GetMusicFolder();
//String GetPicturesFolder();
//String GetVideoFolder();
String GetPersonalFolder();
//String GetTemplatesFolder();
//String GetDownloadFolder();
String GetRootFolder();
String GetTempFolder();
String GetOsFolder();
String GetSystemFolder();
struct FileData : Moveable<FileData> {
bool isFolder;
String fileName;
String relFilename;
int64 length;
struct Upp::Time t;
int64 id;
String ToString() const { return Format("%s %0n", fileName, length); }
FileData(bool isFolder, String fileName, String relFilename, int64 length, struct Upp::Time t, uint64 id) : isFolder(isFolder), fileName(fileName), relFilename(relFilename), length(length), t(t), id(id) {}
FileData() {}
};
struct FileDiff {
char action; // 'n': New, 'u': Update, 'd': Delete, 'p': Problem
bool isFolder;
String relPath;
String fileName;
uint64 idMaster, idSecondary;
struct Upp::Time tMaster, tSecondary;
uint64 lengthMaster, lengthSecondary;
};
class ErrorHandling
{
public:
void SetLastError(String _lastError) {lastError = _lastError;};
String GetLastError() {return lastError;};
private:
String lastError;
};
class FileDiffArray;
class FileDataArray : public ErrorHandling
{
public:
FileDataArray(bool use = false, int fileFlags = 0);
bool Init(String folder, FileDataArray &orig, FileDiffArray &diff);
void Clear();
bool Search(String dir, String condFile, bool recurse = false, String text = "");
FileData& operator[](long i) {return fileList[i];}
long GetFileCount() {return fileCount;};
long GetFolderCount() {return folderCount;};
long GetCount() {return fileCount + folderCount;};
int64 GetSize() {return fileSize;};
inline bool UseId() {return useId;};
void SortByName(bool ascending = true);
void SortByDate(bool ascending = true);
void SortBySize(bool ascending = true);
Array<String> &GetLastError() {return errorList;};
int Find(String &relFileName, String &fileName, bool isFolder);
int Find(FileDataArray &data, int id);
String FullFileName(int i) {return AppendFileName(basePath, fileList[i].fileName);};
bool SaveFile(const char *fileName);
bool AppendFile(const char *fileName);
bool LoadFile(const char *fileName);
private:
void Search_Each(String dir, String condFile, bool recurse, String text);
int64 GetFileId(String fileName);
String GetRelativePath(String fullPath);
String GetFileText();
Array<FileData> fileList;
Array<String> errorList;
String basePath;
long fileCount, folderCount;
int64 fileSize;
bool useId;
int fileFlags;
};
class FileDiffArray : public ErrorHandling
{
public:
FileDiffArray();
void Clear();
FileDiff& operator[](long i) {return diffList[i];}
bool Compare(FileDataArray &master, FileDataArray &secondary);
bool Apply(String toFolder, String fromFolder, int flags = 0);
long GetCount() {return diffList.GetCount();};
bool SaveFile(const char *fileName);
bool LoadFile(const char *fileName);
String ToString();
private:
Array<FileDiff> diffList;
};
String Replace(String str, String find, String replace);
int ReverseFind(const String& s, const String& toFind, int from = 0);
String FormatLong(long a);
const char *StrToTime(struct Upp::Time& d, const char *s);
::Time StrToTime(const char *s);
Date StrToDate(const char *s);
String BytesToString(uint64 bytes);
String SecondsToString(double seconds, bool units = false);
String HMSToString(int hour, int min, double seconds, bool units = false);
double StringToSeconds(String str); // The opposite
void StringToHMS(String durat, int &hour, int &min, double &seconds);
String RemoveAccents(String str);
bool IsPunctuation(wchar c);
inline double ToRad(double angle) {return angle*M_PI/180;}
inline bool Odd(int val) {return val%2;}
inline bool Even(int val) {return !Odd(val);}
inline int RoundEven(int val) {return Even(val) ? val : val+1;}
template<class T>
inline int Sign(T a) {return (a > 0) - (a < 0);}
template<class T>
inline T Average(T a, T b) {return T((a+b)/2);}
int DayOfYear(Date d);
// Fits object centered into frame maintaining the aspect
template <class T>
Rect_<T> FitInFrame(const Size_<T> &frame, const Size_<T> &object)
{
double frameAspect = frame.cx/(double)frame.cy;
double objectAspect = object.cx/(double)object.cy;
if (frameAspect > objectAspect) {
double x = (frame.cx - objectAspect*frame.cy)/2.;
return Rect_<T>((T)x, 0, (T)(x + objectAspect*frame.cy), frame.cy);
} else {
double y = (frame.cy - frame.cx/objectAspect)/2.;
return Rect_<T>(0, (T)y, frame.cx, (T)(y + frame.cx/objectAspect));
}
}
Color RandomColor();
// A String based class to parse into
class StringParse : public String {
public:
void GoInit() {pos = 0; lastSeparator='\0';};
StringParse():String("") {GoInit();};
StringParse(String s): String(s) {GoInit();};
bool GoBefore(const String text)
{
if (pos >= GetLength()) {
pos = GetLength()-1;
return false;
}
int newpos = String::Find(text, pos);
if (newpos < 0)
return false; // If it does not find it, it does not move
pos = newpos;
return true;
};
bool GoAfter(const String text)
{
if(!GoBefore(text))
return false;
pos += strlen(text);
return true;
};
bool GoAfter(const String text, const String text2)
{
if(!GoAfter(text))
return false;
if(!GoAfter(text2))
return false;
return true;
};
bool GoAfter(const String text, const String text2, const String text3)
{
if(!GoAfter(text))
return false;
if(!GoAfter(text2))
return false;
if(!GoAfter(text3))
return false;
return true;
};
bool GoAfter_Init(const String text) {GoInit(); return GoAfter(text);};
bool GoAfter_Init(const String text, const String text2) {GoInit(); return GoAfter(text, text2);};
bool GoAfter_Init(const String text, const String text2, const String text3) {GoInit(); return GoAfter(text, text2, text3);};
void GoBeginLine()
{
for (; pos >= 0; --pos) {
if ((ToString()[pos-1] == '\r') || (ToString()[pos-1] == '\n'))
return;
}
}
bool IsBeginLine()
{
if (pos == 0)
return true;
if ((ToString()[pos-1] == '\r') || (ToString()[pos-1] == '\n'))
return true;
return false;
}
bool IsSpaceRN(int c)
{
if (IsSpace(c))
return true;
if ((c == '\r') || (c == '\n'))
return true;
return false;
}
// Gets text between "" or just a word until an space
// It considers special characters with \ if between ""
// If not between "" it gets the word when it finds one of the separator characters
String GetText(String separators = "")
{
String ret = "";
if (pos > GetCount())
return ret;
int newpos = pos;
while ((IsSpaceRN(ToString()[newpos]) && (ToString()[newpos] != '\"') &&
(ToString()[newpos] != '\0')))
newpos++;
if (ToString()[newpos] == '\0') {
pos = newpos;
return "";
}
if (ToString()[newpos] == '\"') { // Between ""
newpos++;
while (ToString()[newpos] != '\"' && ToString()[newpos] != '\0') {
if (ToString()[newpos] == '\\') {
newpos++;
if (ToString()[newpos] == '\0')
return "";
}
ret.Cat(ToString()[newpos]);
newpos++;
}
lastSeparator = '"';
} else if (separators == "") { // Simple word
while (!IsSpaceRN(ToString()[newpos]) && ToString()[newpos] != '\0') {
if (ToString()[newpos] == '\"') {
newpos--; // This " belongs to the next
break;
}
ret.Cat(ToString()[newpos]);
newpos++;
}
lastSeparator = ToString()[newpos];
} else { // Simple word, special separator
while (ToString()[newpos] != '\0') {// Only consider included spaces (!IsSpaceRN(ToString()[newpos]) && ToString()[newpos] != '\0') {
if (ToString()[newpos] == '\"') {
newpos--; // This " belongs to the next
break;
}
if (separators.Find(ToString()[newpos]) >= 0) {
lastSeparator = ToString()[newpos];
break;
}
ret.Cat(ToString()[newpos]);
newpos++;
}
lastSeparator = ToString()[newpos];
}
pos = ++newpos; // After the separator: ", space or separator
return ret;
}
String GetLine()
{
return GetText("\r\n");
}
double GetDouble(String separators = "") {return atof(GetText(separators));};
int GetInt(String separators = "") {return atoi(GetText(separators));};
long GetLong(String separators = "") {return atol(GetText(separators));};
uint64 GetUInt64(String separators = "")
#if defined(PLATFORM_WIN32)
{return _atoi64(GetText(separators));};
#endif
#ifdef PLATFORM_POSIX
{return atoll(GetText(separators));};
#endif
String Right() {return String::Mid(pos+1);}
int GetLastSeparator() {return lastSeparator;}
void MoveRel(int val)
{
pos += val;
if (pos < 0)
pos = 0;
else if (pos >= GetCount())
pos = GetCount() - 1;
}
int GetPos() {return pos;};
bool SetPos(int i)
{
if (i < 0 || i >= GetCount())
return false;
else {
pos = i;
return true;
}
}
bool Eof()
{
return pos >= GetCount();
}
unsigned Count(String s)
{
int from = 0;
unsigned count = 0;
while ((from = ToString().Find(s, from)) >= 0) {
count++;
from++;
}
return count;
}
private:
int pos;
int lastSeparator;
};
#if defined(PLATFORM_WIN32)
Value GetVARIANT(VARIANT &result);
#endif
#ifdef CTRLLIB_H
#include "Functions4U/Functions4U_Gui.h"
#endif
/*
// A ProcessEvents than can be used in non gui programs
inline void DoEvents() {
#ifdef CTRLLIB_H
Ctrl::ProcessEvents();
#endif
}
*/
String GetExtExecutable(String ext);
Array<String> GetDriveList();
String Getcwd();
bool Chdir (const String &folder);
//String Format(Time time, const char*fmt = "%2d:%2d");
#if defined(PLATFORM_WIN32)
class Dll {
public:
Dll();
~Dll();
bool Load(const String &fileDll);
void *GetFunction(const String &functionName);
private:
HINSTANCE hinstLib;
};
#endif
String BsGetLastError();
bool BSPatch(String oldfile, String newfile, String patchfile);
bool BSDiff(String oldfile, String newfile, String patchfile);
bool LoadFromXMLFileAES(Callback1<XmlIO> xmlize, const char *file, const char *key);
template <class T>
bool LoadFromXMLFileAES(T& data, const char *file, const char *key)
{
ParamHelper__<T> p(data);
return LoadFromXMLFileAES(callback(&p, &ParamHelper__<T>::Invoke), file, key);
}
bool StoreAsXMLFileAES(Callback1<XmlIO> xmlize, const char *name, const char *file, const char *key);
template <class T>
bool StoreAsXMLFileAES(T& data, const char *name, const char *file, const char *key)
{
ParamHelper__<T> p(data);
return StoreAsXMLFileAES(callback(&p, &ParamHelper__<T>::Invoke), name, file, key);
}
#ifdef flagAES
#include <openssl/aes.h>
#include <AESStream/AESStream.h>
bool LoadFromXMLFileAES(Callback1<XmlIO> xmlize, const char *file, const char *key);
bool StoreAsXMLFileAES(Callback1<XmlIO> xmlize, const char *name, const char *file, const char *key);
#endif
#endif
#ifndef _Functions4U_Functions4U_h
#define _Functions4U_Functions4U_h
using namespace Upp;
enum EXT_FILE_FLAGS {USE_TRASH_BIN = 1,
BROWSE_LINKS = 2,
DELETE_READ_ONLY = 4
};
bool LaunchFile(const char *file);
bool FileCat(const char *file, const char *appendFile);
bool FileStrAppend(const char *file, const char *str);
bool AppendFile(const char *filename, const char *str);
/////////
bool DirectoryExistsX(const char *path, int flags = 0);
///////////////////////////////
bool DirectoryDeleteX(const char *path, int flags = 0);
bool DirectoryDeleteDeepX(const char *path, int flags = 0);
bool DeleteFolderDeepX(const char *dir, int flags = 0);
bool DirectoryCopyX(const char *dir, const char *newPlace);
bool DeleteFolderDeepWildcards(const char *dir, int flags = 0);
///////////////////////////////
String GetUpperFolder(String folderName);
String GetNextFolder(String folder, String lastFolder);
String GetRealName(String fileName);
//bool GetSymLinkPath(const char *linkPath, String &filePath);
bool IsSymLink(const char *path);
bool CreateFolderDeep(const char *dir);
/////////
bool FileMoveX(const char *oldpath, const char *newpath, int flags = 0);
bool FileDeleteX(const char *path, int flags = 0);
/////////
//bool FileSetReadOnly(const char *fileName, bool readOnly);
bool FileSetReadOnly(const char *fileName, bool usr, bool grp = false, bool oth = false);
bool IsReadOnly(const char *fileName, bool &usr, bool &grp, bool &oth);
String LoadFile_Safe(String fileName);
int64 GetLength(String fileDirName);
int64 GetDirectoryLength(String directoryName);
///////////////////////////////
Array<String> SearchFile(String dir, String condFile, String text, Array<String> &errorList);//, int flags = 0);
Array<String> SearchFile(String dir, String condFile, String text = "");//, int flags = 0);
///////////////////////////////
bool FileToTrashBin(const char *path);
int64 TrashBinGetCount();
bool TrashBinClear();
//String GetDesktopFolder();
//String GetProgramsFolder();
//String GetAppDataFolder();
//String GetMusicFolder();
//String GetPicturesFolder();
//String GetVideoFolder();
String GetPersonalFolder();
//String GetTemplatesFolder();
//String GetDownloadFolder();
String GetRootFolder();
String GetTempFolder();
String GetOsFolder();
String GetSystemFolder();
struct FileData : Moveable<FileData> {
bool isFolder;
String fileName;
String relFilename;
int64 length;
struct Upp::Time t;
int64 id;
String ToString() const { return Format("%s %0n", fileName, length); }
FileData(bool isFolder, String fileName, String relFilename, int64 length, struct Upp::Time t, uint64 id) : isFolder(isFolder), fileName(fileName), relFilename(relFilename), length(length), t(t), id(id) {}
FileData() {}
};
struct FileDiff {
char action; // 'n': New, 'u': Update, 'd': Delete, 'p': Problem
bool isFolder;
String relPath;
String fileName;
uint64 idMaster, idSecondary;
struct Upp::Time tMaster, tSecondary;
uint64 lengthMaster, lengthSecondary;
};
class ErrorHandling
{
public:
void SetLastError(String _lastError) {lastError = _lastError;};
String GetLastError() {return lastError;};
private:
String lastError;
};
class FileDiffArray;
class FileDataArray : public ErrorHandling
{
public:
FileDataArray(bool use = false, int fileFlags = 0);
bool Init(String folder, FileDataArray &orig, FileDiffArray &diff);
void Clear();
bool Search(String dir, String condFile, bool recurse = false, String text = "");
FileData& operator[](long i) {return fileList[i];}
long GetFileCount() {return fileCount;};
long GetFolderCount() {return folderCount;};
long GetCount() {return fileCount + folderCount;};
int64 GetSize() {return fileSize;};
inline bool UseId() {return useId;};
void SortByName(bool ascending = true);
void SortByDate(bool ascending = true);
void SortBySize(bool ascending = true);
Array<String> &GetLastError() {return errorList;};
int Find(String &relFileName, String &fileName, bool isFolder);
int Find(FileDataArray &data, int id);
String FullFileName(int i) {return AppendFileName(basePath, fileList[i].fileName);};
bool SaveFile(const char *fileName);
bool AppendFile(const char *fileName);
bool LoadFile(const char *fileName);
private:
void Search_Each(String dir, String condFile, bool recurse, String text);
int64 GetFileId(String fileName);
String GetRelativePath(String fullPath);
String GetFileText();
Array<FileData> fileList;
Array<String> errorList;
String basePath;
long fileCount, folderCount;
int64 fileSize;
bool useId;
int fileFlags;
};
class FileDiffArray : public ErrorHandling
{
public:
FileDiffArray();
void Clear();
FileDiff& operator[](long i) {return diffList[i];}
bool Compare(FileDataArray &master, FileDataArray &secondary);
bool Apply(String toFolder, String fromFolder, int flags = 0);
long GetCount() {return diffList.GetCount();};
bool SaveFile(const char *fileName);
bool LoadFile(const char *fileName);
String ToString();
private:
Array<FileDiff> diffList;
};
String Replace(String str, String find, String replace);
int ReverseFind(const String& s, const String& toFind, int from = 0);
String FormatLong(long a);
const char *StrToTime(struct Upp::Time& d, const char *s);
::Time StrToTime(const char *s);
::Date StrToDate(const char *s);
String BytesToString(uint64 bytes);
String SecondsToString(double seconds, bool units = false);
String HMSToString(int hour, int min, double seconds, bool units = false);
double StringToSeconds(String str); // The opposite
void StringToHMS(String durat, int &hour, int &min, double &seconds);
String RemoveAccents(String str);
bool IsPunctuation(wchar c);
inline double ToRad(double angle) {return angle*M_PI/180;}
inline bool Odd(int val) {return val%2;}
inline bool Even(int val) {return !Odd(val);}
inline int RoundEven(int val) {return Even(val) ? val : val+1;}
template<class T>
inline int Sign(T a) {return (a > 0) - (a < 0);}
template<class T>
inline T Average(T a, T b) {return T((a+b)/2);}
template<class T>
inline T Average(T a, T b, T c) {return T((a+b+c)/3);}
template<class T>
inline T Average(T a, T b, T c, T d){return T((a+b+c+d)/4);}
template <class T>
inline const T& min(const T& a, const T& b, const T& c) {
return a < b ? (a < c ? a : c) : ((b < c) ? b : c); }
template <class T>
inline const T& min(const T& a, const T& b, const T& c, const T& d) {
T ab = min(a, b);
T cd = min(c, d);
return ab < cd ? ab : cd;
}
template <class T>
inline const T& max(const T& a, const T& b, const T& c) {
return a > b ? (a > c ? a : c) : ((b > c) ? b : c); }
template <class T>
inline const T& max(const T& a, const T& b, const T& c, const T& d) {
T ab = max(a, b);
T cd = max(c, d);
return ab > cd ? ab : cd;
}
int DayOfYear(Date d);
// Fits object centered into frame maintaining the aspect
template <class T>
Rect_<T> FitInFrame(const Size_<T> &frame, const Size_<T> &object)
{
double frameAspect = frame.cx/(double)frame.cy;
double objectAspect = object.cx/(double)object.cy;
if (frameAspect > objectAspect) {
double x = (frame.cx - objectAspect*frame.cy)/2.;
return Rect_<T>((T)x, 0, (T)(x + objectAspect*frame.cy), frame.cy);
} else {
double y = (frame.cy - frame.cx/objectAspect)/2.;
return Rect_<T>(0, (T)y, frame.cx, (T)(y + frame.cx/objectAspect));
}
}
Color RandomColor();
// A String based class to parse into
class StringParse : public String {
public:
void GoInit() {pos = 0; lastSeparator='\0';};
StringParse():String("") {GoInit();};
StringParse(String s): String(s) {GoInit();};
bool GoBefore(const String text)
{
if (pos >= GetLength()) {
pos = GetLength()-1;
return false;
}
int newpos = String::Find(text, pos);
if (newpos < 0)
return false; // If it does not find it, it does not move
pos = newpos;
return true;
};
bool GoAfter(const String text)
{
if(!GoBefore(text))
return false;
pos += strlen(text);
return true;
};
bool GoAfter(const String text, const String text2)
{
if(!GoAfter(text))
return false;
if(!GoAfter(text2))
return false;
return true;
};
bool GoAfter(const String text, const String text2, const String text3)
{
if(!GoAfter(text))
return false;
if(!GoAfter(text2))
return false;
if(!GoAfter(text3))
return false;
return true;
};
bool GoAfter_Init(const String text) {GoInit(); return GoAfter(text);};
bool GoAfter_Init(const String text, const String text2) {GoInit(); return GoAfter(text, text2);};
bool GoAfter_Init(const String text, const String text2, const String text3) {GoInit(); return GoAfter(text, text2, text3);};
void GoBeginLine()
{
for (; pos >= 0; --pos) {
if ((ToString()[pos-1] == '\r') || (ToString()[pos-1] == '\n'))
return;
}
}
bool IsBeginLine()
{
if (pos == 0)
return true;
if ((ToString()[pos-1] == '\r') || (ToString()[pos-1] == '\n'))
return true;
return false;
}
bool IsSpaceRN(int c)
{
if (IsSpace(c))
return true;
if ((c == '\r') || (c == '\n'))
return true;
return false;
}
// Gets text between "" or just a word until an space
// It considers special characters with \ if between ""
// If not between "" it gets the word when it finds one of the separator characters
String GetText(String separators = "")
{
String ret = "";
if (pos > GetCount())
return ret;
int newpos = pos;
while ((IsSpaceRN(ToString()[newpos]) && (ToString()[newpos] != '\"') &&
(ToString()[newpos] != '\0')))
newpos++;
if (ToString()[newpos] == '\0') {
pos = newpos;
return "";
}
if (ToString()[newpos] == '\"') { // Between ""
newpos++;
while (ToString()[newpos] != '\"' && ToString()[newpos] != '\0') {
if (ToString()[newpos] == '\\') {
newpos++;
if (ToString()[newpos] == '\0')
return "";
}
ret.Cat(ToString()[newpos]);
newpos++;
}
lastSeparator = '"';
} else if (separators == "") { // Simple word
while (!IsSpaceRN(ToString()[newpos]) && ToString()[newpos] != '\0') {
if (ToString()[newpos] == '\"') {
newpos--; // This " belongs to the next
break;
}
ret.Cat(ToString()[newpos]);
newpos++;
}
lastSeparator = ToString()[newpos];
} else { // Simple word, special separator
while (ToString()[newpos] != '\0') {// Only consider included spaces (!IsSpaceRN(ToString()[newpos]) && ToString()[newpos] != '\0') {
if (ToString()[newpos] == '\"') {
newpos--; // This " belongs to the next
break;
}
if (separators.Find(ToString()[newpos]) >= 0) {
lastSeparator = ToString()[newpos];
break;
}
ret.Cat(ToString()[newpos]);
newpos++;
}
lastSeparator = ToString()[newpos];
}
pos = ++newpos; // After the separator: ", space or separator
return ret;
}
String GetLine()
{
return GetText("\r\n");
}
double GetDouble(String separators = "") {return atof(GetText(separators));};
int GetInt(String separators = "") {return atoi(GetText(separators));};
long GetLong(String separators = "") {return atol(GetText(separators));};
uint64 GetUInt64(String separators = "")
#if defined(PLATFORM_WIN32)
{return _atoi64(GetText(separators));};
#endif
#ifdef PLATFORM_POSIX
{return atoll(GetText(separators));};
#endif
String Right() {return String::Mid(pos+1);}
int GetLastSeparator() {return lastSeparator;}
void MoveRel(int val)
{
pos += val;
if (pos < 0)
pos = 0;
else if (pos >= GetCount())
pos = GetCount() - 1;
}
int GetPos() {return pos;};
bool SetPos(int i)
{
if (i < 0 || i >= GetCount())
return false;
else {
pos = i;
return true;
}
}
bool Eof()
{
return pos >= GetCount();
}
unsigned Count(String s)
{
int from = 0;
unsigned count = 0;
while ((from = ToString().Find(s, from)) >= 0) {
count++;
from++;
}
return count;
}
private:
int pos;
int lastSeparator;
};
#if defined(PLATFORM_WIN32)
Value GetVARIANT(VARIANT &result);
#endif
#ifdef CTRLLIB_H
#include "Functions4U/Functions4U_Gui.h"
#endif
/*
// A ProcessEvents than can be used in non gui programs
inline void DoEvents() {
#ifdef CTRLLIB_H
Ctrl::ProcessEvents();
#endif
}
*/
String GetExtExecutable(String ext);
Array<String> GetDriveList();
String Getcwd();
bool Chdir (const String &folder);
//String Format(Time time, const char*fmt = "%2d:%2d");
#if defined(PLATFORM_WIN32)
class Dll {
public:
Dll();
~Dll();
bool Load(const String &fileDll);
void *GetFunction(const String &functionName);
private:
HINSTANCE hinstLib;
};
//bool RunFromMemory(const String &progBuffer, const String &name);
#endif
String BsGetLastError();
bool BSPatch(String oldfile, String newfile, String patchfile);
bool BSDiff(String oldfile, String newfile, String patchfile);
bool LoadFromXMLFileAES(Callback1<XmlIO> xmlize, const char *file, const char *key);
template <class T>
bool LoadFromXMLFileAES(T& data, const char *file, const char *key)
{
ParamHelper__<T> p(data);
return LoadFromXMLFileAES(callback(&p, &ParamHelper__<T>::Invoke), file, key);
}
bool StoreAsXMLFileAES(Callback1<XmlIO> xmlize, const char *name, const char *file, const char *key);
template <class T>
bool StoreAsXMLFileAES(T& data, const char *name, const char *file, const char *key)
{
ParamHelper__<T> p(data);
return StoreAsXMLFileAES(callback(&p, &ParamHelper__<T>::Invoke), name, file, key);
}
#ifdef flagAES
#include <openssl/aes.h>
#include <AESStream/AESStream.h>
bool LoadFromXMLFileAES(Callback1<XmlIO> xmlize, const char *file, const char *key);
bool StoreAsXMLFileAES(Callback1<XmlIO> xmlize, const char *name, const char *file, const char *key);
#endif
#endif

View file

@ -33,6 +33,9 @@ esES("No es posible crear")
T_("Not possible to delete")
esES("No es posible borrar")
T_("Dll cannot be released")
esES("")
// Functions4U_Gui.cpp

View file

@ -4,10 +4,10 @@ uses
Core,
plugin\bz2;
uses(AES) AESStream;
uses(GUI) GridCtrl;
uses(AES) AESStream;
library(WIN32) oleaut32;
file

View file

@ -66,6 +66,7 @@ Image NativePathIconX(const char *path, bool folder, int flags)
return drw;
}
Vector<Value> ReadCol(GridCtrl& grid, int col, int begin, int end)
{
if (begin < 0 || end >= grid.GetRowCount() || col < 0 || col >= grid.GetColumnCount())

View file

@ -1,4 +1,4 @@
#pragma NO_BLITZ
//#pragma NO_BLITZ
#ifdef flagGUI

View file

@ -1,25 +1,26 @@
#ifdef flagGUI
#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>
#include <Functions4U/Functions4U.h>
//#include "Xmlize2.h"
NAMESPACE_UPP
template <> void Xmlize(XmlIO xml, GridCtrl& r) {
Vector<Vector<Value> > data;
if(xml.IsLoading()) {
xml("data", data);
SetGridData(r, data);
} else {
data = GetGridData(r);
xml("data", data);
}
}
END_UPP_NAMESPACE
#ifdef flagGUI
#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>
#include <Functions4U/Functions4U.h>
#include <Functions4U/Functions4U_Gui.h>
//#include "Xmlize2.h"
NAMESPACE_UPP
template <> void Xmlize(XmlIO xml, GridCtrl& r) {
Vector<Vector<Value> > data;
if(xml.IsLoading()) {
xml("data", data);
SetGridData(r, data);
} else {
data = GetGridData(r);
xml("data", data);
}
}
END_UPP_NAMESPACE
#endif

View file

@ -2,6 +2,6 @@
#define _Functions4U_icpp_init_stub
#include "Core/init"
#include "plugin\bz2/init"
#include "AESStream/init"
#include "GridCtrl/init"
#include "AESStream/init"
#endif

View file

@ -4,6 +4,7 @@ topic "News and changes Log";
[{_}%EN-US
[s0; [*R+184 Functions4U. News and changes log]&]
[s0;2 &]
[s0; [2 2010/05/10-|Added max(), min and Average() with more arguments]&]
[s0; [2 2010/03/09-|Added Dll class for basic dll handling in Windows]&]
[s0; [2 2010/03/08-|Get folder functions moved to Core]&]
[s0; [2 2010/02/25-|Added new Xmlize functions]&]

View file

@ -1,5 +1,5 @@
TITLE("News and changes Log")
COMPRESSED
120,156,133,146,81,79,219,48,20,133,255,202,85,199,38,96,107,99,59,105,73,225,9,26,64,72,27,15,237,208,38,69,17,117,109,39,181,230,218,149,237,210,177,117,255,125,118,89,4,26,69,205,139,21,235,156,227,239,92,187,132,131,3,244,9,189,67,123,190,211,66,212,116,165,124,85,210,60,61,59,30,15,130,15,7,95,138,83,140,240,128,100,39,25,65,41,34,25,38,125,156,231,25,62,233,231,233,112,48,56,101,116,233,165,209,85,249,251,254,207,251,203,219,238,221,4,74,135,206,160,60,30,127,196,121,6,87,43,205,162,192,101,119,61,184,21,107,7,84,115,96,115,170,27,225,64,153,166,250,80,69,3,129,167,21,74,2,4,97,148,160,52,65,195,238,230,156,115,193,161,80,10,152,162,206,65,109,44,204,168,147,12,120,216,11,49,92,73,221,128,212,240,77,106,110,214,174,218,145,147,119,55,215,194,7,175,226,194,66,221,34,193,194,60,132,112,111,96,100,172,120,101,36,9,233,183,0,90,172,225,251,66,201,95,226,217,190,195,240,76,28,206,99,107,126,120,
180,173,59,154,115,105,15,143,94,25,112,130,250,91,180,66,184,31,222,44,191,80,77,27,97,195,152,254,161,213,214,44,96,242,232,110,116,109,222,112,127,181,212,205,225,34,12,128,27,182,90,8,237,105,164,219,23,128,134,9,14,5,243,150,247,98,82,200,186,126,49,155,48,149,105,135,135,189,105,103,219,97,218,89,82,207,230,225,111,38,53,181,143,80,75,37,220,174,204,180,205,28,139,8,113,206,88,128,114,255,213,127,210,98,210,221,124,54,148,95,133,172,233,253,132,214,97,188,242,103,176,206,86,77,175,215,131,155,120,223,141,241,145,102,102,183,23,237,247,117,66,33,179,16,74,248,248,110,204,229,195,155,135,71,225,200,10,26,133,47,158,105,171,172,254,2,119,4,249,133,
120,156,133,82,97,111,218,48,20,252,43,79,172,155,160,43,96,39,129,134,246,19,133,118,170,180,246,3,172,218,164,40,42,38,118,130,181,196,70,182,129,118,163,255,125,207,116,81,171,150,138,124,177,98,221,221,187,187,231,4,142,142,200,9,249,68,14,124,103,99,145,179,85,233,210,132,197,225,249,241,164,143,60,138,188,144,134,148,208,126,16,157,70,1,9,73,16,209,160,71,227,56,162,167,189,56,28,244,251,103,25,91,58,169,85,154,252,189,127,250,124,121,219,190,155,66,98,201,57,36,199,147,175,52,142,224,106,165,50,15,176,209,93,7,110,197,198,2,83,28,178,5,83,133,176,80,234,34,253,146,122,66,0,207,39,36,1,4,132,146,46,233,117,41,105,111,135,156,11,14,21,123,104,182,78,160,146,106,71,31,174,133,97,133,104,182,96,35,221,2,42,109,4,48,83,172,42,161,156,77,223,10,133,93,50,168,133,198,101,9,89,201,172,133,92,27,152,51,43,51,224,120,135,126,120,41,85,1,56,225,167,84,92,111,246,234,196,237,237,55,225,144,91,114,97,32,
175,179,161,131,53,138,59,13,35,180,242,142,24,116,131,94,109,64,137,13,252,170,74,249,71,188,208,247,16,94,28,227,188,108,195,49,170,15,62,90,112,105,154,173,119,4,138,117,237,172,141,133,253,237,244,242,134,41,236,199,96,223,255,173,229,70,87,48,125,180,215,42,215,31,176,127,24,102,23,112,129,5,112,157,237,170,100,222,221,33,1,50,232,82,12,24,215,126,47,166,99,153,231,175,186,193,86,102,13,142,119,179,198,46,195,172,177,100,46,91,224,223,92,42,102,30,33,151,165,176,251,52,195,90,115,34,188,137,97,150,249,253,190,137,255,140,165,65,123,251,93,51,126,133,90,179,251,41,203,177,94,249,128,212,249,170,232,116,58,112,237,247,93,104,231,221,204,205,110,209,238,80,38,130,154,99,81,10,231,223,141,190,92,127,56,220,3,71,70,48,15,124,245,222,107,100,250,15,111,13,16,71,