Skip to content

Commit 6666b0c

Browse files
committed
Add natural sort; Up version to 0.1.2
1 parent 835f3a5 commit 6666b0c

File tree

8 files changed

+222
-8
lines changed

8 files changed

+222
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ set(CMAKE_CXX_STANDARD 11)
66
find_package(wxWidgets COMPONENTS core base REQUIRED)
77
include(${wxWidgets_USE_FILE})
88

9-
add_executable(launch_ssh src/App.cpp src/MainFrame.cpp src/HistoryFile.cpp)
9+
add_executable(launch_ssh src/App.cpp src/MainFrame.cpp src/HistoryFile.cpp src/strnatcmp.c)
1010
target_link_libraries(launch_ssh ${wxWidgets_LIBRARIES})

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=0.1.1
1+
VERSION=0.1.2
22
CODENAME=`lsb_release -c | cut -f2`
33

44
.PHONY: all clean build-deb
@@ -9,7 +9,7 @@ clean: rm -rf build/*
99

1010
build-deb:
1111
mkdir -p build/deb/DEBIAN build/deb/usr/bin build/deb/usr/share/applications
12-
cp debian/control build/deb/DEBIAN/
12+
sed -e 's/\$$VERSION\$$/${VERSION}/g' debian/control > build/deb/DEBIAN/control
1313
cp debian/launch-ssh.desktop build/deb/usr/share/applications/
1414
cp cmake-build-release/launch_ssh build/deb/usr/bin/
1515
dpkg-deb --build build/deb build/launch-ssh_${VERSION}-${CODENAME}_amd64.deb

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Delete records from your history you don't want using the `DELETE` key.
2424

2525
Download and install:
2626

27-
* [launch-ssh_0.1-xenial_amd64.deb](https://github.com/DemonTPx/launch-ssh/releases/download/0.1/launch-ssh_0.1-xenial_amd64.deb)
28-
* [launch-ssh_0.1-artful_amd64.deb](https://github.com/DemonTPx/launch-ssh/releases/download/0.1/launch-ssh_0.1-artful_amd64.deb)
27+
* [launch-ssh_0.1.2-bionic_amd64.deb](https://github.com/DemonTPx/launch-ssh/releases/download/0.1/launch-ssh_0.1.2-bionic_amd64.deb)
28+
* [launch-ssh_0.1.2-xenial_amd64.deb](https://github.com/DemonTPx/launch-ssh/releases/download/0.1/launch-ssh_0.1.2-xenial_amd64.deb)
2929

3030
It is recommended that you add a global keyboard shortcut (eg. to `SUPER+Q`) which runs `launch_ssh` to easily launch SSH sessions using this application.
3131

@@ -45,10 +45,14 @@ Then run the following commands from the working copy:
4545

4646
## Credits
4747

48+
The `strnatcmp` code was written by Martin Pool and was taken from [Github](https://github.com/sourcefrog/natsort).
49+
4850
The icon was taken from [moka-icon-theme](https://github.com/snwh/moka-icon-theme) by [Sam Hewitt](https://github.com/snwh) which is under a [Creative Commons Attribution ShareAlike 4.0](https://creativecommons.org/licenses/by-sa/4.0) license.
4951

5052
## License
5153

5254
The code is under MIT ([LICENSE](LICENSE)).
5355

56+
The `strnatcmp` code's license can be found in the [source](src/strnatcmp.c) and [header](src/strnatcmp.h) files itself.
57+
5458
The icon is under [Creative Commons Attribution ShareAlike 4.0](https://creativecommons.org/licenses/by-sa/4.0)

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: launch-ssh
2-
Version: 0.1.1
2+
Version: $VERSION$
33
Maintainer: Bert Hekman <demontpx@gmail.com>
44
Homepage: https://github.com/DemonTPx/launch-ssh
55
Architecture: amd64

src/MainFrame.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "MainFrame.hpp"
22

3+
extern "C" {
4+
#include "strnatcmp.h"
5+
}
6+
37
#include <algorithm>
48

59
MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size):
@@ -162,7 +166,7 @@ void MainFrame::Launch(const wxString &target) {
162166

163167
if (history.Index(target) == wxNOT_FOUND) {
164168
history.push_back(target);
165-
history.Sort();
169+
history.Sort(NaturalCompare);
166170
historyFile.Save(history);
167171
}
168172
}
@@ -220,4 +224,8 @@ void MainFrame::DeleteSelection() {
220224
}
221225

222226
RefreshList();
223-
}
227+
}
228+
229+
int MainFrame::NaturalCompare(const wxString &a, const wxString &b) {
230+
return strnatcmp(a.c_str(), b.c_str());
231+
}

src/MainFrame.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class MainFrame : public wxFrame {
3333

3434
void DeleteSelection();
3535

36+
static int NaturalCompare(const wxString &a, const wxString &b);
37+
3638
HistoryFile historyFile;
3739

3840
wxArrayString history;

src/strnatcmp.c

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/* -*- mode: c; c-file-style: "k&r" -*-
2+
strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
3+
Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
Permission is granted to anyone to use this software for any purpose,
8+
including commercial applications, and to alter it and redistribute it
9+
freely, subject to the following restrictions:
10+
1. The origin of this software must not be misrepresented; you must not
11+
claim that you wrote the original software. If you use this software
12+
in a product, an acknowledgment in the product documentation would be
13+
appreciated but is not required.
14+
2. Altered source versions must be plainly marked as such, and must not be
15+
misrepresented as being the original software.
16+
3. This notice may not be removed or altered from any source distribution.
17+
*/
18+
19+
20+
/* partial change history:
21+
*
22+
* 2004-10-10 mbp: Lift out character type dependencies into macros.
23+
*
24+
* Eric Sosman pointed out that ctype functions take a parameter whose
25+
* value must be that of an unsigned int, even on platforms that have
26+
* negative chars in their default char type.
27+
*/
28+
29+
#include <stddef.h> /* size_t */
30+
#include <ctype.h>
31+
32+
#include "strnatcmp.h"
33+
34+
35+
/* These are defined as macros to make it easier to adapt this code to
36+
* different characters types or comparison functions. */
37+
static inline int
38+
nat_isdigit(nat_char a)
39+
{
40+
return isdigit((unsigned char) a);
41+
}
42+
43+
44+
static inline int
45+
nat_isspace(nat_char a)
46+
{
47+
return isspace((unsigned char) a);
48+
}
49+
50+
51+
static inline nat_char
52+
nat_toupper(nat_char a)
53+
{
54+
return toupper((unsigned char) a);
55+
}
56+
57+
58+
static int
59+
compare_right(nat_char const *a, nat_char const *b)
60+
{
61+
int bias = 0;
62+
63+
/* The longest run of digits wins. That aside, the greatest
64+
value wins, but we can't know that it will until we've scanned
65+
both numbers to know that they have the same magnitude, so we
66+
remember it in BIAS. */
67+
for (;; a++, b++) {
68+
if (!nat_isdigit(*a) && !nat_isdigit(*b))
69+
return bias;
70+
if (!nat_isdigit(*a))
71+
return -1;
72+
if (!nat_isdigit(*b))
73+
return +1;
74+
if (*a < *b) {
75+
if (!bias)
76+
bias = -1;
77+
} else if (*a > *b) {
78+
if (!bias)
79+
bias = +1;
80+
} else if (!*a && !*b)
81+
return bias;
82+
}
83+
84+
return 0;
85+
}
86+
87+
88+
static int
89+
compare_left(nat_char const *a, nat_char const *b)
90+
{
91+
/* Compare two left-aligned numbers: the first to have a
92+
different value wins. */
93+
for (;; a++, b++) {
94+
if (!nat_isdigit(*a) && !nat_isdigit(*b))
95+
return 0;
96+
if (!nat_isdigit(*a))
97+
return -1;
98+
if (!nat_isdigit(*b))
99+
return +1;
100+
if (*a < *b)
101+
return -1;
102+
if (*a > *b)
103+
return +1;
104+
}
105+
106+
return 0;
107+
}
108+
109+
110+
static int
111+
strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
112+
{
113+
int ai, bi;
114+
nat_char ca, cb;
115+
int fractional, result;
116+
117+
ai = bi = 0;
118+
while (1) {
119+
ca = a[ai]; cb = b[bi];
120+
121+
/* skip over leading spaces or zeros */
122+
while (nat_isspace(ca))
123+
ca = a[++ai];
124+
125+
while (nat_isspace(cb))
126+
cb = b[++bi];
127+
128+
/* process run of digits */
129+
if (nat_isdigit(ca) && nat_isdigit(cb)) {
130+
fractional = (ca == '0' || cb == '0');
131+
132+
if (fractional) {
133+
if ((result = compare_left(a+ai, b+bi)) != 0)
134+
return result;
135+
} else {
136+
if ((result = compare_right(a+ai, b+bi)) != 0)
137+
return result;
138+
}
139+
}
140+
141+
if (!ca && !cb) {
142+
/* The strings compare the same. Perhaps the caller
143+
will want to call strcmp to break the tie. */
144+
return 0;
145+
}
146+
147+
if (fold_case) {
148+
ca = nat_toupper(ca);
149+
cb = nat_toupper(cb);
150+
}
151+
152+
if (ca < cb)
153+
return -1;
154+
155+
if (ca > cb)
156+
return +1;
157+
158+
++ai; ++bi;
159+
}
160+
}
161+
162+
163+
int
164+
strnatcmp(nat_char const *a, nat_char const *b) {
165+
return strnatcmp0(a, b, 0);
166+
}
167+
168+
169+
/* Compare, recognizing numeric string and ignoring case. */
170+
int
171+
strnatcasecmp(nat_char const *a, nat_char const *b) {
172+
return strnatcmp0(a, b, 1);
173+
}

src/strnatcmp.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* -*- mode: c; c-file-style: "k&r" -*-
2+
strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
3+
Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
Permission is granted to anyone to use this software for any purpose,
8+
including commercial applications, and to alter it and redistribute it
9+
freely, subject to the following restrictions:
10+
1. The origin of this software must not be misrepresented; you must not
11+
claim that you wrote the original software. If you use this software
12+
in a product, an acknowledgment in the product documentation would be
13+
appreciated but is not required.
14+
2. Altered source versions must be plainly marked as such, and must not be
15+
misrepresented as being the original software.
16+
3. This notice may not be removed or altered from any source distribution.
17+
*/
18+
19+
20+
/* CUSTOMIZATION SECTION
21+
*
22+
* You can change this typedef, but must then also change the inline
23+
* functions in strnatcmp.c */
24+
typedef char nat_char;
25+
26+
int strnatcmp(nat_char const *a, nat_char const *b);
27+
int strnatcasecmp(nat_char const *a, nat_char const *b);

0 commit comments

Comments
 (0)