Skip to content

Commit 23305a9

Browse files
Replace str{n,}casecmp with string_case_compare
Configure string_case_compare.h to either delegate to <strings.h> functions or declare replacements.
1 parent 0b1a006 commit 23305a9

25 files changed

+210
-145
lines changed

config/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ add_library(config
66
common.cpp common.h
77
config2.h
88
msdos.h
9+
"${CMAKE_CURRENT_BINARY_DIR}/include/string_case_compare.h" ${STRING_CASE_COMPARE_SOURCES}
910
typedef.h
1011
user_id.h
12+
"${STRING_CASE_COMPARE_H}"
1113
cmake/config.h.in
1214
cmake/configure_trn.cmake
15+
cmake/string_case_compare.Linux.h.in
16+
cmake/string_case_compare.Windows.h.in
1317
"${CMAKE_CURRENT_BINARY_DIR}/include/config.h")
1418
target_include_directories(config PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include" .)
1519
set_target_properties(config PROPERTIES FOLDER "Libraries")

config/cmake/configure_trn.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function(configure_trn)
77
#
88
check_include_file(pwd.h I_PWD)
99
check_include_file(sgtty.h I_SGTTY)
10+
check_include_file(strings.h I_STRINGS)
1011
check_include_file(sys/filio.h I_SYS_FILIO)
1112
check_include_file(sys/ioctl.h I_SYS_IOCTL)
1213
check_include_file(sys/stat.h I_SYS_STAT)
@@ -20,6 +21,9 @@ function(configure_trn)
2021
if(I_PWD)
2122
check_symbol_exists(getpwent "pwd.h" HAS_GETPWENT)
2223
endif()
24+
if(I_STRINGS)
25+
check_symbol_exists(strcasecmp "strings.h" HAS_STRCASECMP)
26+
endif()
2327
if(I_UNISTD)
2428
check_symbol_exists(getcwd "unistd.h" HAS_GETCWD)
2529
check_symbol_exists(getdomainname "unistd.h" HAS_GETDOMAINNAME)
@@ -62,5 +66,17 @@ function(configure_trn)
6266
set(EXTRAINEWS "")
6367
set(GROUPDESC "")
6468
set(NEWSSPOOL "%X/spool")
69+
6570
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/config.h.in" include/config.h)
71+
set(STRING_CASE_COMPARE_SOURCES "")
72+
if(NOT HAS_STRCASECMP)
73+
set(STRING_CASE_COMPARE_SOURCES "string_case_compare.cpp")
74+
endif()
75+
set(STRING_CASE_COMPARE_H "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/string_case_compare.${CMAKE_HOST_SYSTEM_NAME}.h.in")
76+
if(NOT EXISTS "${STRING_CASE_COMPARE_H}")
77+
message(FATAL_ERROR "Unknown system ${CMAKE_HOST_SYSTEM_NAME}; expected 'Linux' or 'Windows'")
78+
endif()
79+
configure_file("${STRING_CASE_COMPARE_H}" "include/string_case_compare.h")
80+
set(STRING_CASE_COMPARE_SOURCES "${STRING_CASE_COMPARE_SOURCES}" PARENT_SCOPE)
81+
set(STRING_CASE_COMPARE_H "${STRING_CASE_COMPARE_H}" PARENT_SCOPE)
6682
endfunction()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <strings.h>
4+
5+
inline int string_case_compare(const char *s1, const char *s2)
6+
{
7+
return strcasecmp(s1, s2);
8+
}
9+
inline int string_case_compare(const char *s1, const char *s2, int len)
10+
{
11+
return strncasecmp(s1, s2, int len);
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
int string_case_compare(const char *s1, const char *s2);
4+
int string_case_compare(const char *s1, const char *s2, int len);

config/string_case_compare.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "string_case_compare.h"
2+
3+
#include "typedef.h"
4+
5+
static Uchar casemap[256] = {
6+
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
7+
0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
8+
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
9+
0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
10+
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
11+
0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
12+
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
13+
0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
14+
0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
15+
0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
16+
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
17+
0x78,0x79,0x7A,0x7B,0x5C,0x5D,0x5E,0x5F,
18+
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
19+
0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
20+
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
21+
0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
22+
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
23+
0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
24+
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
25+
0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
26+
0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
27+
0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
28+
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
29+
0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
30+
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,
31+
0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
32+
0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,
33+
0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
34+
0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,
35+
0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
36+
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
37+
0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
38+
};
39+
40+
int string_case_compare(const char *s1, const char *s2)
41+
{
42+
do {
43+
if (casemap[(Uchar)*s1++] != casemap[(Uchar)*s2])
44+
return casemap[(Uchar)s1[-1]] - casemap[(Uchar)*s2];
45+
} while (*s2++ != '\0');
46+
return 0;
47+
}
48+
49+
int string_case_compare(const char *s1, const char *s2, int len)
50+
{
51+
while (len--) {
52+
if (casemap[(Uchar)*s1++] != casemap[(Uchar)*s2])
53+
return casemap[(Uchar)s1[-1]] - casemap[(Uchar)*s2];
54+
if (*s2++ == '\0')
55+
break;
56+
}
57+
return 0;
58+
}

inews/inews.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <string>
66

7+
#include <string_case_compare.h>
8+
79
#include "common.h"
810

911
#include "env.h"
@@ -173,9 +175,9 @@ int main(int argc, char *argv[])
173175
break;
174176
}
175177
in_header = true;
176-
if (!strncasecmp(cp, "From:", 5))
178+
if (!string_case_compare(cp, "From:", 5))
177179
has_fromline = true;
178-
else if (!strncasecmp(cp, "Path:", 5))
180+
else if (!string_case_compare(cp, "Path:", 5))
179181
has_pathline = true;
180182
}
181183
artpos += len;
@@ -359,7 +361,7 @@ int nntp_handle_timeout()
359361
static bool handling_timeout = false;
360362
char last_command_save[NNTP_STRLEN];
361363

