summaryrefslogtreecommitdiff
path: root/algorithm.cpp
blob: 00357e9d3eb17a61540dc33505f6d5025e4b6a01 (plain)
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
#include "algorithm.h"
#include <wx/intl.h>
#include <cmath>
#include <wx/log.h>
#include "library/resources.h"

#ifdef FFS_WIN
#include <wx/msw/wrapwin.h> //includes "windows.h"

#elif defined FFS_LINUX
#include <string.h>
#include <errno.h>
#endif

using namespace FreeFileSync;


wxString FreeFileSync::formatFilesizeToShortString(const wxULongLong& filesize)
{
    return formatFilesizeToShortString(filesize.ToDouble());
}


wxString FreeFileSync::formatFilesizeToShortString(const double filesize)
{
    double nrOfBytes = filesize;

    wxString unit = _(" Byte");
    if (nrOfBytes > 999)
    {
        nrOfBytes/= 1024;
        unit = _(" kB");
        if (nrOfBytes > 999)
        {
            nrOfBytes/= 1024;
            unit = _(" MB");
            if (nrOfBytes > 999)
            {
                nrOfBytes/= 1024;
                unit = _(" GB");
                if (nrOfBytes > 999)
                {
                    nrOfBytes/= 1024;
                    unit = _(" TB");
                    if (nrOfBytes > 999)
                    {
                        nrOfBytes/= 1024;
                        unit = _(" PB");
                    }
                }
            }
        }
    }

    wxString temp;
    if (unit == _(" Byte")) //no decimal places in case of bytes
    {
        double integer = 0;
        modf(nrOfBytes, &integer); //get integer part of nrOfBytes
        temp = globalFunctions::numberToWxString(int(integer));
    }
    else
    {
        nrOfBytes*= 100;    //we want up to two decimal places
        double integer = 0;
        modf(nrOfBytes, &integer); //get integer part of nrOfBytes

        temp = globalFunctions::numberToWxString(int(integer));

        int length = temp.Len();

        switch (length)
        {
        case 0:
            return _("Error");
        case 1:
            temp = wxString(wxT("0")) + GlobalResources::DECIMAL_POINT + wxT("0") + temp;
            break; //0,01
        case 2:
            temp = wxString(wxT("0")) + GlobalResources::DECIMAL_POINT + temp;
            break; //0,11
        case 3:
            temp.insert(1, GlobalResources::DECIMAL_POINT);
            break; //1,11
        case 4:
            temp = temp.substr(0, 3);
            temp.insert(2, GlobalResources::DECIMAL_POINT);
            break; //11,1
        case 5:
            temp = temp.substr(0, 3);
            break; //111
        default:
            return _("Error");
        }
    }
    return (temp + unit);
}


Zstring FreeFileSync::getFormattedDirectoryName(const Zstring& dirname)
{   //Formatting is needed since functions expect the directory to end with '\' to be able to split the relative names.
    //Also improves usability.

    Zstring dirnameTmp = dirname;
    dirnameTmp.Trim(true);  //remove whitespace characters from right
    dirnameTmp.Trim(false); //remove whitespace characters from left

    if (dirnameTmp.empty()) //an empty string is interpreted as "\"; this is not desired
        return Zstring();

    if (!endsWithPathSeparator(dirnameTmp))
        dirnameTmp += GlobalResources::FILE_NAME_SEPARATOR;

    //don't do directory formatting with wxFileName, as it doesn't respect //?/ - prefix
    return dirnameTmp;
}


void FreeFileSync::swapGrids(FileCompareResult& grid)
{
    for (FileCompareResult::iterator i = grid.begin(); i != grid.end(); ++i)
    {
        //swap compare result
        if (i->cmpResult == FILE_LEFT_SIDE_ONLY)
            i->cmpResult = FILE_RIGHT_SIDE_ONLY;
        else if (i->cmpResult == FILE_RIGHT_SIDE_ONLY)
            i->cmpResult = FILE_LEFT_SIDE_ONLY;
        else if (i->cmpResult == FILE_RIGHT_NEWER)
            i->cmpResult = FILE_LEFT_NEWER;
        else if (i->cmpResult == FILE_LEFT_NEWER)
            i->cmpResult = FILE_RIGHT_NEWER;

        //swap file descriptors
        std::swap(i->fileDescrLeft, i->fileDescrRight);
    }
}

