File: irc_string.c

package info (click to toggle)
dircproxy 1.0.5-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,120 kB
  • ctags: 740
  • sloc: ansic: 9,466; sh: 2,946; makefile: 113; perl: 70
file content (112 lines) | stat: -rw-r--r-- 2,223 bytes parent folder | download | duplicates (9)
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
/* dircproxy
 * Copyright (C) 2002 Scott James Remnant <scott@netsplit.com>.
 * All Rights Reserved.
 *
 * irc_string.c
 *  - Case conversion functions for IRC protocol
 *  - Comparison and match functions for IRC protocol
 * --
 * @(#) $Id: irc_string.c,v 1.8 2001/12/21 20:15:55 keybuk Exp $
 *
 * This file is distributed according to the GNU General Public
 * License.  For full details, read the top of 'main.c' or the
 * file called COPYING that was distributed with this code.
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <dircproxy.h>
#include "match.h"
#include "sprintf.h"
#include "irc_string.h"

/* forward declarations */
static int _irc_tolower(int);
static int _irc_toupper(int);

/* IRC version of tolower */
static int _irc_tolower(int c) {
  switch (c) {
    case '[':
      return '{';
    case ']':
      return '}';
    case '\\':
      return '|';
    default:
      return tolower(c);
  }
}

/* IRC version of tolower */
static int _irc_toupper(int c) {
  switch (c) {
    case '{':
      return '[';
    case '}':
      return ']';
    case '|':
      return '\\';
    default:
      return toupper(c);
  }
}

/* Changes the case of a string to lowercase */
char *irc_strlwr(char *str) {
  char *c;

  c = str;
  while (*c) {
    *c = _irc_tolower(*c);
    c++;
  }

  return str;
}

/* Changes the case of a string to uppercase */
char *irc_strupr(char *str) {
  char *c;

  c = str;
  while (*c) {
    *c = _irc_toupper(*c);
    c++;
  }

  return str;
}

/* Compare two irc strings, ignoring case.  This is done so much, I've dropped
   a simple version of the strcmp algorithm here rather than doing two mallocs
   lowercasing etc. */
int irc_strcasecmp(const char *s1, const char *s2) {
  while (_irc_tolower(*s1) == _irc_tolower(*s2)) {
    if (!*s1)
      return 0;

    s1++;
    s2++;
  }

  return _irc_tolower(*s1) - _irc_tolower(*s2);
}

/* Match an irc string against wildcards, ignoring case */
int irc_strcasematch(const char *str, const char *mask) {
  char *newstr, *newmask;
  int ret;

  newstr = irc_strlwr(x_strdup(str));
  newmask = irc_strlwr(x_strdup(mask));

  ret = strmatch(newstr, newmask);

  free(newstr);
  free(newmask);

  return ret;
}