362-
if (!strcasecmp(g_last_command,"quit"))
364+
if (!string_case_compare(g_last_command,"quit"))
363365
return 0;
364366
if (handling_timeout)
365367
return -1;

libtrn/addng.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
*/
33
/* This software is copyrighted as detailed in the LICENSE file. */
44

5+
#include <string_case_compare.h>
6+
57
#include "common.h"
68
#include "addng.h"
79

@@ -388,7 +390,7 @@ static int agorder_number(const ADDGROUP **app1, const ADDGROUP **app2)
388390

389391
static int agorder_groupname(const ADDGROUP **app1, const ADDGROUP **app2)
390392
{
391-
return strcasecmp((*app1)->name, (*app2)->name) * g_sel_direction;
393+
return string_case_compare((*app1)->name, (*app2)->name) * g_sel_direction;
392394
}
393395

394396
static int agorder_count(const ADDGROUP **app1, const ADDGROUP **app2)

libtrn/color.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
** are used, otherwise only normal monochrome video attributes.
2626
*/
2727

28+
#include <string_case_compare.h>
29+
2830
#include "common.h"
2931
#include "color.h"
3032

@@ -127,7 +129,7 @@ void color_rc_attribute(const char *object, char *value)
127129
/* Find the specified object. */
128130
int i;
129131
for (i = 0; i < MAX_COLORS; i++) {
130-
if (!strcasecmp(object, s_objects[i].name))
132+
if (!string_case_compare(object, s_objects[i].name))
131133
break;
132134
}
133135
if (i >= MAX_COLORS) {

libtrn/datasrc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
/* This software is copyrighted as detailed in the LICENSE file. */
55

6+
#include <string_case_compare.h>
7+
68
#include "common.h"
79
#include "datasrc.h"
810

@@ -191,7 +193,7 @@ char *read_datasrcs(const char *filename)
191193
while ((s = next_ini_section(s,&section,&cond)) != nullptr) {
192194
if (*cond && !check_ini_cond(cond))
193195
continue;
194-
if (!strncasecmp(section, "group ", 6))
196+
if (!string_case_compare(section, "group ", 6))
195197
continue;
196198
s = parse_ini_section(s, s_datasrc_ini);
197199
if (!s)

libtrn/decode.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
*/
33
/* This software is copyrighted as detailed in the LICENSE file. */
44

5+
#include <string_case_compare.h>
6+
57
#include "common.h"
68
#include "decode.h"
79

@@ -83,15 +85,15 @@ static bool bad_filename(const char *filename)
8385
int len = strlen(filename);
8486
#ifdef MSDOS
8587
if (len == 3) {
86-
if (!strcasecmp(filename, "aux") || !strcasecmp(filename, "con")
87-
|| !strcasecmp(filename, "nul") || !strcasecmp(filename, "prn"))
88+
if (!string_case_compare(filename, "aux") || !string_case_compare(filename, "con")
89+
|| !string_case_compare(filename, "nul") || !string_case_compare(filename, "prn"))
8890
return true;
8991
}
9092
else if (len == 4) {
91-
if (!strcasecmp(filename, "com1") || !strcasecmp(filename, "com2")
92-
|| !strcasecmp(filename, "com3") || !strcasecmp(filename, "com4")
93-
|| !strcasecmp(filename, "lpt1") || !strcasecmp(filename, "lpt2")
94-
|| !strcasecmp(filename, "lpt3"))
93+
if (!string_case_compare(filename, "com1") || !string_case_compare(filename, "com2")
94+
|| !string_case_compare(filename, "com3") || !string_case_compare(filename, "com4")
95+
|| !string_case_compare(filename, "lpt1") || !string_case_compare(filename, "lpt2")
96+
|| !string_case_compare(filename, "lpt3"))
9597
return true;
9698
}
9799
#else
@@ -123,11 +125,11 @@ char *decode_subject(ART_NUM artnum, int *partp, int *totalp)
123125
/* Skip leading whitespace and other garbage */
124126
char *s = subject;
125127
while (is_hor_space(*s) || *s == '-') s++;
126-
if (!strncasecmp(s, "repost", 6)) {
128+
if (!string_case_compare(s, "repost", 6)) {
127129
for (s += 6; is_hor_space(*s) || *s == ':' || *s == '-'; s++);
128130
}
129131

130-
while (!strncasecmp(s, "re:", 3)) {
132+
while (!string_case_compare(s, "re:", 3)) {
131133
s = skip_space(s + 3);
132134
}
133135

@@ -202,7 +204,7 @@ char *decode_subject(ART_NUM artnum, int *partp, int *totalp)
202204
}
203205

204206
/* look for "6 parts" or "part 1" */
205-
if (!strncasecmp("part", s, 4)) {
207+
if (!string_case_compare("part", s, 4)) {
206208
if (s[4] == 's') {
207209
for (t = s; t >= subject && !isdigit(*t); t--);
208210
if (t > subject) {

0 commit comments

Comments
 (0)