Skip to content

Commit

Permalink
add quote split string
Browse files Browse the repository at this point in the history
  • Loading branch information
ubuntu14 committed Apr 14, 2016
1 parent ecc0869 commit a6f78cc
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 31 deletions.
3 changes: 3 additions & 0 deletions app/redis_tools/redis_builder/changes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

6) 2016.4.14
6.1) feature: �������ڼ�Ⱥģʽ�� CONFIG �����֧��

5) 2016.4.12
5.1) feature: redis_dump ��֧�����ȴӼ�Ⱥ�еĴӽڵ��ȡ����Լ�������ڵ��ѹ��
5.2) feature: ���ڿ���ʹ�ôӽڵ��ȡ����IJ�������������ʹ�ôӽڵ�
Expand Down
117 changes: 104 additions & 13 deletions app/redis_tools/redis_builder/redis_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ void redis_commands::help(void)
" \033[1;36;40mredis_addr\033[0m\r\n");
printf("> \033[1;33;40mdbsize\033[0m\r\n");
printf("> \033[1;33;40mnodes\033[0m\r\n");
printf("> \033[1;33;40mconfig set|get parameter\033[0m\r\n");
printf("> \033[1;33;40mconfig rewrite\033[0m\r\n");
printf("> \033[1;33;40mconfig resetstat\033[0m\r\n");
#else
printf("> keys pattern limit\r\n");
printf("> get [:limit] parameter ...\r\n");
Expand All @@ -110,6 +113,9 @@ void redis_commands::help(void)
printf("> server redis_addr\r\n");
printf("> dbsize\r\n");
printf("> nodes\r\n");
printf("> config set|get parameter\r\n");
printf("> config rewrite\r\n");
printf("> config resetstat\r\n");
#endif
}

Expand All @@ -135,7 +141,8 @@ void redis_commands::run(void)
continue;
}

std::vector<acl::string>& tokens = buf.split2(" \t");
std::vector<acl::string>& tokens = buf.quote_split2(" \t");

acl::string& cmd = tokens[0];
cmd.lower();

Expand All @@ -159,6 +166,8 @@ void redis_commands::run(void)
check_ttl(tokens);
else if (cmd == "dbsize")
get_dbsize(tokens);
else if (cmd == "config")
config(tokens);
#ifdef HAS_READLINE
else if (cmd == "clear" || cmd == "cl")
{
Expand Down Expand Up @@ -827,22 +836,15 @@ void redis_commands::request(const std::vector<acl::string>& tokens)
if (result == NULL)
{
printf("request error: %s\r\n", cmd.result_error());
for (std::vector<acl::string>::const_iterator cit =
tokens.begin(); cit != tokens.end(); ++cit)
{
if (cit == tokens.begin())
printf("%s", (*cit).c_str());
else
printf(" %s", (*cit).c_str());
}
show_request(tokens);
printf("\r\n");
return;
}

show_result(*result);
}

void redis_commands::show_result(const acl::redis_result& result)
bool redis_commands::show_result(const acl::redis_result& result)
{
acl::string buf;
size_t size;
Expand All @@ -856,7 +858,7 @@ void redis_commands::show_result(const acl::redis_result& result)
break;
case acl::REDIS_RESULT_ERROR:
printf("-%s\r\n", result.get_error());
break;
return false;
case acl::REDIS_RESULT_STATUS:
printf("+%s\r\n", result.get_status());
break;
Expand All @@ -882,10 +884,99 @@ void redis_commands::show_result(const acl::redis_result& result)
break;
case acl::REDIS_RESULT_UNKOWN:
printf("unknown type: %d\r\n", (int) type);
break;
default:
printf("unknown type: %d\r\n", (int) type);
break;
return false;
}

return true;
}

