mirror of
https://github.com/RubyMetric/chsrc
synced 2025-08-14 00:20:47 +08:00
Use less macros in core.c
This commit is contained in:
parent
1f1008c440
commit
16d643a8ee
@ -664,7 +664,7 @@ get_target (const char *input, TargetOp code, char *option)
|
||||
}
|
||||
else if (TargetOp_Measure_Source==code)
|
||||
{
|
||||
select_mirror_autoly (target->sources, target->sources_n, input);
|
||||
auto_select_mirror (target->sources, target->sources_n, input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -456,14 +456,13 @@ chsrc_check_file (char *path)
|
||||
/**
|
||||
* 用于 _setsrc 函数,检测用户输入的镜像站code,是否存在于该target可用源中
|
||||
*
|
||||
* @note 一个源Source必定来自于一个Provider,所以该函数名叫 query_provider_exist
|
||||
* @note 一个源Source必定来自于一个Provider,所以该函数名叫 query_mirror_exist
|
||||
*
|
||||
* @param target 目标名
|
||||
* @param input 如果用户输入 default 或者 def,则选择第一个源
|
||||
*/
|
||||
#define find_mirror(s, input) query_provider_exist(s##_sources, s##_sources_n, (char*)#s+3, input)
|
||||
int
|
||||
query_provider_exist (Source_t *sources, size_t size, char *target, char *input)
|
||||
query_mirror_exist (Source_t *sources, size_t size, char *target, char *input)
|
||||
{
|
||||
if (chef_is_url (input))
|
||||
{
|
||||
@ -500,9 +499,9 @@ query_provider_exist (Source_t *sources, size_t size, char *target, char *input)
|
||||
{
|
||||
char *msg1 = ENGLISH ? " is " : " 是 ";
|
||||
char *msg2 = ENGLISH ? "'s ONLY mirror available currently, thanks for their generous support"
|
||||
: " 目前唯一可用镜像站,感谢他们的慷慨支持";
|
||||
: " 目前唯一可用镜像站,感谢他们的慷慨支持";
|
||||
const char *name = ENGLISH ? sources[1].mirror->abbr
|
||||
: sources[1].mirror->name;
|
||||
: sources[1].mirror->name;
|
||||
chsrc_succ (xy_strjoin (4, name, msg1, target, msg2));
|
||||
}
|
||||
|
||||
@ -868,9 +867,8 @@ sources_prepare_speedurl_with_postfix (Source_t sources[], int n, char *postfix)
|
||||
/**
|
||||
* 自动测速选择镜像站和源
|
||||
*/
|
||||
#define auto_select_mirror(s) select_mirror_autoly(s##_sources, s##_sources_n, (char*)#s+3)
|
||||
int
|
||||
select_mirror_autoly (Source_t *sources, size_t size, const char *target_name)
|
||||
auto_select_mirror (Source_t *sources, size_t size, const char *target_name)
|
||||
{
|
||||
/* reset 时选择默认源 */
|
||||
if (chsrc_in_reset_mode())
|
||||
@ -951,14 +949,14 @@ select_mirror_autoly (Source_t *sources, size_t size, const char *target_name)
|
||||
char *msg2 = ENGLISH ? "'s ONLY mirror available currently, thanks for their generous support"
|
||||
: " 目前唯一可用镜像站,感谢他们的慷慨支持";
|
||||
const char *name = ENGLISH ? sources[fast_idx].mirror->abbr
|
||||
: sources[fast_idx].mirror->name;
|
||||
: sources[fast_idx].mirror->name;
|
||||
say (xy_strjoin (5, msg1, bdgreen(name), green(is), green(target_name), green(msg2)));
|
||||
}
|
||||
else
|
||||
{
|
||||
char *msg = ENGLISH ? "FASTEST mirror site: " : "最快镜像站: ";
|
||||
const char *name = ENGLISH ? sources[fast_idx].mirror->abbr
|
||||
: sources[fast_idx].mirror->name;
|
||||
: sources[fast_idx].mirror->name;
|
||||
say (xy_2strjoin (msg, green(name)));
|
||||
}
|
||||
|
||||
@ -973,9 +971,19 @@ select_mirror_autoly (Source_t *sources, size_t size, const char *target_name)
|
||||
}
|
||||
|
||||
|
||||
#define use_specific_mirror_or_auto_select(input, s) \
|
||||
(NULL!=(input)) ? find_mirror(s, input) : auto_select_mirror(s)
|
||||
|
||||
int
|
||||
use_specific_mirror_or_auto_select (char *input, Target_t *t)
|
||||
{
|
||||
if (input)
|
||||
{
|
||||
return query_mirror_exist (t->sources, t->sources_n, t->name, input);
|
||||
}
|
||||
else
|
||||
{
|
||||
return auto_select_mirror (t->sources, t->sources_n, t->name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
@ -997,8 +1005,9 @@ source_has_empty_url (Source_t *source)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 确定所换源的信息,存储在局部变量 @var:source 中
|
||||
* @brief 为该 target 确定最终将使用的源
|
||||
*
|
||||
* 用户*只可能*通过下面5种方式来换源,无论哪一种都会返回一个 Source_t 出来
|
||||
*
|
||||
@ -1007,33 +1016,37 @@ source_has_empty_url (Source_t *source)
|
||||
* 3. 用户什么都没指定, 即 chsrc set <target>
|
||||
* 4. 用户正在重置源, 即 chsrc reset <target>
|
||||
*
|
||||
* 如果处于 Target Group 模式下,leader target 已经测速过了,follower target 不能再次测速,而是直接选择 leader 测过的结果
|
||||
* 如果处于 Target Group 模式下,leader target 已经测速过了,follower target
|
||||
* 不能再次测速,而是直接选择 leader 测过的结果
|
||||
*
|
||||
* 5. leader target 测速出来的某个源
|
||||
*
|
||||
* @dependency 已存在的局部变量 @var:option
|
||||
*/
|
||||
#define chsrc_yield_source(for_what) \
|
||||
Source_t source; \
|
||||
if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index==-1) \
|
||||
{ \
|
||||
ProgStatus.leader_selected_index = use_specific_mirror_or_auto_select (option, for_what); \
|
||||
source = for_what##_sources[ProgStatus.leader_selected_index]; \
|
||||
} \
|
||||
else if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index!=-1) \
|
||||
{ \
|
||||
source = for_what##_sources[ProgStatus.leader_selected_index]; \
|
||||
} \
|
||||
else if (chef_is_url (option)) \
|
||||
{ \
|
||||
Source_t __tmp = { &UserDefinedProvider, option }; \
|
||||
source = __tmp; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
int __index = use_specific_mirror_or_auto_select (option, for_what); \
|
||||
source = for_what##_sources[__index]; \
|
||||
Source_t
|
||||
chsrc_yield_source (Target_t *t, char *option)
|
||||
{
|
||||
Source_t source;
|
||||
if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index==-1)
|
||||
{
|
||||
ProgStatus.leader_selected_index = use_specific_mirror_or_auto_select (option, t);
|
||||
source = t->sources[ProgStatus.leader_selected_index];
|
||||
}
|
||||
else if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index!=-1)
|
||||
{
|
||||
source = t->sources[ProgStatus.leader_selected_index];
|
||||
}
|
||||
else if (chef_is_url (option))
|
||||
{
|
||||
Source_t tmp = { &UserDefinedProvider, option };
|
||||
source = tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
int index = use_specific_mirror_or_auto_select (option, t);
|
||||
source = t->sources[index];
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1047,9 +1060,8 @@ source_has_empty_url (Source_t *source)
|
||||
* 1. 告知用户选择了什么源和镜像
|
||||
* 2. 对选择的源和镜像站进行一定的校验
|
||||
*/
|
||||
#define chsrc_confirm_source() confirm_source(&source)
|
||||
void
|
||||
confirm_source (Source_t *source)
|
||||
chsrc_confirm_source (Source_t *source)
|
||||
{
|
||||
// 由于实现问题,我们把本应该独立出去的上游默认源,也放在了可以换源的数组中,而且放在第一个
|
||||
// chsrc 已经规避用户使用未实现的 `chsrc reset`
|
||||
@ -1077,7 +1089,15 @@ confirm_source (Source_t *source)
|
||||
hr();
|
||||
}
|
||||
|
||||
#define chsrc_yield_source_and_confirm(for_what) chsrc_yield_source(for_what);chsrc_confirm_source()
|
||||
|
||||
|
||||
Source_t
|
||||
chsrc_yield_source_and_confirm (Target_t t)
|
||||
{
|
||||
Source_t source = chsrc_yield_source(&t);
|
||||
chsrc_confirm_source(&source);
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -137,13 +137,15 @@ Contributor_t;
|
||||
|
||||
typedef struct Target_t
|
||||
{
|
||||
char *name; /* 目标名称,该名称必须是 menu 中的 aliase 之一 */
|
||||
|
||||
void (*getfn) (char *option);
|
||||
void (*setfn) (char *option);
|
||||
void (*resetfn) (char *option);
|
||||
|
||||
Feature_t (*featfn) (char *option);
|
||||
|
||||
Source_t *sources;
|
||||
Source_t sources;
|
||||
size_t sources_n;
|
||||
|
||||
bool can_english; /* 是否支持英文输出 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user