2021-10-02 19:20:18 +08:00
|
|
|
/*
|
|
|
|
Author: lyon
|
|
|
|
Tencent QQ: 645275593
|
|
|
|
*/
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
#include "dataString.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
char* strAppendWithSize_unlimited(char* strOut, char* pData, int32_t Size) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t strOut_i = strGetSize(strOut);
|
|
|
|
for (int32_t i = 0; i < Size; i++) {
|
|
|
|
strOut[strOut_i + i] = pData[i];
|
|
|
|
}
|
|
|
|
strOut_i += Size;
|
|
|
|
// add \0 to the end of strOut
|
|
|
|
strOut[strOut_i] = 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2021-10-02 19:20:18 +08:00
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strCut(char* strOut, char* strIn, char startSign, char endSign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t Size = strGetSize(strIn);
|
|
|
|
int32_t isStart = 0;
|
|
|
|
int32_t isEnd = 0;
|
|
|
|
int32_t iOut = 0;
|
|
|
|
for (int32_t i = 0; i < Size; i++) {
|
|
|
|
if (!isStart && (strIn[i] == startSign)) {
|
|
|
|
isStart = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isStart && (strIn[i] == endSign)) {
|
|
|
|
isEnd = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isStart) {
|
|
|
|
strOut[iOut] = strIn[i];
|
|
|
|
iOut++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add \0 */
|
|
|
|
strOut[iOut] = 0;
|
|
|
|
if (isStart && isEnd) {
|
|
|
|
/* succeed */
|
|
|
|
return strOut;
|
|
|
|
}
|
|
|
|
/* faild */
|
|
|
|
return NULL;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strDeleteChar(char* strOut, char* strIn, char ch) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t iOut = 0;
|
|
|
|
for (int32_t i = 0; i < strGetSize(strIn); i++) {
|
|
|
|
if (ch == strIn[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strOut[iOut] = strIn[i];
|
|
|
|
iOut++;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
/* add \0 */
|
|
|
|
strOut[iOut] = 0;
|
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strDeleteEnter(char* str) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return strDeleteChar(str, str, '\n');
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strAppendWithSize(char* strOut, char* pData, int32_t Size) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t strOut_i = strGetSize(strOut);
|
|
|
|
for (int32_t i = 0; i < Size; i++) {
|
|
|
|
strOut[strOut_i + i] = pData[i];
|
|
|
|
}
|
|
|
|
strOut_i += Size;
|
|
|
|
// add \0 to the end of strOut
|
|
|
|
strOut[strOut_i] = 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2021-10-02 19:20:18 +08:00
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t strCountSign(char* strIn, char sign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t count = 0;
|
|
|
|
for (int32_t i = 0; i < strGetSize(strIn); i++) {
|
|
|
|
if (sign == strIn[i]) {
|
|
|
|
count++;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
return count;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t strGetTokenNum(char* strIn, char sign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return strCountSign(strIn, sign) + 1;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t strGetSize(char* pData) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return strlen(pData);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strAppend_unlimited(char* strOut, char* pData) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint32_t Size = 0;
|
|
|
|
Size = strGetSize(pData);
|
|
|
|
return strAppendWithSize_unlimited(strOut, pData, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* strGetLastLine(char *strOut, char *strIn){
|
|
|
|
int32_t size = strGetSize(strIn);
|
|
|
|
char sign = '\n';
|
|
|
|
uint32_t beginIndex = 0;
|
|
|
|
|
|
|
|
/* skip the latest '\n' */
|
|
|
|
for (int32_t i = size - 2; i > -1; i--) {
|
|
|
|
if (strIn[i] == sign) {
|
|
|
|
beginIndex = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(strOut, strIn + beginIndex, size - beginIndex);
|
|
|
|
strOut[size - beginIndex + 1] = 0;
|
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strGetLastToken(char* strOut, char* strIn, char sign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t size = strGetSize(strIn);
|
|
|
|
int32_t isGetSign = 0;
|
|
|
|
int32_t buffSize = 0;
|
|
|
|
for (int32_t i = size - 1; i > -1; i--) {
|
|
|
|
if (strIn[i] != sign) {
|
|
|
|
strOut[size - i - 1] = strIn[i];
|
|
|
|
buffSize++;
|
|
|
|
}
|
|
|
|
if (strIn[i] == sign) {
|
|
|
|
isGetSign = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t i = 0;
|
|
|
|
for (i = 0; i < buffSize / 2; i++) {
|
|
|
|
char buff = strOut[i];
|
|
|
|
strOut[i] = strOut[buffSize - i - 1];
|
|
|
|
strOut[buffSize - i - 1] = buff;
|
|
|
|
}
|
|
|
|
strOut[buffSize] = 0;
|
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strPopToken(char* strOut, char* strIn, char sign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t getSign = 0;
|
|
|
|
int32_t iPoped = 0;
|
|
|
|
int32_t iOut = 0;
|
|
|
|
int32_t size = strGetSize(strIn);
|
|
|
|
int32_t i = 0;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (getSign) {
|
|
|
|
strIn[iPoped++] = strIn[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strIn[i] != sign) {
|
|
|
|
strOut[iOut++] = strIn[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strIn[i] == sign) {
|
|
|
|
getSign = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strOut[iOut] = 0;
|
|
|
|
strIn[iPoped] = 0;
|
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strGetFirstToken(char* strOut, char* strIn, char sign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t size = strGetSize(strIn);
|
|
|
|
for (int32_t i = 0; i < size; i++) {
|
|
|
|
if (strIn[i] != sign) {
|
|
|
|
strOut[i] = strIn[i];
|
|
|
|
}
|
|
|
|
if (strIn[i] == sign) {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
return strOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t strGetToken(char* string, char** argv, char sign) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int32_t argc = 0;
|
|
|
|
int32_t i = 0;
|
|
|
|
// arg_i point32_t to the arg operated now
|
|
|
|
int32_t arg_i = 0;
|
|
|
|
// if not found ' ', then put chars from CMD to argv_tem
|
|
|
|
int32_t char_i = 0;
|
|
|
|
for (i = 0; (i < strGetSize(string)); i++) {
|
|
|
|
if (string[i] != sign) {
|
|
|
|
argv[arg_i][char_i] = string[i];
|
|
|
|
char_i++;
|
|
|
|
}
|
|
|
|
if (string[i] == sign) {
|
|
|
|
// write '\0' to the end of argv
|
|
|
|
argv[arg_i][char_i] = 0;
|
|
|
|
arg_i++;
|
|
|
|
char_i = 0;
|
|
|
|
}
|
|
|
|
// write '\0' to the end of last argv
|
|
|
|
argv[arg_i][char_i] = 0;
|
|
|
|
}
|
|
|
|
argc = arg_i + 1;
|
|
|
|
return argc;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strAppend(char* strOut, char* pData) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint32_t Size = 0;
|
|
|
|
Size = strGetSize(pData);
|
|
|
|
return strAppendWithSize(strOut, pData, Size);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t strIsStartWith(char* str, char* strStart) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == str || NULL == strStart) {
|
|
|
|
/* input is null */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint32_t size = strGetSize(strStart);
|
|
|
|
uint32_t CMDName_get = 1;
|
|
|
|
for (int32_t i = 0; i < size; i++) {
|
|
|
|
if (str[i] != strStart[i]) {
|
|
|
|
CMDName_get = 0;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
return CMDName_get;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t strEqu(char* str1, char* str2) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return !strcmp(str1, str2);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strRemovePrefix(char* inputStr, char* prefix, char* outputStr) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (!strIsStartWith(inputStr, prefix)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2021-10-02 19:20:18 +08:00
|
|
|
for (int32_t i = strGetSize(prefix); i < strGetSize(inputStr); i++) {
|
|
|
|
outputStr[i - strGetSize(prefix)] = inputStr[i];
|
|
|
|
}
|
|
|
|
return outputStr;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strClear(char* str) {
|
2021-10-02 19:20:18 +08:00
|
|
|
for (int32_t i = 0; i < sizeof(str); i++) {
|
|
|
|
str[i] = 0;
|
|
|
|
}
|
|
|
|
return str;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t strIsContain(char* str, char ch) {
|
2021-10-02 19:20:18 +08:00
|
|
|
for (int32_t i = 0; i < strGetSize(str); i++) {
|
|
|
|
if (str[i] == ch) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
return 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* strCopy(char* strBuff, char* strIn) {
|
2021-10-02 19:20:18 +08:00
|
|
|
memcpy(strBuff, strIn, strGetSize(strIn));
|
|
|
|
return strBuff;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|