/*
void FreeFileSync::adjustModificationTimes(const Zstring& parentDirectory, const int timeInSeconds, ErrorHandler* errorHandler) throw(AbortThisProcess)
{
#ifndef __WXDEBUG__
    wxLogNull noWxLogs; //prevent wxWidgets logging
#endif

    if (timeInSeconds == 0)
        return;

    std::vector<Zstring> fileList;
    std::vector<Zstring> dirList;

    while (true)  //should be executed in own scope so that directory access does not disturb deletion
    {
        try
        {   //get all files and folders from directory (and subdirectories)
            FreeFileSync::getAllFilesAndDirs(parentDirectory, fileList, dirList);
            break;
        }
        catch (const FileError& e)
        {
            ErrorHandler::Response rv = errorHandler->reportError(e.show());
            if (rv == ErrorHandler::IGNORE_ERROR)
                break;
            else if (rv == ErrorHandler::RETRY)
                ;   //continue with loop
            else
                assert (false);
        }
    }

    //this part is only a bit slower than direct Windows API-access!
    wxDateTime modTime;
    for (unsigned int j = 0; j < fileList.size(); ++j)
    {
        while (true)  //own scope for directory access to not disturb file access!
        {
            try
            {
                wxFileName file(fileList[j].c_str());
                if (!file.GetTimes(NULL, &modTime, NULL)) //get modification time
                    throw FileError(Zstring(_("Error changing modification time:")) + wxT(" \"") + fileList[j] + wxT("\""));

                modTime.Add(wxTimeSpan(0, 0, timeInSeconds, 0));

                if (!file.SetTimes(NULL, &modTime, NULL)) //get modification time
                    throw FileError(Zstring(_("Error changing modification time:")) + wxT(" \"") + fileList[j] + wxT("\""));

                break;
            }
            catch (const FileError& error)
            {
                ErrorHandler::Response rv = errorHandler->reportError(error.show());
                if (rv == ErrorHandler::IGNORE_ERROR)
                    break;
                else if (rv == ErrorHandler::RETRY)
                    ;   //continue with loop
                else
                    assert (false);
            }
        }
    }
}
*/

void compoundStringToTable(const Zstring& compoundInput, const DefaultChar* delimiter, std::vector<Zstring>& output)
{
    output.clear();
    Zstring input(compoundInput);

    //make sure input ends with delimiter - no problem with empty strings here
    if (!input.EndsWith(delimiter))
        input += delimiter;

    unsigned int indexStart = 0;
    unsigned int indexEnd   = 0;
    while ((indexEnd = input.find(delimiter, indexStart)) != Zstring::npos)
    {
        if (indexStart != indexEnd) //do not add empty strings
        {
            Zstring newEntry = input.substr(indexStart, indexEnd - indexStart);

            newEntry.Trim(true);  //remove whitespace characters from right
            newEntry.Trim(false); //remove whitespace characters from left

            if (!newEntry.empty())
                output.push_back(newEntry);
        }
        indexStart = indexEnd + 1;
    }
}


inline
void mergeVectors(std::vector<Zstring>& changing, const std::vector<Zstring>& input)
{
    for (std::vector<Zstring>::const_iterator i = input.begin(); i != input.end(); ++i)
        changing.push_back(*i);
}


std::vector<Zstring> compoundStringToFilter(const Zstring& filterString)
{
    //delimiters may be ';' or '\n'
    std::vector<Zstring> filterList;
    std::vector<Zstring> filterPreProcessing;
    compoundStringToTable(filterString, wxT(";"), filterPreProcessing);

    for (std::vector<Zstring>::const_iterator i = filterPreProcessing.begin(); i != filterPreProcessing.end(); ++i)
    {
        std::vector<Zstring> newEntries;
        compoundStringToTable(*i, wxT("\n"), newEntries);
        mergeVectors(filterList, newEntries);
    }

    return filterList;
}


