Skip to content

Commit 780fefc

Browse files
lauxinwlijinxia
authored andcommitted
tools: acrn-crashlog: string utils for acrn-crashlog
This file provides some string operations for acrn-crashlog. Signed-off-by: Liu Xinwu <xinwu.liu@intel.com> Reviewed-by: Zhang Yanmin <yanmin.zhang@intel.com> Reviewed-by: Liu Chuansheng <chuansheng.liu@intel.com> Reviewed-by: Zhao Yakui <yakui.zhao@intel.com> Reviewed-by: Geoffroy Van Cutsem <geoffroy.vancutsem@intel.com> Acked-by: Eddie Dong <Eddie.dong@intel.com>
1 parent 6f9dfa4 commit 780fefc

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

tools/acrn-crashlog/common/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
objects = log_sys.o
1+
objects = log_sys.o \
2+
strutils.o
23

34
all: check_obj $(objects)
45
check_obj:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation
3+
* SPDX-License-Identifier: BSD-3-Clause
4+
*/
5+
6+
#ifndef __STRUTILS_H__
7+
#define __STRUTILS_H__
8+
9+
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
10+
11+
int strlinelen(char *str);
12+
char *strrstr(char *s, char *str);
13+
char *next_line(char *buf);
14+
char *strtrim(char *str);
15+
int strcnt(char *str, char c);
16+
17+
#endif

tools/acrn-crashlog/common/strutils.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation
3+
* SPDX-License-Identifier: BSD-3-Clause
4+
*/
5+
6+
#include <string.h>
7+
#include <errno.h>
8+
9+
/**
10+
* Get the length of line.
11+
*
12+
* @param str Start address of line.
13+
*
14+
* @return the length of line if successful, or -1 if not.
15+
* This function return length of string if string doesn't contain \n.
16+
*/
17+
int strlinelen(char *str)
18+
{
19+
char *tag;
20+
21+
if (!str)
22+
return -1;
23+
24+
tag = strchr(str, '\n');
25+
if (tag)
26+
return tag - str + 1;
27+
28+
return strlen(str);
29+
}
30+
31+
/**
32+
* Find the last occurrence of the substring str in the string s.
33+
*
34+
* @param s Range of search.
35+
* @param substr String to be found.
36+
*
37+
* @return a pointer to the beginning of the substring,
38+
* or NULL if the substring is not found.
39+
*/
40+
char *strrstr(char *s, char *substr)
41+
{
42+
char *found;
43+
char *p = s;
44+
45+
while ((found = strstr(p, substr)))
46+
p = found + 1;
47+
48+
if (p != s)
49+
return p - 1;
50+
51+
return NULL;
52+
}
53+
54+
char *next_line(char *buf)
55+
{
56+
char *p;
57+
58+
p = strchr(buf, '\n');
59+
/* if meet end of buf, the return value is also NULL */
60+
if (p)
61+
return p + 1;
62+
63+
return NULL;
64+
}
65+
66+
static char *strtriml(char *str)
67+
{
68+
char *p = str;
69+
70+
while (*p == ' ')
71+
p++;
72+
return memmove(str, p, strlen(p) + 1);
73+
}
74+
75+
static char *strtrimr(char *str)
76+
{
77+
char *end = str + strlen(str) - 1;
78+
79+
while (*end == ' ' && end >= str) {
80+
*end = 0;
81+
end--;
82+
}
83+
84+
return str;
85+
}
86+
87+
char *strtrim(char *str)
88+
{
89+
strtrimr(str);
90+
return strtriml(str);
91+
}
92+
93+
int strcnt(char *str, char c)
94+
{
95+
int cnt = 0;
96+
char *p = str;
97+
char *found;
98+
99+
if (!str)
100+
return -EINVAL;
101+
102+
while ((found = strchr(p, c))) {
103+
p = found + 1;
104+
cnt++;
105+
}
106+
107+
return cnt;
108+
}

0 commit comments

Comments
 (0)