File: bkSort.c

package info (click to toggle)
isomaster 1.3.9-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,024 kB
  • ctags: 844
  • sloc: ansic: 11,224; makefile: 235; sh: 109; python: 11
file content (117 lines) | stat: -rw-r--r-- 3,211 bytes parent folder | download | duplicates (5)
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
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software 
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/

/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
* 
******************************************************************************/

#include <string.h>
#include <sys/types.h>

#include "bk.h"
#include "bkInternal.h"
#include "bkSort.h"

/* strings cannot be equal */
bool rightIsBigger(char* leftStr, char* rightStr)
{
    size_t leftLen;
    size_t rightLen;
    size_t count;
    bool resultFound;
    bool rc;
    
    leftLen = strlen(leftStr);
    rightLen = strlen(rightStr);
    
    resultFound = false;
    for(count = 0; count < leftLen && count < rightLen && !resultFound; count++)
    {
        if(rightStr[count] > leftStr[count])
        {
            resultFound = true;
            rc = true;
        }
        else if(rightStr[count] < leftStr[count])
        {
            resultFound = true;
            rc = false;
        }
    }
    
    if(!resultFound)
    /* strings are the same up to the length of the shorter one */
    {
        if(rightLen > leftLen)
            rc = true;
        else
            rc = false;
    }
    
    return rc;
}

void sortDir(DirToWrite* dir, int filenameType)
{
    BaseToWrite* child;
    BaseToWrite** outerPtr;
    BaseToWrite** innerPtr;
    
    child = dir->children;
    while(child != NULL)
    {
        if(IS_DIR(child->posixFileMode))
            sortDir(DIRTW_PTR(child), filenameType);
        
        child = child->next;
    }
    
    outerPtr = &(dir->children);
    while(*outerPtr != NULL)
    {
        innerPtr = &((*outerPtr)->next);
        while(*innerPtr != NULL)
        {
            if( (filenameType & FNTYPE_JOLIET &&
                 rightIsBigger((*innerPtr)->nameJoliet, (*outerPtr)->nameJoliet)) || 
                (filenameType & FNTYPE_9660 &&
                 rightIsBigger((*innerPtr)->name9660, (*outerPtr)->name9660)) )
            {
                BaseToWrite* outer = *outerPtr;
                BaseToWrite* inner = *innerPtr;
                
                if( (*outerPtr)->next != *innerPtr )
                {
                    BaseToWrite* oldInnerNext = inner->next;

                    *outerPtr = inner;
                    *innerPtr = outer;
                    
                    inner->next = outer->next;
                    outer->next = oldInnerNext;
                }
                else
                {
                    BaseToWrite* oldInnerNext = inner->next;

                    *outerPtr = inner;
                    innerPtr = &(inner->next);
                    
                    inner->next = outer;
                    outer->next = oldInnerNext;
                }
            }
            
            innerPtr = &((*innerPtr)->next);
        }
        
        outerPtr = &((*outerPtr)->next);
    }
}