inline
void formatFilterString(Zstring& filter)
{
#ifdef FFS_WIN
    //Windows does NOT distinguish between upper/lower-case
    filter.MakeLower();
#elif defined FFS_LINUX
    //Linux DOES distinguish between upper/lower-case
//nothing to do here
#else
    adapt;
#endif
}


void FreeFileSync::filterGridData(FileCompareResult& currentGridData, const wxString& includeFilter, const wxString& excludeFilter)
{
    //no need for regular expressions! In tests wxRegex was by factor of 10 slower than wxString::Matches()!!

    //load filter into vectors of strings
    //delimiters may be ';' or '\n'
    std::vector<Zstring> includeList = compoundStringToFilter(includeFilter.c_str());
    std::vector<Zstring> excludeList = compoundStringToFilter(excludeFilter.c_str());

    //format entries
    for (std::vector<Zstring>::iterator i = includeList.begin(); i != includeList.end(); ++i)
        formatFilterString(*i);
    for (std::vector<Zstring>::iterator i = excludeList.begin(); i != excludeList.end(); ++i)
        formatFilterString(*i);

//##############################################################

    //filter currentGridData
    for (FileCompareResult::iterator i = currentGridData.begin(); i != currentGridData.end(); ++i)
    {
        Zstring filenameLeft  = i->fileDescrLeft.fullName;
        Zstring filenameRight = i->fileDescrRight.fullName;

        formatFilterString(filenameLeft);
        formatFilterString(filenameRight);

        //process include filters
        if (i->fileDescrLeft.objType != FileDescrLine::TYPE_NOTHING)
        {
            bool includedLeft = false;
            for (std::vector<Zstring>::const_iterator j = includeList.begin(); j != includeList.end(); ++j)
                if (filenameLeft.Matches(*j))
                {
                    includedLeft = true;
                    break;
                }

            if (!includedLeft)
            {
                i->selectedForSynchronization = false;
                continue;
            }
        }

        if (i->fileDescrRight.objType != FileDescrLine::TYPE_NOTHING)
        {
            bool includedRight = false;
            for (std::vector<Zstring>::const_iterator j = includeList.begin(); j != includeList.end(); ++j)
                if (filenameRight.Matches(*j))
                {
                    includedRight = true;
                    break;
                }

            if (!includedRight)
            {
                i->selectedForSynchronization = false;
                continue;
            }
        }

        //process exclude filters
        if (i->fileDescrLeft.objType != FileDescrLine::TYPE_NOTHING)
        {
            bool excluded = false;
            for (std::vector<Zstring>::const_iterator j = excludeList.begin(); j != excludeList.end(); ++j)
                if (filenameLeft.Matches(*j))
                {
                    excluded = true;
                    break;
                }

            if (excluded)
            {
                i->selectedForSynchronization = false;
                continue;
            }
        }

        if (i->fileDescrRight.objType != FileDescrLine::TYPE_NOTHING)
        {
            bool excluded = false;
            for (std::vector<Zstring>::const_iterator j = excludeList.begin(); j != excludeList.end(); ++j)
                if (filenameRight.Matches(*j))
                {
                    excluded = true;
                    break;
                }

            if (excluded)
            {
                i->selectedForSynchronization = false;
                continue;
            }
        }

        i->selectedForSynchronization = true;
    }
}


void FreeFileSync::includeAllRowsOnGrid(FileCompareResult& currentGridData)
{
    //remove all filters on currentGridData
    for (FileCompareResult::iterator i = currentGridData.begin(); i != currentGridData.end(); ++i)
        i->selectedForSynchronization = true;
}


void FreeFileSync::excludeAllRowsOnGrid(FileCompareResult& currentGridData)
{
    //exclude all rows on currentGridData
    for (FileCompareResult::iterator i = currentGridData.begin(); i != currentGridData.end(); ++i)
        i->selectedForSynchronization = false;
}


