c 소스를 c++로 구현하는 방안에 대해서 질문드려요.

balgarac1의 이미지

유저에게 명령어 입력을 받으면 명령어 처리를 하는 로직 속에서

void openConnection(char * openCmd);
void nlist(char *nlistCmd);
void ls(char *lsCmd);
void get(char *getCmd);
void put(char *putCmd);
void cd(char *cdCmd);
void quit(char *quitCmd);
void pwd(char *pwdCmd);
void bye(char *byeCmd);
void help(char *helpCmd);
void debugSet(char *debugCmd);
 
typedef struct _FtpCmdHandler {
	char cmd[5];
	void (*handler)(char * arg);
	char * usage;
} FtpCmdHandler;
 
FtpCmdHandler ftpCmdHandler[] = {
		{CMD_OPEN, openConnection, "open <IP address>\t-- connect to FTP server"},
		{CMD_NLIST, nlist, "nlist\t -- name list"},
		{CMD_LS, ls, "ls\t -- directory list"},
		{CMD_GET, get, "get <filename>\t-- download the file"},
		{CMD_PUT, put, "put <filename>\t-- upload the file"},
		{CMD_PWD, pwd, "pwd\t-- print working directory"},
		{CMD_CD, cd, "cd\t-- change working directory"},
		{CMD_QUIT, quit, "quit\t-- close FTP session and exit"},
		{CMD_BYE, bye, "bye\t-- close FTP session and exit"},
		{CMD_HELP, help, "help\t-- print usage"},
		{CMD_DEBUG, debugSet, "debug\t-- print debug message"}
};
 
이런 식으로 구조체 안에 함수 포인터를 이용해
 
void commandHandle(char * cmd) {
	int i;
	int cmdNum;
	cmdNum = sizeof(ftpCmdHandler) / sizeof(FtpCmdHandler);
 
	for (i = 0; i < cmdNum; i++) {
		if (!strncmp(cmd, ftpCmdHandler[i].cmd, strlen(ftpCmdHandler[i].cmd))) {
			(*(ftpCmdHandler[i].handler))(cmd);
			break;
		}
	}
	if (i == cmdNum)
		printf("?Invalid command\n");
}

입력 받은 명령어 처리를 하는데요.
c++에서 이런 로직으로 구현해보고 싶은데 어떤 방법으로 구현할 수 있을까요??
힌트좀 주세요.

익명 사용자의 이미지

std::map을 쓰세요

익명 사용자의 이미지

map이면
key값은 명령어 문자열
value값은 함수 주소 값인데.. 그럼 용법을 나타내는 문자열 값은 포기해야 되나요??

익명 사용자의 이미지

commandhandler를 정의해서 value로 쓰면 되죠

익명 사용자의 이미지

typedef void (*cmdHandler)(void) <- value 값을 넣을 함수 포인터를 typedef로.

map cmdMap; <- map 객체 생성.

cmdMap.insert("who", this->who); <- 키 값으로 명렁어 "who" 넣고, 그에 따른 value를 함수 who를 저장하는 이런 식으로 코드가 작성되지 않나요? 용법은 어디에 들어갈 수 있죠??

void who(void)
{
..
}

kukyakya의 이미지

map을 이용하시려면, 실제로 컴파일해보진 않았지만 대충 컨셉을 다음과 같이 이용하시면 될 것 같습니다.

std::map<std::string,
         std::pair<void (*)(const std::string&), std::string> > handler;
 
handler[CMD_OPEN]  = std::make_pair(&openConnection, "open <IP address>\t-- connect to FTP server");
handler[CMD_NLIST] = std::make_pair(&nlist,          "nlist\t -- name list");
.
.
.
 
void commandHandle(const std::string& cmd) {
  const auto it = handler.find(cmd);
 
  if(it != handler.end()) {
    const auto& value = it->second;
 
    value.first(value.second);
  }
  else {
    std::cout << "?Invalid command" << std::endl;
  }

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.