Fix mvcp_util_strip() using strcpy() on overlapping strings.
[melted] / src / mvcp / mvcp_util.c
1 /*
2 * mvcp_util.c -- General Purpose Client Utilities
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* System header files */
22 #include <string.h>
23 #include <ctype.h>
24
25 /* Application header files */
26 #include "mvcp_util.h"
27
28 /** Remove LF or CR/LF terminations from the input string.
29 */
30
31 char *mvcp_util_chomp( char *input )
32 {
33 if ( input != NULL )
34 {
35 int length = strlen( input );
36 if ( length && input[ length - 1 ] == '\n' )
37 input[ length - 1 ] = '\0';
38 if ( length > 1 && input[ length - 2 ] == '\r' )
39 input[ length - 2 ] = '\0';
40 }
41 return input;
42 }
43
44 /** Remove leading and trailing spaces from the input string.
45 */
46
47 char *mvcp_util_trim( char *input )
48 {
49 if ( input != NULL )
50 {
51 int length = strlen( input );
52 int first = 0;
53 while( first < length && isspace( input[ first ] ) )
54 first ++;
55 memmove( input, input + first, length - first + 1 );
56 length = length - first;
57 while ( length > 0 && isspace( input[ length - 1 ] ) )
58 input[ -- length ] = '\0';
59 }
60 return input;
61 }
62
63 /** Strip the specified string of leading and trailing 'value' (ie: ").
64 */
65
66 char *mvcp_util_strip( char *input, char value )
67 {
68 if ( input != NULL )
69 {
70 char *ptr = strrchr( input, value );
71 if ( ptr != NULL )
72 *ptr = '\0';
73 if ( input[ 0 ] == value )
74 memmove( input, input + 1, strlen( input ) );
75 }
76 return input;
77 }