//add(!) all files and subfolder gridlines that are dependent from the directory
template <bool searchLeftSide>
void addSubElementsOneSide(const FileCompareResult& grid, const FileCompareLine& relevantRow, std::set<int>& subElements)
{
    const FileDescrLine* fileDescr = NULL; //get descriptor for file to be deleted; evaluated at compile time
    if (searchLeftSide)
        fileDescr = &relevantRow.fileDescrLeft;
    else
        fileDescr = &relevantRow.fileDescrRight;

    if (fileDescr->objType == FileDescrLine::TYPE_DIRECTORY)
    {
        Zstring relevantDirectory(fileDescr->relativeName.c_str(), fileDescr->relativeName.length());
        relevantDirectory += GlobalResources::FILE_NAME_SEPARATOR; //FILE_NAME_SEPARATOR needed to exclude subfile/dirs only

        for (FileCompareResult::const_iterator i = grid.begin(); i != grid.end(); ++i)
        {
            if (searchLeftSide) //evaluated at compile time
            {
                if (i->fileDescrLeft.relativeName.StartsWith(relevantDirectory))
                    subElements.insert(i - grid.begin());
            }
            else
            {
                if (i->fileDescrRight.relativeName.StartsWith(relevantDirectory))
                    subElements.insert(i - grid.begin());
            }
        }
    }
}


//add(!) all files and subfolder gridlines that are dependent from the directory
void FreeFileSync::addSubElements(const FileCompareResult& grid, const FileCompareLine& relevantRow, std::set<int>& subElements)
{
    addSubElementsOneSide<true>(grid, relevantRow, subElements);
    addSubElementsOneSide<false>(grid, relevantRow, subElements);
}


//############################################################################################################
struct SortedFileName
{
    unsigned position;
    Zstring name;

    bool operator < (const SortedFileName& b) const
    {
        return position < b.position;
    }
};


//assemble message containing all files to be deleted
wxString FreeFileSync::deleteFromGridAndHDPreview(const FileCompareResult& grid,
        const std::set<int>& rowsToDeleteOnLeft,
        const std::set<int>& rowsToDeleteOnRight,
        const bool deleteOnBothSides)
{
    if (deleteOnBothSides)
    {
        //mix selected rows from left and right
        std::set<int> rowsToDelete = rowsToDeleteOnLeft;
        for (std::set<int>::const_iterator i = rowsToDeleteOnRight.begin(); i != rowsToDeleteOnRight.end(); ++i)
            rowsToDelete.insert(*i);

        wxString filesToDelete;
        for (std::set<int>::const_iterator i = rowsToDelete.begin(); i != rowsToDelete.end(); ++i)
        {
            const FileCompareLine& currentCmpLine = grid[*i];

            if (currentCmpLine.fileDescrLeft.objType != FileDescrLine::TYPE_NOTHING)
                filesToDelete += (currentCmpLine.fileDescrLeft.fullName + wxT("\n")).c_str();

            if (currentCmpLine.fileDescrRight.objType != FileDescrLine::TYPE_NOTHING)
                filesToDelete += (currentCmpLine.fileDescrRight.fullName + wxT("\n")).c_str();

            filesToDelete+= wxT("\n");
        }

        return filesToDelete;
    }
    else //delete selected files only
    {
        std::set<SortedFileName> outputTable; //sort selected files from left and right ascending by row number

        SortedFileName newEntry;
        for (std::set<int>::const_iterator i = rowsToDeleteOnLeft.begin(); i != rowsToDeleteOnLeft.end(); ++i)
        {
            const FileCompareLine& currentCmpLine = grid[*i];

            if (currentCmpLine.fileDescrLeft.objType != FileDescrLine::TYPE_NOTHING)
            {
                newEntry.position = *i * 10;
                newEntry.name = currentCmpLine.fileDescrLeft.fullName;
                outputTable.insert(newEntry);
            }
        }

        for (std::set<int>::const_iterator i = rowsToDeleteOnRight.begin(); i != rowsToDeleteOnRight.end(); ++i)
        {
            const FileCompareLine& currentCmpLine = grid[*i];

            if (currentCmpLine.fileDescrRight.objType != FileDescrLine::TYPE_NOTHING)
            {
                newEntry.position = *i * 10 + 1; //ensure files on left are before files on right for the same row number
                newEntry.name = currentCmpLine.fileDescrRight.fullName;
                outputTable.insert(newEntry);
            }
        }

        wxString filesToDelete;
        for (std::set<SortedFileName>::iterator i = outputTable.begin(); i != outputTable.end(); ++i)
            filesToDelete += (i->name + wxT("\n")).c_str();

        return filesToDelete;
    }
}


