Blame


1 0f3e84f4 2003-01-13 alex /*
2 0f3e84f4 2003-01-13 alex * ngIRCd -- The Next Generation IRC Daemon
3 39f1ddd9 2005-01-25 alex * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
4 0f3e84f4 2003-01-13 alex *
5 0f3e84f4 2003-01-13 alex * This program is free software; you can redistribute it and/or modify
6 0f3e84f4 2003-01-13 alex * it under the terms of the GNU General Public License as published by
7 0f3e84f4 2003-01-13 alex * the Free Software Foundation; either version 2 of the License, or
8 0f3e84f4 2003-01-13 alex * (at your option) any later version.
9 0f3e84f4 2003-01-13 alex * Please read the file COPYING, README and AUTHORS for more information.
10 0f3e84f4 2003-01-13 alex *
11 0f3e84f4 2003-01-13 alex * Tool functions
12 0f3e84f4 2003-01-13 alex */
13 0f3e84f4 2003-01-13 alex
14 0f3e84f4 2003-01-13 alex
15 0f3e84f4 2003-01-13 alex #include "portab.h"
16 0f3e84f4 2003-01-13 alex
17 097c7bd7 2006-04-09 alex static char UNUSED id[] = "$Id: tool.c,v 1.6 2006/04/09 12:53:07 alex Exp $";
18 0f3e84f4 2003-01-13 alex
19 0f3e84f4 2003-01-13 alex #include "imp.h"
20 0f3e84f4 2003-01-13 alex #include <assert.h>
21 0f3e84f4 2003-01-13 alex #include <ctype.h>
22 0f3e84f4 2003-01-13 alex #include <stdio.h>
23 0f3e84f4 2003-01-13 alex #include <string.h>
24 0f3e84f4 2003-01-13 alex
25 0f3e84f4 2003-01-13 alex #include "exp.h"
26 0f3e84f4 2003-01-13 alex #include "tool.h"
27 0f3e84f4 2003-01-13 alex
28 0f3e84f4 2003-01-13 alex
29 cba92708 2006-03-22 alex /**
30 cba92708 2006-03-22 alex * Removes all leading and trailing whitespaces of a string.
31 cba92708 2006-03-22 alex * @param String The string to remove whitespaces from.
32 cba92708 2006-03-22 alex */
33 8adff592 2005-03-19 fw GLOBAL void
34 cba92708 2006-03-22 alex ngt_TrimStr(char *String)
35 0f3e84f4 2003-01-13 alex {
36 cba92708 2006-03-22 alex char *start, *end;
37 0f3e84f4 2003-01-13 alex
38 cba92708 2006-03-22 alex assert(String != NULL);
39 0f3e84f4 2003-01-13 alex
40 0f3e84f4 2003-01-13 alex start = String;
41 0f3e84f4 2003-01-13 alex
42 cba92708 2006-03-22 alex /* Remove whitespaces at the beginning of the string ... */
43 097c7bd7 2006-04-09 alex while (*start == ' ' || *start == '\t' ||
44 097c7bd7 2006-04-09 alex *start == '\n' || *start == '\r')
45 cba92708 2006-03-22 alex start++;
46 cba92708 2006-03-22 alex
47 bebfbedf 2006-03-24 fw if (!*start) {
48 097c7bd7 2006-04-09 alex *String = '\0';
49 bebfbedf 2006-03-24 fw return;
50 bebfbedf 2006-03-24 fw }
51 097c7bd7 2006-04-09 alex
52 cba92708 2006-03-22 alex /* ... and at the end: */
53 cba92708 2006-03-22 alex end = strchr(start, '\0');
54 bebfbedf 2006-03-24 fw end--;
55 cba92708 2006-03-22 alex while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
56 097c7bd7 2006-04-09 alex && end >= start)
57 cba92708 2006-03-22 alex end--;
58 cba92708 2006-03-22 alex
59 cba92708 2006-03-22 alex /* New trailing NULL byte */
60 cba92708 2006-03-22 alex *(++end) = '\0';
61 cba92708 2006-03-22 alex
62 bebfbedf 2006-03-24 fw memmove(String, start, (size_t)(end - start)+1);
63 0f3e84f4 2003-01-13 alex } /* ngt_TrimStr */
64 0f3e84f4 2003-01-13 alex
65 0f3e84f4 2003-01-13 alex
66 8adff592 2005-03-19 fw GLOBAL char *
67 8adff592 2005-03-19 fw ngt_LowerStr( char *String )
68 0f3e84f4 2003-01-13 alex {
69 0f3e84f4 2003-01-13 alex /* String in Kleinbuchstaben konvertieren. Der uebergebene
70 0f3e84f4 2003-01-13 alex * Speicherbereich wird durch das Ergebnis ersetzt, zusaetzlich
71 0f3e84f4 2003-01-13 alex * wird dieser auch als Pointer geliefert. */
72 0f3e84f4 2003-01-13 alex
73 8adff592 2005-03-19 fw char *ptr;
74 0f3e84f4 2003-01-13 alex
75 0f3e84f4 2003-01-13 alex assert( String != NULL );
76 0f3e84f4 2003-01-13 alex
77 0f3e84f4 2003-01-13 alex /* Zeichen konvertieren */
78 0f3e84f4 2003-01-13 alex ptr = String;
79 0f3e84f4 2003-01-13 alex while( *ptr )
80 0f3e84f4 2003-01-13 alex {
81 0f3e84f4 2003-01-13 alex *ptr = tolower( *ptr );
82 0f3e84f4 2003-01-13 alex ptr++;
83 0f3e84f4 2003-01-13 alex }
84 0f3e84f4 2003-01-13 alex
85 0f3e84f4 2003-01-13 alex return String;
86 0f3e84f4 2003-01-13 alex } /* ngt_LowerStr */
87 0f3e84f4 2003-01-13 alex
88 0f3e84f4 2003-01-13 alex
89 8adff592 2005-03-19 fw GLOBAL void
90 8adff592 2005-03-19 fw ngt_TrimLastChr( char *String, const char Chr)
91 39f1ddd9 2005-01-25 alex {
92 39f1ddd9 2005-01-25 alex /* If last character in the string matches Chr, remove it.
93 39f1ddd9 2005-01-25 alex * Empty strings are handled correctly. */
94 39f1ddd9 2005-01-25 alex
95 8adff592 2005-03-19 fw unsigned int len;
96 39f1ddd9 2005-01-25 alex
97 39f1ddd9 2005-01-25 alex assert( String != NULL );
98 39f1ddd9 2005-01-25 alex
99 39f1ddd9 2005-01-25 alex len = strlen( String );
100 39f1ddd9 2005-01-25 alex if( len == 0 ) return;
101 39f1ddd9 2005-01-25 alex
102 39f1ddd9 2005-01-25 alex len--;
103 39f1ddd9 2005-01-25 alex
104 39f1ddd9 2005-01-25 alex if( String[len] == Chr ) String[len] = '\0';
105 39f1ddd9 2005-01-25 alex } /* ngt_TrimLastChr */
106 39f1ddd9 2005-01-25 alex
107 39f1ddd9 2005-01-25 alex
108 0f3e84f4 2003-01-13 alex /* -eof- */