void redis_commands::show_request(const std::vector<acl::string>& tokens)
{
for (std::vector<acl::string>::const_iterator cit =
tokens.begin(); cit != tokens.end(); ++cit)
{
if (cit == tokens.begin())
printf("%s", (*cit).c_str());
else
printf(" \"%s\"", (*cit).c_str());
}
}

void redis_commands::config_usage(void)
{
logger_error("> usage: config get parameter");
logger_error("> usage: config set parameter");
logger_error("> usage: config rewrite");
logger_error("> usage: config resetstat");
}

void redis_commands::config(const std::vector<acl::string>& tokens)
{
if (tokens.size() < 2)
{
config_usage();
return;
}

acl::redis_client client(addr_, conn_timeout_, rw_timeout_);
client.set_password(passwd_);
acl::redis redis(&client);
std::vector<const acl::redis_node*> nodes;

redis_util::get_all_nodes(redis, nodes);
if (nodes.empty())
{
logger_error("no node of the cluster: %s", addr_.c_str());
return;
}

for (std::vector<const acl::redis_node*>::const_iterator cit
= nodes.begin(); cit != nodes.end(); ++cit)
{
const char* addr = (*cit)->get_addr();
if (addr == NULL || *addr == 0)
{
logger_error("addr NULL");
continue;
}

config(addr, tokens);
}
}

void redis_commands::config(const char* addr,
const std::vector<acl::string>& tokens)
{
if (tokens.size() < 2)
{
config_usage();
return;
}
request_one(addr, tokens);
}

void redis_commands::request_one(const char* addr,
const std::vector<acl::string>& tokens)
{
acl::redis_client client(addr, conn_timeout_, rw_timeout_);
client.set_password(passwd_);
acl::redis redis(&client);
const acl::redis_result* result = redis.request(tokens);
if (result == NULL)
{
printf("request error: %s\r\n", redis.result_error());
show_request(tokens);
printf("\r\n");
}
else
{
if (show_result(*result) == false)
{
printf("request error\r\n");
show_request(tokens);
printf("\r\n");
}
}
}
10 changes: 9 additions & 1 deletion app/redis_tools/redis_builder/redis_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@ class redis_commands
void check_ttl(const std::vector<acl::string>& tokens);
void get_dbsize(const std::vector<acl::string>& tokens);
void request(const std::vector<acl::string>& tokens);
void show_result(const acl::redis_result& result);
bool show_result(const acl::redis_result& result);

void show_request(const std::vector<acl::string>& tokens);

void config(const std::vector<acl::string>& tokens);
void config(const char* addr, const std::vector<acl::string>& tokens);
void config_usage(void);
void request_one(const char* addr,
const std::vector<acl::string>& tokens);
};
36 changes: 36 additions & 0 deletions app/redis_tools/redis_builder/redis_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,39 @@ const std::map<acl::string, acl::redis_node*>* redis_util::get_masters2(

return masters;
}

void redis_util::get_all_nodes(acl::redis& redis,
std::vector<const acl::redis_node*>& nodes)
{
const std::map<acl::string, acl::redis_node*>* masters
= get_masters(redis);
if (masters == NULL)
{
logger_error("get_masters NULL");
return;
}

for (std::map<acl::string, acl::redis_node*>::const_iterator cit
= masters->begin(); cit != masters->end(); ++cit)
{
add_slaves(cit->second, nodes);
}
}

void redis_util::add_slaves(const acl::redis_node* node,
std::vector<const acl::redis_node*>& nodes)
{
if (node == NULL)
return;
nodes.push_back(node);

const std::vector<acl::redis_node*>* slaves = node->get_slaves();
if (slaves == NULL)
return;

for (std::vector<acl::redis_node*>::const_iterator cit
= slaves->begin(); cit != slaves->end(); ++cit)
{
add_slaves(*cit, nodes);
}
}
7 changes: 7 additions & 0 deletions app/redis_tools/redis_builder/redis_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ class redis_util
static void get_nodes(acl::redis& redis, bool prefer_master,
std::vector<acl::redis_node*>& nodes);

