C/C++教程

c参数解析

本文主要是介绍c参数解析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 
#include <iostream>
using namespace std;
#include <string.h>

#include <stdlib.h>

#define ARGS_BUF 1024
#define ARGS_MAX 10

struct args {
    char  buf[ARGS_BUF];
    int   argc;
    char* argv[ARGS_MAX];
};
static char*
_strchrskip(char* s, int c) {
    while (*s && *s == c) s++;
    return s;
}
static void
_parse(struct args* A, int max) {
    if (max <= 0)
        max = ARGS_MAX;
    else if (max > ARGS_MAX)
        max = ARGS_MAX;

    int n = 0;
    char* p = A->buf;
    char* next;
    while (*p) {
        p = _strchrskip(p, ' ');
        if (*p == '\0')
            break;
        
        A->argv[n] = p; 
        if (++n >= max)
            break;

        next = strchr(p, ' ');
        if (next == NULL)
            break;
        
        *next = '\0';
        p = next+1;
    }
    A->argc = n;
}
int
args_parsestr(struct args* A, int max, const char* str) {
    strncpy(A->buf, str, ARGS_BUF-1);
    _parse(A, max);
    return A->argc;
}
int
args_parsestrl(struct args* A, int max, const char* str, size_t l) {
    if (l == 0) {
        A->argc = 0;
        return 0;
    }
    if (l >= ARGS_BUF)
        l = ARGS_BUF - 1;
    memcpy(A->buf, str, l);
    A->buf[l] = '\0';
    _parse(A, max);
    return A->argc;
}

struct np_state {
    int cap; 
    void** ud; 
    int maxfd;
    fd_set rfds;
    fd_set wfds;
    fd_set rtmp;
    fd_set wtmp;
};

int main()
{
  
  cout << sizeof(ud) << endl;
  return 0;
}

 

这篇关于c参数解析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!