-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.d
More file actions
1402 lines (1199 loc) · 31.1 KB
/
Copy pathstorage.d
File metadata and controls
1402 lines (1199 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* Kotivox
*
* Copyright 2009 Mitja Ursic
* odtihmal@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************/
module storage;
import tango.io.FileScan;
import tango.core.Array;
import Txt = tango.text.Util;
import Unicode = tango.text.Unicode;
import Integer = tango.text.convert.Integer;
import dwt.DWT;
import dwt.widgets.DateTime;
import config;
import util;
import auth;
import crypt;
private class Day
{
private char[] name;
private char[] text;
private char[] textDigest;
// [0] - start of category
// [1] - length of category
// [2] - 0 for normal text, 1 for bold - indicating category title
private int[][] categoryRanges;
private char[] catRangesDigest;
private static Day[] days;
private static Day[] sortByName()
{
char[][] names;
Day[] sortedDays;
foreach(day; days)
names ~= day.name;
sort(names);
foreach(nm; names)
{
foreach(day; days)
if(day.name == nm)
sortedDays ~= day;
}
return sortedDays;
}
this(char[] name, char[] text)
{
this.name = name;
this.text = text;
this.digest;
}
/*
Digest text and category ranges for comparison later.
*/
private void digest()
{
this.textDigest = util.digest(this.text);
this.catRangesDigest = util.digest(serialize(this.categoryRanges));
}
/*
Decrypt user days.
*/
private static void load()
{
// Gather all day files for this user.
days = null;
foreach(file; (new FileScan)(Auth.userDirPath, USER_DAY_FILE_EXTENSION).files)
{
// Decrypt text and store it.
char[] textOut = k_decrypt_to_string(file.path ~ file.file, Auth.cipherKey);
Day day = new Day(file.name, textOut);
days ~= day;
day.loadCategoryRanges;
day.digest;
}
}
private static bool dayExists(char[] dayName)
{
foreach(Day d; days)
if(d.name == dayName) return true;
return false;
}
private static void daySetText(char[] dayName, char[] text)
{
if(dayExists(dayName))
{
foreach(d; days)
if(d.name == dayName)
{
d.text = text;
break;
}
}
else
{
// Invalidate digest so that new day gets saved.
Day day = new Day(dayName, text);
day.textDigest = "";
days ~= day;
}
}
private static char[] dayGetText(char[] dayName)
{
foreach(Day d; days)
if(d.name == dayName) return d.text;
return "";
}
private static void setCategoryRanges(char[] dayName, int[][] ranges)
{
foreach(day; days)
if(dayName == day.name)
{
day.categoryRanges = ranges;
break;
}
}
private static int[][] getCategoryRanges(char[] dayName)
{
foreach(day; days)
if(dayName == day.name) return day.categoryRanges;
return null;
}
/*
Store encrypted category ranges into file.
*/
private bool saveCategoryRanges()
{
char[] catRangesFileName = Auth.userDirPath ~ this.name ~ USER_CATEGORY_RANGES_FILE_EXTENSION;
// No category ranges, remove file.
if(this.categoryRanges.length <= 0)
{
FilePath catRangesFile = new FilePath(catRangesFileName);
if(catRangesFile.exists) catRangesFile.remove;
return false;
}
// Have ranges changed since loading?
if(this.catRangesDigest == util.digest(serialize(this.categoryRanges))) return false;
char[] rangesTxt;
int i = 0;
foreach(c; this.categoryRanges)
{
rangesTxt ~= Integer.toString(i++) ~ " ";
rangesTxt ~= Integer.toString(c[0]) ~ " ";
rangesTxt ~= Integer.toString(c[1]) ~ " ";
rangesTxt ~= Integer.toString(c[2]) ~ "\n";
}
k_encrypt_from_string(rangesTxt,
catRangesFileName,
Auth.cipherKey);
return true;
}
/*
Decrypt user category ranges.
*/
private void loadCategoryRanges()
{
// Decrypt possible category ranges and store them.
FilePath catRangesFile = new FilePath(Auth.userDirPath ~ this.name ~ USER_CATEGORY_RANGES_FILE_EXTENSION);
if(catRangesFile.exists)
{
char[] ranges = k_decrypt_to_string(catRangesFile.path ~ catRangesFile.file, Auth.cipherKey);
int[][] catRanges;
foreach(char[] range; parseLines(ranges))
{
char[][] rangeVals = Txt.split(range, " ");
int start = Integer.toInt(rangeVals[0]);
int length = Integer.toInt(rangeVals[1]);
int fontStyle = Integer.toInt(rangeVals[2]);
catRanges ~= [start, length, fontStyle];
}
this.categoryRanges = catRanges;
}
}
private static void save()
{
char[] textFilePath;
char[] text;
foreach(day; days)
{
day.saveCategoryRanges;
textFilePath = Auth.userDirPath ~ day.name ~ USER_DAY_FILE_EXTENSION;
// Remove existing text file if no more text.
text = day.text;
if(0 == text.length)
{
FilePath textFile = new FilePath(textFilePath);
if(textFile.exists) textFile.remove;
continue;
}
// Has text changed since loading?
if(day.textDigest == util.digest(day.text)) continue;
k_encrypt_from_string(text,
textFilePath,
Auth.cipherKey);
day.digest;
}
}
}
private class Category
{
private int id;
private char[] name;
private static Category[] categories;
// Category retrieval counter.
private static int catRetrCount = 0;
private static char[] origDigest;
this(int id, char[] name)
{
this.id = id;
this.name = name;
}
private static int[] getIds()
{
int[] ids;
foreach(c; categories) ids ~= c.id;
return ids;
}
/*
Add new category with name to categories array.
Return id of added category.
*/
private static int add()
{
int id = getFreeSlot(getIds);
categories ~= new Category(id, CATEGORY_TEXT ~ " " ~ Integer.toString(id + 1));
return id;
}
/*
Removes category of given id from categories array.
*/
private static void remove(int id)
{
Category new_categories[];
foreach(Category category; categories)
{
if(category.id == id) continue;
new_categories ~= category;
}
categories = new_categories;
catRetrCount = categories.length;
}
private static void categoryName(int id, char[] name)
{
foreach(Category c; categories)
{
if(c.id == id)
{
c.name = sanitizeStr(name);
break;
}
}
}
/*
Encrypt categories into file.
*/
private static void save()
{
char[] content;
foreach(Category c; categories)
{
// Skip categories with empty names.
if(Txt.trim(c.name).length <= 0) continue;
content ~= Integer.toString(c.id) ~ " " ~ c.name ~ "\n";
}
char[] catFileName = Auth.userDirPath ~ USER_CATEGORIES_FILE;
// No categories, remove file if exists.
if(content.length <= 0)
{
FilePath catFile = new FilePath(catFileName);
if(catFile.exists) catFile.remove;
return;
}
// Unchanged.
if(digest(content) == Category.origDigest) return;
// Encrypt categories into file.
k_encrypt_from_string(content,
catFileName,
Auth.cipherKey);
Category.origDigest = digest(content);
}
/*
Decrypt user categories.
*/
private static void load()
{
// Does categories file exist?
char[] filename = Auth.userDirPath ~ USER_CATEGORIES_FILE;
if(!(new FilePath(filename)).exists) return;
// Decrypt categories file and store categories.
char[] content = k_decrypt_to_string(filename, Auth.cipherKey);
Category.origDigest = digest(content);
categories = null;
foreach(char[] id, char[] name; parseLines(content))
categories ~= new Category(Integer.toInt(id), name);
catRetrCount = categories.length;
}
/*
Return name of category with ID.
*/
private static char[] categoryName(int id)
{
foreach(Category c; categories)
if(c.id == id) return c.name;
return "";
}
/*
Return ID of category with name.
*/
private static int getID(char[] name)
{
foreach(Category c; categories)
if(Unicode.toLower(c.name) == Unicode.toLower(Txt.trim(name))) return c.id;
return -1;
}
/*
Return associative array with category id as key
and category name as value.
*/
private static char[][] get()
{
if(0 == catRetrCount)
{
// Reset category count.
catRetrCount = categories.length;
return null;
}
char[][] cat;
cat = new char[][2];
int idx = categories.length - catRetrCount;
cat[0] = Integer.toString(categories[idx].id);
cat[1] = categories[idx].name;
catRetrCount--;
return cat;
}
/*
Return complement category IDs.
*/
private static int[] invCategoryIDs(int[] selCategories)
{
int[] invCategories;
foreach(c; categories)
{
if(!contains(selCategories, c.id))
invCategories ~= c.id;
}
return invCategories;
}
}
private class SearchResultPage
{
private int index;
private char[] content;
static char[] txtKeywords;
static SearchResultPage[] resultPages;
this(int index, char[] content)
{
this.index = index;
this.content = Txt.substitute(content, "&", "&&");
}
// save keywords for display later, replace & with &&
static void keywords(char[] keywords)
{
this.txtKeywords = Txt.substitute(keywords, "&", "&&");
}
static char[] keywords()
{
return this.txtKeywords;
}
static int getNextIndex()
{
if(0 == resultPages.length) return 0;
return resultPages[resultPages.length - 1].index + 1;
}
/*
Return true when location is inside category of given id
*/
static bool isInCategory(int location, int id, Day day)
{
char[][int] catNames;
foreach(c; Category.categories)
catNames[c.id] ~= c.name;
struct Category
{
int id;
int start;
int end;
}
Category[] dayCategories;
// for each day, get location at each category
// start at title and end at the end of the body
// also get id of each category
// save id, start and end in Category struct array
// if the same paragraph is categorized by two or
// more categories, store same category boundaries for each id
int start = -1;
int end = -1;
char[] title;
foreach(range; day.categoryRanges)
{
int bold = range[2];
// At category title.
if(1 == bold)
{
title = day.text[range[0]..range[0] + range[1]];
int begin = 0;
foreach(id, name; catNames)
{
int location;
begin = location;
if(Txt.locatePattern(Unicode.toLower(title),
Unicode.toLower(name),
begin) < title.length)
start = range[0];
}
}
// At category body.
// And end only if start is valid.
else if((0 == bold) && (-1 < start))
{
int tempEnd = start + title.length + range[1];
if(start < tempEnd) end = tempEnd;
}
if((-1 < start) && (0 < end))
{
// Split category names and add them among
// day categories.
foreach(name; Txt.split(title, ","))
{
int catID = Storage.getCategoryID(name);
Category c = {catID, start, end};
dayCategories ~= c;
}
start = -1;
end = -1;
}
}
foreach(dc; dayCategories)
{
if((dc.id == id) && (dc.start <= location) && (location <= dc.end))
return true;
}
return false;
}
/*
Match texts of all days against keywords.
Exclude categorized texts by default.
Match against categorized text whose categories are provided in categories array.
Store search results in array, each element representing one result page.
Return true if any results found, false otherwise.
Always return matches inside non-categorized text.
*/
private static bool compileSearchResults(char[] keywordStr, int[] categories)
{
// Clear previous search.
resultPages = null;
char[] content;
int appendLength = SEARCH_RESULT_LENGTH / 2;
uint numResults;
foreach(day; Day.sortByName.reverse)
{
int end = day.text.length;
int location = 0;
char[] result;
// Skip emtpy days.
if(end <= 0) continue;
entry: do
{
location = Txt.locatePattern(Unicode.toLower(day.text),
Unicode.toLower(keywordStr),
location);
// Match found.
// Prepend and append some text around keywordStr.
// Prepend link above the result.
if((0 == location) || (location < end))
{
// Is the start of found match inside given categories?
int[] invCategories = Category.invCategoryIDs(categories);
foreach(id; invCategories)
{
if(isInCategory(location, id, day))
{
location += keywordStr.length;
continue entry;
}
}
char[] head;
char[] core;
char[] tail;
// Start head after first whitespace so we don't
// cut any variable-width characters.
if(0 <= (location - appendLength))
{
head = day.text[location - appendLength..location];
int start = find(head, " ") + 1;
if(start < head.length) head = head[start..$];
}
core = day.text[location..location + keywordStr.length];
// End tail after last whitespace so we don't
// cut any variable-width characters.
if((location + keywordStr.length + appendLength) <= day.text.length)
{
tail = day.text[location + keywordStr.length..location + keywordStr.length + appendLength];
tail = tail[0..rfind(tail, " ")];
}
char[] dateStr = dateFormat("%A, %e. %B, %Y", dateStrToDate(day.name));
result ~= "<a href=\"JUMP" ~ day.name ~ Integer.toString(location) ~ "-" ~ this.keywords ~ "\">" ~ dateStr ~ "</a>\n";
result ~= head ~ Unicode.toUpper(core) ~ tail;
result ~= "\n\n";
numResults++;
if(SEARCH_RESULT_PAGE_LENGTH < (content.length + result.length))
{
char[] matches = 0 < content.length ? content : result;
resultPages ~= new SearchResultPage(getNextIndex, matches);
content = result;
}
else content ~= result;
result = "";
}
location += keywordStr.length;
}while(location < end);
}
// Save what's left of search results.
if(0 < content.length)
resultPages ~= new SearchResultPage(getNextIndex, content);
if(!numResults) return false;
return true;
}
private static char[] getPager(int currentPage)
{
if(resultPages.length < 2) return "";
char[] pageLinks;
foreach(page; resultPages)
{
if(page.index == currentPage)
pageLinks ~= " " ~ Integer.toString(page.index + 1);
else
{
pageLinks ~= " <a href=\"PAGE" ~ Integer.toString(page.index) ~ "\">";
pageLinks ~= Integer.toString(page.index + 1) ~ "</a>";
}
}
return Txt.trim(pageLinks);
}
}
private class Note
{
private int id;
private char[] name;
private char[] origName;
private char[] filename;
private char[] content;
private char[] origDigest;
private static Note[] notes;
this(char[] name, char[] filename = "", char[] content = "")
{
this.id = getFreeSlot(getIds);
this.name = sanitizeStr(name);
this.origName = this.name;
if(filename.length <= 0) filename = randStr ~ NOTE_FILE_EXTENSION;
this.filename = filename;
this.content = content;
this.origDigest = digest(this.content);
}
private static int add()
{
int id = getFreeSlot(getIds);
notes ~= new Note(NOTES_TEXT ~ " " ~ Integer.toString(id + 1));
return id;
}
/*
Removes note of given id from notes array.
*/
private static void remove(int id)
{
Note new_notes[];
foreach(note; notes)
{
if(note.id == id) continue;
new_notes ~= note;
}
notes = new_notes;
}
// Return note name.
private static char[] noteName(int id)
{
foreach(note; notes)
if(note.id == id) return note.name;
return "I CANNOT FIND NOTE " ~ Integer.toString(id) ~ ".";
}
// Set note name.
private static void noteName(int id, char[] name)
{
foreach(note; notes)
{
if(note.id == id)
{
note.name = sanitizeStr(name);
break;
}
}
}
private static void noteContent(int id, char[] content)
{
foreach(n; notes)
{
if(n.id == id)
{
n.content = content;
break;
}
}
}
private static char[] noteContent(int id)
{
foreach(n; notes)
if(n.id == id) return n.content;
return "";
}
private static int[] getIds()
{
int[] ids;
foreach(n; notes) ids ~= n.id;
return ids;
}
/*
Encrypt notes into files.
*/
private static void save()
{
char[] noteFiles;
foreach(note; notes)
{
char[] noteFilePath = Auth.userDirPath ~ note.filename;
// Skip note with empty name.
if(note.name.length <= 0)
{
// Delete note file if exists.
FilePath noteFile = new FilePath(noteFilePath);
if(noteFile.exists) noteFile.remove;
continue;
}
noteFiles ~= noteFilePath;
// Skip note with unchanged content.
if((note.origName == note.name) &&
(digest(note.content) == note.origDigest))
continue;
// Write name of the note in first line.
char[] content = note.name ~ "\n" ~ note.content;
k_encrypt_from_string(content,
noteFilePath,
Auth.cipherKey);
note.origName = note.name;
note.origDigest = digest(note.content);
}
// Remove obsolete note files.
foreach(file; (new FileScan)(Auth.userDirPath, NOTE_FILE_EXTENSION).files)
if(!contains(noteFiles, file.path ~ file.file))
file.remove;
}
/*
Decrypt notes from files.
*/
private static void load()
{
notes = null;
foreach(file; (new FileScan)(Auth.userDirPath, NOTE_FILE_EXTENSION).files)
{
// Decrypt text and store it.
char[] textOut = k_decrypt_to_string(file.path ~ file.file, Auth.cipherKey);
char[][] lines = Txt.splitLines(textOut);
// First line contains note's name.
char[] name = lines[0];
// The rest of the lines is content.
char[] content = "";
for(int i = 1; i < lines.length; i++)
content ~= lines[i] ~ "\n";
notes ~= new Note(name,
file.file,
content);
}
}
private static char[][int] getNotes()
{
char[][int] noteList;
foreach(note; notes) noteList[note.id] = note.name;
return noteList;
}
}
/**********************************************************************************************************
* Jerry Seinfeld's task chain
*
* Get a big wall calendar that has a whole year on one page
* and hang it on a prominent wall.
* Get a big red magic marker.
*
* Each day that you do your task, put a big red X over that day.
* "After a few days you'll have a chain. Just keep at it
* and the chain will grow longer every day. You'll like seeing that chain,
* especially when you get a few weeks under your belt.
* Your only job next is to not break the chain."
*
* "Don't break the chain."
*
* http://lifehacker.com/software/motivation/jerry-seinfelds-productivity-secret-281626.php
* http://www.thesimpledollar.com/2007/07/26/applying-jerry-seinfelds-chain-concept-to-personal-finance/
*********************************************************************************************************/
private class Chain
{
private int id;
private char[] name;
private char[] desc;
private char[] filename;
private Date startDate;
private bool locked = false;
private int[] dates;
private bool changed = false;
private static Chain[] chains;
this(char[] name,
Date startDate = today,
char[] desc = "",
bool locked = false,
char[] filename = randStr ~ CHAIN_FILE_EXTENSION,
int[] dates = null)
{
this.id = getFreeSlot(getIds);
this.name = sanitizeStr(name);
this.startDate = startDate;
this.desc = desc;
this.locked = locked;
this.filename = filename;
this.dates = dates;
}
private static int[] getIds()
{
int[] ids;
foreach(n; chains) ids ~= n.id;
return ids;
}
private static int add()
{
int id = getFreeSlot(getIds);
chains ~= new Chain(CHAIN_TEXT ~ " " ~ Integer.toString(id + 1));
chains[$ - 1].changed = true;
return id;
}
private static void remove(int id)
{
Chain new_chains[];
foreach(chain; chains)
{
if(chain.id == id) continue;
new_chains ~= chain;
}
chains = new_chains;
}
private static char[][int] getChains()
{
char[][int] chainlist;
foreach(chain; chains) chainlist[chain.id] ~= chain.name;
return chainlist;
}
private static char[] chainName(int id)
{
foreach(chain; chains)
if(chain.id == id) return chain.name;
return "I CANNOT FIND CHAIN " ~ Integer.toString(id) ~ ".";
}
private static void chainName(int id, char[] name)
{
foreach(chain; chains)
{
if(chain.id == id)
{
chain.name = sanitizeStr(name);
chain.changed = true;
break;
}
}
}
private static char[] chainDesc(int id)
{
foreach(chain; chains)
if(chain.id == id) return chain.desc;
return "I CANNOT FIND CHAIN " ~ Integer.toString(id) ~ ".";
}
private static void chainDesc(int id, char[] desc)
{
foreach(chain; chains)
{
if(chain.id == id)
{
chain.desc = sanitizeStr(desc, CHAIN_DESCRIPTION_LENGTH, false);
chain.changed = true;
break;
}
}
}
/*
Add date to chain of given id.
*/
private static void addDate(int id, int date)
{
foreach(chain; chains)
if(chain.id == id)
{
if(!contains(chain.dates, date))
{
chain.dates ~= date;
chain.changed = true;
break;
}
}
}
/*
Remove date from chain of given id.
*/
private static void removeDate(int id, int date)
{
int[] dates;
foreach(chain; chains)
if(chain.id == id)
{
foreach(cdate; chain.dates)
{
if(cdate == date) continue;
dates ~= cdate;
}
chain.dates = dates;
chain.changed = true;
break;
}
}
/*
Return start date.
*/
private static Date getStartDate(int id)
{
foreach(chain; chains)
if(chain.id == id) return chain.startDate;
static Date date = {day: -1};
return date;
}
/*
Return marked days.