// get all the nodes of the cluster
static void get_all_nodes(acl::redis& redis,
std::vector<const acl::redis_node*>& nodes);

private:
static const std::map<acl::string, acl::redis_node*>*
get_masters2(acl::redis&);

static void add_slaves(const acl::redis_node* node,
std::vector<const acl::redis_node*>& nodes);
};
4 changes: 4 additions & 0 deletions lib_acl/changes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
�޸���ʷ�б���

------------------------------------------------------------------------
542) 2016.4.14
542.1) feature: acl_argv_split.c �����ַ����ָ����� acl_argv_quote_split ����
�ڶ��ַ������зָ�ʱ�����ƻ���������/˫�������õ��ַ���

541) 2016.3.29
541.1) bugfix: acl_vstream.c ����� stdin, stdout, stderr ��Ҫ�����⴦��

Expand Down
21 changes: 21 additions & 0 deletions lib_acl/include/stdlib/acl_argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ ACL_API ACL_ARGV *acl_argv_split_append(ACL_ARGV *argvp, const char *str,
ACL_API ACL_ARGV *acl_argv_splitn_append(ACL_ARGV *argvp, const char *str,
const char *delim, size_t n);

/**
* 根据源字符串及分隔字符串生成一个字符串动态数组,针对由 "" 或 '' 引用的
* 字符串不做分隔
* @param str {const char*} 源字符串
* @param delim {const char*} 分隔字符串
* @return {ACL_ARGV*}
*/
ACL_API ACL_ARGV *acl_argv_quote_split(const char *str, const char *delim);

/**
* 根据源字符串及分隔字符串生成一个字符串动态数组,针对由 "" 或 '' 引用的
* 字符串不做分隔,其中将传入的内存池对象做为内存分配器
* @param str {const char*} 源字符串
* @param delim {const char*} 分隔字符串
* @param dbuf {ACL_DBUF_POOL*} 内存池对象,可以为空,当为空时则采用
* 缺省的内存分配方式
* @return {ACL_ARGV*}
*/
ACL_API ACL_ARGV *acl_argv_quote_split4(const char *str, const char *delim,
ACL_DBUF_POOL *dbuf);

#define ACL_ARGV_END ((char *) 0)

# ifdef __cplusplus
Expand Down
39 changes: 39 additions & 0 deletions lib_acl/samples/string/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
#include "lib_acl.h"

static void quote_split(const char* str, const char* delim)
{
ACL_ITER iter;
ACL_ARGV *tokens = acl_argv_quote_split(str, delim);

printf("---------------------------------------------\r\n");
printf("str = [%s], delim = [%s]\r\n", str, delim);
acl_foreach(iter, tokens) {
const char* ptr = (const char*) iter.data;
printf("%s\r\n", ptr);
}

acl_argv_free(tokens);
}

static void test_quote_split(void)
{
struct {
const char* str;
const char* delim;
} data[] = {
{ "hello world", " \t" },
{ "\t hello\' world\"", "\t " },
{ "hi \"hello world\"", "\t " },
{ "\t 'myname is zsx.' \"what\'s your name?\" Good! ", "\t " },
{ "name = \"zsx xsz\", name2 = 'xsz,= zsx'", "\t =," },
{ NULL, NULL },
};
int i;

for (i = 0; data[i].str != NULL && data[i].delim; i++)
quote_split(data[i].str, data[i].delim);
}

int main(void)
{
char *src = acl_mystrdup("hello \tworld! you're welcome to China!");
Expand Down Expand Up @@ -41,5 +75,10 @@ int main(void)
printf("buf: %s\r\n", acl_vstring_str(buf));
acl_vstring_free(buf);

printf("Enter any key to continue ...\r\n");
getchar();

test_quote_split();

return 0;
}
Loading

0 comments on commit a6f78cc

Please sign in to comment.