c commonly used string processing functions

strtok decomposes a set of strings
strdup strcpy strncpy strlcpy string copy
strcmp string comparison
strlen calculation string size
strlcat string connection
strstr judges whether string str2 is a substring of str1
getopt_long long command line analysis


1. strtok-->decompose a set of strings

原型 : char * strtok (char s [], const char * delim);
Function: decompose a string into a set of strings;
Parameters: s is the string to be decomposed, and delim is the separator string.
example:
strtok("abc,def,ghi",","), finally can be divided into abc def ghi. Especially in dotted decimal IP extraction applications are more.

Two, strdup strcpy strncpy strlcpy-->string copy function

(1) strdup
Prototype: extern char *strdup(char *s);
Function: copy the string to the newly created location, generally appear in pairs with the free() function
Parameters: s string to be copied
Example: char *d=strdup(s);
Copy the string s to d
(2) strcpy is   based on /0 as the end judgment
: : Char * strcpy (char * dest, const char * src);
Function: Copy the string starting from the src address and containing the'\0' terminator to the address space starting with dest.
Parameters: src string to be copied, dest is the destination parameter,
返回值:dest是目的参数的指针 增加灵活性如支持链式表达
(3)strncpy strncpy 并不帮你保证 /0
原型:char *strncpy(char *dest, const char *src, int n)
功能:把src所指向的字符串中以src地址开始的前n个字节复制到dest所指的数组中,并返回dest
例子:strncpy(dest,name,3);
(4)strlcpy 不需要我们去手动负责 /0 了,仅需要把 sizeof(dst) 告之 strlcpy 即可
原型:size_t   strlcpy(char *dst, const char *src, size_t siz);
功能:把src复制siz字节到dst  是安全的
返回值:strlcpy返回strlen(src),用于判断src是否被截断。

三、strcmp  比较字符串

原型:extern int strcmp(const char *s1,const char *s2);
功能:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止
返回值:
当s1<s2时,返回为负数;
当s1=s2时,返回值= 0;
当s1>s2时,返回正数。

四、strlen 计算字符串的长度不包含‘\0’

原型:extern unsigned int strlen(char *s);
功能:计算字符串的长度不包含‘\0’

五、strlcat 字符串连接

原型: size_t  strlcpy(char *dst, const char *src, size_t size);
功能:字符串连接
参数:dst目标参数,src源参数,size连接后字符的长度
返回值:返回他们尝试创建的字符串的长度  保证所有的目标字符串都以NUL 字符结尾
例子:n = strlcpy(pname, dir, sizeof(pname));

六、strstr

原型:strstr(string,search)
功能:判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
参数:string母字符串,search子字符串
例子:
strstr("Helloworld!","world")
七、getopt_long

原型:int getopt_long(int argc, char * const argv[],const char *optstring,
                  const struct option *longopts, int *longindex);
功能:长选项的命令行解析
参数:函数中的argc和argv通常直接从main()的两个参数传递而来。optsting是选项参数组成的字符串:
参数longopts,其实是一个结构的实例:
struct option {
const char *name; //name表示的是长参数名
int has_arg; //has_arg有3个值, no_argument(或者是0),表示该参数后面不跟参数值
// required_argument(或者是1),表示该参数后面一定要跟个参数值
// optional_argument(或者是2),表示该参数后面可以跟,也可以不跟参数值
int *flag;
//用来决定,getopt_long()的返回值到底是什么。如果flag是null(通常情况),则函数会返回与该项option匹配的val值;如果flag不是NULL,则将val值赋予flag所指向的内存,并且返回值设置为0。
int val; //和flag联合决定返回值
}


例子:
static const struct option OPTIONS[] = {
  { "send_intent", required_argument, NULL, 's' },//向intent文件中写入数据
  { "update_package", required_argument, NULL, 'u' },验证ota package路径下更新包文件
  { "update_package_ex", required_argument, NULL, 'e'},
  { "wipe_data", no_argument, NULL, 'w' },//擦除data分区
  { "wipe_cache", no_argument, NULL, 'c' },//擦除cache分区
  { "wipe_full",no_argument,NULL,'f'},
  { "show_text", no_argument, NULL, 't' },//显示主菜单
  { "just_exit", no_argument, NULL, 'x' },//退出并重启
  { "locale", required_argument, NULL, 'l' },//本地化
  { NULL, 0, NULL, 0 },

    while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
解析命令行选项参数
        switch (arg) {
        case 'p': previous_runs = atoi(optarg); break;//退出recovery后关闭系统 没有定义p
        case 's': send_intent = optarg; break;//向intent文件中写入数据  send_intent要写入的数据
        case 'u': update_package = optarg; break;//验证ota package路径下更新包文件  升级系统  update_package更新包所在的路径
        case 'w': wipe_data = wipe_cache = 1; break;//擦除数据
        case 'c': wipe_cache = 1; break;//擦除CACHE分区
        case 'f': wipe_full = 1;break;
        case 't': show_text = 1; break;//指示升级时是否显示UI  
        case 'x': just_exit = true; break;//退出并重启
        case 'l': locale = optarg; break;//指定Local //本地化
        case 'e': update_package_ex = optarg;break;
        case '?':
            LOGE("Invalid command argument\n");
            continue;
        }
    }


Guess you like

Origin blog.csdn.net/hgz_gs/article/details/51721306