简体   繁体   English

用户输入的字符串在c中运行特定的功能

[英]User entered string run a particular function in c

Guys so I'm working on the web service assignment and I have the server dishing out random stuff and reading the uri but now i want to have the server run a different function depending on what it reads in the uri. 伙计们,所以我正在进行Web服务分配,我让服务器发出随机的东西并阅读uri但现在我想让服务器运行不同的功能,具体取决于它在uri中读取的内容。 I understand that we can do this with function pointers but i'm not exactly sure how to read char* and assign it to a function pointer and have it invoke that function. 我知道我们可以用函数指针来做这个,但我不确定如何读取char *并将其分配给函数指针并让它调用该函数。 Example of what I'm trying to do: http://pastebin.com/FadCVH0h 我正在尝试做的例子: http//pastebin.com/FadCVH0h

I could use a switch statement i believe but wondering if there's a better way. 我可以使用一个开关声明我相信,但想知道是否有更好的方法。

For such a thing, you will need a table that maps char * strings to function pointers. 对于这样的事情,您将需要一个将char *字符串映射到函数指针的表。 The program segfaults when you assign a function pointer to string because technically, a function pointer is not a string. 将函数指针指定给字符串时,程序会出现段错误,因为从技术上讲,函数指针不是字符串。

Note: the following program is for demonstration purpose only. 注意:以下程序仅用于演示目的。 No bounds checking is involved, and it contains hard-coded values and magic numbers 不涉及边界检查,它包含硬编码值和幻数

Now: 现在:

void print1()
{
   printf("here");
}

void print2() 
{
   printf("Hello world");
}
struct Table {
  char ptr[100];
  void (*funcptr)(void)
}table[100] = {
{"here", print1},
{"hw", helloWorld}
};

int main(int argc, char *argv[])
{
   int i = 0;
   for(i = 0; i < 2; i++){
      if(!strcmp(argv[1],table[i].ptr) { table[i].funcptr(); return 0;}
   }
   return 0;
}

I'm gonna give you a quite simple example, that I think, is useful to understand how good can be functions pointers in C. (If for example you would like to make a shell) 我想给你一个非常简单的例子,我认为,这对于理解C中的函数指针有多好是有用的。(例如,如果你想创建一个shell)

For example if you had a struct like this: 例如,如果您有这样的结构:

typedef struct s_function_pointer
{
    char*      cmp_string;
    int        (*function)(char* line);
}              t_function_pointer;

Then, you could set up a t_function_pointer array which you'll browse: 然后,您可以设置一个t_function_pointer数组,您将浏览它:

int     ls_function(char* line)
{
      // do whatever you want with your ls function to parse line
      return 0;
}

int     echo_function(char* line)
{
      // do whatever you want with your echo function to parse line
      return 0;
}

void    treat_input(t_function_pointer* functions, char* line)
{
       int    counter;
       int    builtin_size;

       builtin_size = 0;
       counter = 0;
       while (functions[counter].cmp_string != NULL)
       {
             builtin_size = strlen(functions[counter].cmp_string);
             if (strncmp(functions[counter].cmp_string, line, builtin_size) == 0)
             {
                   if (functions[counter].function(line + builtin_size) < 0)
                          printf("An error has occured\n");
             }
             counter = counter + 1;
       }
}

int     main(void)
{
     t_function_pointer      functions[] = {{"ls", &ls_function},
                                            {"echo", &echo_function},
                                            {NULL, NULL}};
     // Of course i'm not gonna do the input treatment part, but just guess it was here, and you'd call treat_input with each line you receive.
     treat_input(functions, "ls -laR");
     treat_input(functions, "echo helloworld");
     return 0;
}

Hope this helps ! 希望这可以帮助 !

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

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