Functions4U: Updated namespaces and time callbacks

git-svn-id: svn://ultimatepp.org/upp/trunk@11810 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
koldo 2018-03-02 13:47:43 +00:00
parent 4f506897a0
commit 4e05e6b41d
19 changed files with 245 additions and 235 deletions

View file

@ -2,7 +2,8 @@
#include "../bsdiff.h"
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
String errMsg;
@ -15,4 +16,3 @@ bool Err(String str) {
return false;
}
END_UPP_NAMESPACE

View file

@ -35,7 +35,8 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05
#include "../bsdiff.h"
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
#define bsmin(x,y) (((x)<(y)) ? (x) : (y))
@ -434,4 +435,3 @@ bool BSDiff(String oldfile, String newfile, String patchfile)
return 1;
}
END_UPP_NAMESPACE

View file

@ -35,7 +35,8 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:
#include "../bsdiff.h"
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
static off_t offtin(u_char *buf)
{
@ -247,4 +248,3 @@ bool BSPatch(String oldfile, String newfile, String patchfile)
return 1;
}
END_UPP_NAMESPACE

View file

@ -1,7 +1,9 @@
#include <Core/Core.h>
#include "Functions4U.h"
using namespace Upp;
#ifdef PLATFORM_WIN32 // || defined (PLATFORM_WIN64)
#define Ptr Ptr_
#define byte byte_
@ -19,7 +21,7 @@
#undef CY
#endif
NAMESPACE_UPP
//NAMESPACE_UPP
#define TFILE <Functions4U/Functions4U.t>
#include <Core/t.h>
@ -2120,7 +2122,7 @@ bool FileDataArray::SaveFile(const char *fileName) {
}
bool FileDataArray::AppendFile(const char *fileName) {
return Upp::AppendFile(fileName, GetFileText());
return ::AppendFile(fileName, GetFileText());
}
bool FileDataArray::LoadFile(const char *fileName)
@ -2652,7 +2654,6 @@ int SysX(const char *cmd, String& out, String& err, double timeOut,
return IsNull(ret) ? 0 : ret;
}
END_UPP_NAMESPACE
// Dummy functions added after TheIDE change
Upp::String GetCurrentMainPackage() {return "dummy";}

View file

