|
47 | 47 | #define MENU_COMPETION_2 (MENU_SIZE + 3) |
48 | 48 | #define MENU_COMPETION_3 (MENU_SIZE + 4) |
49 | 49 | #define MAX_COMPLETIONS 4 |
| 50 | +#define MAX_CACHE 8 |
50 | 51 |
|
51 | 52 | #define FONT_SCALE_INTERVAL 10 |
52 | 53 | #define FONT_MIN 20 |
|
56 | 57 |
|
57 | 58 | System *g_system; |
58 | 59 |
|
| 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 | + |
59 | 73 | System::System() : |
60 | 74 | _output(NULL), |
61 | 75 | _state(kInitState), |
| 76 | + _cache(MAX_CACHE), |
62 | 77 | _lastEventTime(0), |
63 | 78 | _eventTicks(0), |
64 | 79 | _touchX(-1), |
@@ -406,27 +421,35 @@ void System::handleEvent(MAEvent &event) { |
406 | 421 | char *System::loadResource(const char *fileName) { |
407 | 422 | char *buffer = NULL; |
408 | 423 | 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 | + } |
417 | 445 | } 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); |
422 | 447 | } |
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); |
425 | 452 | } |
426 | | - _output->setStatus(NULL); |
427 | | - dev_fclose(handle); |
428 | | - v_free(var_p); |
429 | | - free(var_p); |
430 | 453 | } |
431 | 454 | return buffer; |
432 | 455 | } |
@@ -612,14 +635,25 @@ void System::setBack() { |
612 | 635 | _output->selectBackScreen(_userScreenId); |
613 | 636 | _output->selectFrontScreen(_userScreenId); |
614 | 637 | _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 | + } |
618 | 651 | } |
619 | 652 | } |
620 | 653 |
|
621 | 654 | void System::setLoadBreak(const char *path) { |
622 | 655 | _loadPath = path; |
| 656 | + _history.push(new strlib::String(path)); |
623 | 657 | _state = kBreakState; |
624 | 658 | brun_break(); |
625 | 659 | } |
|
0 commit comments