class RemoveAtExit //this class ensures, that the result of the method below is ALWAYS written on exit, even if exceptions are thrown!
{
public:
    RemoveAtExit(FileCompareResult& grid) :
            gridToWrite(grid) {}

    ~RemoveAtExit()
    {
        removeRowsFromVector(gridToWrite, rowsProcessed);
    }

    void removeRow(int nr)
    {
        rowsProcessed.insert(nr);
    }

private:
    FileCompareResult& gridToWrite;
    std::set<int> rowsProcessed;
};


template <bool leftSide> //update compareGrid row information after deletion from leftSide (or !leftSide)
inline
void updateCmpLineAfterDeletion(FileCompareLine& relevantRow, const int rowNr, RemoveAtExit& markForRemoval)
{
    FileDescrLine* fileDescr        = NULL; //get descriptor for file to be deleted; evaluated at compile time
    FileDescrLine* fileDescrPartner = NULL; //file descriptor for "other side"
    if (leftSide)
    {
        fileDescr        = &relevantRow.fileDescrLeft;
        fileDescrPartner = &relevantRow.fileDescrRight;
    }
    else
    {
        fileDescr        = &relevantRow.fileDescrRight;
        fileDescrPartner = &relevantRow.fileDescrLeft;
    }


    //remove deleted entries from grid
    if (fileDescrPartner->objType == FileDescrLine::TYPE_NOTHING)
        markForRemoval.removeRow(rowNr);
    else
    {
        //initialize fileDescr for deleted file/folder
        const Zstring saveDir = fileDescr->directory;
        *fileDescr = FileDescrLine();
        fileDescr->directory = saveDir;

        //adapt the compare result
        if (leftSide) //again evaluated at compile time
            relevantRow.cmpResult = FILE_RIGHT_SIDE_ONLY;
        else
            relevantRow.cmpResult = FILE_LEFT_SIDE_ONLY;
    }
}


template <bool leftSide>
void deleteFromGridAndHDOneSide(FileCompareResult& grid,
                                const std::set<int>& rowsToDeleteOneSide,
                                const bool useRecycleBin,
                                RemoveAtExit& markForRemoval,
                                ErrorHandler* errorHandler)
{
    for (std::set<int>::iterator i = rowsToDeleteOneSide.begin(); i != rowsToDeleteOneSide.end(); ++i)
    {
        FileCompareLine& currentCmpLine = grid[*i];

        FileDescrLine* fileDescr = NULL; //get descriptor for file to be deleted; evaluated at compile time
        if (leftSide)
            fileDescr = &currentCmpLine.fileDescrLeft;
        else
            fileDescr = &currentCmpLine.fileDescrRight;

        while (true)
        {
            try
            {
                if (fileDescr->objType == FileDescrLine::TYPE_FILE)
                    FreeFileSync::removeFile(fileDescr->fullName, useRecycleBin);
                else if (fileDescr->objType == FileDescrLine::TYPE_DIRECTORY)
                    FreeFileSync::removeDirectory(fileDescr->fullName, useRecycleBin);
                else if (fileDescr->objType == FileDescrLine::TYPE_NOTHING)
                    break; //nothing to do
                else
                {
                    assert(false);
                    break;
                }

                //retrieve all files and subfolder gridlines that are dependent from this deleted entry
                std::set<int> rowsToDelete;
                rowsToDelete.insert(*i);
                addSubElementsOneSide<leftSide>(grid, grid[*i], rowsToDelete);

                //remove deleted entries from grid (or adapt it if deleted from one side only)
                for (std::set<int>::iterator j = rowsToDelete.begin(); j != rowsToDelete.end(); ++j)
                    updateCmpLineAfterDeletion<leftSide>(grid[*j], *j, markForRemoval);

                break;
            }
            catch (const FileError& error)
            {
                //if (updateClass) -> is mandatory
                ErrorHandler::Response rv = errorHandler->reportError(error.show());

                if (rv == ErrorHandler::IGNORE_ERROR)
                    break;

                else if (rv == ErrorHandler::RETRY)
                    ;   //continue in loop
                else
                    assert (false);
            }
        }
    }
}