@ -4,7 +4,7 @@
#include <float.h>
#include <Draw/Draw.h>
#ifdef flagGUI
///#include <ide/Browser/Browser.h>
//#include <CtrlLib/CtrlLib.h>
#include <Web/Web.h>
#include "GatherTpp.h"
#endif
@ -14,7 +14,8 @@
#include "LocalProcess2.h"
#include <random>
NAMESPACE_UPP
using namespace Upp;
enum EXT_FILE_FLAGS {NO_FLAG = 0,
USE_TRASH_BIN = 1,
@ -205,8 +206,7 @@ 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);
Upp::Time StrToTime(const char *s);
Date StrToDate(const char *s);
String BytesToString(uint64 bytes, bool units = true);
@ -498,9 +498,9 @@ Value GetVARIANT(VARIANT &result);
String WideToString(LPCWSTR wcs, int len = -1);
#endif
#ifdef CTRLLIB_H
#include "Functions4U/Functions4U_Gui.h"
#endif
//#ifdef CTRLLIB_H
// #include "Functions4U/Functions4U_Gui.h"
//#endif
String GetExtExecutable(const String ext);
@ -580,190 +580,6 @@ private:
return v
class RealTimeStop {
public:
RealTimeStop() {
/*#ifdef CTRLLIB_H
callbackOn = false;
lastTick = -1;
#endif */
Start();
}
void Reset() {
timeElapsed = lastTimeElapsed = 0;
/*#ifdef CTRLLIB_H
if (!callbackOn) {
SetTimeCallback(-5*1000, callback(this, &RealTimeStop::Tick), this);
callbackOn = true;
}
#endif*/
isPaused = true;
Continue();
}
void Start() {Reset();}
void Pause(bool pause) {
if (pause)
Pause();
else
Continue();
}
void Pause() {
if (!isPaused) {
timeElapsed += (tmGetTimeX() - time0);
isPaused = true;
}
}
void Continue() {
if (isPaused) {
time0 = tmGetTimeX();
isPaused = false;
}
}
double Seconds() {
if (isPaused)
return timeElapsed;
else
return timeElapsed + (tmGetTimeX() - time0);
}
double Elapsed() {
double t = Seconds();
double elapsed = t - lastTimeElapsed;
lastTimeElapsed = t;
return elapsed;
}
void SetBack(double secs) {
timeElapsed -= secs;
}
bool IsPaused() {return isPaused;}
private:
double timeElapsed; // Time elapsed
double time0; // Time of last Continue()
double lastTimeElapsed;
bool isPaused;
/*ifdef CTRLLIB_H
bool callbackOn;
double lastTick;
void Tick() {
double tActual = tmGetTimeX();
if (!isPaused && lastTick > -1) {
double deltaLastTick = tActual - lastTick;
if (deltaLastTick > 5*10) // Some external issue has stopped normal running
SetBack(deltaLastTick); // Timeout timer is fixed accordingly
}
lastTick = tActual;
}
#endif*/
};
class LocalProcessX {
public:
LocalProcessX() : status(STOP_OK), callbackOn(false) {}
~LocalProcessX() {Stop();}
enum ProcessStatus {RUNNING = 1, STOP_OK = 0, STOP_TIMEOUT = -1, STOP_USER = -2, STOP_NORESPONSE = -3};
bool Start(const char *cmd, const char *envptr = 0, const char *dir = 0, double refreshTime = -1,
double maxTimeWithoutOutput = -1, double maxRunTime = -1, bool convertcharset = true) {
status = STOP_OK;
p.ConvertCharset(convertcharset);
timeElapsed.Start();
timeWithoutOutput.Start();
if(!p.Start(cmd, envptr, dir))
return false;
status = RUNNING;
this->maxTimeWithoutOutput = maxTimeWithoutOutput;
this->maxRunTime = maxRunTime;
this->refreshTime = refreshTime;
#ifdef CTRLLIB_H
if (refreshTime > 0) {
if (!callbackOn) {
SetTimeCallback(-int(refreshTime*1000), callback(this, &LocalProcessX::Perform), this);
callbackOn = true;
}
}
#endif
return true;
}
void Perform() {
if (status <= 0)
return;
String out;
p.Read(out);
if(p.IsRunning()) {
#ifdef PLATFORM_WIN32
if (!p.IsPaused()) {
#endif
if (maxTimeWithoutOutput > 0 && timeWithoutOutput.Seconds() > maxTimeWithoutOutput)
status = STOP_NORESPONSE;
else if (maxRunTime > 0 && timeElapsed.Seconds() > maxRunTime)
status = STOP_TIMEOUT;
#ifdef PLATFORM_WIN32
}
#endif
} else
status = STOP_OK;
bool resetTimeout = false;
if (!out.IsEmpty())
resetTimeout = true;
if (!WhenTimer(timeElapsed.Seconds(), out, status <= 0, resetTimeout))
status = STOP_USER;
if (resetTimeout)
timeWithoutOutput.Reset();
if (status < 0)
p.Kill();
#ifdef CTRLLIB_H
if (callbackOn) {
KillTimeCallback(this);
callbackOn = false;
}
#endif
}
void Stop(ProcessStatus status = STOP_USER) {
if (!IsRunning())
return;
this->status = status;
p.Kill();
#ifdef CTRLLIB_H
if (callbackOn) {
KillTimeCallback(this);
callbackOn = false;
}
#endif
}
#ifdef PLATFORM_WIN32
void Pause() {
p.Pause();
if (p.IsRunning()) {
timeElapsed.Pause(p.IsPaused());
timeWithoutOutput.Pause(p.IsPaused());
}
}
bool IsPaused() {return p.IsPaused();}
double Seconds() {return timeElapsed.Seconds();}
#endif
void Write(String str) {p.Write(str);}
int GetStatus() {return status;}
bool IsRunning() {return status > 0;}
Gate4<double, String&, bool, bool&> WhenTimer;
#ifdef PLATFORM_WIN32
DWORD GetPid() {return p.GetPid();}
#endif
private:
LocalProcess2 p;
RealTimeStop timeElapsed, timeWithoutOutput;
ProcessStatus status;
double maxTimeWithoutOutput, maxRunTime;
double refreshTime;
double lastPerform, lastPause;
bool callbackOn;
};
template <class T>
class ThreadSafe {
@ -845,6 +661,204 @@ void Shuffle(C &data, int randomSeed = Null) {
ShuffleDescending(data, generator);
}
END_UPP_NAMESPACE
class RealTimeStop
#ifdef CTRLLIB_H
: public Ctrl
#endif
{
typedef RealTimeStop CLASSNAME;
public:
RealTimeStop() {
#ifdef CTRLLIB_H
callbackOn = false;
lastTick = -1;
#endif
Start();
}
void Reset() {
timeElapsed = lastTimeElapsed = 0;
#ifdef CTRLLIB_H
if (!callbackOn) {
timeCallback.Set(-5*1000, THISBACK(Tick));
callbackOn = true;
}
#endif
isPaused = true;
Continue();
}
void Start() {Reset();}
void Pause(bool pause) {
if (pause)
Pause();
else
Continue();
}
void Pause() {
if (!isPaused) {
timeElapsed += (tmGetTimeX() - time0);
isPaused = true;
}
}
void Continue() {
if (isPaused) {
time0 = tmGetTimeX();
isPaused = false;
}
}
double Seconds() {
if (isPaused)
return timeElapsed;
else
return timeElapsed + (tmGetTimeX() - time0);
}
double Elapsed() {
double t = Seconds();
double elapsed = t - lastTimeElapsed;
lastTimeElapsed = t;
return elapsed;
}
void SetBack(double secs) {
timeElapsed -= secs;
}
bool IsPaused() {return isPaused;}
private:
double timeElapsed; // Time elapsed
double time0; // Time of last Continue()
double lastTimeElapsed;
bool isPaused;
#ifdef CTRLLIB_H
bool callbackOn;
double lastTick;
TimeCallback timeCallback;
void Tick() {
double tActual = tmGetTimeX();
if (!isPaused && lastTick > -1) {
double deltaLastTick = tActual - lastTick;
if (deltaLastTick > 5*10) // Some external issue has stopped normal running
SetBack(deltaLastTick); // Timeout timer is fixed accordingly
}
lastTick = tActual;
}
#endif
};
class LocalProcessX
#ifdef CTRLLIB_H
: public Ctrl
#endif
{
typedef LocalProcessX CLASSNAME;
public:
LocalProcessX() : status(STOP_OK), callbackOn(false) {}
~LocalProcessX() {Stop();}
enum ProcessStatus {RUNNING = 1, STOP_OK = 0, STOP_TIMEOUT = -1, STOP_USER = -2, STOP_NORESPONSE = -3};
bool Start(const char *cmd, const char *envptr = 0, const char *dir = 0, double refreshTime = -1,
double maxTimeWithoutOutput = -1, double maxRunTime = -1, bool convertcharset = true) {
status = STOP_OK;
p.ConvertCharset(convertcharset);
timeElapsed.Start();
timeWithoutOutput.Start();
if(!p.Start(cmd, envptr, dir))
return false;
status = RUNNING;
this->maxTimeWithoutOutput = maxTimeWithoutOutput;
this->maxRunTime = maxRunTime;
this->refreshTime = refreshTime;
#ifdef CTRLLIB_H
if (refreshTime > 0) {
if (!callbackOn) {
timeCallback.Set(-int(refreshTime*1000), THISBACK(Perform));
callbackOn = true;
}
}
#endif
return true;
}
void Perform() {
if (status <= 0)
return;
String out;
p.Read(out);
if(p.IsRunning()) {
#ifdef PLATFORM_WIN32
if (!p.IsPaused()) {
#endif
if (maxTimeWithoutOutput > 0 && timeWithoutOutput.Seconds() > maxTimeWithoutOutput)
status = STOP_NORESPONSE;
else if (maxRunTime > 0 && timeElapsed.Seconds() > maxRunTime)
status = STOP_TIMEOUT;
#ifdef PLATFORM_WIN32
}
#endif
} else
status = STOP_OK;
bool resetTimeout = false;
if (!out.IsEmpty())
resetTimeout = true;
if (!WhenTimer(timeElapsed.Seconds(), out, status <= 0, resetTimeout))
status = STOP_USER;
if (resetTimeout)
timeWithoutOutput.Reset();
if (status < 0)
p.Kill();
#ifdef CTRLLIB_H
if (callbackOn) {
timeCallback.Kill();
callbackOn = false;
}
#endif
}
void Stop(ProcessStatus status = STOP_USER) {
if (!IsRunning())
return;
this->status = status;
p.Kill();
#ifdef CTRLLIB_H
if (callbackOn) {
timeCallback.Kill();
callbackOn = false;
}
#endif
}
#ifdef PLATFORM_WIN32
void Pause() {
p.Pause();
if (p.IsRunning()) {
timeElapsed.Pause(p.IsPaused());
timeWithoutOutput.Pause(p.IsPaused());
}
}
bool IsPaused() {return p.IsPaused();}
double Seconds() {return timeElapsed.Seconds();}
#endif
void Write(String str) {p.Write(str);}
int GetStatus() {return status;}
bool IsRunning() {return status > 0;}
Gate4<double, String&, bool, bool&> WhenTimer;
#ifdef PLATFORM_WIN32
DWORD GetPid() {return p.GetPid();}
#endif
private:
LocalProcess2 p;
RealTimeStop timeElapsed, timeWithoutOutput;
ProcessStatus status;
double maxTimeWithoutOutput, maxRunTime;
double refreshTime;
double lastPerform, lastPause;
bool callbackOn;
#ifdef CTRLLIB_H
TimeCallback timeCallback;
#endif
};
#endif

View file

@ -6,9 +6,10 @@
#include <plugin/gif/gif.h>
#include <plugin/bmp/bmp.h>
#include "Functions4U.h"
#include <Functions4U/Functions4U_Gui.h>
using namespace Upp;
NAMESPACE_UPP
Image NativePathIconX(const char *path, bool folder, int flags)
{
@ -147,6 +148,4 @@ int GetEditWidth(const String str) {
}
END_UPP_NAMESPACE
#endif

View file

@ -1,7 +1,10 @@
#ifndef _Functions4U_Functions4U_Gui_h_
#define _Functions4U_Functions4U_Gui_h_
#ifdef CTRLLIB_H
#include <Functions4U/Functions4U.h>
using namespace Upp;
inline const RGBA *GetPixel(const Image &img, int x, int y) {
return &img[y][x];
@ -92,4 +95,3 @@ void DrawRectLine(Draw& w, Rect &r, int lineWidth, const Color &color);
int GetEditWidth(const String str);
#endif
#endif

View file

@ -7,7 +7,8 @@
#include "GatherTpp.h"
#include <Functions4U/Html/htmld.h>
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
static Topic ReadTopic(const char *text)
{
@ -428,6 +429,5 @@ String GatherTpp::Www(const char *topic, int lang, String topicLocation) {
return GatherTopics(String().Cat() << topicLocation << topic << "$" << "en-us");
}
END_UPP_NAMESPACE
#endif

View file

@ -1,7 +1,7 @@
#ifndef _GatherTpp_h_
#define _GatherTpp_h_
NAMESPACE_UPP
using namespace Upp;
class GatherTpp {
public:
@ -46,6 +46,5 @@ private:
String ChangeTopicLanguage(const String &topic, int lang);
END_UPP_NAMESPACE
#endif

View file

@ -1,7 +1,7 @@
#include <Core/Core.h>
#include "htmld.h"
NAMESPACE_UPP
using namespace Upp;
String ToHtmlD(const char *s)
{
@ -656,4 +656,3 @@ HtmlTagD HtmlFntSizeD(int size)
return HtmlTagD("span").Attr("style", Sprintf("font-size: %dpt", size));
}
END_UPP_NAMESPACE

View file

@ -1,7 +1,7 @@
#ifndef __Html_htmld__
#define __Html_htmld__
NAMESPACE_UPP
using namespace Upp;
String ToHtmlD(const char *s);
@ -303,6 +303,5 @@ HtmlTagD HtmlArialD();
HtmlTagD HtmlRomanD();
HtmlTagD HtmlFntSizeD(int size);
END_UPP_NAMESPACE
#endif

View file

@ -1,7 +1,7 @@
#include <Core/Core.h>
#include "LocalProcess2.h"
namespace Upp {
using namespace Upp;
#ifdef PLATFORM_WIN32
#include <TlHelp32.h>
@ -710,4 +710,4 @@ void LocalProcess2::Pause() {
}
#endif
}

View file

@ -1,7 +1,7 @@
#ifndef _Functions4U_LocalProcess2_h
#define _Functions4U_LocalProcess2_h
NAMESPACE_UPP
using namespace Upp;
class LocalProcess2 : public AProcess {
public:
@ -81,6 +81,5 @@ public:
virtual ~LocalProcess2() { Kill(); }
};
END_UPP_NAMESPACE
#endif

View file

@ -1,11 +1,11 @@
#ifdef flagGUI
#include <CtrlLib/CtrlLib.h>
#include "Functions4U.h"
#include "Functions4U_Gui.h"
NAMESPACE_UPP
using namespace Upp;
//NAMESPACE_UPP
inline bool iscidplus(int c) {
return iscib(c) || c == '.' || (c >= '0' && c <= '9');
@ -463,7 +463,6 @@ QtfRichObject QtfEquation(const String &str) {
return QtfRichObject(CreateDrawingObject(dw.GetResult(), sz, sz));
}
END_UPP_NAMESPACE
#endif

View file

@ -2,7 +2,8 @@
#include "StaticPlugin.h"
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
Array<StaticPlugin::PluginData>& StaticPlugin::Plugins() {
static Array<PluginData> x;
@ -68,4 +69,3 @@ bool StaticPlugin::Init(const char *_name) {
}
*/
END_UPP_NAMESPACE

View file

@ -1,7 +1,8 @@
#ifndef _Functions4U_StaticPlugin_h_
#define _Functions4U_StaticPlugin_h_
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
#define PluginRegister(a, b, c) a::Register<b>(c, typeid(a).name())
#define PluginInit(a, b) (a)._Init(b, typeid(a).name())
@ -56,6 +57,5 @@ public:
String &GetName() {return name;};
};
END_UPP_NAMESPACE
#endif

View file

@ -2,8 +2,8 @@
#include "Functions4U.h"
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
Color GetSvgColor(const char *color) {
struct Svg_color {
@ -159,4 +159,3 @@ Color GetSvgColor(const char *color) {
return Null;
}
END_UPP_NAMESPACE

View file

@ -1,7 +1,8 @@
#ifndef _Functions4U_SvgColors_h_
#define _Functions4U_SvgColors_h_
NAMESPACE_UPP
//NAMESPACE_UPP
using namespace Upp;
struct ColorDef {
const char *name;
@ -158,6 +159,5 @@ inline Color Whitesmoke() {return Color(245,245,245);}
//inline Color Yellow() {return Color(255,255,0);}
inline Color Yellowgreen() {return Color(154,205,50);}
END_UPP_NAMESPACE
#endif

View file

@ -4,7 +4,8 @@
#define ftello ftell
#define fseeko fseek
NAMESPACE_UPP
using namespace Upp;
extern String errMsg;
String BsGetLastError();
@ -28,6 +29,5 @@ bool Err(String str);
#endif
#endif
END_UPP_NAMESPACE
#endif