Skip to content

Commit 344d433

Browse files
committed
COMMON: add simple cache and history handling
1 parent 6491bf9 commit 344d433

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

ide/android/assets/main.bas

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ sub serverInfo()
155155
fi
156156
end
157157

158+
func fileCmpFunc(l, r)
159+
local f1 = lower(l)
160+
local f2 = lower(r)
161+
fileCmpFunc = IFF(f1 == f2, 0, IFF(f1 > f2, 1, -1))
162+
end
163+
158164
sub listFiles(byref frm, path, byref basList, byref dirList)
159165
local fileList, ent, name, lastItem, bn, bn_back
160166

@@ -181,15 +187,15 @@ sub listFiles(byref frm, path, byref basList, byref dirList)
181187

182188
for ent in fileList
183189
name = ent
184-
if (isdir(path + name)) then
190+
if (isdir(path + name) && left(name, 1) != ".") then
185191
dirList << name
186192
else if (lower(right(ent, 4)) == ".bas") then
187193
basList << name
188194
endif
189195
next ent
190196

191-
sort dirList
192-
sort basList
197+
sort dirList use fileCmpFunc(x,y)
198+
sort basList use filecmpfunc(x,y)
193199

194200
lastItem = len(dirList) - 1
195201

src/ui/system.cpp

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#define MENU_COMPETION_2 (MENU_SIZE + 3)
4848
#define MENU_COMPETION_3 (MENU_SIZE + 4)
4949
#define MAX_COMPLETIONS 4
50+
#define MAX_CACHE 8
5051

5152
#define FONT_SCALE_INTERVAL 10
5253
#define FONT_MIN 20
@@ -56,9 +57,23 @@
5657

5758
System *g_system;
5859

60+
void Cache::add(const char *key, const char *value) {
61+
if (_size == _count) {
62+
// overwrite at next index position
63+
_head[_index]->empty();
64+
_head[_index]->append(key);
65+
_head[_index + 1]->empty();
66+
_head[_index + 1]->append(value);
67+
_index = (_index + 2) % _size;
68+
} else {
69+
Properties::put(key, value);
70+
}
71+
}
72+
5973
System::System() :
6074
_output(NULL),
6175
_state(kInitState),
76+
_cache(MAX_CACHE),
6277
_lastEventTime(0),
6378
_eventTicks(0),
6479
_touchX(-1),
@@ -406,27 +421,35 @@ void System::handleEvent(MAEvent &event) {
406421
char *System::loadResource(const char *fileName) {
407422
char *buffer = NULL;
408423
if (strstr(fileName, "://") != NULL) {
409-
int handle = 1;
410-
var_t *var_p = v_new();
411-
dev_file_t *f = dev_getfileptr(handle);
412-
_output->setStatus("Loading...");
413-
_output->redraw();
414-
if (dev_fopen(handle, fileName, 0)) {
415-
if (http_read(f, var_p) == 0) {
416-
systemPrint("\nfailed to read %s\n", fileName);
424+
String *cached = _cache.get(fileName);
425+
if (cached != NULL) {
426+
int len = cached->length();
427+
buffer = (char *)malloc(len + 1);
428+
memcpy(buffer, cached->c_str(), len);
429+
} else {
430+
int handle = 1;
431+
var_t *var_p = v_new();
432+
dev_file_t *f = dev_getfileptr(handle);
433+
_output->setStatus("Loading...");
434+
_output->redraw();
435+
if (dev_fopen(handle, fileName, 0)) {
436+
if (http_read(f, var_p) == 0) {
437+
systemPrint("\nfailed to read %s\n", fileName);
438+
} else {
439+
int len = var_p->v.p.size;
440+
buffer = (char *)malloc(len + 1);
441+
memcpy(buffer, var_p->v.p.ptr, len);
442+
buffer[len] = '\0';
443+
_cache.add(fileName, buffer);
444+
}
417445
} else {
418-
int len = var_p->v.p.size;
419-
buffer = (char *)malloc(len + 1);
420-
memcpy(buffer, var_p->v.p.ptr, len);
421-
buffer[len] = '\0';
446+
systemPrint("\nfailed to open %s\n", fileName);
422447
}
423-
} else {
424-
systemPrint("\nfailed to open %s\n", fileName);
448+
_output->setStatus(NULL);
449+
dev_fclose(handle);
450+
v_free(var_p);
451+
free(var_p);
425452
}
426-
_output->setStatus(NULL);
427-
dev_fclose(handle);
428-
v_free(var_p);
429-
free(var_p);
430453
}
431454
return buffer;
432455
}
@@ -612,14 +635,25 @@ void System::setBack() {
612635
_output->selectBackScreen(_userScreenId);
613636
_output->selectFrontScreen(_userScreenId);
614637
_userScreenId = -1;
615-
} else {
616-
// quit app when shell is active
617-
setExit(_mainBas);
638+
}
639+
640+
// quit app when shell is active
641+
setExit(_mainBas);
642+
643+
// follow history when available and not exiting
644+
if (!_mainBas) {
645+
// remove the current item
646+
_history.pop();
647+
if (_history.peek() != NULL) {
648+
_loadPath.empty();
649+
_loadPath.append(_history.peek());
650+
}
618651
}
619652
}
620653

621654
void System::setLoadBreak(const char *path) {
622655
_loadPath = path;
656+
_history.push(new strlib::String(path));
623657
_state = kBreakState;
624658
brun_break();
625659
}

src/ui/system.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#include "platform/fltk/system.h"
1919
#else
2020

21+
struct Cache : public strlib::Properties {
22+
Cache(int size) : Properties(size * 2), _index(0) {}
23+
void add(const char *key, const char *value);
24+
int _index;
25+
};
26+
2127
struct System {
2228
System();
2329
virtual ~System();
@@ -107,8 +113,10 @@ struct System {
107113
kDoneState // thread has terminated
108114
} _state;
109115

116+
strlib::Stack<String *> _history;
110117
strlib::String _loadPath;
111118
strlib::String _activeFile;
119+
Cache _cache;
112120
int _lastEventTime;
113121
int _eventTicks;
114122
int _touchX;

0 commit comments

Comments
 (0)