简体   繁体   English

计算C中的字符串标记数量

[英]Counting the amount of string tokens in C

I need to make a program that will emulate the terminal of Linux. 我需要制作一个可以模拟Linux终端的程序。 Since some system calls requires 1,2 or more arguments, I want to make sure that the number of parameters given are correct. 由于某些系统调用需要1,2或更多参数,因此我想确保给出的参数数量正确。 I'm using strtok() to separate the call name from the arguments, but I need to know how many tokens strtok() created to compare it. 我正在使用strtok()将调用名称与参数分开,但是我需要知道strtok()创建了多少个令牌来进行比较。

Here's and example code: 这是示例代码:

char *comand = (char*) malloc(sizeof(char)*100);
char *token;
char *path1 = (char*) malloc(sizeof(char)*100);
char *path2= (char*) malloc(sizeof(char)*100);



    fgets(comand, 100, stdin);

    printf( "\nYou entered: %s \n", comand);

    token = strtok(comand ," ");

    //Check the number of tokens and add a condition in each IF to match

    if (strcmp("ls",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("cat",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("cp",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
        token = strtok(NULL," ");
        strcpy(path2,token);

    }
    else if (strcmp("mv",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
        token = strtok(NULL," ");
        strcpy(path2,token);    

    }
    else if (strcmp("find",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("rm",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("mkdir",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
    }
    else if (strcmp("rmdir",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
    }
    else if (strcmp("quit",token) == 0) {
        exit(0);
    }
    else print("Number of parameters do not match);

the only thing strtok() does is look for the next occurance of the delimiter and overwrite that character with a \\0 and return the pointer with the offset added. strtok()唯一要做的是查找下一个定界符,并用\\0覆盖该字符,并返回添加了偏移量的指针。 the pointer is kept in a static variable that's why a subsequent call to it with a NULL for the char * will perform it on the last string used from the offset that the last delimiter was found. 指针保存在静态变量中,这就是为什么随后对char *使用NULL对其进行调用的原因,它将在找到最后一个定界符的偏移量开始使用的最后一个字符串上执行该指针。

this page has a very nice example: http://en.cppreference.com/w/c/string/byte/strtok 此页面有一个非常不错的示例: http : //en.cppreference.com/w/c/string/byte/strtok

If you only want to count the arguments it would be easier to use strchr() , this function searches for a character and returns a pointer to its location. 如果只想计算参数,则使用strchr()会更容易,此函数将搜索字符并返回指向其位置的指针。 you could use it like this. 您可以像这样使用它。

unsigned int i = 0;
char *temp = token;
while ( (temp = strchr(temp, '') != NULL) ) {
    ++i;
}

this has the added benefit of not modifying your original char array while strtok() does! 这具有在strtok()进行时不修改原始char数组的附加好处!

I would handle this within the functions you create for each command. 我将在为每个命令创建的函数中处理此问题。

you pass all options to the function and there you parse it. 您将所有选项传递给函数,然后在那里解析它。 either with strtok() or whatever else you want to use. 使用strtok()或其他任何您想使用的东西。

This keeps it nice and clean within the sub-routines and you will always know what to expect. 这使子例程中的内容保持整洁,您将始终知道期望什么。

if (strcmp("ls",token) == 0) {
    token = strtok(NULL," ");
    strcpy(path1,token);      // I would maybe change the path variable name to args
    ret = lscmd(path1);
    if (ret == -1) {
         // invalid input detected
    }  
}

then you would have a ls function 那么你会有一个ls函数

int lsdcmd(char *args) {
    // parse args for argumants you are looking for, return -1 if it fails

    // do whatever you want to do.
}

You can count the arguments using strtok this way: 您可以通过以下方式使用strtok计算参数:

Example: 例:

const char* delimiter = ",";
char* tokens[MAX_NUM_OF_ARGS];

unsigned int index = 0;
char* temp = strtok(buff,delimiter);
while (temp!=NULL){
    if(index<MAX_NUM_OF_ARGS){
        tokens[index]=temp;
    }
    index++;
    temp = strtok(NULL,delimiter);
}

Then later you can iterate through the array of pointers (tokens) and compare them... 然后,您可以遍历指针(令牌)数组并进行比较...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM