How to Use strings and corresponding functions

Source: Internet
Author: User
Define operation functions
static void cmd_capture (char *par) {}static void cmd_fill (char *par) {}static void cmd_type (char *par) {}static void cmd_rename (char *par) {}static void cmd_copy (char *par) {}static void cmd_delete (char *par) {}static void cmd_dir (char *par) {}static void cmd_format (char *par) {}static void cmd_help (char *par) {}

Type Definition

typedef struct scmd {  char val[8];  void (*func)(char *par);} SCMD;

Definition of matching commands and functions

Static const SCMD cmd [] = {"CAP", 1__capture, "TYPE", 1__type, "REN", 1__rename, "COPY", 1__copy, "DEL", 1__delete, "DIR", 1__dir, "FORMAT", 1__format, "HELP", 1__help, "FILL", 1__fill ,"? ", Cmd_help };

 

#define CMD_COUNT   (sizeof (cmd) / sizeof (cmd[0]))

 

for (i = 0; i < CMD_COUNT; i++) {      if (strcmp (sp, (const char *)&cmd[i].val)) {        continue;      }      cmd[i].func (next);                     /* execute command function    */      break;    }    if (i == CMD_COUNT) {      printf ("\nCommand error\n");    }

For example, strings and corresponding functions can be matched.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.