-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpstr.cpp
116 lines (109 loc) · 2.61 KB
/
ftpstr.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
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
113
114
115
116
/**
*start from the very beginning,and to create greatness
*@author: LinChuangwei
*@E-mail:979951191@qq.com
*@brief:字符串处理类实现
*/
#include "ftpstr.h"
void ftpstr::fangjinggao()
{
LCWFTPD_LOG(DEBUG,"防警告");
}
/**
*str_trim_crlf - 去除\r\n
*@str:要处理的字符串
*/
void ftpstr::str_trim_crlf(char* str)
{
char* p = &str[strlen(str) -1];//p指向str最后一个字符
while(*p == '\r' || *p =='\n')
{
*p-- = '\0';
}
}
/**
*str_split - 分割字符串
*@str:要处理的字符串
*@left:存放分割后左边的字符串
*@right:存放分割后右边的字符串
*@c:分割依据
*/
void ftpstr::str_split(const char* str,char* left,char* right,char c)
{
//查找第一个c字符所在位置
char* p = strchr((char*)str,c);//strstr是查找字符串的,strchr是查找字符的
if (p == NULL)//没有找到
{
strcpy(left,str);
}
else
{
*p = '\0';
strcpy(left,str);//复制左边
strcpy(right,p+1);//复制右边
}
}
/**
*str_upper - 字母转换成大写
*@str:要处理的字符串
*/
void ftpstr::str_upper(char* str)
{
while(*str)
{
*str = toupper(*str);//转化后通过返回值返回
str++;
}
}
/**
*delspace - 去除字符串首尾的空格
*@str:要处理的字符串
*返回处理好的字符串
*/
char* ftpstr::delspace(char* str)
{
char* end,*sp,*ep;//结尾指针,两个游标
size_t len;//用于保存长度长度
sp = str;//sp指针首先指向行首
end = ep = str + strlen(str) -1;//end指针指向结尾
//从行首开始去空格
while(sp <= end && isspace(*sp))//是空格返回非零
sp++;//移动sp指针直到指向的不是空格
//从行尾去除空格
while(ep >= sp && isspace(*ep))
ep--;
len = (ep < sp)? 0 :(ep - sp)+1;//计算长度+1,指向换行符
sp[len] = '\0';//换行符置为'\0'
return sp;
}
/**
*str_octal_to_uint - 字符串表示的8进制数转换为unsigned int
*@str:要处理的字符串
*返回转化后的unsigned int类型的数
*/
unsigned int ftpstr::str_octal_to_uint(char* str)
{
unsigned int result = 0;
int seen_non_zero_digit = 0;//8进制0开头
char digit;
while(*str)
{
digit = *str;//取出其中的一位
//isdigit检查参数c是否为阿拉伯数字0到9
if (!isdigit(digit) || digit > '7')//是不是数字或者大于7
{
break;
}
if (digit != '0')//查找到第一个不为0的数
{
seen_non_zero_digit = 1;
}
if (seen_non_zero_digit)
{
result <<= 3;//*8,移位比乘法效率高
result += (digit - '0');
}
str++;
}
return result;
}