void FreeFileSync::deleteFromGridAndHD(FileCompareResult& grid,
                                       const std::set<int>& rowsToDeleteOnLeft,
                                       const std::set<int>& rowsToDeleteOnRight,
                                       const bool deleteOnBothSides,
                                       const bool useRecycleBin,
                                       ErrorHandler* errorHandler)
{
    //remove deleted rows from grid (AFTER all rows to be deleted are known: consider row references!
    RemoveAtExit markForRemoval(grid); //ensure that grid is always written to, even if method is exitted via exceptions

    if (deleteOnBothSides)
    {
        //mix selected rows from left and right
        std::set<int> rowsToDeleteBothSides = rowsToDeleteOnLeft;
        for (std::set<int>::const_iterator i = rowsToDeleteOnRight.begin(); i != rowsToDeleteOnRight.end(); ++i)
            rowsToDeleteBothSides.insert(*i);

        deleteFromGridAndHDOneSide<true>(grid,
                                         rowsToDeleteBothSides,
                                         useRecycleBin,
                                         markForRemoval,
                                         errorHandler);

        deleteFromGridAndHDOneSide<false>(grid,
                                          rowsToDeleteBothSides,
                                          useRecycleBin,
                                          markForRemoval,
                                          errorHandler);
    }
    else
    {
        deleteFromGridAndHDOneSide<true>(grid,
                                         rowsToDeleteOnLeft,
                                         useRecycleBin,
                                         markForRemoval,
                                         errorHandler);

        deleteFromGridAndHDOneSide<false>(grid,
                                          rowsToDeleteOnRight,
                                          useRecycleBin,
                                          markForRemoval,
                                          errorHandler);
    }
}
//############################################################################################################


inline
void writeTwoDigitNumber(unsigned int number, wxChar*& position)
{
    assert (number < 100);

    *position = '0' + number / 10;
    position[1] = '0' + number % 10;

    position += 2;
}


inline
void writeFourDigitNumber(unsigned int number, wxChar*& position)
{
    assert (number < 10000);

    *position = '0' + number / 1000;
    number %= 1000;
    position[1] = '0' + number / 100;
    number %= 100;
    position[2] = '0' + number / 10;
    number %= 10;
    position[3] = '0' + number;

    position += 4;
}


