-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
74 lines (59 loc) · 1.44 KB
/
util.cpp
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
//*****************************************************************************
//
// Copyright (C) 2005 Steve Connet. All rights reserved.
//
// Source File Name : utils.c
// Author : Steve Connet
//
// File Overview : Commonly used C functions
//
//*****************************************************************************
#include "util.h"
#include <string.h>
#include <ctype.h>
#include <unistd.h>
// safely convert a byte to lower-case
int utils::safetolower(int c)
{
// don't convert if not upper
if(isupper(c))
{
return tolower(c);
}
return c;
}
// Reverse the passed character string. The passed string must be NULL
// terminated. Returns a pointer to the beginning of the reversed
// string.
char *utils::strrev(char *str)
{
char *p1, *p2;
if(! str || ! *str)
{
return str;
}
for(p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
} // strrev
// Moves the passed pointer forward until there are no more
// spaces. Returns the incremented pointer. Do not use this with
// dynamically allocted strings.
char *utils::trimleft(char *p)
{
while(isspace(*p))
{
++p;
}
return p;
} // trimleft
// Reverses the string then calles trim_left. Returns the incremented
// pointer.
char *utils::trimright(char *p)
{
return strrev(trimleft(strrev(p)));
} // trimright