summaryrefslogtreecommitdiff
blob: f2f96a82d26e345d9d97c51c4b04b8227055a956 (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
From d8c939211426b1754fc8c28f5c6d5f9e11c54842 Mon Sep 17 00:00:00 2001
From: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
Date: Mon, 26 Jul 2010 18:17:27 +0200
Subject: [PATCH 1/2] paludis sort world

---
 paludis/environments/paludis/world.cc            |    2 +
 paludis/environments/paludis/world_TEST.cc       |    4 +-
 paludis/environments/paludis/world_TEST_setup.sh |    4 +-
 paludis/set_file.cc                              |   56 ++++++++++++++++++++++
 paludis/set_file.hh                              |    5 ++
 5 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/paludis/environments/paludis/world.cc b/paludis/environments/paludis/world.cc
index b546d4f..8d281e9 100644
--- a/paludis/environments/paludis/world.cc
+++ b/paludis/environments/paludis/world.cc
@@ -123,6 +123,7 @@ World::_add_string_to_world(const std::string & n) const
                 n::type() = sft_simple
                 ));
     bool result(world.add(n));
+    world.sort();
     world.rewrite();
 
     return result;
@@ -156,6 +157,7 @@ World::_remove_string_from_world(const std::string & n) const
                 ));
 
         result = world.remove(n);
+        world.sort();
         world.rewrite();
     }
 
diff --git a/paludis/environments/paludis/world_TEST.cc b/paludis/environments/paludis/world_TEST.cc
index cbb2f7f..237d1df 100644
--- a/paludis/environments/paludis/world_TEST.cc
+++ b/paludis/environments/paludis/world_TEST.cc
@@ -47,9 +47,9 @@ TEST(World, Updates)
     SafeIFStream f(*w);
     std::string ff((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
     EXPECT_EQ(
-            "cat/unchanged\n"
+            "cat/after\n"
             "cat/alsounchanged\n"
-            "cat/after\n",
+            "cat/unchanged\n",
             ff
             );
 }
diff --git a/paludis/environments/paludis/world_TEST_setup.sh b/paludis/environments/paludis/world_TEST_setup.sh
index f81a62b..0d8916c 100755
--- a/paludis/environments/paludis/world_TEST_setup.sh
+++ b/paludis/environments/paludis/world_TEST_setup.sh
@@ -5,8 +5,8 @@ mkdir world_TEST_dir || exit 2
 cd world_TEST_dir || exit 3
 
 cat <<END > world
-cat/unchanged
-cat/before
 cat/alsounchanged
+cat/before
+cat/unchanged
 END
 
diff --git a/paludis/set_file.cc b/paludis/set_file.cc
index 646ccf4..f786c5d 100644
--- a/paludis/set_file.cc
+++ b/paludis/set_file.cc
@@ -60,12 +60,15 @@ namespace
         protected:
             SetFileHandler();
 
+            static bool compareNoCase(const std::string &, const std::string &);
+
         public:
             virtual ~SetFileHandler();
 
             virtual std::shared_ptr<SetSpecTree> contents() const = 0;
             virtual bool add(const std::string &) = 0;
             virtual bool remove(const std::string &) = 0;
+            virtual void sort() = 0;
             virtual void rewrite() const = 0;
     };
 
@@ -87,6 +90,7 @@ namespace
             virtual std::shared_ptr<SetSpecTree> contents() const;
             virtual bool add(const std::string &);
             virtual bool remove(const std::string &);
+            virtual void sort();
             virtual void rewrite() const;
     };
 
@@ -103,6 +107,7 @@ namespace
             virtual std::shared_ptr<SetSpecTree> contents() const;
             virtual bool add(const std::string &) PALUDIS_ATTRIBUTE((noreturn));
             virtual bool remove(const std::string &) PALUDIS_ATTRIBUTE((noreturn));
+            virtual void sort() PALUDIS_ATTRIBUTE((noreturn));
             virtual void rewrite() const PALUDIS_ATTRIBUTE((noreturn));
     };
 
@@ -124,6 +129,7 @@ namespace
             virtual std::shared_ptr<SetSpecTree> contents() const;
             virtual bool add(const std::string &);
             virtual bool remove(const std::string &);
+            virtual void sort();
             virtual void rewrite() const;
     };
 
@@ -303,6 +309,22 @@ SetFileHandler::~SetFileHandler()
 {
 }
 
+bool
+SetFileHandler::compareNoCase(const std::string & first, const std::string & second)
+{
+    for (unsigned int i(0) ; i < first.length() && i < second.length() ; ++i)
+    {
+        if (tolower(first[i]) < tolower(second[i]))
+            return true;
+        else if (tolower(first[i]) > tolower(second[i]))
+            return false;
+    }
+    if (first.length() < second.length())
+        return true;
+    return false;
+}
+
+
 SimpleHandler::SimpleHandler(const SetFileParams & p) :
     _p(p)
 {
@@ -408,6 +430,17 @@ SimpleHandler::remove(const std::string & p)
 }
 
 void
+SimpleHandler::sort()
+{
+    Lock l(_mutex);
+
+    Context context("When sorting simple set file '" + stringify(_p.file_name()) + "':");
+
+    _lines.sort(SetFileHandler::compareNoCase);
+    _contents.reset();
+}
+
+void
 SimpleHandler::rewrite() const
 {
     Lock l(_mutex);
@@ -501,6 +534,17 @@ PaludisConfHandler::remove(const std::string & p)
 }
 
 void
+PaludisConfHandler::sort()
+{
+    Context context("When sorting paludis conf set file '" + stringify(_p.file_name()) + "':");
+
+    Lock l(_mutex);
+
+    _lines.sort(SetFileHandler::compareNoCase);
+    _contents.reset();
+}
+
+void
 PaludisConfHandler::rewrite() const
 {
     Context context("When rewriting paludis conf set file '" + stringify(_p.file_name()) + "':");
@@ -572,6 +616,12 @@ PaludisBashHandler::remove(const std::string & p)
 }
 
 void
+PaludisBashHandler::sort()
+{
+    throw SetFileError(_p.file_name(), "Cannot modify bash script '" + stringify(_p.file_name()) + "'");
+}
+
+void
 PaludisBashHandler::rewrite() const
 {
     throw SetFileError(_p.file_name(), "Cannot modify bash script '" + stringify(_p.file_name()) + "'");
@@ -609,6 +659,12 @@ SetFile::contents() const
 }
 
 void
+SetFile::sort()
+{
+    _imp->handler->sort();
+}
+
+void
 SetFile::rewrite() const
 {
     _imp->handler->rewrite();
diff --git a/paludis/set_file.hh b/paludis/set_file.hh
index e3a853c..019ccbb 100644
--- a/paludis/set_file.hh
+++ b/paludis/set_file.hh
@@ -132,6 +132,11 @@ namespace paludis
             const std::shared_ptr<const SetSpecTree> contents() const;
 
             /**
+             * Sort our contents.
+             */
+            void sort();
+
+            /**
              * Rewrite our contents.
              */
             void rewrite() const;
-- 
1.7.6.rc2.8.g28eb.dirty