diff options
Diffstat (limited to 'src/history.c')
-rw-r--r-- | src/history.c | 66 |
1 files changed, 37 insertions, 29 deletions
diff --git a/src/history.c b/src/history.c index 5be24608..9779795a 100644 --- a/src/history.c +++ b/src/history.c | |||
@@ -55,39 +55,38 @@ iHistory *copy_History(const iHistory *d) { | |||
55 | return copy; | 55 | return copy; |
56 | } | 56 | } |
57 | 57 | ||
58 | void save_History(const iHistory *d, const char *dirPath) { | 58 | void serialize_History(const iHistory *d, iStream *outs) { |
59 | iString *line = new_String(); | 59 | writeU16_Stream(outs, d->recentPos); |
60 | iFile *f = newCStr_File(concatPath_CStr(dirPath, "recent.txt")); | 60 | writeU16_Stream(outs, size_Array(&d->recent)); |
61 | if (open_File(f, writeOnly_FileMode | text_FileMode)) { | 61 | iConstForEach(Array, i, &d->recent) { |
62 | iConstForEach(Array, i, &d->recent) { | 62 | const iRecentUrl *item = i.value; |
63 | const iRecentUrl *item = i.value; | 63 | serialize_String(&item->url, outs); |
64 | format_String(line, "%04x %s\n", item->scrollY, cstr_String(&item->url)); | 64 | write32_Stream(outs, item->scrollY); |
65 | writeData_File(f, cstr_String(line), size_String(line)); | 65 | if (item->cachedResponse) { |
66 | write8_Stream(outs, 1); | ||
67 | serialize_GmResponse(item->cachedResponse, outs); | ||
68 | } | ||
69 | else { | ||
70 | write8_Stream(outs, 0); | ||
66 | } | 71 | } |
67 | } | 72 | } |
68 | iRelease(f); | 73 | } |
69 | delete_String(line); | 74 | |
70 | } | 75 | void deserialize_History(iHistory *d, iStream *ins) { |
71 | 76 | clear_History(d); | |
72 | void load_History(iHistory *d, const char *dirPath) { | 77 | d->recentPos = readU16_Stream(ins); |
73 | iFile *f = newCStr_File(concatPath_CStr(dirPath, "recent.txt")); | 78 | size_t count = readU16_Stream(ins); |
74 | if (open_File(f, readOnly_FileMode | text_FileMode)) { | 79 | while (count--) { |
75 | const iRangecc src = range_Block(collect_Block(readAll_File(f))); | 80 | iRecentUrl item; |
76 | iRangecc line = iNullRange; | 81 | init_RecentUrl(&item); |
77 | while (nextSplit_Rangecc(&src, "\n", &line)) { | 82 | deserialize_String(&item.url, ins); |
78 | iRangecc nonwhite = line; | 83 | item.scrollY = read32_Stream(ins); |
79 | trim_Rangecc(&nonwhite); | 84 | if (read8_Stream(ins)) { |
80 | if (isEmpty_Range(&nonwhite)) continue; | 85 | item.cachedResponse = new_GmResponse(); |
81 | int scroll = 0; | 86 | deserialize_GmResponse(item.cachedResponse, ins); |
82 | sscanf(nonwhite.start, "%04x", &scroll); | ||
83 | iRecentUrl item; | ||
84 | init_RecentUrl(&item); | ||
85 | item.scrollY = scroll; | ||
86 | initRange_String(&item.url, (iRangecc){ nonwhite.start + 5, nonwhite.end }); | ||
87 | pushBack_Array(&d->recent, &item); | ||
88 | } | 87 | } |
88 | pushBack_Array(&d->recent, &item); | ||
89 | } | 89 | } |
90 | iRelease(f); | ||
91 | } | 90 | } |
92 | 91 | ||
93 | void clear_History(iHistory *d) { | 92 | void clear_History(iHistory *d) { |
@@ -123,6 +122,15 @@ const iString *url_History(const iHistory *d, size_t pos) { | |||
123 | return collectNew_String(); | 122 | return collectNew_String(); |
124 | } | 123 | } |
125 | 124 | ||
125 | iRecentUrl *findUrl_History(iHistory *d, const iString *url) { | ||
126 | iReverseForEach(Array, i, &d->recent) { | ||
127 | if (cmpStringCase_String(url, &((iRecentUrl *) i.value)->url) == 0) { | ||
128 | return i.value; | ||
129 | } | ||
130 | } | ||
131 | return NULL; | ||
132 | } | ||
133 | |||
126 | void replace_History(iHistory *d, const iString *url) { | 134 | void replace_History(iHistory *d, const iString *url) { |
127 | /* Update in the history. */ | 135 | /* Update in the history. */ |
128 | iRecentUrl *item = mostRecentUrl_History(d); | 136 | iRecentUrl *item = mostRecentUrl_History(d); |