Use less macros in core.c

This commit is contained in:
Aoran Zeng 2025-08-09 21:32:27 +08:00
parent 1f1008c440
commit 16d643a8ee
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
3 changed files with 61 additions and 39 deletions

View File

@ -664,7 +664,7 @@ get_target (const char *input, TargetOp code, char *option)
} }
else if (TargetOp_Measure_Source==code) 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; return true;
} }

View File

@ -456,14 +456,13 @@ chsrc_check_file (char *path)
/** /**
* _setsrc codetarget可用源中 * _setsrc codetarget可用源中
* *
* @note Source必定来自于一个Provider query_provider_exist * @note Source必定来自于一个Provider query_mirror_exist
* *
* @param target * @param target
* @param input default def * @param input default def
*/ */
#define find_mirror(s, input) query_provider_exist(s##_sources, s##_sources_n, (char*)#s+3, input)
int 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)) 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 *msg1 = ENGLISH ? " is " : "";
char *msg2 = ENGLISH ? "'s ONLY mirror available currently, thanks for their generous support" char *msg2 = ENGLISH ? "'s ONLY mirror available currently, thanks for their generous support"
: " 目前唯一可用镜像站,感谢他们的慷慨支持"; : " 目前唯一可用镜像站,感谢他们的慷慨支持";
const char *name = ENGLISH ? sources[1].mirror->abbr 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)); 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 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 时选择默认源 */ /* reset 时选择默认源 */
if (chsrc_in_reset_mode()) 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" char *msg2 = ENGLISH ? "'s ONLY mirror available currently, thanks for their generous support"
: " 目前唯一可用镜像站,感谢他们的慷慨支持"; : " 目前唯一可用镜像站,感谢他们的慷慨支持";
const char *name = ENGLISH ? sources[fast_idx].mirror->abbr 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))); say (xy_strjoin (5, msg1, bdgreen(name), green(is), green(target_name), green(msg2)));
} }
else else
{ {
char *msg = ENGLISH ? "FASTEST mirror site: " : "最快镜像站: "; char *msg = ENGLISH ? "FASTEST mirror site: " : "最快镜像站: ";
const char *name = ENGLISH ? sources[fast_idx].mirror->abbr 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))); 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 bool
@ -997,8 +1005,9 @@ source_has_empty_url (Source_t *source)
} }
/** /**
* @brief @var:source * @brief target 使
* *
* **5 Source_t * **5 Source_t
* *
@ -1007,33 +1016,37 @@ source_has_empty_url (Source_t *source)
* 3. chsrc set <target> * 3. chsrc set <target>
* 4. chsrc reset <target> * 4. chsrc reset <target>
* *
* Target Group leader target follower target leader * Target Group leader target follower target
* leader
* *
* 5. leader target * 5. leader target
* *
* @dependency @var:option
*/ */
#define chsrc_yield_source(for_what) \ Source_t
Source_t source; \ chsrc_yield_source (Target_t *t, char *option)
if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index==-1) \ {
{ \ Source_t source;
ProgStatus.leader_selected_index = use_specific_mirror_or_auto_select (option, for_what); \ if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index==-1)
source = for_what##_sources[ProgStatus.leader_selected_index]; \ {
} \ ProgStatus.leader_selected_index = use_specific_mirror_or_auto_select (option, t);
else if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index!=-1) \ source = t->sources[ProgStatus.leader_selected_index];
{ \
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]; \
} }
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. * 1.
* 2. * 2.
*/ */
#define chsrc_confirm_source() confirm_source(&source)
void void
confirm_source (Source_t *source) chsrc_confirm_source (Source_t *source)
{ {
// 由于实现问题,我们把本应该独立出去的上游默认源,也放在了可以换源的数组中,而且放在第一个 // 由于实现问题,我们把本应该独立出去的上游默认源,也放在了可以换源的数组中,而且放在第一个
// chsrc 已经规避用户使用未实现的 `chsrc reset` // chsrc 已经规避用户使用未实现的 `chsrc reset`
@ -1077,7 +1089,15 @@ confirm_source (Source_t *source)
hr(); 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;
}
/** /**

View File

@ -137,13 +137,15 @@ Contributor_t;
typedef struct Target_t typedef struct Target_t
{ {
char *name; /* 目标名称,该名称必须是 menu 中的 aliase 之一 */
void (*getfn) (char *option); void (*getfn) (char *option);
void (*setfn) (char *option); void (*setfn) (char *option);
void (*resetfn) (char *option); void (*resetfn) (char *option);
Feature_t (*featfn) (char *option); Feature_t (*featfn) (char *option);
Source_t *sources; Source_t sources;
size_t sources_n; size_t sources_n;
bool can_english; /* 是否支持英文输出 */ bool can_english; /* 是否支持英文输出 */