wxString FreeFileSync::utcTimeToLocalString(const wxLongLong& utcTime)
{
#ifdef FFS_WIN
    //convert ansi C time to FILETIME
    wxLongLong fileTimeLong(utcTime);
    fileTimeLong += wxLongLong(2, 3054539008UL); //timeshift between ansi C time and FILETIME in seconds == 11644473600s
    fileTimeLong *= 10000000;

    FILETIME lastWriteTimeUtc;
    lastWriteTimeUtc.dwLowDateTime  = fileTimeLong.GetLo();             //GetLo() returns unsigned
    lastWriteTimeUtc.dwHighDateTime = unsigned(fileTimeLong.GetHi());   //GetHi() returns signed

    FILETIME localFileTime;
    if (FileTimeToLocalFileTime(   //convert to local time
                &lastWriteTimeUtc, //pointer to UTC file time to convert
                &localFileTime 	   //pointer to converted file time
            ) == 0)
        throw RuntimeException(wxString(_("Conversion error:")) + wxT(" FILETIME -> local FILETIME"));

    SYSTEMTIME time;
    if (FileTimeToSystemTime(
                &localFileTime, //pointer to file time to convert
                &time 	        //pointer to structure to receive system time
            ) == 0)
        throw RuntimeException(wxString(_("Conversion error:")) + wxT(" FILETIME -> SYSTEMTIME"));

    //assemble time string (performance optimized)
    wxChar formattedTime[21];
    wxChar* p = formattedTime;

    writeFourDigitNumber(time.wYear, p);
    *(p++) = wxChar('-');
    writeTwoDigitNumber(time.wMonth, p);
    *(p++) = wxChar('-');
    writeTwoDigitNumber(time.wDay, p);
    *(p++) = wxChar(' ');
    *(p++) = wxChar(' ');
    writeTwoDigitNumber(time.wHour, p);
    *(p++) = wxChar(':');
    writeTwoDigitNumber(time.wMinute, p);
    *(p++) = wxChar(':');
    writeTwoDigitNumber(time.wSecond, p);
    *p = 0;

    return wxString(formattedTime);

#elif defined FFS_LINUX
    tm* timeinfo;
    const time_t fileTime = utcTime.ToLong();
    timeinfo = localtime(&fileTime); //convert to local time
    char buffer[50];
    strftime(buffer, 50, "%Y-%m-%d  %H:%M:%S", timeinfo);

    return wxString(buffer);
#endif
}


#ifdef FFS_WIN
Zstring FreeFileSync::getLastErrorFormatted(const unsigned long lastError) //try to get additional Windows error information
{
    unsigned long lastErrorCode = lastError;
    //determine error code if none was specified
    if (lastErrorCode == 0)
        lastErrorCode = GetLastError();

    Zstring output = Zstring(wxT("Windows Error Code ")) + wxString::Format(wxT("%u"), lastErrorCode).c_str();

    WCHAR buffer[1001];
    if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lastErrorCode, 0, buffer, 1001, NULL) != 0)
        output += Zstring(wxT(": ")) + buffer;
    return output;
}

#elif defined FFS_LINUX
Zstring FreeFileSync::getLastErrorFormatted(const int lastError) //try to get additional Linux error information
{
    int lastErrorCode = lastError;
    //determine error code if none was specified
    if (lastErrorCode == 0)
        lastErrorCode = errno;

    Zstring output = Zstring(wxT("Linux Error Code ")) + wxString::Format(wxT("%i"), lastErrorCode).c_str();
    output += Zstring(wxT(": ")) + strerror(lastErrorCode);
    return output;
}
#endif

