diff --git a/bazaar/Functions4U/Functions4U.h b/bazaar/Functions4U/Functions4U.h index 5ccb820d3..e3807b033 100644 --- a/bazaar/Functions4U/Functions4U.h +++ b/bazaar/Functions4U/Functions4U.h @@ -1,583 +1,582 @@ -#ifndef _Functions4U_Functions4U_h -#define _Functions4U_Functions4U_h - -#include -#include -#ifdef flagGUI -#include -#include -#include "GatherTpp.h" -#endif - -#include -#include "StaticPlugin.h" - -NAMESPACE_UPP - - -enum EXT_FILE_FLAGS {USE_TRASH_BIN = 1, - BROWSE_LINKS = 2, - DELETE_READ_ONLY = 4, - ASK_BEFORE_DELETE = 8 -}; - -bool LaunchFile(const char *file); - -bool FileCat(const char *file, const char *appendFile); - -int FileCompare(const char *path1, const char *path2); - -Upp::int64 FindStringInFile(const char *file, const String text, Upp::int64 pos0 = 0); - -bool FileStrAppend(const char *file, const char *str); -bool AppendFile(const char *filename, const char *str); - -String AppendFileName(const String& path1, const char *path2, const char *path3); - -inline String Trim(const String& s) {return TrimBoth(s);}; - -String FitFileName(String fileName, int len); - -String Tokenize(const String &str, const String &token, int &pos); - -///////// -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); -/////////////////////////////// - -bool UpperFolder(const char *folderName); -String GetUpperFolder(const String &folderName); -String GetNextFolder(const String &folder, const String &lastFolder); -String FileRealName(const char *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 SetReadOnly(const char *path, bool readOnly); -bool ReadOnly(const char *path, bool readOnly) -#if defined(__MINGW32__) - __attribute__ ((deprecated)); -#else - ; -#endif - -bool SetReadOnly(const char *path, bool usr, bool grp, bool oth); -bool ReadOnly(const char *path, bool usr, bool grp, bool oth) -#if defined(__MINGW32__) - __attribute__ ((deprecated)); -#else - ; -#endif -bool IsReadOnly(const char *path, bool &usr, bool &grp, bool &oth); - -String LoadFile_Safe(String fileName); - -Upp::int64 GetLength(const char *fileDirName); -Upp::int64 GetDirectoryLength(const char *directoryName); - -/////////////////////////////// -Upp::Array SearchFile(String dir, const Upp::Array &condFiles, const Upp::Array &condFolders, - const Upp::Array &extFiles, const Upp::Array &extFolders, - const String &text, Upp::Array &errorList); -Upp::Array SearchFile(String dir, String condFile, String text, Upp::Array &errorList);//, int flags = 0); -Upp::Array SearchFile(String dir, String condFile, String text = "");//, int flags = 0); -/////////////////////////////// - -bool FileToTrashBin(const char *path); -Upp::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 { - bool isFolder; - String fileName; - String relFilename; - Upp::int64 length; - struct Upp::Time t; - Upp::int64 id; - - String ToString() const { return Format("%s %0n", fileName, length); } - - FileData(bool isFolder, String fileName, String relFilename, Upp::int64 length, - struct Upp::Time t, Upp::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; - Upp::uint64 idMaster, idSecondary; - struct Upp::Time tMaster, tSecondary; - Upp::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;}; - Upp::int64 GetSize() {return fileSize;}; - inline bool UseId() {return useId;}; - void SortByName(bool ascending = true); - void SortByDate(bool ascending = true); - void SortBySize(bool ascending = true); - Upp::Array &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); - Upp::int64 GetFileId(String fileName); - String GetRelativePath(const String &fullPath); - String GetFileText(); - - Upp::Array fileList; - Upp::Array errorList; - String basePath; - long fileCount, folderCount; - Upp::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, const String folderFrom, - Upp::Array &excepFolders, Upp::Array &excepFiles, int sensSecs = 0); - 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: - Upp::Array diffList; -}; - -String Replace(String str, String find, String replace); -String Replace(String str, char find, char 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); Now included in TimeDate -Time StrToTime(const char *s); -Date StrToDate(const char *s); - -String BytesToString(Upp::uint64 bytes, bool units = true); - -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 FormatDoubleAdjust(double d, double range); - -String RemoveAccents(String str); -String RemoveAccent(wchar c); -bool IsPunctuation(wchar c); - -inline double ToRad(double angle) {return angle*M_PI/180.;} -inline double ToDeg(double angle) {return angle*180./M_PI;} - -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 -inline int Sign(T a) {return (a > 0) - (a < 0);} -template -inline T Average(T a, T b) {return T(a+b)/2;} -template -inline T Average(T a, T b, T c) {return T(a+b+c)/3;} -template -inline T Average(T a, T b, T c, T d){return T(a+b+c+d)/4;} -template -inline T pow2(T a) {return (a*a);} -template -inline T pow3(T a) {return (a*a*a);} -template -inline T pow4(T a) {return pow2(pow2(a));} -template -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 -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 -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 -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; -} - -template -inline bool Between(const T& val, const T& min, const T& max) { - return val >= min && val <= max; -} - -template -T AngleAdd360(T ang, T val) { - ang += val; - while (ang >= 360) - ang -= 360; - while (ang < 0) - ang += 360; - return ang; -} - -template -inline const T Distance(const T& x1, const T& y1, const T& x2, const T& y2) { - return sqrt(pow2(x1-x2) + pow2(y1-y2)); } - -template -inline const T Distance(const Point_& p1, const Point_& p2) { - return Distance(p1.x, p1.y, p2.x, p2.y); } - -template -inline const T Distance(const T& x1, const T& y1, const T& z1, const T& x2, const T& y2, const T& z2) { - return sqrt(pow2(x1-x2) + pow2(y1-y2) + pow2(z1-z2)); } - -template -inline const double Angle(const T& x1, const T& y1, const T& x2, const T& y2) { - return atan2(y2-y2, x2-x1); -} - -template -inline const double Angle(const Point_& p1, const Point_& p2) { - return Angle(p1.x, p1.y, p2.x, p2.y); -} - -// 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() || pos == -1) - 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));}; - Upp::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); -String WideToString(LPCWSTR wcs, int len = -1); -#endif - -#ifdef CTRLLIB_H - #include "Functions4U/Functions4U_Gui.h" -#endif - -String GetExtExecutable(String ext); - -Upp::Array GetDriveList(); - -// Replaced by GetCurrentDirectory() and SetCurrentDirectory() -// String Getcwd(); -// bool Chdir (const String &folder); - -//String Format(Time time, const char*fmt = "%2d:%2d"); - -class Dl { -public: - Dl(); - ~Dl(); - bool Load(const String &fileDll); - void *GetFunction(const String &functionName); - -private: -#if defined(PLATFORM_WIN32) - HINSTANCE hinstLib; -#else - void *hinstLib; -#endif -}; - -typedef Dl Dll; - -//bool RunFromMemory(const String &progBuffer, const String &name); - - -String BsGetLastError(); -bool BSPatch(String oldfile, String newfile, String patchfile); -bool BSDiff(String oldfile, String newfile, String patchfile); - - -template -Rect_ FitInFrame(const Size_ &frame, const Size_ &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)x, 0, (T)(x + objectAspect*frame.cy), frame.cy); - } else { - double y = (frame.cy - frame.cx/objectAspect)/2.; - return Rect_(0, (T)y, frame.cx, (T)(y + frame.cx/objectAspect)); - } -} - -Color RandomColor(); - -Image GetRect(const Image& orig, const Rect &r); - -class _NRFuse { -public: - _NRFuse(bool *_inside) {inside = _inside; failed = true;} - ~_NRFuse() {if (!failed) *inside = false;} - bool failed; -private: - bool *inside; -}; - -#define NON_REENTRANT_V static bool _insideNR; _NRFuse _fuseNR(&_insideNR); \ - if(!_insideNR) { \ - _insideNR = true; \ - _fuseNR.failed = false; \ - } else \ - return -#define NON_REENTRANT(v) static bool _insideNR; _NRFuse _fuseNR(&_insideNR); \ - if(!_insideNR) { \ - _insideNR = true; \ - _fuseNR.failed = false; \ - } else \ - return v - -END_UPP_NAMESPACE - -#endif +#ifndef _Functions4U_Functions4U_h +#define _Functions4U_Functions4U_h + +#include +#include +#ifdef flagGUI +#include +#include "GatherTpp.h" +#endif + +#include +#include "StaticPlugin.h" + +NAMESPACE_UPP + + +enum EXT_FILE_FLAGS {USE_TRASH_BIN = 1, + BROWSE_LINKS = 2, + DELETE_READ_ONLY = 4, + ASK_BEFORE_DELETE = 8 +}; + +bool LaunchFile(const char *file); + +bool FileCat(const char *file, const char *appendFile); + +int FileCompare(const char *path1, const char *path2); + +Upp::int64 FindStringInFile(const char *file, const String text, Upp::int64 pos0 = 0); + +bool FileStrAppend(const char *file, const char *str); +bool AppendFile(const char *filename, const char *str); + +String AppendFileName(const String& path1, const char *path2, const char *path3); + +inline String Trim(const String& s) {return TrimBoth(s);}; + +String FitFileName(String fileName, int len); + +String Tokenize(const String &str, const String &token, int &pos); + +///////// +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); +/////////////////////////////// + +bool UpperFolder(const char *folderName); +String GetUpperFolder(const String &folderName); +String GetNextFolder(const String &folder, const String &lastFolder); +String FileRealName(const char *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 SetReadOnly(const char *path, bool readOnly); +bool ReadOnly(const char *path, bool readOnly) +#if defined(__MINGW32__) + __attribute__ ((deprecated)); +#else + ; +#endif + +bool SetReadOnly(const char *path, bool usr, bool grp, bool oth); +bool ReadOnly(const char *path, bool usr, bool grp, bool oth) +#if defined(__MINGW32__) + __attribute__ ((deprecated)); +#else + ; +#endif +bool IsReadOnly(const char *path, bool &usr, bool &grp, bool &oth); + +String LoadFile_Safe(String fileName); + +Upp::int64 GetLength(const char *fileDirName); +Upp::int64 GetDirectoryLength(const char *directoryName); + +/////////////////////////////// +Upp::Array SearchFile(String dir, const Upp::Array &condFiles, const Upp::Array &condFolders, + const Upp::Array &extFiles, const Upp::Array &extFolders, + const String &text, Upp::Array &errorList); +Upp::Array SearchFile(String dir, String condFile, String text, Upp::Array &errorList);//, int flags = 0); +Upp::Array SearchFile(String dir, String condFile, String text = "");//, int flags = 0); +/////////////////////////////// + +bool FileToTrashBin(const char *path); +Upp::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 { + bool isFolder; + String fileName; + String relFilename; + Upp::int64 length; + struct Upp::Time t; + Upp::int64 id; + + String ToString() const { return Format("%s %0n", fileName, length); } + + FileData(bool isFolder, String fileName, String relFilename, Upp::int64 length, + struct Upp::Time t, Upp::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; + Upp::uint64 idMaster, idSecondary; + struct Upp::Time tMaster, tSecondary; + Upp::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;}; + Upp::int64 GetSize() {return fileSize;}; + inline bool UseId() {return useId;}; + void SortByName(bool ascending = true); + void SortByDate(bool ascending = true); + void SortBySize(bool ascending = true); + Upp::Array &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); + Upp::int64 GetFileId(String fileName); + String GetRelativePath(const String &fullPath); + String GetFileText(); + + Upp::Array fileList; + Upp::Array errorList; + String basePath; + long fileCount, folderCount; + Upp::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, const String folderFrom, + Upp::Array &excepFolders, Upp::Array &excepFiles, int sensSecs = 0); + 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: + Upp::Array diffList; +}; + +String Replace(String str, String find, String replace); +String Replace(String str, char find, char 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); Now included in TimeDate +Time StrToTime(const char *s); +Date StrToDate(const char *s); + +String BytesToString(Upp::uint64 bytes, bool units = true); + +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 FormatDoubleAdjust(double d, double range); + +String RemoveAccents(String str); +String RemoveAccent(wchar c); +bool IsPunctuation(wchar c); + +inline double ToRad(double angle) {return angle*M_PI/180.;} +inline double ToDeg(double angle) {return angle*180./M_PI;} + +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 +inline int Sign(T a) {return (a > 0) - (a < 0);} +template +inline T Average(T a, T b) {return T(a+b)/2;} +template +inline T Average(T a, T b, T c) {return T(a+b+c)/3;} +template +inline T Average(T a, T b, T c, T d){return T(a+b+c+d)/4;} +template +inline T pow2(T a) {return (a*a);} +template +inline T pow3(T a) {return (a*a*a);} +template +inline T pow4(T a) {return pow2(pow2(a));} +template +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 +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 +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 +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; +} + +template +inline bool Between(const T& val, const T& min, const T& max) { + return val >= min && val <= max; +} + +template +T AngleAdd360(T ang, T val) { + ang += val; + while (ang >= 360) + ang -= 360; + while (ang < 0) + ang += 360; + return ang; +} + +template +inline const T Distance(const T& x1, const T& y1, const T& x2, const T& y2) { + return sqrt(pow2(x1-x2) + pow2(y1-y2)); } + +template +inline const T Distance(const Point_& p1, const Point_& p2) { + return Distance(p1.x, p1.y, p2.x, p2.y); } + +template +inline const T Distance(const T& x1, const T& y1, const T& z1, const T& x2, const T& y2, const T& z2) { + return sqrt(pow2(x1-x2) + pow2(y1-y2) + pow2(z1-z2)); } + +template +inline const double Angle(const T& x1, const T& y1, const T& x2, const T& y2) { + return atan2(y2-y2, x2-x1); +} + +template +inline const double Angle(const Point_& p1, const Point_& p2) { + return Angle(p1.x, p1.y, p2.x, p2.y); +} + +// 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() || pos == -1) + 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));}; + Upp::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); +String WideToString(LPCWSTR wcs, int len = -1); +#endif + +#ifdef CTRLLIB_H + #include "Functions4U/Functions4U_Gui.h" +#endif + +String GetExtExecutable(String ext); + +Upp::Array GetDriveList(); + +// Replaced by GetCurrentDirectory() and SetCurrentDirectory() +// String Getcwd(); +// bool Chdir (const String &folder); + +//String Format(Time time, const char*fmt = "%2d:%2d"); + +class Dl { +public: + Dl(); + ~Dl(); + bool Load(const String &fileDll); + void *GetFunction(const String &functionName); + +private: +#if defined(PLATFORM_WIN32) + HINSTANCE hinstLib; +#else + void *hinstLib; +#endif +}; + +typedef Dl Dll; + +//bool RunFromMemory(const String &progBuffer, const String &name); + + +String BsGetLastError(); +bool BSPatch(String oldfile, String newfile, String patchfile); +bool BSDiff(String oldfile, String newfile, String patchfile); + + +template +Rect_ FitInFrame(const Size_ &frame, const Size_ &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)x, 0, (T)(x + objectAspect*frame.cy), frame.cy); + } else { + double y = (frame.cy - frame.cx/objectAspect)/2.; + return Rect_(0, (T)y, frame.cx, (T)(y + frame.cx/objectAspect)); + } +} + +Color RandomColor(); + +Image GetRect(const Image& orig, const Rect &r); + +class _NRFuse { +public: + _NRFuse(bool *_inside) {inside = _inside; failed = true;} + ~_NRFuse() {if (!failed) *inside = false;} + bool failed; +private: + bool *inside; +}; + +#define NON_REENTRANT_V static bool _insideNR; _NRFuse _fuseNR(&_insideNR); \ + if(!_insideNR) { \ + _insideNR = true; \ + _fuseNR.failed = false; \ + } else \ + return +#define NON_REENTRANT(v) static bool _insideNR; _NRFuse _fuseNR(&_insideNR); \ + if(!_insideNR) { \ + _insideNR = true; \ + _fuseNR.failed = false; \ + } else \ + return v + +END_UPP_NAMESPACE + +#endif diff --git a/bazaar/Functions4U/Functions4U.upp b/bazaar/Functions4U/Functions4U.upp index 92bc4ebef..408629284 100644 --- a/bazaar/Functions4U/Functions4U.upp +++ b/bazaar/Functions4U/Functions4U.upp @@ -3,7 +3,6 @@ description "Functions and classes for general use\377B255,170,0"; uses Core, plugin\bz2, - Web, Draw, plugin\jpg, plugin\gif, diff --git a/bazaar/Functions4U/GatherTpp.cpp b/bazaar/Functions4U/GatherTpp.cpp index df66b7e69..8e4036f06 100644 --- a/bazaar/Functions4U/GatherTpp.cpp +++ b/bazaar/Functions4U/GatherTpp.cpp @@ -1,345 +1,381 @@ -#ifdef flagGUI -#include -#include - -#include "Functions4U/Functions4U.h" -#include "GatherTpp.h" - -NAMESPACE_UPP - -struct ScanTopicIterator : RichText::Iterator { - VectorMap *reflink; - String link; - StaticCriticalSection reflink_lock; - - ScanTopicIterator(VectorMap *reflink) : reflink(reflink) {}; - virtual bool operator()(int pos, const RichPara& para) - { - if(!IsNull(para.format.label)) - reflink->Add(para.format.label, link); - return false; - } -}; - -void GatherTpp::GatherRefLinks(const char *upp) -{ - for(FindFile pff(AppendFileName(upp, "*.*")); pff; pff.Next()) { - if(pff.IsFolder()) { - String package = pff.GetName(); - String pdir = AppendFileName(upp, package); - TopicLink tl; - tl.package = package; - for(FindFile ff(AppendFileName(pdir, "*.tpp")); ff; ff.Next()) { - if(ff.IsFolder()) { - String group = GetFileTitle(ff.GetName() ); - tl.group = group; - String dir = AppendFileName(pdir, ff.GetName()); - for(FindFile ft(AppendFileName(dir, "*.tpp")); ft; ft.Next()) { - if(ft.IsFile()) { - String path = AppendFileName(dir, ft.GetName()); - tl.topic = GetFileTitle(ft.GetName()); - String link = TopicLinkString(tl); - ScanTopicIterator sti(&reflink); - sti.link = link; - ParseQTF(ReadTopic(LoadFile(path))).Iterate(sti); - } - } - } - } - } - } -} - -struct GatherLinkIterator : RichText::Iterator { - VectorMap *reflink; - Index link; - - GatherLinkIterator(VectorMap *reflink) : reflink(reflink) {}; - virtual bool operator()(int pos, const RichPara& para) - { - for(int i = 0; i < para.GetCount(); i++) { - String l = para[i].format.link; - if(!IsNull(l)) { - if(l[0] == ':') { - int q = reflink->Find(l); - int w = q; - if(q < 0) - q = reflink->Find(l + "::class"); - if(q < 0) - q = reflink->Find(l + "::struct"); - if(q < 0) - q = reflink->Find(l + "::union"); - if(q >= 0) - l = (*reflink)[q]; - } - link.FindAdd(Nvl(reflink->Get(l, Null), l)); - } - } - return false; - } -}; - -String GetIndexTopic(String file) -{ - String topic = GetFileTitle(file); - String folder = GetFileFolder(file); - String topicLocation = GetFileTitle(folder); - folder = GetUpperFolder(folder); - topicLocation = GetFileTitle(folder) + "/" + topicLocation; - - return "topic://" + topicLocation + "/" + topic; -} - -int CharFilterLbl(int c) -{ - return IsAlNum(c) ? c : '.'; -} - -void QtfAsPdf(PdfDraw &pdf, const char *qtf) -{ - RichText txt = ParseQTF(qtf); - Size page = Size(3968, 6074); - UPP::Print(pdf, txt, page); -} - -Htmls RoundFrame(Htmls data, String border, Color bg) -{ - return HtmlPackedTable().BgColor(bg).Width(-100) - .Attr("style", "border-style: solid; border-width: 1px; border-color: #" + border + ";") - / HtmlLine() / data; -} - -bool ContainsAt(const String &source, const String &pattern, int pos) -{ - return pos >= 0 - && pos + pattern.GetLength() <= source.GetLength() - && 0 == memcmp(source.Begin() + pos, pattern.Begin(), pattern.GetLength()); -} - -bool StartsWith(const String &source, const String &pattern) -{ - return ContainsAt(source, pattern, 0); -} - -bool EndsWith(const String &source, const String &pattern) -{ - return ContainsAt(source, pattern, source.GetLength() - pattern.GetLength()); -} - -String GatherTpp::QtfAsHtml(const char *qtf, Index& css, - const VectorMap& links, - const VectorMap& labels, - const String& outdir, const String& fn) -{ - return EncodeHtml(ParseQTF(qtf), css, links, labels, outdir, fn, Zoom(8, 40), escape, 40); -} - -String GetText(const char *s) -{ - return GetTopic(s).text; -} - -void GatherTpp::ExportPage(int i, String htmlFolder, String keywords) -{ - Index css; - String path = links.GetKey(i); - - String text = GetText(path); - int h; - h = ParseQTF(tt[i].text).GetHeight(1000); - - String qtflangs; - String strlang; - - String page = tt[i]; - page = QtfAsHtml(page, css, links, labels, htmlFolder, links[i]); - - Color paper = SWhite; - Color bg = Color(210, 217, 210); - - Htmls html; - html << - HtmlPackedTable().Width(-100) / - HtmlLine().ColSpan(3) + - HtmlPackedTable().Width(-100) / ( - HtmlLine().ColSpan(3).BgColor(bg).Height(6) / "" + - HtmlRow() / ( - HtmlTCell().Width(-100).BgColor(bg) / ( - RoundFrame(page , "6E89AE;padding: 10px;", White) - ) - ) - ); - - String topicTitle = tt.GetKey(i); - String pageTitle = tt[i].title; - if(IsNull(pageTitle)) - pageTitle = title; -/* - if(StartsWith(topicTitle, "examples$")) - pageTitle = "Demos / " + pageTitle; - else if(StartsWith(topicTitle, "reference$")) - pageTitle = "Examples / " + pageTitle; -*/ - if(pageTitle != title) - pageTitle << " :: " << title; - - Htmls content = - "\n" + - HtmlHeader(pageTitle, AsCss(css) + - "a.l1 { text-decoration:none; font-size: 8pt; font-family: sans-serif; " - "font-weight: normal; }\n" - "a.l1:link { color:#000000; }\n" - "a.l1:visited { color:#000080; }\n" - "a.l1:hover { color:#9933CC; }\n" - "a.l1:active { color:#000000; }\n" - "a.l2 { text-decoration:none; font-size: 12pt; font-family: sans-serif; " - "font-variant: small-caps; }\n" - "a.l2:link { color:#0066FF; }\n" - "a.l2:visited { color:#FF6600; }\n" - "a.l2:hover { color:#BC0624; }\n" - "a.l2:active { color:#BC0024; }\n", - "" - "" - ) - .BgColor(bg) - .Alink(Red).Link(Black).Vlink(Blue) - / html; - SaveFile(AppendFileName(htmlFolder, links[i]), content); -} - -String GatherTpp::TopicFileName(const char *topic) -{ - TopicLink tl = ParseTopicLink(topic); - String file = AppendFileName(dir, AppendFileName(tl.group + ".tpp", tl.topic + ".tpp")); - if (FileExists(file)) - return file; - - for (int i = 0; i < rootFolders.GetCount(); ++i) { - if (rootFolders[i] != dir) { - file = AppendFileName(rootFolders[i], AppendFileName(tl.package , AppendFileName(tl.group + ".tpp", tl.topic + ".tpp"))); - if (FileExists(file)) - return file; - } - } - return ""; -} - -String TopicFileNameHtml(const char *topic) -{ - TopicLink tl = ParseTopicLink(topic); - return tl.group + "$" + tl.package+ "$" + tl.topic + ".html"; -} - -String GatherTpp::GatherTopics(const char *topic, String& title) -{ - int q = tt.Find(topic); - if(q < 0) { - Topic p = ReadTopic(LoadFile(TopicFileName(topic))); - title = p.title; - String t = p; - if(IsNull(t)) - return "index.html"; - tt.Add(topic) = p; - GatherLinkIterator ti(&(reflink)); - ParseQTF(t).Iterate(ti); - for(int i = 0; i < ti.link.GetCount(); i++) { - String dummy; - GatherTopics(ti.link[i], dummy); - } - } else - title = tt[q].title; - return TopicFileNameHtml(topic); -} - - -String GatherTpp::GatherTopics(const char *topic) -{ - String dummy; - return GatherTopics(topic, dummy); -} - -bool GatherTpp::Load(String indexFile, Gate2 progress) { - indexTopic = GetIndexTopic(indexFile); - for (int i = 0; i < rootFolders.GetCount(); ++i) { - if (progress(i+1, rootFolders.GetCount())) - return false; - dir = rootFolders[i]; - - if (!DirectoryExists(dir)) - return false; - - GatherRefLinks(dir); - - if (i == 0) - GatherTopics(indexTopic); - } - return true; -} - -bool GatherTpp::MakeHtml(const char *folder, Gate2 progress) { - DeleteFolderDeep(folder); - DirectoryCreate(folder); - - for(int i = 0; i < tt.GetCount(); i++) { - String topic = tt.GetKey(i); - links.Add(topic, topic == indexTopic ? "index.html" : - memcmp(topic, "topic://", 8) ? topic : TopicFileNameHtml(topic)); - } - for(int i = 0; i < reflink.GetCount(); i++) { - String l = reflink.GetKey(i); - String lbl = Filter(l, CharFilterLbl); - String f = links.Get(reflink[i], Null) + '#' + lbl; - links.Add(l, f); - static const char *x[] = { "::struct", "::class", "::union" }; - for(int ii = 0; ii < 3; ii++) { - String e = x[ii]; - if(EndsWith(l, e)) { - links.Add(l.Mid(0, l.GetLength() - e.GetLength()), f); - } - } - labels.Add(l, lbl); - } - - for(int i = 0; i < tt.GetCount(); i++) { - if (progress(i+1, tt.GetCount())) - return false; - ExportPage(i, folder); - } - return true; -} - -bool GatherTpp::MakePdf(const char *filename, Gate2 progress) { - PdfDraw pdf; - for(int i = 0; i < tt.GetCount(); i++) { - if (progress(i+1, tt.GetCount())) - return false; - bool dopdf = true; - for (int j = 0; j < i; ++j) { - if (tt[j].text == tt[i].text) { - dopdf = false; - break; - } - } - if (dopdf) - QtfAsPdf(pdf, tt[i]); - } - SaveFile(filename, pdf.Finish()); - return true; -} - -int GatherTpp::FindTopic(const String name) { - return tt.Find(name); -} - -Topic &GatherTpp::GetTopic(int id) { - return tt[id]; -} - -Topic &GatherTpp::AddTopic(const String name) { - return tt.Add(name); -} - -END_UPP_NAMESPACE - +#ifdef flagGUI +#include +#include + +#include "Functions4U/Functions4U.h" +#include "GatherTpp.h" + +NAMESPACE_UPP + +struct ScanTopicIterator : RichText::Iterator { + VectorMap *reflink; + String link; + StaticCriticalSection reflink_lock; + + ScanTopicIterator(VectorMap *reflink) : reflink(reflink) {}; + virtual bool operator()(int pos, const RichPara& para) + { + if(!IsNull(para.format.label)) + reflink->Add(para.format.label, link); + return false; + } +}; + +void GatherTpp::GatherRefLinks(const char *upp) +{ + for(FindFile pff(AppendFileName(upp, "*.*")); pff; pff.Next()) { + if(pff.IsFolder()) { + String package = pff.GetName(); + String pdir = AppendFileName(upp, package); + TopicLink tl; + tl.package = package; + for(FindFile ff(AppendFileName(pdir, "*.tpp")); ff; ff.Next()) { + if(ff.IsFolder()) { + String group = GetFileTitle(ff.GetName() ); + tl.group = group; + String dir = AppendFileName(pdir, ff.GetName()); + for(FindFile ft(AppendFileName(dir, "*.tpp")); ft; ft.Next()) { + if(ft.IsFile()) { + String path = AppendFileName(dir, ft.GetName()); + tl.topic = GetFileTitle(ft.GetName()); + String link = TopicLinkString(tl); + ScanTopicIterator sti(&reflink); + sti.link = link; + ParseQTF(ReadTopic(LoadFile(path))).Iterate(sti); + } + } + } + } + } + } +} + +struct GatherLinkIterator : RichText::Iterator { + VectorMap *reflink; + Index link; + + GatherLinkIterator(VectorMap *reflink) : reflink(reflink) {}; + virtual bool operator()(int pos, const RichPara& para) + { + for(int i = 0; i < para.GetCount(); i++) { + String l = para[i].format.link; + if(!IsNull(l)) { + if(l[0] == ':') { + int q = reflink->Find(l); + if(q < 0) + q = reflink->Find(l + "::class"); + if(q < 0) + q = reflink->Find(l + "::struct"); + if(q < 0) + q = reflink->Find(l + "::union"); + if(q >= 0) + l = (*reflink)[q]; + } + link.FindAdd(Nvl(reflink->Get(l, Null), l)); + } + } + return false; + } +}; + +String GetIndexTopic(String file) +{ + String topic = GetFileTitle(file); + String folder = GetFileFolder(file); + String topicLocation = GetFileTitle(folder); + folder = GetUpperFolder(folder); + topicLocation = GetFileTitle(folder) + "/" + topicLocation; + + return "topic://" + topicLocation + "/" + topic; +} + +int CharFilterLbl(int c) +{ + return IsAlNum(c) ? c : '.'; +} + +void QtfAsPdf(PdfDraw &pdf, const char *qtf) +{ + RichText txt = ParseQTF(qtf); + Size page = Size(3968, 6074); + UPP::Print(pdf, txt, page); +} + +/* +Htmls RoundFrame(Htmls data, String border, Color bg) +{ + return HtmlPackedTable().BgColor(bg).Width(-100) + .Attr("style", "border-style: solid; border-width: 1px; border-color: #" + border + ";") + / HtmlLine() / data; +} +*/ + +bool ContainsAt(const String &source, const String &pattern, int pos) +{ + return pos >= 0 + && pos + pattern.GetLength() <= source.GetLength() + && 0 == memcmp(source.Begin() + pos, pattern.Begin(), pattern.GetLength()); +} + +bool StartsWith(const String &source, const String &pattern) +{ + return ContainsAt(source, pattern, 0); +} + +bool EndsWith(const String &source, const String &pattern) +{ + return ContainsAt(source, pattern, source.GetLength() - pattern.GetLength()); +} + +String GatherTpp::QtfAsHtml(const char *qtf, Index& css, + const VectorMap& links, + const VectorMap& labels, + const String& outdir, const String& fn) +{ + return EncodeHtml(ParseQTF(qtf), css, links, labels, outdir, fn, Zoom(8, 40), escape, 40); +} + +String GetText(const char *s) +{ + return GetTopic(s).text; +} + +void GatherTpp::ExportPage(int i, String htmlFolder, String keywords) +{ + Index css; + String path = links.GetKey(i); + + String text = GetText(path); + + String qtflangs; + String strlang; + + String page = tt[i]; + page = QtfAsHtml(page, css, links, labels, htmlFolder, links[i]); + +/* + Color paper = SWhite; + Color bg = Color(210, 217, 210); + + Htmls html; + html << + HtmlPackedTable().Width(-100) / + HtmlLine().ColSpan(3) + + HtmlPackedTable().Width(-100) / ( + HtmlLine().ColSpan(3).BgColor(bg).Height(6) / "" + + HtmlRow() / ( + HtmlTCell().Width(-100).BgColor(bg) / ( + RoundFrame(page , "6E89AE;padding: 10px;", White) + ) + ) + ); +*/ + String topicTitle = tt.GetKey(i); + String pageTitle = tt[i].title; + if(IsNull(pageTitle)) + pageTitle = title; +/* + if(StartsWith(topicTitle, "examples$")) + pageTitle = "Demos / " + pageTitle; + else if(StartsWith(topicTitle, "reference$")) + pageTitle = "Examples / " + pageTitle; +*/ + if(pageTitle != title) + pageTitle << " :: " << title; +/* + Htmls content = + "\n" + + HtmlHeader(pageTitle, AsCss(css) + + "a.l1 { text-decoration:none; font-size: 8pt; font-family: sans-serif; " + "font-weight: normal; }\n" + "a.l1:link { color:#000000; }\n" + "a.l1:visited { color:#000080; }\n" + "a.l1:hover { color:#9933CC; }\n" + "a.l1:active { color:#000000; }\n" + "a.l2 { text-decoration:none; font-size: 12pt; font-family: sans-serif; " + "font-variant: small-caps; }\n" + "a.l2:link { color:#0066FF; }\n" + "a.l2:visited { color:#FF6600; }\n" + "a.l2:hover { color:#BC0624; }\n" + "a.l2:active { color:#BC0024; }\n", + "" + "" + ) + .BgColor(bg) + .Alink(Red).Link(Black).Vlink(Blue) + / html; +*/ + + SaveFile(AppendFileName(htmlFolder, links[i]), + String().Cat() << + "" + "" + "" + "" + "" + "" << pageTitle << "" + "" + "" + "" + "
" + "" + "" + "" + "" + "
" + "" + "" + "
" << page << "
" + "
" + "" + ); +} + +String GatherTpp::TopicFileName(const char *topic) +{ + TopicLink tl = ParseTopicLink(topic); + String file = AppendFileName(dir, AppendFileName(tl.group + ".tpp", tl.topic + ".tpp")); + if (FileExists(file)) + return file; + + for (int i = 0; i < rootFolders.GetCount(); ++i) { + if (rootFolders[i] != dir) { + file = AppendFileName(rootFolders[i], AppendFileName(tl.package , AppendFileName(tl.group + ".tpp", tl.topic + ".tpp"))); + if (FileExists(file)) + return file; + } + } + return ""; +} + +String TopicFileNameHtml(const char *topic) +{ + TopicLink tl = ParseTopicLink(topic); + return tl.group + "$" + tl.package+ "$" + tl.topic + ".html"; +} + +String GatherTpp::GatherTopics(const char *topic, String& title) +{ + int q = tt.Find(topic); + if(q < 0) { + Topic p = ReadTopic(LoadFile(TopicFileName(topic))); + title = p.title; + String t = p; + if(IsNull(t)) + return "index.html"; + tt.Add(topic) = p; + GatherLinkIterator ti(&(reflink)); + ParseQTF(t).Iterate(ti); + for(int i = 0; i < ti.link.GetCount(); i++) { + String dummy; + GatherTopics(ti.link[i], dummy); + } + } else + title = tt[q].title; + return TopicFileNameHtml(topic); +} + + +String GatherTpp::GatherTopics(const char *topic) +{ + String dummy; + return GatherTopics(topic, dummy); +} + +bool GatherTpp::Load(String indexFile, Gate2 progress) { + indexTopic = GetIndexTopic(indexFile); + for (int i = 0; i < rootFolders.GetCount(); ++i) { + if (progress(i+1, rootFolders.GetCount())) + return false; + dir = rootFolders[i]; + + if (!DirectoryExists(dir)) + return false; + + GatherRefLinks(dir); + + if (i == 0) + GatherTopics(indexTopic); + } + return true; +} + +bool GatherTpp::MakeHtml(const char *folder, Gate2 progress) { + DeleteFolderDeep(folder); + DirectoryCreate(folder); + + for(int i = 0; i < tt.GetCount(); i++) { + String topic = tt.GetKey(i); + links.Add(topic, topic == indexTopic ? "index.html" : + memcmp(topic, "topic://", 8) ? topic : TopicFileNameHtml(topic)); + } + for(int i = 0; i < reflink.GetCount(); i++) { + String l = reflink.GetKey(i); + String lbl = Filter(l, CharFilterLbl); + String f = links.Get(reflink[i], Null) + '#' + lbl; + links.Add(l, f); + static const char *x[] = { "::struct", "::class", "::union" }; + for(int ii = 0; ii < 3; ii++) { + String e = x[ii]; + if(EndsWith(l, e)) { + links.Add(l.Mid(0, l.GetLength() - e.GetLength()), f); + } + } + labels.Add(l, lbl); + } + + for(int i = 0; i < tt.GetCount(); i++) { + if (progress(i+1, tt.GetCount())) + return false; + ExportPage(i, folder); + } + return true; +} + +bool GatherTpp::MakePdf(const char *filename, Gate2 progress) { + PdfDraw pdf; + for(int i = 0; i < tt.GetCount(); i++) { + if (progress(i+1, tt.GetCount())) + return false; + bool dopdf = true; + for (int j = 0; j < i; ++j) { + if (tt[j].text == tt[i].text) { + dopdf = false; + break; + } + } + if (dopdf) + QtfAsPdf(pdf, tt[i]); + } + SaveFile(filename, pdf.Finish()); + return true; +} + +int GatherTpp::FindTopic(const String name) { + return tt.Find(name); +} + +Topic &GatherTpp::GetTopic(int id) { + return tt[id]; +} + +Topic &GatherTpp::AddTopic(const String name) { + return tt.Add(name); +} + +END_UPP_NAMESPACE + #endif \ No newline at end of file diff --git a/bazaar/Functions4U/GatherTpp.h b/bazaar/Functions4U/GatherTpp.h index b9bdb2396..a2e7604e4 100644 --- a/bazaar/Functions4U/GatherTpp.h +++ b/bazaar/Functions4U/GatherTpp.h @@ -1,45 +1,44 @@ -#ifndef _GatherTpp_h_ -#define _GatherTpp_h_ - -NAMESPACE_UPP - -class GatherTpp { -public: - //GatherTpp(); - void AddFolder(const char *folder) {rootFolders.Add(folder);}; - bool Load(String indexFile, Gate2 progress = false); - - int FindTopic(const String name); - Topic &GetTopic(int id); - Topic &AddTopic(const String name); - String GatherTopics(const char *topic, String& title); - - bool MakeHtml(const char *folder, Gate2 progress = false); - bool MakePdf(const char *filename, Gate2 progress = false); - - -private: - Upp::Array rootFolders; - String dir; - VectorMap escape; - VectorMap links; - VectorMap labels; - VectorMap reflink; - VectorMap tt; - - Htmls header; - String keywords; // - String title; // - String indexTopic; - - String TopicFileName(const char *topic); - String GatherTopics(const char *topic); - void GatherRefLinks(const char *upp); - void ExportPage(int i, String htmlFolder, String keywords = ""); - String QtfAsHtml(const char *qtf, Index& css, const VectorMap& links, - const VectorMap& labels, const String& outdir, const String& fn = Null); -}; - -END_UPP_NAMESPACE - -#endif +#ifndef _GatherTpp_h_ +#define _GatherTpp_h_ + +NAMESPACE_UPP + +class GatherTpp { +public: + //GatherTpp(); + void AddFolder(const char *folder) {rootFolders.Add(folder);}; + bool Load(String indexFile, Gate2 progress = false); + + int FindTopic(const String name); + Topic &GetTopic(int id); + Topic &AddTopic(const String name); + String GatherTopics(const char *topic, String& title); + + bool MakeHtml(const char *folder, Gate2 progress = false); + bool MakePdf(const char *filename, Gate2 progress = false); + + +private: + Upp::Array rootFolders; + String dir; + VectorMap escape; + VectorMap links; + VectorMap labels; + VectorMap reflink; + VectorMap tt; + + String keywords; // + String title; // + String indexTopic; + + String TopicFileName(const char *topic); + String GatherTopics(const char *topic); + void GatherRefLinks(const char *upp); + void ExportPage(int i, String htmlFolder, String keywords = ""); + String QtfAsHtml(const char *qtf, Index& css, const VectorMap& links, + const VectorMap& labels, const String& outdir, const String& fn = Null); +}; + +END_UPP_NAMESPACE + +#endif diff --git a/bazaar/Functions4U/init b/bazaar/Functions4U/init index 2e2faf7f7..61f86b0ee 100644 --- a/bazaar/Functions4U/init +++ b/bazaar/Functions4U/init @@ -2,7 +2,6 @@ #define _Functions4U_icpp_init_stub #include "Core/init" #include "plugin\bz2/init" -#include "Web/init" #include "Draw/init" #include "plugin\jpg/init" #include "plugin\gif/init"