/*Statistical theory: detect daylight saving time (DST) switch by comparing files that exist on both sides (and have same filesizes). If there are "enough"
that have a shift by +-1h then assert that DST switch occured.
What is "enough" =: N? N should be large enough AND small enough that the following two errors remain small:

Error 1: A DST switch is detected although there was none
Error 2: A DST switch is not detected although it took place

Error 1 results in lower bound, error 2 in upper bound for N.

Target: Choose N such that probability of error 1 and error 2 is lower than 0.001 (0.1%)

Definitions:
p1: probability that a file with same filesize on both sides was changed nevertheless
p2: probability that a changed file has +1h shift in filetime due to a change

M: number of files with same filesize on both sides in total
N: number of files with same filesize and time-diff +1h when DST check shall detect "true"

X: number of files with same filesize that have a +1h difference after change

Error 1 ("many files have +1h shift by chance") imposes:
Probability of error 1: (binomial distribution)

P(X >= N) = 1 - P(X <= N - 1) =
1 - sum_i=0^N-1 p3^i * (1 - p3)^(M - i) (M above i)   shall be   <= 0.0005

with p3 := p1 * p2

Probability of error 2 also will be <= 0.0005 if we choose N as lowest number that satisfies the preceding formula. Proof is left to the reader.

The function M |-> N behaves almost linearly and can be empirically approximated by:

N(M) =

2                   for      0 <= M <= 500
125/1000000 * M + 5 for    500 <  M <= 50000
77/1000000 * M + 10 for  50000 <  M <= 400000
60/1000000 * M + 35 for 400000 <  M


inline
bool sameFileTime(const time_t a, const time_t b)
{
    if (a < b)
        return b - a <= FILE_TIME_PRECISION;
    else
        return a - b <= FILE_TIME_PRECISION;
}


#ifdef FFS_WIN
unsigned int getThreshold(const unsigned filesWithSameSizeTotal)
{
    if (filesWithSameSizeTotal <= 500)
        return 2;
    else if (filesWithSameSizeTotal <= 50000)
        return unsigned(125.0/1000000 * filesWithSameSizeTotal + 5.0);
    else if (filesWithSameSizeTotal <= 400000)
        return unsigned(77.0/1000000 * filesWithSameSizeTotal + 10.0);
    else
        return unsigned(60.0/1000000 * filesWithSameSizeTotal + 35.0);
}


void FreeFileSync::checkForDSTChange(const FileCompareResult& gridData,
                                     const std::vector<FolderPair>& directoryPairsFormatted,
                                     int& timeShift, wxString& driveName)
{
    driveName.Clear();
    timeShift = 0;

    TIME_ZONE_INFORMATION dummy;
    DWORD rv = GetTimeZoneInformation(&dummy);
    if (rv == TIME_ZONE_ID_UNKNOWN) return;
    bool dstActive = rv == TIME_ZONE_ID_DAYLIGHT;

    for (std::vector<FolderPair>::const_iterator i = directoryPairsFormatted.begin(); i != directoryPairsFormatted.end(); ++i)
    {
        bool leftDirIsFat = isFatDrive(i->leftDirectory);
        bool rightDirIsFat = isFatDrive(i->rightDirectory);

        if (leftDirIsFat || rightDirIsFat)
        {
            unsigned int filesTotal        = 0; //total number of files (with same size on both sides)
            unsigned int plusOneHourCount  = 0; //number of files with +1h time shift
            unsigned int minusOneHourCount = 0; // "

            for (FileCompareResult::const_iterator j = gridData.begin(); j != gridData.end(); ++j)
            {
                const FileDescrLine& leftFile  = j->fileDescrLeft;
                const FileDescrLine& rightFile = j->fileDescrRight;

                if (    leftFile.objType == FileDescrLine::TYPE_FILE && rightFile.objType == FileDescrLine::TYPE_FILE &&
                        leftFile.fileSize == rightFile.fileSize &&
                        leftFile.directory.CmpNoCase(i->leftDirectory.c_str()) == 0 && //Windows does NOT distinguish between upper/lower-case
                        rightFile.directory.CmpNoCase(i->rightDirectory.c_str()) == 0) //
                {
                    ++filesTotal;

                    if (sameFileTime(leftFile.lastWriteTimeRaw - 3600, rightFile.lastWriteTimeRaw))
                        ++plusOneHourCount;
                    else if (sameFileTime(leftFile.lastWriteTimeRaw + 3600, rightFile.lastWriteTimeRaw))
                        ++minusOneHourCount;
                }
            }

            unsigned int threshold = getThreshold(filesTotal);
            if (plusOneHourCount >= threshold)
            {
                if (dstActive)
                {
                    if (rightDirIsFat) //it should be FAT; else this were some kind of error
                    {
                        timeShift = 3600;
                        driveName = getDriveName(i->rightDirectory);
                    }
                }
                else
                {
                    if (leftDirIsFat) //it should be FAT; else this were some kind of error
                    {
                        timeShift = -3600;
                        driveName = getDriveName(i->leftDirectory);
                    }
                }
                return;
            }
            else if (minusOneHourCount >= threshold)
            {
                if (dstActive)
                {
                    if (leftDirIsFat) //it should be FAT; else this were some kind of error
                    {
                        timeShift = 3600;
                        driveName = getDriveName(i->leftDirectory);
                    }
                }
                else
                {
                    if (rightDirIsFat) //it should be FAT; else this were some kind of error
                    {
                        timeShift = -3600;
                        driveName = getDriveName(i->rightDirectory);
                    }
                }
                return;
            }
        }
    }
}
#endif  //FFS_WIN
*/
bgstack15